Kernel-Infrastructure.st 26 KB

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