Kernel-Infrastructure.st 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. Smalltalk current createPackage: 'Kernel-Infrastructure'!
  2. Object subclass: #InspectorHandler
  3. instanceVariableNames: ''
  4. package: 'Kernel-Infrastructure'!
  5. !InspectorHandler commentStamp!
  6. I am responsible for inspecting object.
  7. My class-side `inspector` inst var holds the current inspector I'm delegating object inspection to.
  8. The default inspector object is the transcript.!
  9. InspectorHandler class instanceVariableNames: 'inspector'!
  10. !InspectorHandler class methodsFor: 'accessing'!
  11. inspector
  12. ^ inspector ifNil: [ inspector := Transcript ]
  13. ! !
  14. !InspectorHandler class methodsFor: 'registration'!
  15. inspect: anObject
  16. ^ self inspector inspect: anObject
  17. !
  18. register: anInspector
  19. inspector := anInspector
  20. ! !
  21. Object subclass: #InterfacingObject
  22. instanceVariableNames: ''
  23. package: 'Kernel-Infrastructure'!
  24. !InterfacingObject commentStamp!
  25. I am superclass of all object that interface with user or environment. `Widget` and a few other classes are subclasses of me. I delegate all of the above APIs to `PlatformInterface`.
  26. ## API
  27. self alert: 'Hey, there is a problem'.
  28. self confirm: 'Affirmative?'.
  29. self prompt: 'Your name:'.
  30. self ajax: #{
  31. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  32. }.!
  33. !InterfacingObject methodsFor: 'actions'!
  34. ajax: anObject
  35. ^PlatformInterface ajax: anObject
  36. !
  37. alert: aString
  38. ^PlatformInterface alert: aString
  39. !
  40. confirm: aString
  41. ^PlatformInterface confirm: aString
  42. !
  43. prompt: aString
  44. ^PlatformInterface prompt: aString
  45. ! !
  46. InterfacingObject subclass: #Environment
  47. instanceVariableNames: ''
  48. package: 'Kernel-Infrastructure'!
  49. !Environment commentStamp!
  50. I provide an unified entry point to manipulate Amber packages, classes and methods.
  51. Typical use cases include IDEs, remote access and restricting browsing.!
  52. !Environment methodsFor: 'accessing'!
  53. allSelectors
  54. ^ (Smalltalk current at: 'allSelectors') value
  55. !
  56. availableClassNames
  57. ^ Smalltalk current classes
  58. collect: [ :each | each name ]
  59. !
  60. availablePackageNames
  61. ^ Smalltalk current packages
  62. collect: [ :each | each name ]
  63. !
  64. availableProtocolsFor: aClass
  65. | protocols |
  66. protocols := aClass protocols.
  67. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  68. ^ protocols asSet asArray
  69. !
  70. classBuilder
  71. ^ ClassBuilder new
  72. !
  73. classNamed: aString
  74. ^ (Smalltalk current at: aString asSymbol)
  75. ifNil: [ self error: 'Invalid class name' ]
  76. !
  77. classes
  78. ^ Smalltalk current classes
  79. !
  80. packages
  81. ^ Smalltalk current packages
  82. !
  83. systemAnnouncer
  84. ^ (Smalltalk current at: #SystemAnnouncer) current
  85. ! !
  86. !Environment methodsFor: 'actions'!
  87. commitPackage: aPackage
  88. aPackage commit
  89. !
  90. copyClass: aClass to: aClassName
  91. (Smalltalk current at: aClassName)
  92. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  93. ClassBuilder new copyClass: aClass named: aClassName
  94. !
  95. eval: aString on: aReceiver
  96. | compiler |
  97. compiler := Compiler new.
  98. [ compiler parseExpression: aString ] on: Error do: [ :ex |
  99. ^ self alert: ex messageText ].
  100. ^ compiler evaluateExpression: aString on: aReceiver
  101. !
  102. inspect: anObject
  103. InspectorHandler inspector inspect: anObject
  104. !
  105. moveClass: aClass toPackage: aPackageName
  106. | package |
  107. package := Package named: aPackageName.
  108. package ifNil: [ self error: 'Invalid package name' ].
  109. package == aClass package ifTrue: [ ^ self ].
  110. aClass package: package
  111. !
  112. moveMethod: aMethod toClass: aClassName
  113. | destinationClass |
  114. destinationClass := Smalltalk current at: aClassName asSymbol.
  115. destinationClass ifNil: [ self error: 'Invalid class name' ].
  116. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  117. destinationClass
  118. compile: aMethod source
  119. category: aMethod protocol.
  120. aMethod methodClass
  121. removeCompiledMethod: aMethod
  122. !
  123. moveMethod: aMethod toProtocol: aProtocol
  124. aMethod category: aProtocol
  125. !
  126. registerErrorHandler: anErrorHandler
  127. ErrorHandler setCurrent: anErrorHandler
  128. !
  129. registerInspector: anInspector
  130. InspectorHandler register: anInspector
  131. !
  132. registerProgressHandler: aProgressHandler
  133. ProgressHandler setCurrent: aProgressHandler
  134. !
  135. removeClass: aClass
  136. Smalltalk current removeClass: aClass
  137. !
  138. removeMethod: aMethod
  139. aMethod methodClass removeCompiledMethod: aMethod
  140. !
  141. removeProtocol: aString from: aClass
  142. (aClass methods
  143. select: [ :each | each protocol = aString ])
  144. do: [ :each | aClass removeCompiledMethod: each ]
  145. !
  146. renameClass: aClass to: aClassName
  147. (Smalltalk current at: aClassName)
  148. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  149. ClassBuilder new renameClass: aClass to: aClassName
  150. !
  151. renameProtocol: aString to: anotherString in: aClass
  152. (aClass methods
  153. select: [ :each | each protocol = aString ])
  154. do: [ :each | each protocol: anotherString ]
  155. !
  156. setClassCommentOf: aClass to: aString
  157. aClass comment: aString
  158. ! !
  159. !Environment methodsFor: 'compiling'!
  160. addInstVarNamed: aString to: aClass
  161. self classBuilder
  162. addSubclassOf: aClass superclass
  163. named: aClass name
  164. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  165. package: aClass package name
  166. !
  167. compileClassComment: aString for: aClass
  168. aClass comment: aString
  169. !
  170. compileClassDefinition: aString
  171. self eval: aString on: DoIt new
  172. !
  173. compileMethod: sourceCode for: class protocol: protocol
  174. ^ class
  175. compile: sourceCode
  176. category: protocol
  177. ! !
  178. !Environment methodsFor: 'error handling'!
  179. evaluate: aBlock on: anErrorClass do: exceptionBlock
  180. "Evaluate a block and catch exceptions happening on the environment stack"
  181. self try: aBlock catch: [ :exception |
  182. (exception isKindOf: (self classNamed: anErrorClass name))
  183. ifTrue: [ exceptionBlock value: exception ]
  184. ifFalse: [ exception signal ] ]
  185. ! !
  186. Object subclass: #JSObjectProxy
  187. instanceVariableNames: 'jsObject'
  188. package: 'Kernel-Infrastructure'!
  189. !JSObjectProxy commentStamp!
  190. I handle sending messages to JavaScript objects, making JavaScript object accessing from Amber fully transparent.
  191. My instances make intensive use of `#doesNotUnderstand:`.
  192. My instances are automatically created by Amber whenever a message is sent to a JavaScript object.
  193. ## Usage examples
  194. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  195. window alert: 'hello world'.
  196. window inspect.
  197. (window jQuery: 'body') append: 'hello world'
  198. 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.
  199. ## Message conversion rules
  200. - `someUser name` becomes `someUser.name`
  201. - `someUser name: 'John'` becomes `someUser name = "John"`
  202. - `console log: 'hello world'` becomes `console.log('hello world')`
  203. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  204. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  205. !JSObjectProxy methodsFor: 'accessing'!
  206. at: aString
  207. <return self['@jsObject'][aString]>
  208. !
  209. at: aString ifAbsent: aBlock
  210. "return the aString property or evaluate aBlock if the property is not defined on the object"
  211. <
  212. var obj = self['@jsObject'];
  213. return aString in obj ? obj[aString] : aBlock._value();
  214. >
  215. !
  216. at: aString ifPresent: aBlock
  217. "return the evaluation of aBlock with the value if the property is defined or return nil"
  218. <
  219. var obj = self['@jsObject'];
  220. return aString in obj ? aBlock._value_(obj[aString]) : nil;
  221. >
  222. !
  223. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  224. "return the evaluation of aBlock with the value if the property is defined
  225. or return value of anotherBlock"
  226. <
  227. var obj = self['@jsObject'];
  228. return aString in obj ? aBlock._value_(obj[aString]) : anotherBlock._value();
  229. >
  230. !
  231. at: aString put: anObject
  232. <self['@jsObject'][aString] = anObject>
  233. !
  234. jsObject
  235. ^jsObject
  236. !
  237. jsObject: aJSObject
  238. jsObject := aJSObject
  239. !
  240. lookupProperty: aString
  241. "Looks up a property in JS object.
  242. Answer the property if it is present, or nil if it is not present."
  243. <return aString in self._jsObject() ? aString : nil>
  244. !
  245. value
  246. "if attribute 'value' exists on the JS object return it,
  247. otherwise return the result of Object>>value."
  248. ^ self
  249. at: 'value'
  250. ifAbsent: [ super value ]
  251. ! !
  252. !JSObjectProxy methodsFor: 'enumerating'!
  253. keysAndValuesDo: aBlock
  254. <
  255. var o = self['@jsObject'];
  256. for(var i in o) {
  257. aBlock._value_value_(i, o[i]);
  258. }
  259. >
  260. ! !
  261. !JSObjectProxy methodsFor: 'printing'!
  262. printOn: aStream
  263. aStream nextPutAll: self printString
  264. !
  265. printString
  266. <
  267. var js = self['@jsObject'];
  268. return js.toString
  269. ? js.toString()
  270. : Object.prototype.toString.call(js)
  271. >
  272. ! !
  273. !JSObjectProxy methodsFor: 'proxy'!
  274. addObjectVariablesTo: aDictionary
  275. <
  276. for(var i in self['@jsObject']) {
  277. aDictionary._at_put_(i, self['@jsObject'][i]);
  278. }
  279. >
  280. !
  281. doesNotUnderstand: aMessage
  282. ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
  283. ifNil: [ super doesNotUnderstand: aMessage ]
  284. ifNotNil: [ :jsSelector |
  285. self
  286. forwardMessage: jsSelector
  287. withArguments: aMessage arguments ]
  288. !
  289. forwardMessage: aString withArguments: anArray
  290. <
  291. return smalltalk.send(self._jsObject(), aString, anArray);
  292. >
  293. !
  294. inspectOn: anInspector
  295. | variables |
  296. variables := Dictionary new.
  297. variables at: '#self' put: self jsObject.
  298. anInspector setLabel: self printString.
  299. self addObjectVariablesTo: variables.
  300. anInspector setVariables: variables
  301. ! !
  302. !JSObjectProxy class methodsFor: 'instance creation'!
  303. on: aJSObject
  304. ^self new
  305. jsObject: aJSObject;
  306. yourself
  307. ! !
  308. Object subclass: #Organizer
  309. instanceVariableNames: ''
  310. package: 'Kernel-Infrastructure'!
  311. !Organizer commentStamp!
  312. I represent categorization information.
  313. ## API
  314. Use `#addElement:` and `#removeElement:` to manipulate instances.!
  315. !Organizer methodsFor: 'accessing'!
  316. addElement: anObject
  317. <self.elements.addElement(anObject)>
  318. !
  319. elements
  320. ^ (self basicAt: 'elements') copy
  321. !
  322. removeElement: anObject
  323. <self.elements.removeElement(anObject)>
  324. ! !
  325. Organizer subclass: #ClassOrganizer
  326. instanceVariableNames: ''
  327. package: 'Kernel-Infrastructure'!
  328. !ClassOrganizer commentStamp!
  329. I am an organizer specific to classes. I hold method categorization information for classes.!
  330. !ClassOrganizer methodsFor: 'accessing'!
  331. addElement: aString
  332. super addElement: aString.
  333. SystemAnnouncer current announce: (ProtocolAdded new
  334. protocol: aString;
  335. theClass: self theClass;
  336. yourself)
  337. !
  338. removeElement: aString
  339. super removeElement: aString.
  340. SystemAnnouncer current announce: (ProtocolRemoved new
  341. protocol: aString;
  342. theClass: self theClass;
  343. yourself)
  344. !
  345. theClass
  346. < return self.theClass >
  347. ! !
  348. Organizer subclass: #PackageOrganizer
  349. instanceVariableNames: ''
  350. package: 'Kernel-Infrastructure'!
  351. !PackageOrganizer commentStamp!
  352. I am an organizer specific to packages. I hold classes categorization information.!
  353. Object subclass: #Package
  354. instanceVariableNames: 'transport'
  355. package: 'Kernel-Infrastructure'!
  356. !Package commentStamp!
  357. 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.
  358. 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.
  359. ## API
  360. Packages are manipulated through "Smalltalk current", like for example finding one based on a name or with `Package class >> #name` directly:
  361. Smalltalk current packageAt: 'Kernel'
  362. Package named: 'Kernel'
  363. 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.
  364. You can fetch a package from the server:
  365. Package load: 'Additional-Examples'!
  366. !Package methodsFor: 'accessing'!
  367. basicTransport
  368. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  369. <return self.transport>
  370. !
  371. name
  372. <return self.pkgName>
  373. !
  374. name: aString
  375. <self.pkgName = aString>
  376. !
  377. organization
  378. ^ self basicAt: 'organization'
  379. !
  380. transport
  381. ^ transport ifNil: [
  382. transport := (PackageTransport fromJson: self basicTransport)
  383. package: self;
  384. yourself ]
  385. !
  386. transport: aPackageTransport
  387. transport := aPackageTransport
  388. ! !
  389. !Package methodsFor: 'classes'!
  390. classes
  391. ^ self organization elements asSet asArray
  392. !
  393. setupClasses
  394. self classes
  395. do: [ :each | ClassBuilder new setupClass: each ];
  396. do: [ :each | each initialize ]
  397. !
  398. sortedClasses
  399. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  400. ^ self class sortedClasses: self classes
  401. ! !
  402. !Package methodsFor: 'dependencies'!
  403. loadDependencies
  404. "Returns list of packages that need to be loaded
  405. before loading this package."
  406. | classes packages |
  407. classes := self loadDependencyClasses.
  408. ^(classes collect: [ :each | each package ]) asSet
  409. remove: self ifAbsent: [];
  410. yourself
  411. !
  412. loadDependencyClasses
  413. "Returns classes needed at the time of loading a package.
  414. These are all that are used to subclass
  415. and to define an extension method"
  416. | starCategoryName |
  417. starCategoryName := '*', self name.
  418. ^(self classes collect: [ :each | each superclass ]) asSet
  419. remove: nil ifAbsent: [];
  420. addAll: (Smalltalk current classes select: [ :each | each protocols includes: starCategoryName ]);
  421. yourself
  422. ! !
  423. !Package methodsFor: 'printing'!
  424. printOn: aStream
  425. super printOn: aStream.
  426. aStream
  427. nextPutAll: ' (';
  428. nextPutAll: self name;
  429. nextPutAll: ')'
  430. ! !
  431. !Package methodsFor: 'testing'!
  432. isPackage
  433. ^ true
  434. ! !
  435. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  436. !Package class methodsFor: 'accessing'!
  437. named: aPackageName
  438. ^Smalltalk current packageAt: aPackageName
  439. !
  440. named: aPackageName ifAbsent: aBlock
  441. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  442. ! !
  443. !Package class methodsFor: 'sorting'!
  444. sortedClasses: classes
  445. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  446. | children others nodes expandedClasses |
  447. children := #().
  448. others := #().
  449. classes do: [:each |
  450. (classes includes: each superclass)
  451. ifFalse: [children add: each]
  452. ifTrue: [others add: each]].
  453. nodes := children collect: [:each |
  454. ClassSorterNode on: each classes: others level: 0].
  455. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  456. expandedClasses := Array new.
  457. nodes do: [:aNode |
  458. aNode traverseClassesWith: expandedClasses].
  459. ^expandedClasses
  460. ! !
  461. Object subclass: #PlatformInterface
  462. instanceVariableNames: ''
  463. package: 'Kernel-Infrastructure'!
  464. !PlatformInterface commentStamp!
  465. I am single entry point to UI and environment interface.
  466. My `initialize` tries several options (for now, browser environment only) to set myself up.
  467. ## API
  468. PlatformInterface alert: 'Hey, there is a problem'.
  469. PlatformInterface confirm: 'Affirmative?'.
  470. PlatformInterface prompt: 'Your name:'.
  471. PlatformInterface ajax: #{
  472. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  473. }.!
  474. PlatformInterface class instanceVariableNames: 'worker'!
  475. !PlatformInterface class methodsFor: 'accessing'!
  476. globals
  477. <return (new Function('return this'))();>
  478. !
  479. setWorker: anObject
  480. worker := anObject
  481. ! !
  482. !PlatformInterface class methodsFor: 'actions'!
  483. ajax: anObject
  484. ^worker
  485. ifNotNil: [ worker ajax: anObject ]
  486. ifNil: [ self error: 'ajax: not available' ]
  487. !
  488. alert: aString
  489. ^worker
  490. ifNotNil: [ worker alert: aString ]
  491. ifNil: [ self error: 'alert: not available' ]
  492. !
  493. confirm: aString
  494. ^worker
  495. ifNotNil: [ worker confirm: aString ]
  496. ifNil: [ self error: 'confirm: not available' ]
  497. !
  498. existsGlobal: aString
  499. ^ PlatformInterface globals
  500. at: aString
  501. ifPresent: [ true ]
  502. ifAbsent: [ false ]
  503. !
  504. prompt: aString
  505. ^worker
  506. ifNotNil: [ worker prompt: aString ]
  507. ifNil: [ self error: 'prompt: not available' ]
  508. ! !
  509. !PlatformInterface class methodsFor: 'initialization'!
  510. initialize
  511. | candidate |
  512. super initialize.
  513. BrowserInterface ifNotNil: [
  514. candidate := BrowserInterface new.
  515. candidate isAvailable ifTrue: [ self setWorker: candidate. ^self ]
  516. ]
  517. ! !
  518. Object subclass: #ProgressHandler
  519. instanceVariableNames: ''
  520. package: 'Kernel-Infrastructure'!
  521. !ProgressHandler commentStamp!
  522. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  523. Subclasses of can register themselves as the current handler with
  524. `ProgressHandler class >> register`.
  525. The default behavior is to simply iterate over the collection.!
  526. !ProgressHandler methodsFor: 'progress handling'!
  527. do: aBlock on: aCollection displaying: aString
  528. aCollection do: aBlock
  529. ! !
  530. ProgressHandler class instanceVariableNames: 'current'!
  531. !ProgressHandler class methodsFor: 'accessing'!
  532. current
  533. ^current ifNil: [ current := self new ]
  534. !
  535. setCurrent: anHandler
  536. current := anHandler
  537. ! !
  538. !ProgressHandler class methodsFor: 'initialization'!
  539. initialize
  540. self register
  541. !
  542. register
  543. ProgressHandler setCurrent: self new
  544. ! !
  545. Object subclass: #Smalltalk
  546. instanceVariableNames: ''
  547. package: 'Kernel-Infrastructure'!
  548. !Smalltalk commentStamp!
  549. I represent the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  550. ## API
  551. I have only one instance, accessed with class-side method `#current`.
  552. The `smalltalk` object holds all class and packages defined in the system.
  553. ## Classes
  554. Classes can be accessed using the following methods:
  555. - `#classes` answers the full list of Smalltalk classes in the system
  556. - `#at:` answers a specific class or `nil`
  557. ## Packages
  558. Packages can be accessed using the following methods:
  559. - `#packages` answers the full list of packages
  560. - `#packageAt:` answers a specific package or `nil`
  561. ## Parsing
  562. The `#parse:` method is used to parse Amber source code.
  563. It requires the `Compiler` package and the `js/parser.js` parser file in order to work.!
  564. !Smalltalk methodsFor: 'accessing'!
  565. at: aString
  566. ^ self basicAt: aString
  567. !
  568. at: aKey ifAbsent: aBlock
  569. ^ (self includesKey: aKey)
  570. ifTrue: [self at: aKey]
  571. ifFalse: aBlock
  572. !
  573. at: aString put: anObject
  574. ^ self basicAt: aString put: anObject
  575. !
  576. includesKey: aKey
  577. <return self.hasOwnProperty(aKey)>
  578. !
  579. parse: aString
  580. | result |
  581. self
  582. try: [result := self basicParse: aString]
  583. catch: [:ex | (self parseError: ex parsing: aString) signal].
  584. ^ result
  585. source: aString;
  586. yourself
  587. !
  588. pseudoVariableNames
  589. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  590. !
  591. readJSObject: anObject
  592. <return self.readJSObject(anObject)>
  593. !
  594. reservedWords
  595. "JavaScript reserved words"
  596. <return self.reservedWords>
  597. !
  598. version
  599. "Answer the version string of Amber"
  600. ^ '0.12.0-pre'
  601. ! !
  602. !Smalltalk methodsFor: 'accessing amd'!
  603. amdRequire
  604. ^ self at: 'amdRequire'
  605. !
  606. defaultAmdNamespace
  607. ^ self at: 'defaultAmdNamespace'
  608. !
  609. defaultAmdNamespace: aString
  610. self at: 'defaultAmdNamespace' put: aString
  611. ! !
  612. !Smalltalk methodsFor: 'classes'!
  613. classes
  614. <return self.classes()>
  615. !
  616. removeClass: aClass
  617. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  618. self deleteClass: aClass.
  619. SystemAnnouncer current
  620. announce: (ClassRemoved new
  621. theClass: aClass;
  622. yourself)
  623. ! !
  624. !Smalltalk methodsFor: 'error handling'!
  625. asSmalltalkException: anObject
  626. "A JavaScript exception may be thrown.
  627. We then need to convert it back to a Smalltalk object"
  628. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  629. ifTrue: [ anObject ]
  630. ifFalse: [ JavaScriptException on: anObject ]
  631. !
  632. parseError: anException parsing: aString
  633. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  634. ! !
  635. !Smalltalk methodsFor: 'globals'!
  636. addGlobalJsVariable: aString
  637. self globalJsVariables add: aString
  638. !
  639. deleteGlobalJsVariable: aString
  640. self globalJsVariables remove: aString ifAbsent:[]
  641. !
  642. globalJsVariables
  643. "Array of global JavaScript variables"
  644. <return self.globalJsVariables>
  645. ! !
  646. !Smalltalk methodsFor: 'packages'!
  647. createPackage: packageName
  648. "Create and bind a new bare package with given name and return it."
  649. <return smalltalk.addPackage(packageName)>
  650. !
  651. packageAt: packageName
  652. <return self.packages[packageName]>
  653. !
  654. packageAt: packageName ifAbsent: aBlock
  655. ^(self packageAt: packageName) ifNil: aBlock
  656. !
  657. packages
  658. "Return all Package instances in the system."
  659. <
  660. var packages = [];
  661. for(var key in self.packages) {
  662. packages.push(self.packages[key]);
  663. }
  664. return packages;
  665. >
  666. !
  667. removePackage: packageName
  668. "Removes a package and all its classes."
  669. | pkg |
  670. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  671. pkg classes do: [:each |
  672. self removeClass: each].
  673. self deletePackage: packageName
  674. !
  675. renamePackage: packageName to: newName
  676. "Rename a package."
  677. | pkg |
  678. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  679. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  680. (self at: 'packages') at: newName put: pkg.
  681. pkg name: newName.
  682. self deletePackage: packageName.
  683. ! !
  684. !Smalltalk methodsFor: 'private'!
  685. basicParse: aString
  686. <return smalltalk.parser.parse(aString)>
  687. !
  688. createPackage: packageName properties: aDict
  689. "Needed to import .st files: they begin with this call."
  690. self deprecatedAPI.
  691. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  692. ^ self createPackage: packageName
  693. !
  694. deleteClass: aClass
  695. "Deletes a class by deleting its binding only. Use #removeClass instead"
  696. <self.removeClass(aClass)>
  697. !
  698. deletePackage: packageName
  699. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  700. To remove a package, use #removePackage instead."
  701. <delete self.packages[packageName]>
  702. ! !
  703. !Smalltalk methodsFor: 'testing'!
  704. isSmalltalkObject: anObject
  705. "Consider anObject a Smalltalk object if it has a 'klass' property.
  706. Note that this may be unaccurate"
  707. <return typeof anObject.klass !!== 'undefined'>
  708. ! !
  709. !Smalltalk class methodsFor: 'accessing'!
  710. current
  711. <return smalltalk>
  712. ! !
  713. !SequenceableCollection methodsFor: '*Kernel-Infrastructure'!
  714. do: aBlock displayingProgress: aString
  715. ProgressHandler current
  716. do: aBlock on: self displaying: aString
  717. ! !
  718. !String methodsFor: '*Kernel-Infrastructure'!
  719. asJavaScriptSelector
  720. "Return first keyword of the selector, without trailing colon."
  721. ^self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  722. ! !