Kernel-Infrastructure.st 25 KB

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