Kernel-Infrastructure.st 22 KB

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