Kernel-Infrastructure.st 27 KB

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