Kernel-Infrastructure.st 26 KB

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