Kernel-Infrastructure.st 22 KB

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