Kernel-Infrastructure.st 26 KB

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