Kernel-Infrastructure.st 27 KB

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