Kernel-Infrastructure.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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: 'transport imports dirty'
  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. imports
  262. ^ imports ifNil: [
  263. | parsed |
  264. parsed := self importsFromJson: self basicImports.
  265. self imports: parsed.
  266. imports ]
  267. !
  268. imports: anArray
  269. self validateImports: anArray.
  270. imports := anArray asSet
  271. !
  272. importsDefinition
  273. ^ String streamContents: [ :stream |
  274. stream write: '{'.
  275. self sortedImportsAsArray
  276. do: [ :each | stream print: each ]
  277. separatedBy: [ stream write: '. ' ].
  278. stream write: '}' ]
  279. !
  280. name
  281. <inlineJS: 'return self.pkgName'>
  282. !
  283. name: aString
  284. self basicName: aString.
  285. self beDirty
  286. !
  287. organization
  288. ^ self basicAt: 'organization'
  289. !
  290. transport
  291. ^ transport ifNil: [
  292. transport := (PackageTransport fromJson: self basicTransport)
  293. package: self;
  294. yourself ]
  295. !
  296. transport: aPackageTransport
  297. transport := aPackageTransport.
  298. aPackageTransport package: self
  299. ! !
  300. !Package methodsFor: 'classes'!
  301. classes
  302. ^ self organization elements
  303. !
  304. setupClasses
  305. self classes do: [ :each | each initialize ]
  306. !
  307. sortedClasses
  308. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  309. ^ self class sortedClasses: self classes
  310. ! !
  311. !Package methodsFor: 'converting'!
  312. importsAsJson
  313. ^ self sortedImportsAsArray collect: [ :each |
  314. each isString
  315. ifTrue: [ each ]
  316. ifFalse: [ each key, '=', each value ]]
  317. !
  318. importsFromJson: anArray
  319. "Parses array of string, eg. #('asdf' 'qwer=tyuo')
  320. into array of Strings and Associations,
  321. eg. {'asdf'. 'qwer'->'tyuo'}"
  322. ^ anArray collect: [ :each |
  323. | split |
  324. split := each tokenize: '='.
  325. split size = 1
  326. ifTrue: [ split first ]
  327. ifFalse: [ split first -> split second ]]
  328. ! !
  329. !Package methodsFor: 'dependencies'!
  330. loadDependencies
  331. "Returns list of packages that need to be loaded
  332. before loading this package."
  333. | classes packages |
  334. classes := self loadDependencyClasses.
  335. ^ (classes collect: [ :each | each package ]) asSet
  336. remove: self ifAbsent: [];
  337. yourself
  338. !
  339. loadDependencyClasses
  340. "Returns classes needed at the time of loading a package.
  341. These are all that are used to subclass
  342. and to define an extension method
  343. as well as all traits used"
  344. | starCategoryName |
  345. starCategoryName := '*', self name.
  346. ^ (self classes collect: [ :each | each superclass ]) asSet
  347. addAll: (Smalltalk classes select: [ :each |
  348. each protocols, (each theMetaClass ifNil: [ #() ] ifNotNil: [ :meta | meta protocols])
  349. includes: starCategoryName ]);
  350. addAll: (Array streamContents: [ :as | self traitCompositions valuesDo: [ :each | as write: (each collect: [ :eachTT | eachTT trait ])]]);
  351. remove: nil ifAbsent: [];
  352. yourself
  353. !
  354. traitCompositions
  355. | traitCompositions |
  356. traitCompositions := Dictionary new.
  357. self classes do: [ :each |
  358. traitCompositions at: each put: each traitComposition.
  359. each theMetaClass ifNotNil: [ :meta | traitCompositions at: meta put: meta traitComposition ] ].
  360. ^ traitCompositions reject: [ :each | each isEmpty ]
  361. ! !
  362. !Package methodsFor: 'initialization'!
  363. initialize
  364. super initialize.
  365. self basicAt: 'organization' put: PackageOrganizer new.
  366. dirty := nil.
  367. imports := nil.
  368. transport := nil
  369. ! !
  370. !Package methodsFor: 'printing'!
  371. printOn: aStream
  372. super printOn: aStream.
  373. aStream
  374. nextPutAll: ' (';
  375. nextPutAll: self name;
  376. nextPutAll: ')'
  377. ! !
  378. !Package methodsFor: 'private'!
  379. basicImports
  380. "Answer the imports literal JavaScript object as setup in the JavaScript file, if any"
  381. <inlineJS: 'return self.imports || []'>
  382. !
  383. basicName: aString
  384. <inlineJS: 'self.pkgName = aString'>
  385. !
  386. basicTransport
  387. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  388. <inlineJS: 'return self.transport'>
  389. !
  390. sortedImportsAsArray
  391. "Answer imports sorted first by type (associations first),
  392. then by value"
  393. ^ self imports asArray
  394. sorted: [ :a :b |
  395. a isString not & b isString or: [
  396. a isString = b isString and: [
  397. a value <= b value ]]]
  398. ! !
  399. !Package methodsFor: 'testing'!
  400. isDirty
  401. ^ dirty ifNil: [ false ]
  402. !
  403. isPackage
  404. ^ true
  405. ! !
  406. !Package methodsFor: 'validation'!
  407. validateImports: aCollection
  408. aCollection do: [ :import |
  409. import isString ifFalse: [
  410. (import respondsTo: #key) ifFalse: [
  411. self error: 'Imports must be Strings or Associations' ].
  412. import key isString & import value isString ifFalse: [
  413. self error: 'Key and value must be Strings' ].
  414. (import key match: '^[a-zA-Z][a-zA-Z0-9]*$') ifFalse: [
  415. self error: 'Keys must be identifiers' ]]]
  416. ! !
  417. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  418. !Package class methodsFor: 'accessing'!
  419. named: aPackageName
  420. ^ Smalltalk
  421. packageAt: aPackageName
  422. ifAbsent: [
  423. Smalltalk createPackage: aPackageName ]
  424. !
  425. named: aPackageName ifAbsent: aBlock
  426. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  427. !
  428. named: aPackageName imports: anArray transport: aTransport
  429. | package |
  430. package := self named: aPackageName.
  431. package imports: anArray.
  432. package transport: aTransport.
  433. ^ package
  434. !
  435. named: aPackageName options: anObject
  436. | package |
  437. package := self named: aPackageName.
  438. package basicAt: 'innerEval' put:
  439. (anObject at: 'innerEval' ifAbsent: [ nil asJavaScriptObject ]).
  440. package basicAt: 'imports' put:
  441. (anObject at: 'imports' ifAbsent: [ #() ]).
  442. package basicAt: 'transport' put:
  443. (anObject at: 'transport' ifAbsent: []).
  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. new: aString
  454. ^ Package new
  455. basicName: aString;
  456. yourself
  457. ! !
  458. !Package class methodsFor: 'sorting'!
  459. sortedClasses: classes
  460. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  461. | children others nodes expandedClasses |
  462. children := #().
  463. others := #().
  464. classes do: [ :each |
  465. (classes includes: each superclass)
  466. ifFalse: [ children add: each ]
  467. ifTrue: [ others add: each ]].
  468. nodes := children collect: [ :each |
  469. ClassSorterNode on: each classes: others level: 0 ].
  470. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  471. expandedClasses := Array new.
  472. nodes do: [ :aNode |
  473. aNode traverseClassesWith: expandedClasses ].
  474. ^ expandedClasses
  475. ! !
  476. Object subclass: #PackageStateObserver
  477. instanceVariableNames: ''
  478. package: 'Kernel-Infrastructure'!
  479. !PackageStateObserver commentStamp!
  480. My current instance listens for any changes in the system that might affect the state of a package (being dirty).!
  481. !PackageStateObserver methodsFor: 'accessing'!
  482. announcer
  483. ^ SystemAnnouncer current
  484. ! !
  485. !PackageStateObserver methodsFor: 'actions'!
  486. observeSystem
  487. self announcer
  488. on: PackageAdded
  489. send: #onPackageAdded:
  490. to: self;
  491. on: ClassAnnouncement
  492. send: #onClassModification:
  493. to: self;
  494. on: MethodAnnouncement
  495. send: #onMethodModification:
  496. to: self;
  497. on: ProtocolAnnouncement
  498. send: #onProtocolModification:
  499. to: self
  500. ! !
  501. !PackageStateObserver methodsFor: 'reactions'!
  502. onClassModification: anAnnouncement
  503. anAnnouncement theClass ifNotNil: [ :theClass | theClass package beDirty ]
  504. !
  505. onMethodModification: anAnnouncement
  506. anAnnouncement method package ifNotNil: [ :package | package beDirty ]
  507. !
  508. onPackageAdded: anAnnouncement
  509. anAnnouncement package beDirty
  510. !
  511. onProtocolModification: anAnnouncement
  512. anAnnouncement package ifNotNil: [ :package | package beDirty ]
  513. ! !
  514. PackageStateObserver class instanceVariableNames: 'current'!
  515. !PackageStateObserver class methodsFor: 'accessing'!
  516. current
  517. ^ current ifNil: [ current := self new ]
  518. ! !
  519. !PackageStateObserver class methodsFor: 'initialization'!
  520. initialize
  521. self current observeSystem
  522. ! !
  523. Error subclass: #ParseError
  524. instanceVariableNames: ''
  525. package: 'Kernel-Infrastructure'!
  526. !ParseError commentStamp!
  527. Instance of ParseError are signaled on any parsing error.
  528. See `Smalltalk >> #parse:`!
  529. Object subclass: #Setting
  530. instanceVariableNames: 'key value defaultValue'
  531. package: 'Kernel-Infrastructure'!
  532. !Setting commentStamp!
  533. I represent a setting **stored** at `Smalltalk settings`.
  534. In the current implementation, `Smalltalk settings` is an object persisted in the localStorage.
  535. ## API
  536. A `Setting` value can be read using `value` and set using `value:`.
  537. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: aDefaultValue`.
  538. To read the value of a setting you can also use the convenience:
  539. `theValueSet := 'any.characteristic' settingValue`
  540. or with a default using:
  541. `theEnsuredValueSet := 'any.characteristic' settingValueIfAbsent: true`!
  542. !Setting methodsFor: 'accessing'!
  543. defaultValue
  544. ^ defaultValue
  545. !
  546. defaultValue: aStringifiableObject
  547. defaultValue := aStringifiableObject
  548. !
  549. key
  550. ^ key
  551. !
  552. key: aString
  553. key := aString
  554. !
  555. value
  556. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  557. !
  558. value: aStringifiableObject
  559. ^ Smalltalk settings at: self key put: aStringifiableObject
  560. ! !
  561. !Setting class methodsFor: 'instance creation'!
  562. at: aString ifAbsent: aDefaultValue
  563. ^ super new
  564. key: aString;
  565. defaultValue: aDefaultValue;
  566. yourself
  567. !
  568. new
  569. self shouldNotImplement
  570. ! !
  571. Object subclass: #SmalltalkImage
  572. instanceVariableNames: 'globalJsVariables packageDictionary'
  573. package: 'Kernel-Infrastructure'!
  574. !SmalltalkImage commentStamp!
  575. I represent the Smalltalk system, wrapping
  576. operations of variable `$core` declared in `support/boot.js`.
  577. ## API
  578. I have only one instance, accessed with global variable `Smalltalk`.
  579. ## Classes
  580. Classes can be accessed using the following methods:
  581. - `#classes` answers the full list of Smalltalk classes in the system
  582. - `#globals #at:` answers a specific global (usually, a class) or `nil`
  583. ## Packages
  584. Packages can be accessed using the following methods:
  585. - `#packages` answers the full list of packages
  586. - `#packageAt:` answers a specific package or `nil`
  587. ## Parsing
  588. The `#parse:` method is used to parse Amber source code.
  589. It requires the `Compiler` package and the `support/parser.js` parser file in order to work.!
  590. !SmalltalkImage methodsFor: 'accessing'!
  591. cancelOptOut: anObject
  592. "A Smalltalk object has a 'a$cls' property.
  593. If this property is shadowed for anObject by optOut:,
  594. the object is treated as plain JS object.
  595. This removes the shadow and anObject is Smalltalk object
  596. again if it was before."
  597. <inlineJS: 'delete anObject.klass; delete anObject.a$cls;'>
  598. !
  599. core
  600. <inlineJS: 'return $core'>
  601. !
  602. globals
  603. <inlineJS: 'return $globals'>
  604. !
  605. includesKey: aKey
  606. <inlineJS: 'return $core.hasOwnProperty(aKey)'>
  607. !
  608. optOut: anObject
  609. "A Smalltalk object has a 'a$cls' property.
  610. This shadows the property for anObject.
  611. The object is treated as plain JS object following this."
  612. <inlineJS: 'anObject.klass = null; anObject.a$cls = null'>
  613. !
  614. parse: aString
  615. | result |
  616. [ result := self basicParse: aString ]
  617. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  618. ^ result
  619. source: aString;
  620. yourself
  621. !
  622. pseudoVariableNames
  623. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  624. !
  625. readJSObject: anObject
  626. <inlineJS: 'return $core.readJSObject(anObject)'>
  627. !
  628. reservedWords
  629. ^ #(
  630. "http://www.ecma-international.org/ecma-262/6.0/#sec-keywords"
  631. break case catch class const continue debugger
  632. default delete do else export extends finally
  633. for function if import in instanceof new
  634. return super switch this throw try typeof
  635. var void while with yield
  636. "in strict mode"
  637. let static
  638. "Amber protected words: these should not be compiled as-is when in code"
  639. arguments
  640. "http://www.ecma-international.org/ecma-262/6.0/#sec-future-reserved-words"
  641. await enum
  642. "in strict mode"
  643. implements interface package private protected public
  644. )
  645. !
  646. settings
  647. ^ SmalltalkSettings
  648. !
  649. version
  650. "Answer the version string of Amber"
  651. ^ '0.20.0-pre'
  652. ! !
  653. !SmalltalkImage methodsFor: 'accessing amd'!
  654. amdRequire
  655. ^ self core at: 'amdRequire'
  656. !
  657. defaultAmdNamespace
  658. ^ 'transport.defaultAmdNamespace' settingValue
  659. !
  660. defaultAmdNamespace: aString
  661. 'transport.defaultAmdNamespace' settingValue: aString
  662. ! !
  663. !SmalltalkImage methodsFor: 'classes'!
  664. classes
  665. <inlineJS: 'return $core.classes()'>
  666. !
  667. removeClass: aClass
  668. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  669. aClass allSubclassesDo: [ :subclass | self error: aClass name, ' has a subclass: ', subclass name ].
  670. aClass traitUsers ifNotEmpty: [ self error: aClass name, ' has trait users.' ].
  671. self deleteClass: aClass.
  672. aClass setTraitComposition: #().
  673. aClass theMetaClass ifNotNil: [ :meta | meta setTraitComposition: #() ].
  674. SystemAnnouncer current
  675. announce: (ClassRemoved new
  676. theClass: aClass;
  677. yourself)
  678. ! !
  679. !SmalltalkImage methodsFor: 'error handling'!
  680. asSmalltalkException: anObject
  681. "A JavaScript exception may be thrown.
  682. We then need to convert it back to a Smalltalk object"
  683. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  684. ifTrue: [ anObject ]
  685. ifFalse: [ JavaScriptException on: anObject ]
  686. !
  687. parseError: anException parsing: aString
  688. | pos |
  689. pos := (anException basicAt: 'location') start.
  690. ^ ParseError new messageText: 'Parse error on line ', pos line ,' column ' , pos column ,' : Unexpected character ', (anException basicAt: 'found')
  691. ! !
  692. !SmalltalkImage methodsFor: 'globals'!
  693. addGlobalJsVariable: aString
  694. self globalJsVariables add: aString
  695. !
  696. deleteGlobalJsVariable: aString
  697. self globalJsVariables remove: aString ifAbsent:[]
  698. !
  699. globalJsVariables
  700. ^ globalJsVariables ifNil: [
  701. globalJsVariables := #(window document process global), self legacyGlobalJsVariables ]
  702. ! !
  703. !SmalltalkImage methodsFor: 'packages'!
  704. createPackage: packageName
  705. | package announcement |
  706. package := self basicCreatePackage: packageName.
  707. announcement := PackageAdded new
  708. package: package;
  709. yourself.
  710. SystemAnnouncer current announce: announcement.
  711. ^ package
  712. !
  713. packageAt: packageName
  714. ^ self packageAt: packageName ifAbsent: []
  715. !
  716. packageAt: packageName ifAbsent: aBlock
  717. ^ self packageDictionary at: packageName ifAbsent: aBlock
  718. !
  719. packageDictionary
  720. ^ packageDictionary ifNil: [ packageDictionary := Dictionary new ]
  721. !
  722. packages
  723. "Return all Package instances in the system."
  724. ^ self packageDictionary values copy
  725. !
  726. removePackage: packageName
  727. "Removes a package and all its classes."
  728. | pkg |
  729. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  730. pkg classes do: [ :each |
  731. self removeClass: each ].
  732. self packageDictionary removeKey: packageName
  733. !
  734. renamePackage: packageName to: newName
  735. "Rename a package."
  736. | pkg |
  737. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  738. (self packageAt: newName) ifNotNil: [ self error: 'Already exists a package called: ', newName ].
  739. pkg name: newName.
  740. self packageDictionary
  741. at: newName put: pkg;
  742. removeKey: packageName
  743. ! !
  744. !SmalltalkImage methodsFor: 'private'!
  745. adoptPackageDictionary
  746. self core packages keysAndValuesDo: [ :key :value | Package named: key options: value ]
  747. !
  748. basicCreatePackage: packageName
  749. "Create and bind a new bare package with given name and return it."
  750. ^ self packageDictionary at: packageName put: (Package new: packageName)
  751. !
  752. basicParse: aString
  753. ^ SmalltalkParser parse: aString
  754. !
  755. deleteClass: aClass
  756. "Deletes a class by deleting its binding only. Use #removeClass instead"
  757. <inlineJS: '$core.removeClass(aClass)'>
  758. !
  759. legacyGlobalJsVariables
  760. "Legacy array of global JavaScript variables.
  761. Only used for BW compat, to be removed."
  762. <inlineJS: 'return $core.globalJsVariables'>
  763. ! !
  764. !SmalltalkImage methodsFor: 'testing'!
  765. existsJsGlobal: aString
  766. ^ Platform globals
  767. at: aString
  768. ifPresent: [ true ]
  769. ifAbsent: [ false ]
  770. !
  771. isSmalltalkObject: anObject
  772. "Consider anObject a Smalltalk object if it has a 'a$cls' property.
  773. Note that this may be unaccurate"
  774. <inlineJS: 'return anObject.a$cls !!= null'>
  775. ! !
  776. SmalltalkImage class instanceVariableNames: 'current'!
  777. !SmalltalkImage class methodsFor: 'initialization'!
  778. initialize
  779. | st |
  780. st := self current.
  781. st globals at: 'Smalltalk' put: st
  782. ! !
  783. !SmalltalkImage class methodsFor: 'instance creation'!
  784. current
  785. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  786. !
  787. new
  788. self shouldNotImplement
  789. ! !
  790. JSObjectProxy setTraitComposition: {TThenable} asTraitComposition!
  791. ! !
  792. !ProtoStream methodsFor: '*Kernel-Infrastructure'!
  793. nextPutJSObject: aJSObject
  794. self nextPut: aJSObject
  795. ! !
  796. !String methodsFor: '*Kernel-Infrastructure'!
  797. asJavaScriptPropertyName
  798. <inlineJS: 'return $core.st2prop(self)'>
  799. !
  800. asSetting
  801. "Answer aSetting dedicated to locally store a value using this string as key.
  802. Nil will be the default value."
  803. ^ Setting at: self ifAbsent: nil
  804. !
  805. asSettingIfAbsent: aDefaultValue
  806. "Answer aSetting dedicated to locally store a value using this string as key.
  807. Make this setting to have aDefaultValue."
  808. ^ Setting at: self ifAbsent: aDefaultValue
  809. !
  810. settingValue
  811. ^ self asSetting value
  812. !
  813. settingValue: aValue
  814. "Sets the value of the setting that will be locally stored using this string as key.
  815. Note that aValue can be any object that can be stringifyed"
  816. ^ self asSetting value: aValue
  817. !
  818. settingValueIfAbsent: aDefaultValue
  819. "Answer the value of the locally stored setting using this string as key.
  820. Use aDefaultValue in case no setting is found"
  821. ^ (self asSettingIfAbsent: aDefaultValue) value
  822. ! !