Kernel-Infrastructure.st 29 KB

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