Kernel-Infrastructure.st 27 KB

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