Kernel-Infrastructure.st 22 KB

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