2
0

Kernel-Infrastructure.st 23 KB

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