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