Kernel-Infrastructure.st 22 KB

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