Kernel-Infrastructure.st 23 KB

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