1
0

Kernel-Infrastructure.st 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  516. !Package class methodsFor: 'accessing'!
  517. named: aPackageName
  518. ^ Smalltalk
  519. packageAt: aPackageName
  520. ifAbsent: [
  521. Smalltalk createPackage: aPackageName ]
  522. !
  523. named: aPackageName ifAbsent: aBlock
  524. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  525. !
  526. named: aPackageName transport: aTransport
  527. | package |
  528. package := self named: aPackageName.
  529. package transport: aTransport.
  530. ^ package
  531. ! !
  532. !Package class methodsFor: 'sorting'!
  533. sortedClasses: classes
  534. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  535. | children others nodes expandedClasses |
  536. children := #().
  537. others := #().
  538. classes do: [ :each |
  539. (classes includes: each superclass)
  540. ifFalse: [ children add: each ]
  541. ifTrue: [ others add: each ]].
  542. nodes := children collect: [ :each |
  543. ClassSorterNode on: each classes: others level: 0 ].
  544. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  545. expandedClasses := Array new.
  546. nodes do: [ :aNode |
  547. aNode traverseClassesWith: expandedClasses ].
  548. ^ expandedClasses
  549. ! !
  550. Object subclass: #PlatformInterface
  551. instanceVariableNames: ''
  552. package: 'Kernel-Infrastructure'!
  553. !PlatformInterface commentStamp!
  554. I am single entry point to UI and environment interface.
  555. My `initialize` tries several options (for now, browser environment only) to set myself up.
  556. ## API
  557. PlatformInterface alert: 'Hey, there is a problem'.
  558. PlatformInterface confirm: 'Affirmative?'.
  559. PlatformInterface prompt: 'Your name:'.
  560. PlatformInterface ajax: #{
  561. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  562. }.!
  563. PlatformInterface class instanceVariableNames: 'worker'!
  564. !PlatformInterface class methodsFor: 'accessing'!
  565. globals
  566. <return (new Function('return this'))();>
  567. !
  568. setWorker: anObject
  569. worker := anObject
  570. ! !
  571. !PlatformInterface class methodsFor: 'actions'!
  572. ajax: anObject
  573. ^ worker
  574. ifNotNil: [ worker ajax: anObject ]
  575. ifNil: [ self error: 'ajax: not available' ]
  576. !
  577. alert: aString
  578. ^ worker
  579. ifNotNil: [ worker alert: aString ]
  580. ifNil: [ self error: 'alert: not available' ]
  581. !
  582. confirm: aString
  583. ^ worker
  584. ifNotNil: [ worker confirm: aString ]
  585. ifNil: [ self error: 'confirm: not available' ]
  586. !
  587. existsGlobal: aString
  588. ^ PlatformInterface globals
  589. at: aString
  590. ifPresent: [ true ]
  591. ifAbsent: [ false ]
  592. !
  593. prompt: aString
  594. ^ worker
  595. ifNotNil: [ worker prompt: aString ]
  596. ifNil: [ self error: 'prompt: not available' ]
  597. ! !
  598. !PlatformInterface class methodsFor: 'initialization'!
  599. initialize
  600. | candidate |
  601. super initialize.
  602. BrowserInterface ifNotNil: [
  603. candidate := BrowserInterface new.
  604. candidate isAvailable ifTrue: [ self setWorker: candidate. ^ self ]
  605. ]
  606. ! !
  607. Object subclass: #Service
  608. instanceVariableNames: ''
  609. package: 'Kernel-Infrastructure'!
  610. !Service commentStamp!
  611. I implement the basic behavior for class registration to a service.
  612. See the `Transcript` class for a concrete service.
  613. ## API
  614. Use class-side methods `#register:` and `#registerIfNone:` to register classes to a specific service.!
  615. Service class instanceVariableNames: 'current'!
  616. !Service class methodsFor: 'accessing'!
  617. current
  618. ^ current
  619. ! !
  620. !Service class methodsFor: 'instance creation'!
  621. new
  622. self shouldNotImplement
  623. ! !
  624. !Service class methodsFor: 'registration'!
  625. register: anObject
  626. current := anObject
  627. !
  628. registerIfNone: anObject
  629. self current ifNil: [ self register: anObject ]
  630. ! !
  631. Service subclass: #ErrorHandler
  632. instanceVariableNames: ''
  633. package: 'Kernel-Infrastructure'!
  634. !ErrorHandler commentStamp!
  635. I am the service used to handle Smalltalk errors.
  636. See `boot.js` `handleError()` function.
  637. Registered service instances must implement `#handleError:` to perform an action on the thrown exception.!
  638. !ErrorHandler class methodsFor: 'error handling'!
  639. handleError: anError
  640. self current handleError: anError
  641. ! !
  642. Service subclass: #Finder
  643. instanceVariableNames: ''
  644. package: 'Kernel-Infrastructure'!
  645. !Finder commentStamp!
  646. I am the service responsible for finding classes/methods.
  647. __There is no default finder.__
  648. ## API
  649. Use `#browse` on an object to find it.!
  650. !Finder class methodsFor: 'finding'!
  651. findClass: aClass
  652. ^ self current findClass: aClass
  653. !
  654. findMethod: aCompiledMethod
  655. ^ self current findMethod: aCompiledMethod
  656. !
  657. findString: aString
  658. ^ self current findString: aString
  659. ! !
  660. Service subclass: #Inspector
  661. instanceVariableNames: ''
  662. package: 'Kernel-Infrastructure'!
  663. !Inspector commentStamp!
  664. I am the service responsible for inspecting objects.
  665. The default inspector object is the transcript.!
  666. !Inspector class methodsFor: 'inspecting'!
  667. inspect: anObject
  668. ^ self current inspect: anObject
  669. ! !
  670. Service subclass: #ProgressHandler
  671. instanceVariableNames: ''
  672. package: 'Kernel-Infrastructure'!
  673. !ProgressHandler commentStamp!
  674. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  675. Registered instances must implement `#do:on:displaying:`.
  676. The default behavior is to simply iterate over the collection, using `NullProgressHandler`.!
  677. !ProgressHandler class methodsFor: 'progress handling'!
  678. do: aBlock on: aCollection displaying: aString
  679. self current do: aBlock on: aCollection displaying: aString
  680. ! !
  681. Service subclass: #Transcript
  682. instanceVariableNames: ''
  683. package: 'Kernel-Infrastructure'!
  684. !Transcript commentStamp!
  685. I am a facade for Transcript actions.
  686. I delegate actions to the currently registered transcript.
  687. ## API
  688. Transcript
  689. show: 'hello world';
  690. cr;
  691. show: anObject.!
  692. !Transcript class methodsFor: 'instance creation'!
  693. open
  694. self current open
  695. ! !
  696. !Transcript class methodsFor: 'printing'!
  697. clear
  698. self current clear
  699. !
  700. cr
  701. self current show: String cr
  702. !
  703. inspect: anObject
  704. self show: anObject
  705. !
  706. show: anObject
  707. self current show: anObject
  708. ! !
  709. Object subclass: #Setting
  710. instanceVariableNames: 'key value defaultValue'
  711. package: 'Kernel-Infrastructure'!
  712. !Setting commentStamp!
  713. I represent a setting accessible via `Smalltalk settings`.
  714. ## API
  715. A `Setting` value can be read using `value` and set using `value:`.
  716. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: 'defaultValue'`.!
  717. !Setting methodsFor: 'accessing'!
  718. defaultValue
  719. ^ defaultValue
  720. !
  721. defaultValue: anObject
  722. defaultValue := anObject
  723. !
  724. key
  725. ^ key
  726. !
  727. key: anObject
  728. key := anObject
  729. !
  730. value
  731. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  732. !
  733. value: aString
  734. ^ Smalltalk settings at: self key put: aString
  735. ! !
  736. !Setting class methodsFor: 'instance creation'!
  737. at: aString ifAbsent: anotherString
  738. ^ super new
  739. key: aString;
  740. defaultValue: anotherString;
  741. yourself
  742. !
  743. new
  744. self shouldNotImplement
  745. ! !
  746. Object subclass: #SmalltalkImage
  747. instanceVariableNames: ''
  748. package: 'Kernel-Infrastructure'!
  749. !SmalltalkImage commentStamp!
  750. I represent the Smalltalk system, wrapping
  751. operations of variable `smalltalk` declared in `support/boot.js`.
  752. ## API
  753. I have only one instance, accessed with global variable `Smalltalk`.
  754. The `smalltalk` object holds all class and packages defined in the system.
  755. ## Classes
  756. Classes can be accessed using the following methods:
  757. - `#classes` answers the full list of Smalltalk classes in the system
  758. - `#at:` answers a specific class or `nil`
  759. ## Packages
  760. Packages can be accessed using the following methods:
  761. - `#packages` answers the full list of packages
  762. - `#packageAt:` answers a specific package or `nil`
  763. ## Parsing
  764. The `#parse:` method is used to parse Amber source code.
  765. It requires the `Compiler` package and the `support/parser.js` parser file in order to work.!
  766. !SmalltalkImage methodsFor: 'accessing'!
  767. at: aString
  768. self deprecatedAPI.
  769. ^ self globals at: aString
  770. !
  771. at: aKey ifAbsent: aBlock
  772. ^ (self includesKey: aKey)
  773. ifTrue: [ self at: aKey ]
  774. ifFalse: [ aBlock value ]
  775. !
  776. at: aString put: anObject
  777. self deprecatedAPI.
  778. ^ self globals at: aString put: anObject
  779. !
  780. current
  781. "Backward compatibility for Smalltalk current ..."
  782. self deprecatedAPI.
  783. ^ self
  784. !
  785. globals
  786. "Future compatibility to be able to use Smalltalk globals at: ..."
  787. <return globals>
  788. !
  789. includesKey: aKey
  790. <return smalltalk.hasOwnProperty(aKey)>
  791. !
  792. parse: aString
  793. | result |
  794. self
  795. try: [ result := self basicParse: aString ]
  796. catch: [ :ex | (self parseError: ex parsing: aString) signal ].
  797. ^ result
  798. source: aString;
  799. yourself
  800. !
  801. pseudoVariableNames
  802. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  803. !
  804. readJSObject: anObject
  805. <return smalltalk.readJSObject(anObject)>
  806. !
  807. reservedWords
  808. "JavaScript reserved words"
  809. <return smalltalk.reservedWords>
  810. !
  811. settings
  812. ^ SmalltalkSettings
  813. !
  814. version
  815. "Answer the version string of Amber"
  816. ^ '0.13.0-pre'
  817. !
  818. vm
  819. "Future compatibility to be able to use Smalltalk vm ..."
  820. <return smalltalk>
  821. ! !
  822. !SmalltalkImage methodsFor: 'accessing amd'!
  823. amdRequire
  824. ^ self vm at: 'amdRequire'
  825. !
  826. defaultAmdNamespace
  827. ^ 'transport.defaultAmdNamespace' settingValue
  828. !
  829. defaultAmdNamespace: aString
  830. 'transport.defaultAmdNamespace' settingValue: aString
  831. ! !
  832. !SmalltalkImage methodsFor: 'classes'!
  833. classes
  834. <return smalltalk.classes()>
  835. !
  836. removeClass: aClass
  837. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  838. self deleteClass: aClass.
  839. SystemAnnouncer current
  840. announce: (ClassRemoved new
  841. theClass: aClass;
  842. yourself)
  843. ! !
  844. !SmalltalkImage methodsFor: 'error handling'!
  845. asSmalltalkException: anObject
  846. "A JavaScript exception may be thrown.
  847. We then need to convert it back to a Smalltalk object"
  848. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  849. ifTrue: [ anObject ]
  850. ifFalse: [ JavaScriptException on: anObject ]
  851. !
  852. parseError: anException parsing: aString
  853. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  854. ! !
  855. !SmalltalkImage methodsFor: 'globals'!
  856. addGlobalJsVariable: aString
  857. self globalJsVariables add: aString
  858. !
  859. deleteGlobalJsVariable: aString
  860. self globalJsVariables remove: aString ifAbsent:[]
  861. !
  862. globalJsVariables
  863. "Array of global JavaScript variables"
  864. <return smalltalk.globalJsVariables>
  865. ! !
  866. !SmalltalkImage methodsFor: 'packages'!
  867. createPackage: packageName
  868. | package announcement |
  869. package := self basicCreatePackage: packageName.
  870. announcement := PackageAdded new
  871. package: package;
  872. yourself.
  873. SystemAnnouncer current announce: announcement.
  874. ^ package
  875. !
  876. packageAt: packageName
  877. <return smalltalk.packages[packageName]>
  878. !
  879. packageAt: packageName ifAbsent: aBlock
  880. ^ (self packageAt: packageName) ifNil: aBlock
  881. !
  882. packages
  883. "Return all Package instances in the system."
  884. <
  885. return Object.keys(smalltalk.packages).map(function(k) {
  886. return smalltalk.packages[k];
  887. })
  888. >
  889. !
  890. removePackage: packageName
  891. "Removes a package and all its classes."
  892. | pkg |
  893. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  894. pkg classes do: [ :each |
  895. self removeClass: each ].
  896. self deletePackage: packageName
  897. !
  898. renamePackage: packageName to: newName
  899. "Rename a package."
  900. | pkg |
  901. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  902. (self packageAt: newName) ifNotNil: [ self error: 'Already exists a package called: ', newName ].
  903. (self at: 'packages') at: newName put: pkg.
  904. pkg name: newName.
  905. self deletePackage: packageName.
  906. ! !
  907. !SmalltalkImage methodsFor: 'private'!
  908. basicCreatePackage: packageName
  909. "Create and bind a new bare package with given name and return it."
  910. <return smalltalk.addPackage(packageName)>
  911. !
  912. basicParse: aString
  913. ^ SmalltalkParser parse: aString
  914. !
  915. createPackage: packageName properties: aDict
  916. "Needed to import .st files: they begin with this call."
  917. self deprecatedAPI.
  918. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  919. ^ self createPackage: packageName
  920. !
  921. deleteClass: aClass
  922. "Deletes a class by deleting its binding only. Use #removeClass instead"
  923. <smalltalk.removeClass(aClass)>
  924. !
  925. deletePackage: packageName
  926. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  927. To remove a package, use #removePackage instead."
  928. <delete smalltalk.packages[packageName]>
  929. ! !
  930. !SmalltalkImage methodsFor: 'testing'!
  931. isSmalltalkObject: anObject
  932. "Consider anObject a Smalltalk object if it has a 'klass' property.
  933. Note that this may be unaccurate"
  934. <return typeof anObject.klass !!== 'undefined'>
  935. ! !
  936. SmalltalkImage class instanceVariableNames: 'current'!
  937. !SmalltalkImage class methodsFor: 'initialization'!
  938. initialize
  939. globals at: 'Smalltalk' put: self current
  940. ! !
  941. !SmalltalkImage class methodsFor: 'instance creation'!
  942. current
  943. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  944. !
  945. new
  946. self shouldNotImplement
  947. ! !
  948. !SequenceableCollection methodsFor: '*Kernel-Infrastructure'!
  949. do: aBlock displayingProgress: aString
  950. ProgressHandler
  951. do: aBlock
  952. on: self
  953. displaying: aString
  954. ! !
  955. !String methodsFor: '*Kernel-Infrastructure'!
  956. asJavaScriptSelector
  957. "Return first keyword of the selector, without trailing colon."
  958. ^ self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  959. !
  960. asSetting
  961. ^ Setting at: self ifAbsent: nil
  962. !
  963. asSettingIfAbsent: aString
  964. ^ Setting at: self ifAbsent: aString
  965. !
  966. settingValue
  967. ^ self asSetting value
  968. !
  969. settingValue: aString
  970. ^ self asSetting value: aString
  971. !
  972. settingValueIfAbsent: aString
  973. ^ (self asSettingIfAbsent: aString) value
  974. ! !