Kernel-Infrastructure.st 26 KB

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