Kernel-Infrastructure.st 25 KB

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