Kernel-Infrastructure.st 27 KB

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