Kernel-Infrastructure.st 26 KB

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