1
0

Kernel-Infrastructure.st 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. definition
  372. ^ String streamContents: [ :stream |
  373. stream
  374. nextPutAll: self class name;
  375. nextPutAll: String lf, String tab;
  376. nextPutAll: ' named: ';
  377. nextPutAll: '''', self name, '''';
  378. nextPutAll: String lf, String tab;
  379. nextPutAll: ' transport: (';
  380. nextPutAll: self transport definition, ')' ]
  381. !
  382. name
  383. <return self.pkgName>
  384. !
  385. name: aString
  386. <self.pkgName = aString>
  387. !
  388. organization
  389. ^ self basicAt: 'organization'
  390. !
  391. transport
  392. ^ transport ifNil: [
  393. transport := (PackageTransport fromJson: self basicTransport)
  394. package: self;
  395. yourself ]
  396. !
  397. transport: aPackageTransport
  398. transport := aPackageTransport.
  399. aPackageTransport package: self
  400. ! !
  401. !Package methodsFor: 'classes'!
  402. classes
  403. ^ self organization elements asSet asArray
  404. !
  405. setupClasses
  406. self classes
  407. do: [ :each | ClassBuilder new setupClass: each ];
  408. do: [ :each | each initialize ]
  409. !
  410. sortedClasses
  411. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  412. ^ self class sortedClasses: self classes
  413. ! !
  414. !Package methodsFor: 'dependencies'!
  415. loadDependencies
  416. "Returns list of packages that need to be loaded
  417. before loading this package."
  418. | classes packages |
  419. classes := self loadDependencyClasses.
  420. ^(classes collect: [ :each | each package ]) asSet
  421. remove: self ifAbsent: [];
  422. yourself
  423. !
  424. loadDependencyClasses
  425. "Returns classes needed at the time of loading a package.
  426. These are all that are used to subclass
  427. and to define an extension method"
  428. | starCategoryName |
  429. starCategoryName := '*', self name.
  430. ^(self classes collect: [ :each | each superclass ]) asSet
  431. remove: nil ifAbsent: [];
  432. addAll: (Smalltalk current classes select: [ :each | each protocols includes: starCategoryName ]);
  433. yourself
  434. ! !
  435. !Package methodsFor: 'printing'!
  436. printOn: aStream
  437. super printOn: aStream.
  438. aStream
  439. nextPutAll: ' (';
  440. nextPutAll: self name;
  441. nextPutAll: ')'
  442. ! !
  443. !Package methodsFor: 'testing'!
  444. isPackage
  445. ^ true
  446. ! !
  447. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  448. !Package class methodsFor: 'accessing'!
  449. named: aPackageName
  450. ^ Smalltalk current
  451. packageAt: aPackageName
  452. ifAbsent: [
  453. Smalltalk current createPackage: aPackageName ]
  454. !
  455. named: aPackageName ifAbsent: aBlock
  456. ^ Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  457. !
  458. named: aPackageName transport: aTransport
  459. | package |
  460. package := self named: aPackageName.
  461. package transport: aTransport.
  462. ^ package
  463. ! !
  464. !Package class methodsFor: 'sorting'!
  465. sortedClasses: classes
  466. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  467. | children others nodes expandedClasses |
  468. children := #().
  469. others := #().
  470. classes do: [:each |
  471. (classes includes: each superclass)
  472. ifFalse: [children add: each]
  473. ifTrue: [others add: each]].
  474. nodes := children collect: [:each |
  475. ClassSorterNode on: each classes: others level: 0].
  476. nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
  477. expandedClasses := Array new.
  478. nodes do: [:aNode |
  479. aNode traverseClassesWith: expandedClasses].
  480. ^expandedClasses
  481. ! !
  482. Object subclass: #PlatformInterface
  483. instanceVariableNames: ''
  484. package: 'Kernel-Infrastructure'!
  485. !PlatformInterface commentStamp!
  486. I am single entry point to UI and environment interface.
  487. My `initialize` tries several options (for now, browser environment only) to set myself up.
  488. ## API
  489. PlatformInterface alert: 'Hey, there is a problem'.
  490. PlatformInterface confirm: 'Affirmative?'.
  491. PlatformInterface prompt: 'Your name:'.
  492. PlatformInterface ajax: #{
  493. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  494. }.!
  495. PlatformInterface class instanceVariableNames: 'worker'!
  496. !PlatformInterface class methodsFor: 'accessing'!
  497. globals
  498. <return (new Function('return this'))();>
  499. !
  500. setWorker: anObject
  501. worker := anObject
  502. ! !
  503. !PlatformInterface class methodsFor: 'actions'!
  504. ajax: anObject
  505. ^worker
  506. ifNotNil: [ worker ajax: anObject ]
  507. ifNil: [ self error: 'ajax: not available' ]
  508. !
  509. alert: aString
  510. ^worker
  511. ifNotNil: [ worker alert: aString ]
  512. ifNil: [ self error: 'alert: not available' ]
  513. !
  514. confirm: aString
  515. ^worker
  516. ifNotNil: [ worker confirm: aString ]
  517. ifNil: [ self error: 'confirm: not available' ]
  518. !
  519. existsGlobal: aString
  520. ^ PlatformInterface globals
  521. at: aString
  522. ifPresent: [ true ]
  523. ifAbsent: [ false ]
  524. !
  525. prompt: aString
  526. ^worker
  527. ifNotNil: [ worker prompt: aString ]
  528. ifNil: [ self error: 'prompt: not available' ]
  529. ! !
  530. !PlatformInterface class methodsFor: 'initialization'!
  531. initialize
  532. | candidate |
  533. super initialize.
  534. BrowserInterface ifNotNil: [
  535. candidate := BrowserInterface new.
  536. candidate isAvailable ifTrue: [ self setWorker: candidate. ^self ]
  537. ]
  538. ! !
  539. Object subclass: #ProgressHandler
  540. instanceVariableNames: ''
  541. package: 'Kernel-Infrastructure'!
  542. !ProgressHandler commentStamp!
  543. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  544. Subclasses of can register themselves as the current handler with
  545. `ProgressHandler class >> register`.
  546. The default behavior is to simply iterate over the collection.!
  547. !ProgressHandler methodsFor: 'progress handling'!
  548. do: aBlock on: aCollection displaying: aString
  549. aCollection do: aBlock
  550. ! !
  551. ProgressHandler class instanceVariableNames: 'current'!
  552. !ProgressHandler class methodsFor: 'accessing'!
  553. current
  554. ^current ifNil: [ current := self new ]
  555. !
  556. setCurrent: anHandler
  557. current := anHandler
  558. ! !
  559. !ProgressHandler class methodsFor: 'initialization'!
  560. initialize
  561. self register
  562. !
  563. register
  564. ProgressHandler setCurrent: self new
  565. ! !
  566. Object subclass: #Smalltalk
  567. instanceVariableNames: ''
  568. package: 'Kernel-Infrastructure'!
  569. !Smalltalk commentStamp!
  570. I represent the global JavaScript variable `smalltalk` declared in `js/boot.js`.
  571. ## API
  572. I have only one instance, accessed with class-side method `#current`.
  573. The `smalltalk` object holds all class and packages defined in the system.
  574. ## Classes
  575. Classes can be accessed using the following methods:
  576. - `#classes` answers the full list of Smalltalk classes in the system
  577. - `#at:` answers a specific class or `nil`
  578. ## Packages
  579. Packages can be accessed using the following methods:
  580. - `#packages` answers the full list of packages
  581. - `#packageAt:` answers a specific package or `nil`
  582. ## Parsing
  583. The `#parse:` method is used to parse Amber source code.
  584. It requires the `Compiler` package and the `js/parser.js` parser file in order to work.!
  585. !Smalltalk methodsFor: 'accessing'!
  586. at: aString
  587. ^ self basicAt: aString
  588. !
  589. at: aKey ifAbsent: aBlock
  590. ^ (self includesKey: aKey)
  591. ifTrue: [self at: aKey]
  592. ifFalse: aBlock
  593. !
  594. at: aString put: anObject
  595. ^ self basicAt: aString put: anObject
  596. !
  597. includesKey: aKey
  598. <return self.hasOwnProperty(aKey)>
  599. !
  600. parse: aString
  601. | result |
  602. self
  603. try: [result := self basicParse: aString]
  604. catch: [:ex | (self parseError: ex parsing: aString) signal].
  605. ^ result
  606. source: aString;
  607. yourself
  608. !
  609. pseudoVariableNames
  610. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  611. !
  612. readJSObject: anObject
  613. <return self.readJSObject(anObject)>
  614. !
  615. reservedWords
  616. "JavaScript reserved words"
  617. <return self.reservedWords>
  618. !
  619. version
  620. "Answer the version string of Amber"
  621. ^ '0.12.0-pre'
  622. ! !
  623. !Smalltalk methodsFor: 'accessing amd'!
  624. amdRequire
  625. ^ self at: 'amdRequire'
  626. !
  627. defaultAmdNamespace
  628. ^ self at: 'defaultAmdNamespace'
  629. !
  630. defaultAmdNamespace: aString
  631. self at: 'defaultAmdNamespace' put: aString
  632. ! !
  633. !Smalltalk methodsFor: 'classes'!
  634. classes
  635. <return self.classes()>
  636. !
  637. removeClass: aClass
  638. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  639. self deleteClass: aClass.
  640. SystemAnnouncer current
  641. announce: (ClassRemoved new
  642. theClass: aClass;
  643. yourself)
  644. ! !
  645. !Smalltalk methodsFor: 'error handling'!
  646. asSmalltalkException: anObject
  647. "A JavaScript exception may be thrown.
  648. We then need to convert it back to a Smalltalk object"
  649. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  650. ifTrue: [ anObject ]
  651. ifFalse: [ JavaScriptException on: anObject ]
  652. !
  653. parseError: anException parsing: aString
  654. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  655. ! !
  656. !Smalltalk methodsFor: 'globals'!
  657. addGlobalJsVariable: aString
  658. self globalJsVariables add: aString
  659. !
  660. deleteGlobalJsVariable: aString
  661. self globalJsVariables remove: aString ifAbsent:[]
  662. !
  663. globalJsVariables
  664. "Array of global JavaScript variables"
  665. <return self.globalJsVariables>
  666. ! !
  667. !Smalltalk methodsFor: 'packages'!
  668. createPackage: packageName
  669. | package announcement |
  670. package := self basicCreatePackage: packageName.
  671. announcement := PackageAdded new
  672. package: package;
  673. yourself.
  674. SystemAnnouncer current announce: announcement.
  675. ^ package
  676. !
  677. packageAt: packageName
  678. <return self.packages[packageName]>
  679. !
  680. packageAt: packageName ifAbsent: aBlock
  681. ^(self packageAt: packageName) ifNil: aBlock
  682. !
  683. packages
  684. "Return all Package instances in the system."
  685. <
  686. var packages = [];
  687. for(var key in self.packages) {
  688. packages.push(self.packages[key]);
  689. }
  690. return packages;
  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. ! !