Kernel-Infrastructure.st 26 KB

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