Kernel-Infrastructure.st 26 KB

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