Kernel-Infrastructure.st 22 KB

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