Kernel-Infrastructure.st 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 isReady'
  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. isReady
  304. ^ isReady
  305. !
  306. isReady: aPromise
  307. isReady := aPromise
  308. !
  309. javaScriptDescriptor: anObject
  310. | basicEval basicImports |
  311. basicEval := anObject at: 'innerEval' ifAbsent: [ nil asJavaScriptObject ].
  312. basicImports := anObject at: 'imports' ifAbsent: [ #() ].
  313. basicTransport := anObject at: 'transport' ifAbsent: [].
  314. anObject at: 'isReady' ifPresent: [ :aPromise | self isReady: aPromise ].
  315. self
  316. evalBlock: basicEval;
  317. imports: (self importsFromJson: basicImports)
  318. !
  319. name
  320. ^ name
  321. !
  322. name: aString
  323. name := aString
  324. !
  325. organization
  326. ^ organization
  327. !
  328. transport
  329. ^ transport ifNil: [
  330. self transport: (PackageTransport fromJson: self basicTransport).
  331. transport ]
  332. !
  333. transport: aPackageTransport
  334. transport := aPackageTransport.
  335. aPackageTransport package: self
  336. ! !
  337. !Package methodsFor: 'classes'!
  338. classes
  339. ^ self organization elements copy
  340. !
  341. setupClasses
  342. self classes do: [ :each | each initialize ]
  343. !
  344. sortedClasses
  345. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  346. ^ self class sortedClasses: self classes
  347. ! !
  348. !Package methodsFor: 'converting'!
  349. importsAsJson
  350. ^ self sortedImportsAsArray collect: [ :each |
  351. each isString
  352. ifTrue: [ each ]
  353. ifFalse: [ each key, '=', each value ]]
  354. !
  355. importsFromJson: anArray
  356. "Parses array of string, eg. #('asdf' 'qwer=tyuo')
  357. into array of Strings and Associations,
  358. eg. {'asdf'. 'qwer'->'tyuo'}"
  359. ^ anArray collect: [ :each |
  360. | split |
  361. split := each tokenize: '='.
  362. split size = 1
  363. ifTrue: [ split first ]
  364. ifFalse: [ split first -> split second ]]
  365. ! !
  366. !Package methodsFor: 'dependencies'!
  367. loadDependencies
  368. "Returns list of packages that need to be loaded
  369. before loading this package."
  370. | classes packages |
  371. classes := self loadDependencyClasses.
  372. ^ (classes collect: [ :each | each package ]) asSet
  373. remove: self ifAbsent: [];
  374. yourself
  375. !
  376. loadDependencyClasses
  377. "Returns classes needed at the time of loading a package.
  378. These are all that are used to subclass
  379. and to define an extension method
  380. as well as all traits used"
  381. | starCategoryName |
  382. starCategoryName := '*', self name.
  383. ^ (self classes collect: [ :each | each superclass ]) asSet
  384. addAll: (Smalltalk classes select: [ :each |
  385. ({each. each theMetaClass} copyWithout: nil) anySatisfy: [ :any |
  386. (any protocols includes: starCategoryName) and: [
  387. (any ownMethodsInProtocol: starCategoryName) notEmpty ]]]);
  388. addAll: (Array streamContents: [ :as | self traitCompositions valuesDo: [ :each | as write: (each collect: [ :eachTT | eachTT trait ])]]);
  389. remove: nil ifAbsent: [];
  390. yourself
  391. !
  392. traitCompositions
  393. | traitCompositions |
  394. traitCompositions := Dictionary new.
  395. self classes do: [ :each |
  396. traitCompositions at: each put: each traitComposition.
  397. each theMetaClass ifNotNil: [ :meta | traitCompositions at: meta put: meta traitComposition ] ].
  398. ^ traitCompositions reject: [ :each | each isEmpty ]
  399. ! !
  400. !Package methodsFor: 'evaluating'!
  401. eval: aString
  402. ^ evalBlock
  403. ifNotNil: [ evalBlock value: aString ]
  404. ifNil: [ Compiler eval: aString ]
  405. ! !
  406. !Package methodsFor: 'initialization'!
  407. initialize
  408. super initialize.
  409. organization := PackageOrganizer new.
  410. evalBlock := nil.
  411. dirty := nil.
  412. imports := nil.
  413. isReady := Promise new.
  414. transport := nil
  415. ! !
  416. !Package methodsFor: 'printing'!
  417. printOn: aStream
  418. super printOn: aStream.
  419. aStream
  420. nextPutAll: ' (';
  421. nextPutAll: self name;
  422. nextPutAll: ')'
  423. ! !
  424. !Package methodsFor: 'private'!
  425. basicTransport
  426. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  427. ^ basicTransport
  428. !
  429. sortedImportsAsArray
  430. "Answer imports sorted first by type (associations first),
  431. then by value"
  432. ^ self imports asArray
  433. sorted: [ :a :b |
  434. a isString not & b isString or: [
  435. a isString = b isString and: [
  436. a value <= b value ]]]
  437. ! !
  438. !Package methodsFor: 'testing'!
  439. isDirty
  440. ^ dirty ifNil: [ false ]
  441. !
  442. isPackage
  443. ^ true
  444. ! !
  445. !Package methodsFor: 'validation'!
  446. validateImports: aCollection
  447. aCollection do: [ :import |
  448. import isString ifFalse: [
  449. (import respondsTo: #key) ifFalse: [
  450. self error: 'Imports must be Strings or Associations' ].
  451. import key isString & import value isString ifFalse: [
  452. self error: 'Key and value must be Strings' ].
  453. (import key match: '^[a-zA-Z][a-zA-Z0-9]*$') ifFalse: [
  454. self error: 'Keys must be identifiers' ]]]
  455. ! !
  456. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  457. !Package class methodsFor: 'accessing'!
  458. named: aPackageName
  459. ^ Smalltalk
  460. packageAt: aPackageName
  461. ifAbsent: [
  462. Smalltalk createPackage: aPackageName ]
  463. !
  464. named: aPackageName ifAbsent: aBlock
  465. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  466. !
  467. named: aPackageName imports: anArray transport: aTransport
  468. | pkg |
  469. pkg := self named: aPackageName.
  470. pkg imports: anArray.
  471. pkg transport: aTransport.
  472. ^ pkg
  473. !
  474. named: aPackageName transport: aTransport
  475. | pkg |
  476. pkg := self named: aPackageName.
  477. pkg transport: aTransport.
  478. ^ pkg
  479. ! !
  480. !Package class methodsFor: 'instance creation'!
  481. named: aString javaScriptDescriptor: anObject
  482. | pkg |
  483. pkg := Smalltalk createPackage: aString.
  484. pkg javaScriptDescriptor: anObject.
  485. ^ pkg
  486. !
  487. new: aString
  488. ^ Package new
  489. name: aString;
  490. yourself
  491. ! !
  492. !Package class methodsFor: 'sorting'!
  493. sortedClasses: classes
  494. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  495. | children others nodes expandedClasses |
  496. children := #().
  497. others := #().
  498. classes do: [ :each |
  499. (classes includes: each superclass)
  500. ifFalse: [ children add: each ]
  501. ifTrue: [ others add: each ]].
  502. nodes := children collect: [ :each |
  503. ClassSorterNode on: each classes: others level: 0 ].
  504. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  505. expandedClasses := Array new.
  506. nodes do: [ :aNode |
  507. aNode traverseClassesWith: expandedClasses ].
  508. ^ expandedClasses
  509. ! !
  510. Object subclass: #PackageStateObserver
  511. instanceVariableNames: ''
  512. package: 'Kernel-Infrastructure'!
  513. !PackageStateObserver commentStamp!
  514. My current instance listens for any changes in the system that might affect the state of a package (being dirty).!
  515. !PackageStateObserver methodsFor: 'accessing'!
  516. announcer
  517. ^ SystemAnnouncer current
  518. ! !
  519. !PackageStateObserver methodsFor: 'actions'!
  520. observeSystem
  521. self announcer
  522. on: PackageAdded
  523. send: #onPackageAdded:
  524. to: self;
  525. on: ClassAnnouncement
  526. send: #onClassModification:
  527. to: self;
  528. on: MethodAnnouncement
  529. send: #onMethodModification:
  530. to: self;
  531. on: ProtocolAnnouncement
  532. send: #onProtocolModification:
  533. to: self
  534. ! !
  535. !PackageStateObserver methodsFor: 'reactions'!
  536. onClassModification: anAnnouncement
  537. anAnnouncement theClass ifNotNil: [ :theClass | theClass package beDirty ]
  538. !
  539. onMethodModification: anAnnouncement
  540. anAnnouncement method package ifNotNil: [ :package | package beDirty ]
  541. !
  542. onPackageAdded: anAnnouncement
  543. anAnnouncement package beDirty
  544. !
  545. onProtocolModification: anAnnouncement
  546. anAnnouncement package ifNotNil: [ :package | package beDirty ]
  547. ! !
  548. PackageStateObserver class instanceVariableNames: 'current'!
  549. !PackageStateObserver class methodsFor: 'accessing'!
  550. current
  551. ^ current ifNil: [ current := self new ]
  552. ! !
  553. !PackageStateObserver class methodsFor: 'initialization'!
  554. initialize
  555. self current observeSystem
  556. ! !
  557. Error subclass: #ParseError
  558. instanceVariableNames: ''
  559. package: 'Kernel-Infrastructure'!
  560. !ParseError commentStamp!
  561. Instance of ParseError are signaled on any parsing error.
  562. See `Smalltalk >> #parse:`!
  563. Object subclass: #Setting
  564. instanceVariableNames: 'key value defaultValue'
  565. package: 'Kernel-Infrastructure'!
  566. !Setting commentStamp!
  567. I represent a setting **stored** at `Smalltalk settings`.
  568. In the current implementation, `Smalltalk settings` is an object persisted in the localStorage.
  569. ## API
  570. A `Setting` value can be read using `value` and set using `value:`.
  571. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: aDefaultValue`.
  572. To read the value of a setting you can also use the convenience:
  573. `theValueSet := 'any.characteristic' settingValue`
  574. or with a default using:
  575. `theEnsuredValueSet := 'any.characteristic' settingValueIfAbsent: true`!
  576. !Setting methodsFor: 'accessing'!
  577. defaultValue
  578. ^ defaultValue
  579. !
  580. defaultValue: aStringifiableObject
  581. defaultValue := aStringifiableObject
  582. !
  583. key
  584. ^ key
  585. !
  586. key: aString
  587. key := aString
  588. !
  589. value
  590. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  591. !
  592. value: aStringifiableObject
  593. ^ Smalltalk settings at: self key put: aStringifiableObject
  594. ! !
  595. !Setting class methodsFor: 'instance creation'!
  596. at: aString ifAbsent: aDefaultValue
  597. ^ super new
  598. key: aString;
  599. defaultValue: aDefaultValue;
  600. yourself
  601. !
  602. new
  603. self shouldNotImplement
  604. ! !
  605. Object subclass: #SmalltalkImage
  606. instanceVariableNames: 'globalJsVariables packageDictionary'
  607. package: 'Kernel-Infrastructure'!
  608. !SmalltalkImage commentStamp!
  609. I represent the Smalltalk system, wrapping
  610. operations of variable `$core` declared in `base/boot.js`.
  611. ## API
  612. I have only one instance, accessed with global variable `Smalltalk`.
  613. ## Classes
  614. Classes can be accessed using the following methods:
  615. - `#classes` answers the full list of Smalltalk classes in the system
  616. - `#globals #at:` answers a specific global (usually, a class) or `nil`
  617. ## Packages
  618. Packages can be accessed using the following methods:
  619. - `#packages` answers the full list of packages
  620. - `#packageAt:` answers a specific package or `nil`
  621. ## Parsing
  622. The `#parse:` method is used to parse Amber source code.
  623. It requires the `Compiler` package and the `base/parser.js` parser file in order to work.!
  624. !SmalltalkImage methodsFor: 'accessing'!
  625. cancelOptOut: anObject
  626. "A Smalltalk object has a 'a$cls' property.
  627. If this property is shadowed for anObject by optOut:,
  628. the object is treated as plain JS object.
  629. This removes the shadow and anObject is Smalltalk object
  630. again if it was before."
  631. <inlineJS: 'delete anObject.a$cls;'>
  632. !
  633. core
  634. <inlineJS: 'return $core'>
  635. !
  636. globals
  637. <inlineJS: 'return $globals'>
  638. !
  639. includesKey: aKey
  640. <inlineJS: 'return $core.hasOwnProperty(aKey)'>
  641. !
  642. optOut: anObject
  643. "A Smalltalk object has a 'a$cls' property.
  644. This shadows the property for anObject.
  645. The object is treated as plain JS object following this."
  646. <inlineJS: 'anObject.a$cls = null'>
  647. !
  648. parse: aString
  649. | result |
  650. [ result := self basicParse: aString ]
  651. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  652. ^ result
  653. source: aString;
  654. yourself
  655. !
  656. pseudoVariableNames
  657. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  658. !
  659. readJSObject: anObject
  660. <inlineJS: 'return $core.readJSObject(anObject)'>
  661. !
  662. reservedWords
  663. ^ #(
  664. "http://www.ecma-international.org/ecma-262/6.0/#sec-keywords"
  665. break case catch class const continue debugger
  666. default delete do else export extends finally
  667. for function if import in instanceof new
  668. return super switch this throw try typeof
  669. var void while with yield
  670. "in strict mode"
  671. let static
  672. "Amber protected words: these should not be compiled as-is when in code"
  673. arguments
  674. "http://www.ecma-international.org/ecma-262/6.0/#sec-future-reserved-words"
  675. await enum
  676. "in strict mode"
  677. implements interface package private protected public
  678. )
  679. !
  680. settings
  681. ^ SmalltalkSettings
  682. !
  683. version
  684. "Answer the version string of Amber"
  685. ^ '0.23.0-pre'
  686. ! !
  687. !SmalltalkImage methodsFor: 'accessing amd'!
  688. amdRequire
  689. ^ self core at: 'amdRequire'
  690. !
  691. defaultAmdNamespace
  692. ^ 'transport.defaultAmdNamespace' settingValue
  693. !
  694. defaultAmdNamespace: aString
  695. 'transport.defaultAmdNamespace' settingValue: aString
  696. ! !
  697. !SmalltalkImage methodsFor: 'classes'!
  698. classes
  699. ^ self core traitsOrClasses copy
  700. !
  701. removeClass: aClass
  702. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  703. aClass allSubclassesDo: [ :subclass | self error: aClass name, ' has a subclass: ', subclass name ].
  704. aClass traitUsers ifNotEmpty: [ self error: aClass name, ' has trait users.' ].
  705. self deleteClass: aClass.
  706. aClass setTraitComposition: #().
  707. aClass theMetaClass ifNotNil: [ :meta | meta setTraitComposition: #() ].
  708. SystemAnnouncer current
  709. announce: (ClassRemoved new
  710. theClass: aClass;
  711. yourself)
  712. ! !
  713. !SmalltalkImage methodsFor: 'error handling'!
  714. asSmalltalkException: anObject
  715. "A JavaScript exception may be thrown.
  716. We then need to convert it back to a Smalltalk object"
  717. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  718. ifTrue: [ anObject ]
  719. ifFalse: [ JavaScriptException on: anObject ]
  720. !
  721. parseError: anException parsing: aString
  722. (anException basicAt: 'location')
  723. ifNil: [ ^ anException pass ]
  724. ifNotNil: [ :loc |
  725. ^ ParseError new
  726. messageText:
  727. 'Parse error on line ', loc start line ,
  728. ' column ' , loc start column ,
  729. ' : Unexpected character ', (anException basicAt: 'found');
  730. yourself ]
  731. ! !
  732. !SmalltalkImage methodsFor: 'globals'!
  733. addGlobalJsVariable: aString
  734. self globalJsVariables add: aString
  735. !
  736. deleteGlobalJsVariable: aString
  737. self globalJsVariables remove: aString ifAbsent:[]
  738. !
  739. globalJsVariables
  740. ^ globalJsVariables ifNil: [
  741. globalJsVariables := #(window document process global) ]
  742. ! !
  743. !SmalltalkImage methodsFor: 'image'!
  744. postLoad
  745. ^ self adoptPackageDescriptors then: [ :pkgs |
  746. | classes |
  747. pkgs do: #beClean.
  748. classes := Smalltalk classes select:
  749. [ :each | pkgs includes: each package ].
  750. classes do: [ :each |
  751. each = self class ifFalse: [ each initialize ] ].
  752. self sweepPackageDescriptors: pkgs ]
  753. ! !
  754. !SmalltalkImage methodsFor: 'packages'!
  755. beClean
  756. "Marks all packages clean."
  757. self packages do: #beClean
  758. !
  759. createPackage: packageName
  760. | package announcement |
  761. package := self basicCreatePackage: packageName.
  762. announcement := PackageAdded new
  763. package: package;
  764. yourself.
  765. SystemAnnouncer current announce: announcement.
  766. ^ package
  767. !
  768. packageAt: packageName ifAbsent: aBlock
  769. ^ self packageDictionary at: packageName ifAbsent: aBlock
  770. !
  771. packageAt: packageName ifPresent: aBlock
  772. ^ self packageDictionary at: packageName ifPresent: aBlock
  773. !
  774. packageDictionary
  775. ^ packageDictionary ifNil: [ packageDictionary := Dictionary new ]
  776. !
  777. packages
  778. "Return all Package instances in the system."
  779. ^ self packageDictionary values copy
  780. !
  781. removePackage: packageName
  782. "Removes a package and all its classes."
  783. | pkg |
  784. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  785. pkg classes do: [ :each |
  786. self removeClass: each ].
  787. self packageDictionary removeKey: packageName
  788. !
  789. renamePackage: packageName to: newName
  790. "Rename a package."
  791. | pkg |
  792. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  793. self packageAt: newName ifPresent: [ self error: 'Already exists a package called: ', newName ].
  794. pkg name: newName; beDirty.
  795. self packageDictionary
  796. at: newName put: pkg;
  797. removeKey: packageName
  798. ! !
  799. !SmalltalkImage methodsFor: 'private'!
  800. adoptPackageDescriptors
  801. ^ self tryAdoptPackageDescriptorsBeyond: Set new
  802. !
  803. basicCreatePackage: packageName
  804. "Create and bind a new bare package with given name and return it."
  805. ^ self packageDictionary at: packageName ifAbsentPut: [ Package new: packageName ]
  806. !
  807. basicParse: aString
  808. ^ SmalltalkParser parse: aString
  809. !
  810. deleteClass: aClass
  811. "Deletes a class by deleting its binding only. Use #removeClass instead"
  812. <inlineJS: '$core.removeClass(aClass)'>
  813. !
  814. sweepPackageDescriptors: pkgs
  815. | pd |
  816. pd := self core packageDescriptors.
  817. pkgs do: [ :each | pd removeKey: each name ]
  818. !
  819. tryAdoptPackageDescriptorsBeyond: aSet
  820. | original |
  821. original := aSet copy.
  822. self core packageDescriptors keysAndValuesDo: [ :key :value |
  823. aSet add: (Package named: key javaScriptDescriptor: value) ].
  824. ^ (aSet allSatisfy: [ :each | original includes: each ])
  825. ifFalse: [ (Promise all: (aSet collect: #isReady)) then: [ self tryAdoptPackageDescriptorsBeyond: aSet ] ]
  826. ifTrue: [ Promise value: aSet ]
  827. ! !
  828. !SmalltalkImage methodsFor: 'testing'!
  829. existsJsGlobal: aString
  830. self deprecatedAPI: 'Use Platform >> includesGlobal: instead'.
  831. ^ Platform includesGlobal: aString
  832. !
  833. isSmalltalkObject: anObject
  834. "Consider anObject a Smalltalk object if it has a 'a$cls' property.
  835. Note that this may be unaccurate"
  836. <inlineJS: 'return anObject.a$cls !!= null'>
  837. ! !
  838. SmalltalkImage class instanceVariableNames: 'current'!
  839. !SmalltalkImage class methodsFor: 'initialization'!
  840. initialize
  841. | st |
  842. st := self current.
  843. st globals at: 'Smalltalk' put: st
  844. ! !
  845. !SmalltalkImage class methodsFor: 'instance creation'!
  846. current
  847. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  848. !
  849. new
  850. self shouldNotImplement
  851. ! !
  852. JSObjectProxy setTraitComposition: {TThenable} asTraitComposition!
  853. ! !
  854. !ProtoStream methodsFor: '*Kernel-Infrastructure'!
  855. nextPutJSObject: aJSObject
  856. self nextPut: aJSObject
  857. ! !
  858. !String methodsFor: '*Kernel-Infrastructure'!
  859. asJavaScriptPropertyName
  860. <inlineJS: 'return $core.st2prop(self)'>
  861. !
  862. asSetting
  863. "Answer aSetting dedicated to locally store a value using this string as key.
  864. Nil will be the default value."
  865. ^ Setting at: self ifAbsent: nil
  866. !
  867. asSettingIfAbsent: aDefaultValue
  868. "Answer aSetting dedicated to locally store a value using this string as key.
  869. Make this setting to have aDefaultValue."
  870. ^ Setting at: self ifAbsent: aDefaultValue
  871. !
  872. settingValue
  873. ^ self asSetting value
  874. !
  875. settingValue: aValue
  876. "Sets the value of the setting that will be locally stored using this string as key.
  877. Note that aValue can be any object that can be stringifyed"
  878. ^ self asSetting value: aValue
  879. !
  880. settingValueIfAbsent: aDefaultValue
  881. "Answer the value of the locally stored setting using this string as key.
  882. Use aDefaultValue in case no setting is found"
  883. ^ (self asSettingIfAbsent: aDefaultValue) value
  884. ! !