1
0

Kernel-Infrastructure.st 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. Smalltalk createPackage: 'Kernel-Infrastructure'!
  2. Object subclass: #ConsoleErrorHandler
  3. instanceVariableNames: ''
  4. package: 'Kernel-Infrastructure'!
  5. !ConsoleErrorHandler commentStamp!
  6. I am manage Smalltalk errors, displaying the stack in the console.!
  7. !ConsoleErrorHandler methodsFor: 'error handling'!
  8. handleError: anError
  9. anError context ifNotNil: [ self logErrorContext: anError context ].
  10. self logError: anError
  11. ! !
  12. !ConsoleErrorHandler methodsFor: 'private'!
  13. log: aString
  14. console log: aString
  15. !
  16. logContext: aContext
  17. aContext home ifNotNil: [
  18. self logContext: aContext home ].
  19. self log: aContext asString
  20. !
  21. logError: anError
  22. self log: anError messageText
  23. !
  24. logErrorContext: aContext
  25. aContext ifNotNil: [
  26. aContext home ifNotNil: [
  27. self logContext: aContext home ]]
  28. ! !
  29. ConsoleErrorHandler class instanceVariableNames: 'current'!
  30. !ConsoleErrorHandler class methodsFor: 'initialization'!
  31. initialize
  32. ErrorHandler registerIfNone: self new
  33. ! !
  34. Object subclass: #InterfacingObject
  35. instanceVariableNames: ''
  36. package: 'Kernel-Infrastructure'!
  37. !InterfacingObject commentStamp!
  38. I am superclass of all object that interface with user or environment. `Widget` and a few other classes are subclasses of me. I delegate all of the above APIs to `PlatformInterface`.
  39. ## API
  40. self alert: 'Hey, there is a problem'.
  41. self confirm: 'Affirmative?'.
  42. self prompt: 'Your name:'.
  43. self ajax: #{
  44. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  45. }.!
  46. !InterfacingObject methodsFor: 'actions'!
  47. ajax: anObject
  48. ^ PlatformInterface ajax: anObject
  49. !
  50. alert: aString
  51. ^ PlatformInterface alert: aString
  52. !
  53. confirm: aString
  54. ^ PlatformInterface confirm: aString
  55. !
  56. prompt: aString
  57. ^ PlatformInterface prompt: aString
  58. ! !
  59. InterfacingObject subclass: #Environment
  60. instanceVariableNames: ''
  61. package: 'Kernel-Infrastructure'!
  62. !Environment commentStamp!
  63. I provide an unified entry point to manipulate Amber packages, classes and methods.
  64. Typical use cases include IDEs, remote access and restricting browsing.!
  65. !Environment methodsFor: 'accessing'!
  66. allSelectors
  67. ^ Smalltalk vm allSelectors
  68. !
  69. availableClassNames
  70. ^ Smalltalk classes
  71. collect: [ :each | each name ]
  72. !
  73. availablePackageNames
  74. ^ Smalltalk packages
  75. collect: [ :each | each name ]
  76. !
  77. availableProtocolsFor: aClass
  78. | protocols |
  79. protocols := aClass protocols.
  80. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  81. ^ protocols asSet asArray sort
  82. !
  83. classBuilder
  84. ^ ClassBuilder new
  85. !
  86. classNamed: aString
  87. ^ (Smalltalk globals at: aString asSymbol)
  88. ifNil: [ self error: 'Invalid class name' ]
  89. !
  90. classes
  91. ^ Smalltalk classes
  92. !
  93. doItReceiver
  94. ^ DoIt new
  95. !
  96. packages
  97. ^ Smalltalk packages
  98. !
  99. systemAnnouncer
  100. ^ (Smalltalk globals at: #SystemAnnouncer) current
  101. ! !
  102. !Environment methodsFor: 'actions'!
  103. commitPackage: aPackage onSuccess: aBlock onError: anotherBlock
  104. aPackage transport
  105. commitOnSuccess: aBlock
  106. onError: anotherBlock
  107. !
  108. copyClass: aClass to: aClassName
  109. (Smalltalk globals at: aClassName)
  110. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  111. ClassBuilder new copyClass: aClass named: aClassName
  112. !
  113. inspect: anObject
  114. Inspector inspect: anObject
  115. !
  116. moveClass: aClass toPackage: aPackageName
  117. | package |
  118. package := Package named: aPackageName.
  119. package ifNil: [ self error: 'Invalid package name' ].
  120. package == aClass package ifTrue: [ ^ self ].
  121. aClass package: package
  122. !
  123. moveMethod: aMethod toClass: aClassName
  124. | destinationClass |
  125. destinationClass := self classNamed: aClassName.
  126. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  127. aMethod methodClass isMetaclass ifTrue: [
  128. destinationClass := destinationClass class ].
  129. destinationClass
  130. compile: aMethod source
  131. protocol: aMethod protocol.
  132. aMethod methodClass
  133. removeCompiledMethod: aMethod
  134. !
  135. moveMethod: aMethod toProtocol: aProtocol
  136. aMethod protocol: aProtocol
  137. !
  138. removeClass: aClass
  139. Smalltalk removeClass: aClass
  140. !
  141. removeMethod: aMethod
  142. aMethod methodClass removeCompiledMethod: aMethod
  143. !
  144. removeProtocol: aString from: aClass
  145. (aClass methodsInProtocol: aString)
  146. do: [ :each | aClass removeCompiledMethod: each ]
  147. !
  148. renameClass: aClass to: aClassName
  149. (Smalltalk globals at: aClassName)
  150. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  151. ClassBuilder new renameClass: aClass to: aClassName
  152. !
  153. renameProtocol: aString to: anotherString in: aClass
  154. (aClass methodsInProtocol: aString)
  155. do: [ :each | each protocol: anotherString ]
  156. !
  157. setClassCommentOf: aClass to: aString
  158. aClass comment: aString
  159. ! !
  160. !Environment methodsFor: 'compiling'!
  161. addInstVarNamed: aString to: aClass
  162. self classBuilder
  163. addSubclassOf: aClass superclass
  164. named: aClass name
  165. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  166. package: aClass package name
  167. !
  168. compileClassComment: aString for: aClass
  169. aClass comment: aString
  170. !
  171. compileClassDefinition: aString
  172. [ self eval: aString on: DoIt new ]
  173. on: Error
  174. do: [ :error | self alert: error messageText ]
  175. !
  176. compileMethod: sourceCode for: class protocol: protocol
  177. ^ class
  178. compile: sourceCode
  179. protocol: protocol
  180. ! !
  181. !Environment methodsFor: 'error handling'!
  182. evaluate: aBlock on: anErrorClass do: exceptionBlock
  183. "Evaluate a block and catch exceptions happening on the environment stack"
  184. aBlock tryCatch: [ :exception |
  185. (exception isKindOf: (self classNamed: anErrorClass name))
  186. ifTrue: [ exceptionBlock value: exception ]
  187. ifFalse: [ exception signal ] ]
  188. ! !
  189. !Environment methodsFor: 'evaluating'!
  190. eval: aString on: aReceiver
  191. | compiler |
  192. compiler := Compiler new.
  193. [ compiler parseExpression: aString ] on: Error do: [ :ex |
  194. ^ self alert: ex messageText ].
  195. ^ compiler evaluateExpression: aString on: aReceiver
  196. !
  197. interpret: aString inContext: anAIContext
  198. "Similar to #eval:on:, with the following differences:
  199. - instead of compiling and running `aString`, `aString` is interpreted using an `ASTInterpreter`
  200. - instead of evaluating against a receiver, evaluate in the context of `anAIContext`"
  201. | compiler ast |
  202. compiler := Compiler new.
  203. [ ast := compiler parseExpression: aString ] on: Error do: [ :ex |
  204. ^ self alert: ex messageText ].
  205. (AISemanticAnalyzer on: anAIContext receiver class)
  206. context: anAIContext;
  207. visit: ast.
  208. ^ anAIContext evaluateNode: ast
  209. ! !
  210. !Environment methodsFor: 'services'!
  211. registerErrorHandler: anErrorHandler
  212. ErrorHandler register: anErrorHandler
  213. !
  214. registerFinder: aFinder
  215. Finder register: aFinder
  216. !
  217. registerInspector: anInspector
  218. Inspector register: anInspector
  219. !
  220. registerProgressHandler: aProgressHandler
  221. ProgressHandler register: aProgressHandler
  222. !
  223. registerTranscript: aTranscript
  224. Transcript register: aTranscript
  225. ! !
  226. ProtoObject subclass: #JSObjectProxy
  227. instanceVariableNames: 'jsObject'
  228. package: 'Kernel-Infrastructure'!
  229. !JSObjectProxy commentStamp!
  230. I handle sending messages to JavaScript objects, making JavaScript object accessing from Amber fully transparent.
  231. My instances make intensive use of `#doesNotUnderstand:`.
  232. My instances are automatically created by Amber whenever a message is sent to a JavaScript object.
  233. ## Usage examples
  234. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  235. window alert: 'hello world'.
  236. window inspect.
  237. (window jQuery: 'body') append: 'hello world'
  238. Amber messages sends are converted to JavaScript function calls or object property access _(in this order)_. If n one of them match, a `MessageNotUnderstood` error will be thrown.
  239. ## Message conversion rules
  240. - `someUser name` becomes `someUser.name`
  241. - `someUser name: 'John'` becomes `someUser name = "John"`
  242. - `console log: 'hello world'` becomes `console.log('hello world')`
  243. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  244. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  245. !JSObjectProxy methodsFor: 'accessing'!
  246. at: aString
  247. <return self['@jsObject'][aString]>
  248. !
  249. at: aString ifAbsent: aBlock
  250. "return the aString property or evaluate aBlock if the property is not defined on the object"
  251. <
  252. var obj = self['@jsObject'];
  253. return aString in obj ? obj[aString] : aBlock._value();
  254. >
  255. !
  256. at: aString ifPresent: aBlock
  257. "return the evaluation of aBlock with the value if the property is defined or return nil"
  258. <
  259. var obj = self['@jsObject'];
  260. return aString in obj ? aBlock._value_(obj[aString]) : nil;
  261. >
  262. !
  263. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  264. "return the evaluation of aBlock with the value if the property is defined
  265. or return value of anotherBlock"
  266. <
  267. var obj = self['@jsObject'];
  268. return aString in obj ? aBlock._value_(obj[aString]) : anotherBlock._value();
  269. >
  270. !
  271. at: aString put: anObject
  272. <return self['@jsObject'][aString] = anObject>
  273. !
  274. jsObject
  275. ^ jsObject
  276. !
  277. jsObject: aJSObject
  278. jsObject := aJSObject
  279. !
  280. lookupProperty: aString
  281. "Looks up a property in JS object.
  282. Answer the property if it is present, or nil if it is not present."
  283. <return aString in self._jsObject() ? aString : nil>
  284. ! !
  285. !JSObjectProxy methodsFor: 'comparing'!
  286. = anObject
  287. anObject class == self class ifFalse: [ ^ false ].
  288. ^ self compareJSObjectWith: anObject jsObject
  289. ! !
  290. !JSObjectProxy methodsFor: 'enumerating'!
  291. asJSON
  292. "Answers the receiver in a stringyfy-friendly fashion"
  293. ^ jsObject
  294. !
  295. keysAndValuesDo: aBlock
  296. <
  297. var o = self['@jsObject'];
  298. for(var i in o) {
  299. aBlock._value_value_(i, o[i]);
  300. }
  301. >
  302. ! !
  303. !JSObjectProxy methodsFor: 'printing'!
  304. printOn: aStream
  305. aStream nextPutAll: self printString
  306. !
  307. printString
  308. <
  309. var js = self['@jsObject'];
  310. return js.toString
  311. ? js.toString()
  312. : Object.prototype.toString.call(js)
  313. >
  314. ! !
  315. !JSObjectProxy methodsFor: 'private'!
  316. compareJSObjectWith: aJSObject
  317. <return self["@jsObject"] === aJSObject>
  318. ! !
  319. !JSObjectProxy methodsFor: 'proxy'!
  320. addObjectVariablesTo: aDictionary
  321. <
  322. for(var i in self['@jsObject']) {
  323. aDictionary._at_put_(i, self['@jsObject'][i]);
  324. }
  325. >
  326. !
  327. doesNotUnderstand: aMessage
  328. ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
  329. ifNil: [ super doesNotUnderstand: aMessage ]
  330. ifNotNil: [ :jsSelector |
  331. self
  332. forwardMessage: jsSelector
  333. withArguments: aMessage arguments ]
  334. !
  335. forwardMessage: aString withArguments: anArray
  336. <
  337. return smalltalk.send(self._jsObject(), aString, anArray);
  338. >
  339. !
  340. inspectOn: anInspector
  341. | variables |
  342. variables := Dictionary new.
  343. variables at: '#self' put: self jsObject.
  344. anInspector setLabel: self printString.
  345. self addObjectVariablesTo: variables.
  346. anInspector setVariables: variables
  347. ! !
  348. !JSObjectProxy class methodsFor: 'instance creation'!
  349. on: aJSObject
  350. ^ self new
  351. jsObject: aJSObject;
  352. yourself
  353. ! !
  354. Object subclass: #NullProgressHandler
  355. instanceVariableNames: ''
  356. package: 'Kernel-Infrastructure'!
  357. !NullProgressHandler commentStamp!
  358. I am the default progress handler. I do not display any progress, and simply iterate over the collection.!
  359. !NullProgressHandler methodsFor: 'progress handling'!
  360. do: aBlock on: aCollection displaying: aString
  361. aCollection do: aBlock
  362. ! !
  363. NullProgressHandler class instanceVariableNames: 'current'!
  364. !NullProgressHandler class methodsFor: 'initialization'!
  365. initialize
  366. ProgressHandler registerIfNone: self new
  367. ! !
  368. Object subclass: #Organizer
  369. instanceVariableNames: ''
  370. package: 'Kernel-Infrastructure'!
  371. !Organizer commentStamp!
  372. I represent categorization information.
  373. ## API
  374. Use `#addElement:` and `#removeElement:` to manipulate instances.!
  375. !Organizer methodsFor: 'accessing'!
  376. addElement: anObject
  377. <self.elements.addElement(anObject)>
  378. !
  379. elements
  380. ^ (self basicAt: 'elements') copy
  381. !
  382. removeElement: anObject
  383. <self.elements.removeElement(anObject)>
  384. ! !
  385. Organizer subclass: #ClassOrganizer
  386. instanceVariableNames: ''
  387. package: 'Kernel-Infrastructure'!
  388. !ClassOrganizer commentStamp!
  389. I am an organizer specific to classes. I hold method categorization information for classes.!
  390. !ClassOrganizer methodsFor: 'accessing'!
  391. addElement: aString
  392. super addElement: aString.
  393. SystemAnnouncer current announce: (ProtocolAdded new
  394. protocol: aString;
  395. theClass: self theClass;
  396. yourself)
  397. !
  398. removeElement: aString
  399. super removeElement: aString.
  400. SystemAnnouncer current announce: (ProtocolRemoved new
  401. protocol: aString;
  402. theClass: self theClass;
  403. yourself)
  404. !
  405. theClass
  406. < return self.theClass >
  407. ! !
  408. Organizer subclass: #PackageOrganizer
  409. instanceVariableNames: ''
  410. package: 'Kernel-Infrastructure'!
  411. !PackageOrganizer commentStamp!
  412. I am an organizer specific to packages. I hold classes categorization information.!
  413. Object subclass: #Package
  414. instanceVariableNames: 'transport'
  415. package: 'Kernel-Infrastructure'!
  416. !Package commentStamp!
  417. I am similar to a "class category" typically found in other Smalltalks like Pharo or Squeak. Amber does not have class categories anymore, it had in the beginning but now each class in the system knows which package it belongs to.
  418. Each package has a name and can be queried for its classes, but it will then resort to a reverse scan of all classes to find them.
  419. ## API
  420. Packages are manipulated through "Smalltalk current", like for example finding one based on a name or with `Package class >> #name` directly:
  421. Smalltalk current packageAt: 'Kernel'
  422. Package named: 'Kernel'
  423. A package differs slightly from a Monticello package which can span multiple class categories using a naming convention based on hyphenation. But just as in Monticello a package supports "class extensions" so a package can define behaviors in foreign classes using a naming convention for method categories where the category starts with an asterisk and then the name of the owning package follows.
  424. You can fetch a package from the server:
  425. Package load: 'Additional-Examples'!
  426. !Package methodsFor: 'accessing'!
  427. basicTransport
  428. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  429. <return self.transport>
  430. !
  431. classTemplate
  432. ^ String streamContents: [ :stream |
  433. stream
  434. nextPutAll: 'Object';
  435. nextPutAll: ' subclass: #NameOfSubclass';
  436. nextPutAll: String lf, String tab;
  437. nextPutAll: 'instanceVariableNames: '''''.
  438. stream
  439. nextPutAll: '''', String lf, String tab;
  440. nextPutAll: 'package: ''';
  441. nextPutAll: self name;
  442. nextPutAll: '''' ]
  443. !
  444. definition
  445. ^ String streamContents: [ :stream |
  446. stream
  447. nextPutAll: self class name;
  448. nextPutAll: String lf, String tab;
  449. nextPutAll: ' named: ';
  450. nextPutAll: '''', self name, '''';
  451. nextPutAll: String lf, String tab;
  452. nextPutAll: ' transport: (';
  453. nextPutAll: self transport definition, ')' ]
  454. !
  455. name
  456. <return self.pkgName>
  457. !
  458. name: aString
  459. <self.pkgName = aString>
  460. !
  461. organization
  462. ^ self basicAt: 'organization'
  463. !
  464. transport
  465. ^ transport ifNil: [
  466. transport := (PackageTransport fromJson: self basicTransport)
  467. package: self;
  468. yourself ]
  469. !
  470. transport: aPackageTransport
  471. transport := aPackageTransport.
  472. aPackageTransport package: self
  473. ! !
  474. !Package methodsFor: 'classes'!
  475. classes
  476. ^ self organization elements
  477. !
  478. setupClasses
  479. self classes
  480. do: [ :each | ClassBuilder new setupClass: each ];
  481. do: [ :each | each initialize ]
  482. !
  483. sortedClasses
  484. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  485. ^ self class sortedClasses: self classes
  486. ! !
  487. !Package methodsFor: 'dependencies'!
  488. loadDependencies
  489. "Returns list of packages that need to be loaded
  490. before loading this package."
  491. | classes packages |
  492. classes := self loadDependencyClasses.
  493. ^ (classes collect: [ :each | each package ]) asSet
  494. remove: self ifAbsent: [];
  495. yourself
  496. !
  497. loadDependencyClasses
  498. "Returns classes needed at the time of loading a package.
  499. These are all that are used to subclass
  500. and to define an extension method"
  501. | starCategoryName |
  502. starCategoryName := '*', self name.
  503. ^ (self classes collect: [ :each | each superclass ]) asSet
  504. remove: nil ifAbsent: [];
  505. addAll: (Smalltalk classes select: [ :each | each protocols, each class protocols includes: starCategoryName ]);
  506. yourself
  507. ! !
  508. !Package methodsFor: 'printing'!
  509. printOn: aStream
  510. super printOn: aStream.
  511. aStream
  512. nextPutAll: ' (';
  513. nextPutAll: self name;
  514. nextPutAll: ')'
  515. ! !
  516. !Package methodsFor: 'testing'!
  517. isPackage
  518. ^ true
  519. ! !
  520. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  521. !Package class methodsFor: 'accessing'!
  522. named: aPackageName
  523. ^ Smalltalk
  524. packageAt: aPackageName
  525. ifAbsent: [
  526. Smalltalk createPackage: aPackageName ]
  527. !
  528. named: aPackageName ifAbsent: aBlock
  529. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  530. !
  531. named: aPackageName transport: aTransport
  532. | package |
  533. package := self named: aPackageName.
  534. package transport: aTransport.
  535. ^ package
  536. ! !
  537. !Package class methodsFor: 'sorting'!
  538. sortedClasses: classes
  539. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  540. | children others nodes expandedClasses |
  541. children := #().
  542. others := #().
  543. classes do: [ :each |
  544. (classes includes: each superclass)
  545. ifFalse: [ children add: each ]
  546. ifTrue: [ others add: each ]].
  547. nodes := children collect: [ :each |
  548. ClassSorterNode on: each classes: others level: 0 ].
  549. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  550. expandedClasses := Array new.
  551. nodes do: [ :aNode |
  552. aNode traverseClassesWith: expandedClasses ].
  553. ^ expandedClasses
  554. ! !
  555. Object subclass: #PlatformInterface
  556. instanceVariableNames: ''
  557. package: 'Kernel-Infrastructure'!
  558. !PlatformInterface commentStamp!
  559. I am single entry point to UI and environment interface.
  560. My `initialize` tries several options (for now, browser environment only) to set myself up.
  561. ## API
  562. PlatformInterface alert: 'Hey, there is a problem'.
  563. PlatformInterface confirm: 'Affirmative?'.
  564. PlatformInterface prompt: 'Your name:'.
  565. PlatformInterface ajax: #{
  566. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  567. }.!
  568. PlatformInterface class instanceVariableNames: 'worker'!
  569. !PlatformInterface class methodsFor: 'accessing'!
  570. globals
  571. <return (new Function('return this'))();>
  572. !
  573. setWorker: anObject
  574. worker := anObject
  575. ! !
  576. !PlatformInterface class methodsFor: 'actions'!
  577. ajax: anObject
  578. ^ worker
  579. ifNotNil: [ worker ajax: anObject ]
  580. ifNil: [ self error: 'ajax: not available' ]
  581. !
  582. alert: aString
  583. ^ worker
  584. ifNotNil: [ worker alert: aString ]
  585. ifNil: [ self error: 'alert: not available' ]
  586. !
  587. confirm: aString
  588. ^ worker
  589. ifNotNil: [ worker confirm: aString ]
  590. ifNil: [ self error: 'confirm: not available' ]
  591. !
  592. existsGlobal: aString
  593. ^ PlatformInterface globals
  594. at: aString
  595. ifPresent: [ true ]
  596. ifAbsent: [ false ]
  597. !
  598. prompt: aString
  599. ^ worker
  600. ifNotNil: [ worker prompt: aString ]
  601. ifNil: [ self error: 'prompt: not available' ]
  602. ! !
  603. !PlatformInterface class methodsFor: 'initialization'!
  604. initialize
  605. | candidate |
  606. super initialize.
  607. BrowserInterface ifNotNil: [
  608. candidate := BrowserInterface new.
  609. candidate isAvailable ifTrue: [ self setWorker: candidate. ^ self ]
  610. ]
  611. ! !
  612. Object subclass: #Service
  613. instanceVariableNames: ''
  614. package: 'Kernel-Infrastructure'!
  615. !Service commentStamp!
  616. I implement the basic behavior for class registration to a service.
  617. See the `Transcript` class for a concrete service.
  618. ## API
  619. Use class-side methods `#register:` and `#registerIfNone:` to register classes to a specific service.!
  620. Service class instanceVariableNames: 'current'!
  621. !Service class methodsFor: 'accessing'!
  622. current
  623. ^ current
  624. ! !
  625. !Service class methodsFor: 'instance creation'!
  626. new
  627. self shouldNotImplement
  628. ! !
  629. !Service class methodsFor: 'registration'!
  630. register: anObject
  631. current := anObject
  632. !
  633. registerIfNone: anObject
  634. self current ifNil: [ self register: anObject ]
  635. ! !
  636. Service subclass: #ErrorHandler
  637. instanceVariableNames: ''
  638. package: 'Kernel-Infrastructure'!
  639. !ErrorHandler commentStamp!
  640. I am the service used to handle Smalltalk errors.
  641. See `boot.js` `handleError()` function.
  642. Registered service instances must implement `#handleError:` to perform an action on the thrown exception.!
  643. !ErrorHandler class methodsFor: 'error handling'!
  644. handleError: anError
  645. self handleUnhandledError: anError
  646. !
  647. handleUnhandledError: anError
  648. anError wasHandled ifTrue: [ ^ self ].
  649. ^ self current handleError: anError
  650. ! !
  651. Service subclass: #Finder
  652. instanceVariableNames: ''
  653. package: 'Kernel-Infrastructure'!
  654. !Finder commentStamp!
  655. I am the service responsible for finding classes/methods.
  656. __There is no default finder.__
  657. ## API
  658. Use `#browse` on an object to find it.!
  659. !Finder class methodsFor: 'finding'!
  660. findClass: aClass
  661. ^ self current findClass: aClass
  662. !
  663. findMethod: aCompiledMethod
  664. ^ self current findMethod: aCompiledMethod
  665. !
  666. findString: aString
  667. ^ self current findString: aString
  668. ! !
  669. Service subclass: #Inspector
  670. instanceVariableNames: ''
  671. package: 'Kernel-Infrastructure'!
  672. !Inspector commentStamp!
  673. I am the service responsible for inspecting objects.
  674. The default inspector object is the transcript.!
  675. !Inspector class methodsFor: 'inspecting'!
  676. inspect: anObject
  677. ^ self current inspect: anObject
  678. ! !
  679. Service subclass: #ProgressHandler
  680. instanceVariableNames: ''
  681. package: 'Kernel-Infrastructure'!
  682. !ProgressHandler commentStamp!
  683. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  684. Registered instances must implement `#do:on:displaying:`.
  685. The default behavior is to simply iterate over the collection, using `NullProgressHandler`.!
  686. !ProgressHandler class methodsFor: 'progress handling'!
  687. do: aBlock on: aCollection displaying: aString
  688. self current do: aBlock on: aCollection displaying: aString
  689. ! !
  690. Service subclass: #Transcript
  691. instanceVariableNames: ''
  692. package: 'Kernel-Infrastructure'!
  693. !Transcript commentStamp!
  694. I am a facade for Transcript actions.
  695. I delegate actions to the currently registered transcript.
  696. ## API
  697. Transcript
  698. show: 'hello world';
  699. cr;
  700. show: anObject.!
  701. !Transcript class methodsFor: 'instance creation'!
  702. open
  703. self current open
  704. ! !
  705. !Transcript class methodsFor: 'printing'!
  706. clear
  707. self current clear
  708. !
  709. cr
  710. self current show: String cr
  711. !
  712. inspect: anObject
  713. self show: anObject
  714. !
  715. show: anObject
  716. self current show: anObject
  717. ! !
  718. Object subclass: #Setting
  719. instanceVariableNames: 'key value defaultValue'
  720. package: 'Kernel-Infrastructure'!
  721. !Setting commentStamp!
  722. I represent a setting accessible via `Smalltalk settings`.
  723. ## API
  724. A `Setting` value can be read using `value` and set using `value:`.
  725. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: 'defaultValue'`.!
  726. !Setting methodsFor: 'accessing'!
  727. defaultValue
  728. ^ defaultValue
  729. !
  730. defaultValue: anObject
  731. defaultValue := anObject
  732. !
  733. key
  734. ^ key
  735. !
  736. key: anObject
  737. key := anObject
  738. !
  739. value
  740. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  741. !
  742. value: aString
  743. ^ Smalltalk settings at: self key put: aString
  744. ! !
  745. !Setting class methodsFor: 'instance creation'!
  746. at: aString ifAbsent: anotherString
  747. ^ super new
  748. key: aString;
  749. defaultValue: anotherString;
  750. yourself
  751. !
  752. new
  753. self shouldNotImplement
  754. ! !
  755. Object subclass: #SmalltalkImage
  756. instanceVariableNames: ''
  757. package: 'Kernel-Infrastructure'!
  758. !SmalltalkImage commentStamp!
  759. I represent the Smalltalk system, wrapping
  760. operations of variable `smalltalk` declared in `support/boot.js`.
  761. ## API
  762. I have only one instance, accessed with global variable `Smalltalk`.
  763. The `smalltalk` object holds all class and packages defined in the system.
  764. ## Classes
  765. Classes can be accessed using the following methods:
  766. - `#classes` answers the full list of Smalltalk classes in the system
  767. - `#at:` answers a specific class or `nil`
  768. ## Packages
  769. Packages can be accessed using the following methods:
  770. - `#packages` answers the full list of packages
  771. - `#packageAt:` answers a specific package or `nil`
  772. ## Parsing
  773. The `#parse:` method is used to parse Amber source code.
  774. It requires the `Compiler` package and the `support/parser.js` parser file in order to work.!
  775. !SmalltalkImage methodsFor: 'accessing'!
  776. at: aString
  777. self deprecatedAPI.
  778. ^ self globals at: aString
  779. !
  780. at: aKey ifAbsent: aBlock
  781. ^ (self includesKey: aKey)
  782. ifTrue: [ self at: aKey ]
  783. ifFalse: [ aBlock value ]
  784. !
  785. at: aString put: anObject
  786. self deprecatedAPI.
  787. ^ self globals at: aString put: anObject
  788. !
  789. current
  790. "Backward compatibility for Smalltalk current ..."
  791. self deprecatedAPI.
  792. ^ self
  793. !
  794. globals
  795. "Future compatibility to be able to use Smalltalk globals at: ..."
  796. <return globals>
  797. !
  798. includesKey: aKey
  799. <return smalltalk.hasOwnProperty(aKey)>
  800. !
  801. parse: aString
  802. | result |
  803. [ result := self basicParse: aString ]
  804. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  805. ^ result
  806. source: aString;
  807. yourself
  808. !
  809. pseudoVariableNames
  810. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  811. !
  812. readJSObject: anObject
  813. <return smalltalk.readJSObject(anObject)>
  814. !
  815. reservedWords
  816. "JavaScript reserved words"
  817. <return smalltalk.reservedWords>
  818. !
  819. settings
  820. ^ SmalltalkSettings
  821. !
  822. version
  823. "Answer the version string of Amber"
  824. ^ '0.13.0-pre'
  825. !
  826. vm
  827. "Future compatibility to be able to use Smalltalk vm ..."
  828. <return smalltalk>
  829. ! !
  830. !SmalltalkImage methodsFor: 'accessing amd'!
  831. amdRequire
  832. ^ self vm at: 'amdRequire'
  833. !
  834. defaultAmdNamespace
  835. ^ 'transport.defaultAmdNamespace' settingValue
  836. !
  837. defaultAmdNamespace: aString
  838. 'transport.defaultAmdNamespace' settingValue: aString
  839. ! !
  840. !SmalltalkImage methodsFor: 'classes'!
  841. classes
  842. <return smalltalk.classes()>
  843. !
  844. removeClass: aClass
  845. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  846. self deleteClass: aClass.
  847. SystemAnnouncer current
  848. announce: (ClassRemoved new
  849. theClass: aClass;
  850. yourself)
  851. ! !
  852. !SmalltalkImage methodsFor: 'error handling'!
  853. asSmalltalkException: anObject
  854. "A JavaScript exception may be thrown.
  855. We then need to convert it back to a Smalltalk object"
  856. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  857. ifTrue: [ anObject ]
  858. ifFalse: [ JavaScriptException on: anObject ]
  859. !
  860. parseError: anException parsing: aString
  861. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  862. ! !
  863. !SmalltalkImage methodsFor: 'globals'!
  864. addGlobalJsVariable: aString
  865. self globalJsVariables add: aString
  866. !
  867. deleteGlobalJsVariable: aString
  868. self globalJsVariables remove: aString ifAbsent:[]
  869. !
  870. globalJsVariables
  871. "Array of global JavaScript variables"
  872. <return smalltalk.globalJsVariables>
  873. ! !
  874. !SmalltalkImage methodsFor: 'packages'!
  875. createPackage: packageName
  876. | package announcement |
  877. package := self basicCreatePackage: packageName.
  878. announcement := PackageAdded new
  879. package: package;
  880. yourself.
  881. SystemAnnouncer current announce: announcement.
  882. ^ package
  883. !
  884. packageAt: packageName
  885. <return smalltalk.packages[packageName]>
  886. !
  887. packageAt: packageName ifAbsent: aBlock
  888. ^ (self packageAt: packageName) ifNil: aBlock
  889. !
  890. packages
  891. "Return all Package instances in the system."
  892. <
  893. return Object.keys(smalltalk.packages).map(function(k) {
  894. return smalltalk.packages[k];
  895. })
  896. >
  897. !
  898. removePackage: packageName
  899. "Removes a package and all its classes."
  900. | pkg |
  901. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  902. pkg classes do: [ :each |
  903. self removeClass: each ].
  904. self deletePackage: packageName
  905. !
  906. renamePackage: packageName to: newName
  907. "Rename a package."
  908. | pkg |
  909. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  910. (self packageAt: newName) ifNotNil: [ self error: 'Already exists a package called: ', newName ].
  911. (self at: 'packages') at: newName put: pkg.
  912. pkg name: newName.
  913. self deletePackage: packageName.
  914. ! !
  915. !SmalltalkImage methodsFor: 'private'!
  916. basicCreatePackage: packageName
  917. "Create and bind a new bare package with given name and return it."
  918. <return smalltalk.addPackage(packageName)>
  919. !
  920. basicParse: aString
  921. ^ SmalltalkParser parse: aString
  922. !
  923. createPackage: packageName properties: aDict
  924. "Needed to import .st files: they begin with this call."
  925. self deprecatedAPI.
  926. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  927. ^ self createPackage: packageName
  928. !
  929. deleteClass: aClass
  930. "Deletes a class by deleting its binding only. Use #removeClass instead"
  931. <smalltalk.removeClass(aClass)>
  932. !
  933. deletePackage: packageName
  934. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  935. To remove a package, use #removePackage instead."
  936. <delete smalltalk.packages[packageName]>
  937. ! !
  938. !SmalltalkImage methodsFor: 'testing'!
  939. isSmalltalkObject: anObject
  940. "Consider anObject a Smalltalk object if it has a 'klass' property.
  941. Note that this may be unaccurate"
  942. <return typeof anObject.klass !!== 'undefined'>
  943. ! !
  944. SmalltalkImage class instanceVariableNames: 'current'!
  945. !SmalltalkImage class methodsFor: 'initialization'!
  946. initialize
  947. globals at: 'Smalltalk' put: self current
  948. ! !
  949. !SmalltalkImage class methodsFor: 'instance creation'!
  950. current
  951. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  952. !
  953. new
  954. self shouldNotImplement
  955. ! !
  956. !SequenceableCollection methodsFor: '*Kernel-Infrastructure'!
  957. do: aBlock displayingProgress: aString
  958. ProgressHandler
  959. do: aBlock
  960. on: self
  961. displaying: aString
  962. ! !
  963. !String methodsFor: '*Kernel-Infrastructure'!
  964. asJavaScriptSelector
  965. "Return first keyword of the selector, without trailing colon."
  966. ^ self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  967. !
  968. asSetting
  969. ^ Setting at: self ifAbsent: nil
  970. !
  971. asSettingIfAbsent: aString
  972. ^ Setting at: self ifAbsent: aString
  973. !
  974. settingValue
  975. ^ self asSetting value
  976. !
  977. settingValue: aString
  978. ^ self asSetting value: aString
  979. !
  980. settingValueIfAbsent: aString
  981. ^ (self asSettingIfAbsent: aString) value
  982. ! !