Kernel-Infrastructure.st 25 KB

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