Kernel-Infrastructure.st 27 KB

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