Kernel-Infrastructure.st 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. Smalltalk createPackage: 'Kernel-Infrastructure'!
  2. Object subclass: #AmberBootstrapInitialization
  3. instanceVariableNames: ''
  4. package: 'Kernel-Infrastructure'!
  5. !AmberBootstrapInitialization class methodsFor: 'initialization'!
  6. initializeClasses
  7. SmalltalkImage initialize.
  8. Smalltalk classes do: [ :each |
  9. each = SmalltalkImage ifFalse: [ each initialize ] ]
  10. ! !
  11. !AmberBootstrapInitialization class methodsFor: 'public api'!
  12. run
  13. self initializeClasses
  14. ! !
  15. ProtoObject subclass: #JSObjectProxy
  16. instanceVariableNames: 'jsObject'
  17. package: 'Kernel-Infrastructure'!
  18. !JSObjectProxy commentStamp!
  19. I handle sending messages to JavaScript objects, making JavaScript object accessing from Amber fully transparent.
  20. My instances make intensive use of `#doesNotUnderstand:`.
  21. My instances are automatically created by Amber whenever a message is sent to a JavaScript object.
  22. ## Usage examples
  23. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  24. window alert: 'hello world'.
  25. window inspect.
  26. (window jQuery: 'body') append: 'hello world'
  27. 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.
  28. ## Message conversion rules
  29. - `someUser name` becomes `someUser.name`
  30. - `someUser name: 'John'` becomes `someUser name = "John"`
  31. - `console log: 'hello world'` becomes `console.log('hello world')`
  32. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  33. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  34. !JSObjectProxy methodsFor: 'accessing'!
  35. at: aString
  36. <inlineJS: 'return $self[''@jsObject''][aString]'>
  37. !
  38. at: aString ifAbsent: aBlock
  39. "return the aString property or evaluate aBlock if the property is not defined on the object"
  40. <inlineJS: '
  41. var obj = $self[''@jsObject''];
  42. return aString in obj ? obj[aString] : aBlock._value();
  43. '>
  44. !
  45. at: aString ifPresent: aBlock
  46. "return the evaluation of aBlock with the value if the property is defined or return nil"
  47. <inlineJS: '
  48. var obj = $self[''@jsObject''];
  49. return aString in obj ? aBlock._value_(obj[aString]) : nil;
  50. '>
  51. !
  52. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  53. "return the evaluation of aBlock with the value if the property is defined
  54. or return value of anotherBlock"
  55. <inlineJS: '
  56. var obj = $self[''@jsObject''];
  57. return aString in obj ? aBlock._value_(obj[aString]) : anotherBlock._value();
  58. '>
  59. !
  60. at: aString put: anObject
  61. <inlineJS: 'return $self[''@jsObject''][aString] = anObject'>
  62. !
  63. in: aValuable
  64. ^ aValuable value: jsObject
  65. !
  66. jsObject
  67. ^ jsObject
  68. ! !
  69. !JSObjectProxy methodsFor: 'comparing'!
  70. = anObject
  71. anObject class == self class ifFalse: [ ^ false ].
  72. ^ JSObjectProxy compareJSObjectOfProxy: self withProxy: anObject
  73. ! !
  74. !JSObjectProxy methodsFor: 'converting'!
  75. asJavaScriptObject
  76. "Answers the receiver in a stringify-friendly fashion"
  77. ^ jsObject
  78. ! !
  79. !JSObjectProxy methodsFor: 'enumerating'!
  80. keysAndValuesDo: aBlock
  81. <inlineJS: '
  82. var o = $self[''@jsObject''];
  83. for(var i in o) {
  84. aBlock._value_value_(i, o[i]);
  85. }
  86. '>
  87. ! !
  88. !JSObjectProxy methodsFor: 'printing'!
  89. printOn: aStream
  90. aStream nextPutAll: self printString
  91. !
  92. printString
  93. <inlineJS: '
  94. var js = $self[''@jsObject''];
  95. return js.toString
  96. ? js.toString()
  97. : Object.prototype.toString.call(js)
  98. '>
  99. ! !
  100. !JSObjectProxy methodsFor: 'promises'!
  101. catch: aBlock
  102. (NativeFunction isNativeFunction: (self at: #then))
  103. ifTrue: [ ^ (TThenable >> #catch:) sendTo: jsObject arguments: {aBlock} ]
  104. ifFalse: [ ^ super catch: aBlock ]
  105. !
  106. on: aClass do: aBlock
  107. (NativeFunction isNativeFunction: (self at: #then))
  108. ifTrue: [ ^ (TThenable >> #on:do:) sendTo: jsObject arguments: {aClass. aBlock} ]
  109. ifFalse: [ ^ super on: aClass do: aBlock ]
  110. !
  111. then: aBlockOrArray
  112. (NativeFunction isNativeFunction: (self at: #then))
  113. ifTrue: [ ^ (TThenable >> #then:) sendTo: jsObject arguments: {aBlockOrArray} ]
  114. ifFalse: [ ^ super then: aBlockOrArray ]
  115. ! !
  116. !JSObjectProxy methodsFor: 'proxy'!
  117. doesNotUnderstand: aMessage
  118. ^ (JSObjectProxy lookupProperty: aMessage selector asJavaScriptPropertyName ofProxy: self)
  119. ifNil: [ super doesNotUnderstand: aMessage ]
  120. ifNotNil: [ :jsSelector |
  121. JSObjectProxy
  122. forwardMessage: jsSelector
  123. withArguments: aMessage arguments
  124. ofProxy: self ]
  125. ! !
  126. !JSObjectProxy methodsFor: 'streaming'!
  127. putOn: aStream
  128. aStream nextPutJSObject: jsObject
  129. ! !
  130. !JSObjectProxy class methodsFor: 'instance creation'!
  131. on: aJSObject
  132. | instance |
  133. instance := self new.
  134. self jsObject: aJSObject ofProxy: instance.
  135. ^ instance
  136. ! !
  137. !JSObjectProxy class methodsFor: 'proxy'!
  138. addObjectVariablesTo: aDictionary ofProxy: aProxy
  139. <inlineJS: '
  140. var jsObject = aProxy[''@jsObject''];
  141. for(var i in jsObject) {
  142. aDictionary._at_put_(i, jsObject[i]);
  143. }
  144. '>
  145. !
  146. compareJSObjectOfProxy: aProxy withProxy: anotherProxy
  147. <inlineJS: '
  148. var anotherJSObject = anotherProxy.a$cls ? anotherProxy["@jsObject"] : anotherProxy;
  149. return aProxy["@jsObject"] === anotherJSObject
  150. '>
  151. !
  152. forwardMessage: aString withArguments: anArray ofProxy: aProxy
  153. <inlineJS: '
  154. return $core.accessJavaScript(aProxy._jsObject(), aString, anArray);
  155. '>
  156. !
  157. jsObject: aJSObject ofProxy: aProxy
  158. <inlineJS: 'aProxy[''@jsObject''] = aJSObject'>
  159. !
  160. lookupProperty: aString ofProxy: aProxy
  161. "Looks up a property in JS object.
  162. Answer the property if it is present, or nil if it is not present."
  163. <inlineJS: 'return aString in aProxy._jsObject() ? aString : nil'>
  164. ! !
  165. Object subclass: #Organizer
  166. instanceVariableNames: ''
  167. package: 'Kernel-Infrastructure'!
  168. !Organizer commentStamp!
  169. I represent categorization information.
  170. ## API
  171. Use `#addElement:` and `#removeElement:` to manipulate instances.!
  172. !Organizer methodsFor: 'accessing'!
  173. addElement: anObject
  174. <inlineJS: '$core.addElement(self.elements, anObject)'>
  175. !
  176. elements
  177. ^ (self basicAt: 'elements') copy
  178. !
  179. removeElement: anObject
  180. <inlineJS: '$core.removeElement(self.elements, anObject)'>
  181. ! !
  182. Organizer subclass: #ClassOrganizer
  183. instanceVariableNames: ''
  184. package: 'Kernel-Infrastructure'!
  185. !ClassOrganizer commentStamp!
  186. I am an organizer specific to classes. I hold method categorization information for classes.!
  187. !ClassOrganizer methodsFor: 'accessing'!
  188. addElement: aString
  189. super addElement: aString.
  190. SystemAnnouncer current announce: (ProtocolAdded new
  191. protocol: aString;
  192. theClass: self theClass;
  193. yourself)
  194. !
  195. removeElement: aString
  196. super removeElement: aString.
  197. SystemAnnouncer current announce: (ProtocolRemoved new
  198. protocol: aString;
  199. theClass: self theClass;
  200. yourself)
  201. !
  202. theClass
  203. <inlineJS: 'return self.theClass'>
  204. ! !
  205. Organizer subclass: #PackageOrganizer
  206. instanceVariableNames: ''
  207. package: 'Kernel-Infrastructure'!
  208. !PackageOrganizer commentStamp!
  209. I am an organizer specific to packages. I hold classes categorization information.!
  210. Object subclass: #Package
  211. instanceVariableNames: 'transport imports dirty'
  212. package: 'Kernel-Infrastructure'!
  213. !Package commentStamp!
  214. 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.
  215. 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.
  216. ## API
  217. Packages are manipulated through "Smalltalk current", like for example finding one based on a name or with `Package class >> #name` directly:
  218. Smalltalk current packageAt: 'Kernel'
  219. Package named: 'Kernel'
  220. 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.
  221. You can fetch a package from the server:
  222. Package load: 'Additional-Examples'!
  223. !Package methodsFor: 'accessing'!
  224. beClean
  225. dirty := false.
  226. SystemAnnouncer current announce: (PackageClean new
  227. package: self;
  228. yourself)
  229. !
  230. beDirty
  231. dirty := true.
  232. SystemAnnouncer current announce: (PackageDirty new
  233. package: self;
  234. yourself)
  235. !
  236. classTemplate
  237. ^ String streamContents: [ :stream | stream
  238. write: 'Object subclass: #NameOfSubclass'; lf;
  239. tab; write: 'instanceVariableNames: '''''; lf;
  240. tab; write: 'package: '; print: self name ]
  241. !
  242. definition
  243. ^ String streamContents: [ :stream | stream
  244. write: self class name; lf;
  245. tab; write: 'named: '; print: self name; lf;
  246. tab; write: { 'imports: '. self importsDefinition }; lf;
  247. tab; write: { 'transport: ('. self transport definition. ')' } ]
  248. !
  249. imports
  250. ^ imports ifNil: [
  251. | parsed |
  252. parsed := self importsFromJson: self basicImports.
  253. self imports: parsed.
  254. imports ]
  255. !
  256. imports: anArray
  257. self validateImports: anArray.
  258. imports := anArray asSet
  259. !
  260. importsDefinition
  261. ^ String streamContents: [ :stream |
  262. stream write: '{'.
  263. self sortedImportsAsArray
  264. do: [ :each | stream print: each ]
  265. separatedBy: [ stream write: '. ' ].
  266. stream write: '}' ]
  267. !
  268. name
  269. <inlineJS: 'return self.pkgName'>
  270. !
  271. name: aString
  272. self basicName: aString.
  273. self beDirty
  274. !
  275. organization
  276. ^ self basicAt: 'organization'
  277. !
  278. transport
  279. ^ transport ifNil: [
  280. transport := (PackageTransport fromJson: self basicTransport)
  281. package: self;
  282. yourself ]
  283. !
  284. transport: aPackageTransport
  285. transport := aPackageTransport.
  286. aPackageTransport package: self
  287. ! !
  288. !Package methodsFor: 'classes'!
  289. classes
  290. ^ self organization elements
  291. !
  292. setupClasses
  293. self classes do: [ :each | each initialize ]
  294. !
  295. sortedClasses
  296. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  297. ^ self class sortedClasses: self classes
  298. ! !
  299. !Package methodsFor: 'converting'!
  300. importsAsJson
  301. ^ self sortedImportsAsArray collect: [ :each |
  302. each isString
  303. ifTrue: [ each ]
  304. ifFalse: [ each key, '=', each value ]]
  305. !
  306. importsFromJson: anArray
  307. "Parses array of string, eg. #('asdf' 'qwer=tyuo')
  308. into array of Strings and Associations,
  309. eg. {'asdf'. 'qwer'->'tyuo'}"
  310. ^ anArray collect: [ :each |
  311. | split |
  312. split := each tokenize: '='.
  313. split size = 1
  314. ifTrue: [ split first ]
  315. ifFalse: [ split first -> split second ]]
  316. ! !
  317. !Package methodsFor: 'dependencies'!
  318. loadDependencies
  319. "Returns list of packages that need to be loaded
  320. before loading this package."
  321. | classes packages |
  322. classes := self loadDependencyClasses.
  323. ^ (classes collect: [ :each | each package ]) asSet
  324. remove: self ifAbsent: [];
  325. yourself
  326. !
  327. loadDependencyClasses
  328. "Returns classes needed at the time of loading a package.
  329. These are all that are used to subclass
  330. and to define an extension method
  331. as well as all traits used"
  332. | starCategoryName |
  333. starCategoryName := '*', self name.
  334. ^ (self classes collect: [ :each | each superclass ]) asSet
  335. addAll: (Smalltalk classes select: [ :each |
  336. each protocols, (each theMetaClass ifNil: [ #() ] ifNotNil: [ :meta | meta protocols])
  337. includes: starCategoryName ]);
  338. addAll: (Array streamContents: [ :as | self traitCompositions valuesDo: [ :each | as write: (each collect: [ :eachTT | eachTT trait ])]]);
  339. remove: nil ifAbsent: [];
  340. yourself
  341. !
  342. traitCompositions
  343. | traitCompositions |
  344. traitCompositions := Dictionary new.
  345. self classes do: [ :each |
  346. traitCompositions at: each put: each traitComposition.
  347. each theMetaClass ifNotNil: [ :meta | traitCompositions at: meta put: meta traitComposition ] ].
  348. ^ traitCompositions reject: [ :each | each isEmpty ]
  349. ! !
  350. !Package methodsFor: 'printing'!
  351. printOn: aStream
  352. super printOn: aStream.
  353. aStream
  354. nextPutAll: ' (';
  355. nextPutAll: self name;
  356. nextPutAll: ')'
  357. ! !
  358. !Package methodsFor: 'private'!
  359. basicImports
  360. "Answer the imports literal JavaScript object as setup in the JavaScript file, if any"
  361. <inlineJS: 'return self.imports || []'>
  362. !
  363. basicName: aString
  364. <inlineJS: 'self.pkgName = aString'>
  365. !
  366. basicTransport
  367. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  368. <inlineJS: 'return self.transport'>
  369. !
  370. sortedImportsAsArray
  371. "Answer imports sorted first by type (associations first),
  372. then by value"
  373. ^ self imports asArray
  374. sorted: [ :a :b |
  375. a isString not & b isString or: [
  376. a isString = b isString and: [
  377. a value <= b value ]]]
  378. ! !
  379. !Package methodsFor: 'testing'!
  380. isDirty
  381. ^ dirty ifNil: [ false ]
  382. !
  383. isPackage
  384. ^ true
  385. ! !
  386. !Package methodsFor: 'validation'!
  387. validateImports: aCollection
  388. aCollection do: [ :import |
  389. import isString ifFalse: [
  390. (import respondsTo: #key) ifFalse: [
  391. self error: 'Imports must be Strings or Associations' ].
  392. import key isString & import value isString ifFalse: [
  393. self error: 'Key and value must be Strings' ].
  394. (import key match: '^[a-zA-Z][a-zA-Z0-9]*$') ifFalse: [
  395. self error: 'Keys must be identifiers' ]]]
  396. ! !
  397. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  398. !Package class methodsFor: 'accessing'!
  399. named: aPackageName
  400. ^ Smalltalk
  401. packageAt: aPackageName
  402. ifAbsent: [
  403. Smalltalk createPackage: aPackageName ]
  404. !
  405. named: aPackageName ifAbsent: aBlock
  406. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  407. !
  408. named: aPackageName imports: anArray transport: aTransport
  409. | package |
  410. package := self named: aPackageName.
  411. package imports: anArray.
  412. package transport: aTransport.
  413. ^ package
  414. !
  415. named: aPackageName transport: aTransport
  416. | package |
  417. package := self named: aPackageName.
  418. package transport: aTransport.
  419. ^ package
  420. ! !
  421. !Package class methodsFor: 'sorting'!
  422. sortedClasses: classes
  423. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  424. | children others nodes expandedClasses |
  425. children := #().
  426. others := #().
  427. classes do: [ :each |
  428. (classes includes: each superclass)
  429. ifFalse: [ children add: each ]
  430. ifTrue: [ others add: each ]].
  431. nodes := children collect: [ :each |
  432. ClassSorterNode on: each classes: others level: 0 ].
  433. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  434. expandedClasses := Array new.
  435. nodes do: [ :aNode |
  436. aNode traverseClassesWith: expandedClasses ].
  437. ^ expandedClasses
  438. ! !
  439. Object subclass: #PackageStateObserver
  440. instanceVariableNames: ''
  441. package: 'Kernel-Infrastructure'!
  442. !PackageStateObserver commentStamp!
  443. My current instance listens for any changes in the system that might affect the state of a package (being dirty).!
  444. !PackageStateObserver methodsFor: 'accessing'!
  445. announcer
  446. ^ SystemAnnouncer current
  447. ! !
  448. !PackageStateObserver methodsFor: 'actions'!
  449. observeSystem
  450. self announcer
  451. on: PackageAdded
  452. send: #onPackageAdded:
  453. to: self;
  454. on: ClassAnnouncement
  455. send: #onClassModification:
  456. to: self;
  457. on: MethodAnnouncement
  458. send: #onMethodModification:
  459. to: self;
  460. on: ProtocolAnnouncement
  461. send: #onProtocolModification:
  462. to: self
  463. ! !
  464. !PackageStateObserver methodsFor: 'reactions'!
  465. onClassModification: anAnnouncement
  466. anAnnouncement theClass ifNotNil: [ :theClass | theClass package beDirty ]
  467. !
  468. onMethodModification: anAnnouncement
  469. anAnnouncement method package ifNotNil: [ :package | package beDirty ]
  470. !
  471. onPackageAdded: anAnnouncement
  472. anAnnouncement package beDirty
  473. !
  474. onProtocolModification: anAnnouncement
  475. anAnnouncement package ifNotNil: [ :package | package beDirty ]
  476. ! !
  477. PackageStateObserver class instanceVariableNames: 'current'!
  478. !PackageStateObserver class methodsFor: 'accessing'!
  479. current
  480. ^ current ifNil: [ current := self new ]
  481. ! !
  482. !PackageStateObserver class methodsFor: 'initialization'!
  483. initialize
  484. self current observeSystem
  485. ! !
  486. Error subclass: #ParseError
  487. instanceVariableNames: ''
  488. package: 'Kernel-Infrastructure'!
  489. !ParseError commentStamp!
  490. Instance of ParseError are signaled on any parsing error.
  491. See `Smalltalk >> #parse:`!
  492. Object subclass: #Setting
  493. instanceVariableNames: 'key value defaultValue'
  494. package: 'Kernel-Infrastructure'!
  495. !Setting commentStamp!
  496. I represent a setting **stored** at `Smalltalk settings`.
  497. In the current implementation, `Smalltalk settings` is an object persisted in the localStorage.
  498. ## API
  499. A `Setting` value can be read using `value` and set using `value:`.
  500. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: aDefaultValue`.
  501. To read the value of a setting you can also use the convenience:
  502. `theValueSet := 'any.characteristic' settingValue`
  503. or with a default using:
  504. `theEnsuredValueSet := 'any.characteristic' settingValueIfAbsent: true`!
  505. !Setting methodsFor: 'accessing'!
  506. defaultValue
  507. ^ defaultValue
  508. !
  509. defaultValue: aStringifiableObject
  510. defaultValue := aStringifiableObject
  511. !
  512. key
  513. ^ key
  514. !
  515. key: aString
  516. key := aString
  517. !
  518. value
  519. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  520. !
  521. value: aStringifiableObject
  522. ^ Smalltalk settings at: self key put: aStringifiableObject
  523. ! !
  524. !Setting class methodsFor: 'instance creation'!
  525. at: aString ifAbsent: aDefaultValue
  526. ^ super new
  527. key: aString;
  528. defaultValue: aDefaultValue;
  529. yourself
  530. !
  531. new
  532. self shouldNotImplement
  533. ! !
  534. Object subclass: #SmalltalkImage
  535. instanceVariableNames: ''
  536. package: 'Kernel-Infrastructure'!
  537. !SmalltalkImage commentStamp!
  538. I represent the Smalltalk system, wrapping
  539. operations of variable `$core` declared in `support/boot.js`.
  540. ## API
  541. I have only one instance, accessed with global variable `Smalltalk`.
  542. ## Classes
  543. Classes can be accessed using the following methods:
  544. - `#classes` answers the full list of Smalltalk classes in the system
  545. - `#globals #at:` answers a specific global (usually, a class) or `nil`
  546. ## Packages
  547. Packages can be accessed using the following methods:
  548. - `#packages` answers the full list of packages
  549. - `#packageAt:` answers a specific package or `nil`
  550. ## Parsing
  551. The `#parse:` method is used to parse Amber source code.
  552. It requires the `Compiler` package and the `support/parser.js` parser file in order to work.!
  553. !SmalltalkImage methodsFor: 'accessing'!
  554. cancelOptOut: anObject
  555. "A Smalltalk object has a 'a$cls' property.
  556. If this property is shadowed for anObject by optOut:,
  557. the object is treated as plain JS object.
  558. This removes the shadow and anObject is Smalltalk object
  559. again if it was before."
  560. <inlineJS: 'delete anObject.klass; delete anObject.a$cls;'>
  561. !
  562. core
  563. <inlineJS: 'return $core'>
  564. !
  565. globals
  566. <inlineJS: 'return $globals'>
  567. !
  568. includesKey: aKey
  569. <inlineJS: 'return $core.hasOwnProperty(aKey)'>
  570. !
  571. optOut: anObject
  572. "A Smalltalk object has a 'a$cls' property.
  573. This shadows the property for anObject.
  574. The object is treated as plain JS object following this."
  575. <inlineJS: 'anObject.klass = null; anObject.a$cls = null'>
  576. !
  577. parse: aString
  578. | result |
  579. [ result := self basicParse: aString ]
  580. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  581. ^ result
  582. source: aString;
  583. yourself
  584. !
  585. pseudoVariableNames
  586. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  587. !
  588. readJSObject: anObject
  589. <inlineJS: 'return $core.readJSObject(anObject)'>
  590. !
  591. reservedWords
  592. "JavaScript reserved words"
  593. <inlineJS: 'return $core.reservedWords'>
  594. !
  595. settings
  596. ^ SmalltalkSettings
  597. !
  598. version
  599. "Answer the version string of Amber"
  600. ^ '0.20.0-pre'
  601. ! !
  602. !SmalltalkImage methodsFor: 'accessing amd'!
  603. amdRequire
  604. ^ self core at: 'amdRequire'
  605. !
  606. defaultAmdNamespace
  607. ^ 'transport.defaultAmdNamespace' settingValue
  608. !
  609. defaultAmdNamespace: aString
  610. 'transport.defaultAmdNamespace' settingValue: aString
  611. ! !
  612. !SmalltalkImage methodsFor: 'classes'!
  613. classes
  614. <inlineJS: 'return $core.classes()'>
  615. !
  616. removeClass: aClass
  617. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  618. aClass allSubclassesDo: [ :subclass | self error: aClass name, ' has a subclass: ', subclass name ].
  619. aClass traitUsers ifNotEmpty: [ self error: aClass name, ' has trait users.' ].
  620. self deleteClass: aClass.
  621. aClass setTraitComposition: #().
  622. aClass class setTraitComposition: #().
  623. SystemAnnouncer current
  624. announce: (ClassRemoved new
  625. theClass: aClass;
  626. yourself)
  627. ! !
  628. !SmalltalkImage methodsFor: 'error handling'!
  629. asSmalltalkException: anObject
  630. "A JavaScript exception may be thrown.
  631. We then need to convert it back to a Smalltalk object"
  632. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  633. ifTrue: [ anObject ]
  634. ifFalse: [ JavaScriptException on: anObject ]
  635. !
  636. parseError: anException parsing: aString
  637. | pos |
  638. pos := (anException basicAt: 'location') start.
  639. ^ ParseError new messageText: 'Parse error on line ', pos line ,' column ' , pos column ,' : Unexpected character ', (anException basicAt: 'found')
  640. ! !
  641. !SmalltalkImage methodsFor: 'globals'!
  642. addGlobalJsVariable: aString
  643. self globalJsVariables add: aString
  644. !
  645. deleteGlobalJsVariable: aString
  646. self globalJsVariables remove: aString ifAbsent:[]
  647. !
  648. globalJsVariables
  649. "Array of global JavaScript variables"
  650. <inlineJS: 'return $core.globalJsVariables'>
  651. ! !
  652. !SmalltalkImage methodsFor: 'packages'!
  653. createPackage: packageName
  654. | package announcement |
  655. package := self basicCreatePackage: packageName.
  656. announcement := PackageAdded new
  657. package: package;
  658. yourself.
  659. SystemAnnouncer current announce: announcement.
  660. ^ package
  661. !
  662. packageAt: packageName
  663. <inlineJS: 'return $core.packages[packageName]'>
  664. !
  665. packageAt: packageName ifAbsent: aBlock
  666. ^ (self packageAt: packageName) ifNil: aBlock
  667. !
  668. packages
  669. "Return all Package instances in the system."
  670. <inlineJS: '
  671. return Object.keys($core.packages).map(function(k) {
  672. return $core.packages[k];
  673. })
  674. '>
  675. !
  676. removePackage: packageName
  677. "Removes a package and all its classes."
  678. | pkg |
  679. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  680. pkg classes do: [ :each |
  681. self removeClass: each ].
  682. self deletePackage: packageName
  683. !
  684. renamePackage: packageName to: newName
  685. "Rename a package."
  686. | pkg |
  687. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  688. (self packageAt: newName) ifNotNil: [ self error: 'Already exists a package called: ', newName ].
  689. pkg name: newName.
  690. self basicRegisterPackage: pkg.
  691. self deletePackage: packageName.
  692. ! !
  693. !SmalltalkImage methodsFor: 'private'!
  694. basicCreatePackage: packageName
  695. "Create and bind a new bare package with given name and return it."
  696. <inlineJS: 'return $core.addPackage(packageName)'>
  697. !
  698. basicParse: aString
  699. ^ SmalltalkParser parse: aString
  700. !
  701. basicRegisterPackage: aPackage
  702. "Put aPackage in $core.packages object."
  703. <inlineJS: '$core.packages[aPackage.pkgName]=aPackage'>
  704. !
  705. deleteClass: aClass
  706. "Deletes a class by deleting its binding only. Use #removeClass instead"
  707. <inlineJS: '$core.removeClass(aClass)'>
  708. !
  709. deletePackage: packageName
  710. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  711. To remove a package, use #removePackage instead."
  712. <inlineJS: 'delete $core.packages[packageName]'>
  713. ! !
  714. !SmalltalkImage methodsFor: 'testing'!
  715. existsJsGlobal: aString
  716. ^ Platform globals
  717. at: aString
  718. ifPresent: [ true ]
  719. ifAbsent: [ false ]
  720. !
  721. isSmalltalkObject: anObject
  722. "Consider anObject a Smalltalk object if it has a 'a$cls' property.
  723. Note that this may be unaccurate"
  724. <inlineJS: 'return anObject.a$cls !!= null'>
  725. ! !
  726. SmalltalkImage class instanceVariableNames: 'current'!
  727. !SmalltalkImage class methodsFor: 'initialization'!
  728. initialize
  729. | st |
  730. st := self current.
  731. st globals at: 'Smalltalk' put: st
  732. ! !
  733. !SmalltalkImage class methodsFor: 'instance creation'!
  734. current
  735. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  736. !
  737. new
  738. self shouldNotImplement
  739. ! !
  740. JSObjectProxy setTraitComposition: {TThenable} asTraitComposition!
  741. ! !
  742. !ProtoStream methodsFor: '*Kernel-Infrastructure'!
  743. nextPutJSObject: aJSObject
  744. self nextPut: aJSObject
  745. ! !
  746. !String methodsFor: '*Kernel-Infrastructure'!
  747. asJavaScriptPropertyName
  748. <inlineJS: 'return $core.st2prop(self)'>
  749. !
  750. asSetting
  751. "Answer aSetting dedicated to locally store a value using this string as key.
  752. Nil will be the default value."
  753. ^ Setting at: self ifAbsent: nil
  754. !
  755. asSettingIfAbsent: aDefaultValue
  756. "Answer aSetting dedicated to locally store a value using this string as key.
  757. Make this setting to have aDefaultValue."
  758. ^ Setting at: self ifAbsent: aDefaultValue
  759. !
  760. settingValue
  761. ^ self asSetting value
  762. !
  763. settingValue: aValue
  764. "Sets the value of the setting that will be locally stored using this string as key.
  765. Note that aValue can be any object that can be stringifyed"
  766. ^ self asSetting value: aValue
  767. !
  768. settingValueIfAbsent: aDefaultValue
  769. "Answer the value of the locally stored setting using this string as key.
  770. Use aDefaultValue in case no setting is found"
  771. ^ (self asSettingIfAbsent: aDefaultValue) value
  772. ! !