Kernel-Infrastructure.st 27 KB

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