Kernel-Infrastructure.st 23 KB

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