Kernel-Infrastructure.st 29 KB

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