Kernel-Infrastructure.st 23 KB

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