Kernel-Infrastructure.st 24 KB

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