Kernel-Infrastructure.st 29 KB

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