2
0

Kernel-Infrastructure.st 23 KB

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