Kernel-Infrastructure.st 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. prompt: aString default: defaultString
  60. ^ PlatformInterface prompt: aString default: defaultString
  61. ! !
  62. InterfacingObject subclass: #Environment
  63. instanceVariableNames: ''
  64. package: 'Kernel-Infrastructure'!
  65. !Environment commentStamp!
  66. I provide an unified entry point to manipulate Amber packages, classes and methods.
  67. Typical use cases include IDEs, remote access and restricting browsing.!
  68. !Environment methodsFor: 'accessing'!
  69. allSelectors
  70. ^ Smalltalk vm allSelectors
  71. !
  72. availableClassNames
  73. ^ Smalltalk classes
  74. collect: [ :each | each name ]
  75. !
  76. availablePackageNames
  77. ^ Smalltalk packages
  78. collect: [ :each | each name ]
  79. !
  80. availableProtocolsFor: aClass
  81. | protocols |
  82. protocols := aClass protocols.
  83. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  84. ^ protocols asSet asArray sort
  85. !
  86. classBuilder
  87. ^ ClassBuilder new
  88. !
  89. classNamed: aString
  90. ^ (Smalltalk globals at: aString asSymbol)
  91. ifNil: [ self error: 'Invalid class name' ]
  92. !
  93. classes
  94. ^ Smalltalk classes
  95. !
  96. doItReceiver
  97. ^ DoIt new
  98. !
  99. packages
  100. ^ Smalltalk packages
  101. !
  102. systemAnnouncer
  103. ^ (Smalltalk globals at: #SystemAnnouncer) current
  104. ! !
  105. !Environment methodsFor: 'actions'!
  106. commitPackage: aPackage onSuccess: aBlock onError: anotherBlock
  107. aPackage transport
  108. commitOnSuccess: aBlock
  109. onError: anotherBlock
  110. !
  111. copyClass: aClass to: aClassName
  112. (Smalltalk globals at: aClassName)
  113. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  114. ClassBuilder new copyClass: aClass named: aClassName
  115. !
  116. inspect: anObject
  117. Inspector inspect: anObject
  118. !
  119. moveClass: aClass toPackage: aPackageName
  120. | package |
  121. package := Package named: aPackageName.
  122. package ifNil: [ self error: 'Invalid package name' ].
  123. package == aClass package ifTrue: [ ^ self ].
  124. aClass package: package
  125. !
  126. moveMethod: aMethod toClass: aClassName
  127. | destinationClass |
  128. destinationClass := self classNamed: aClassName.
  129. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  130. aMethod methodClass isMetaclass ifTrue: [
  131. destinationClass := destinationClass class ].
  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 evaluate: aString for: 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. aBlock tryCatch: [ :exception |
  188. (exception isKindOf: (self classNamed: anErrorClass name))
  189. ifTrue: [ exceptionBlock value: exception ]
  190. ifFalse: [ exception signal ] ]
  191. ! !
  192. !Environment methodsFor: 'evaluating'!
  193. evaluate: aString for: anObject
  194. ^ Evaluator evaluate: aString for: anObject
  195. ! !
  196. !Environment methodsFor: 'services'!
  197. registerErrorHandler: anErrorHandler
  198. ErrorHandler register: anErrorHandler
  199. !
  200. registerFinder: aFinder
  201. Finder register: aFinder
  202. !
  203. registerInspector: anInspector
  204. Inspector register: anInspector
  205. !
  206. registerProgressHandler: aProgressHandler
  207. ProgressHandler register: aProgressHandler
  208. !
  209. registerTranscript: aTranscript
  210. Transcript register: aTranscript
  211. ! !
  212. ProtoObject subclass: #JSObjectProxy
  213. instanceVariableNames: 'jsObject'
  214. package: 'Kernel-Infrastructure'!
  215. !JSObjectProxy commentStamp!
  216. I handle sending messages to JavaScript objects, making JavaScript object accessing from Amber fully transparent.
  217. My instances make intensive use of `#doesNotUnderstand:`.
  218. My instances are automatically created by Amber whenever a message is sent to a JavaScript object.
  219. ## Usage examples
  220. JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
  221. window alert: 'hello world'.
  222. window inspect.
  223. (window jQuery: 'body') append: 'hello world'
  224. 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.
  225. ## Message conversion rules
  226. - `someUser name` becomes `someUser.name`
  227. - `someUser name: 'John'` becomes `someUser name = "John"`
  228. - `console log: 'hello world'` becomes `console.log('hello world')`
  229. - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
  230. __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
  231. !JSObjectProxy methodsFor: 'accessing'!
  232. at: aString
  233. <return self['@jsObject'][aString]>
  234. !
  235. at: aString ifAbsent: aBlock
  236. "return the aString property or evaluate aBlock if the property is not defined on the object"
  237. <
  238. var obj = self['@jsObject'];
  239. return aString in obj ? obj[aString] : aBlock._value();
  240. >
  241. !
  242. at: aString ifPresent: aBlock
  243. "return the evaluation of aBlock with the value if the property is defined or return nil"
  244. <
  245. var obj = self['@jsObject'];
  246. return aString in obj ? aBlock._value_(obj[aString]) : nil;
  247. >
  248. !
  249. at: aString ifPresent: aBlock ifAbsent: anotherBlock
  250. "return the evaluation of aBlock with the value if the property is defined
  251. or return value of anotherBlock"
  252. <
  253. var obj = self['@jsObject'];
  254. return aString in obj ? aBlock._value_(obj[aString]) : anotherBlock._value();
  255. >
  256. !
  257. at: aString put: anObject
  258. <return self['@jsObject'][aString] = anObject>
  259. !
  260. jsObject
  261. ^ jsObject
  262. !
  263. jsObject: aJSObject
  264. jsObject := aJSObject
  265. !
  266. lookupProperty: aString
  267. "Looks up a property in JS object.
  268. Answer the property if it is present, or nil if it is not present."
  269. <return aString in self._jsObject() ? aString : nil>
  270. ! !
  271. !JSObjectProxy methodsFor: 'comparing'!
  272. = anObject
  273. anObject class == self class ifFalse: [ ^ false ].
  274. ^ self compareJSObjectWith: anObject jsObject
  275. ! !
  276. !JSObjectProxy methodsFor: 'enumerating'!
  277. asJSON
  278. "Answers the receiver in a stringyfy-friendly fashion"
  279. ^ jsObject
  280. !
  281. keysAndValuesDo: aBlock
  282. <
  283. var o = self['@jsObject'];
  284. for(var i in o) {
  285. aBlock._value_value_(i, o[i]);
  286. }
  287. >
  288. ! !
  289. !JSObjectProxy methodsFor: 'printing'!
  290. printOn: aStream
  291. aStream nextPutAll: self printString
  292. !
  293. printString
  294. <
  295. var js = self['@jsObject'];
  296. return js.toString
  297. ? js.toString()
  298. : Object.prototype.toString.call(js)
  299. >
  300. ! !
  301. !JSObjectProxy methodsFor: 'private'!
  302. compareJSObjectWith: aJSObject
  303. <return self["@jsObject"] === aJSObject>
  304. ! !
  305. !JSObjectProxy methodsFor: 'proxy'!
  306. addObjectVariablesTo: aDictionary
  307. <
  308. for(var i in self['@jsObject']) {
  309. aDictionary._at_put_(i, self['@jsObject'][i]);
  310. }
  311. >
  312. !
  313. doesNotUnderstand: aMessage
  314. ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
  315. ifNil: [ super doesNotUnderstand: aMessage ]
  316. ifNotNil: [ :jsSelector |
  317. self
  318. forwardMessage: jsSelector
  319. withArguments: aMessage arguments ]
  320. !
  321. forwardMessage: aString withArguments: anArray
  322. <
  323. return smalltalk.send(self._jsObject(), aString, anArray);
  324. >
  325. !
  326. inspectOn: anInspector
  327. | variables |
  328. variables := Dictionary new.
  329. variables at: '#self' put: self jsObject.
  330. anInspector setLabel: self printString.
  331. self addObjectVariablesTo: variables.
  332. anInspector setVariables: variables
  333. ! !
  334. !JSObjectProxy class methodsFor: 'instance creation'!
  335. on: aJSObject
  336. ^ self new
  337. jsObject: aJSObject;
  338. yourself
  339. ! !
  340. Object subclass: #NullProgressHandler
  341. instanceVariableNames: ''
  342. package: 'Kernel-Infrastructure'!
  343. !NullProgressHandler commentStamp!
  344. I am the default progress handler. I do not display any progress, and simply iterate over the collection.!
  345. !NullProgressHandler methodsFor: 'progress handling'!
  346. do: aBlock on: aCollection displaying: aString
  347. aCollection do: aBlock
  348. ! !
  349. NullProgressHandler class instanceVariableNames: 'current'!
  350. !NullProgressHandler class methodsFor: 'initialization'!
  351. initialize
  352. ProgressHandler registerIfNone: self new
  353. ! !
  354. Object subclass: #Organizer
  355. instanceVariableNames: ''
  356. package: 'Kernel-Infrastructure'!
  357. !Organizer commentStamp!
  358. I represent categorization information.
  359. ## API
  360. Use `#addElement:` and `#removeElement:` to manipulate instances.!
  361. !Organizer methodsFor: 'accessing'!
  362. addElement: anObject
  363. <self.elements.addElement(anObject)>
  364. !
  365. elements
  366. ^ (self basicAt: 'elements') copy
  367. !
  368. removeElement: anObject
  369. <self.elements.removeElement(anObject)>
  370. ! !
  371. Organizer subclass: #ClassOrganizer
  372. instanceVariableNames: ''
  373. package: 'Kernel-Infrastructure'!
  374. !ClassOrganizer commentStamp!
  375. I am an organizer specific to classes. I hold method categorization information for classes.!
  376. !ClassOrganizer methodsFor: 'accessing'!
  377. addElement: aString
  378. super addElement: aString.
  379. SystemAnnouncer current announce: (ProtocolAdded new
  380. protocol: aString;
  381. theClass: self theClass;
  382. yourself)
  383. !
  384. removeElement: aString
  385. super removeElement: aString.
  386. SystemAnnouncer current announce: (ProtocolRemoved new
  387. protocol: aString;
  388. theClass: self theClass;
  389. yourself)
  390. !
  391. theClass
  392. < return self.theClass >
  393. ! !
  394. Organizer subclass: #PackageOrganizer
  395. instanceVariableNames: ''
  396. package: 'Kernel-Infrastructure'!
  397. !PackageOrganizer commentStamp!
  398. I am an organizer specific to packages. I hold classes categorization information.!
  399. Object subclass: #Package
  400. instanceVariableNames: 'transport dirty'
  401. package: 'Kernel-Infrastructure'!
  402. !Package commentStamp!
  403. 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.
  404. 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.
  405. ## API
  406. Packages are manipulated through "Smalltalk current", like for example finding one based on a name or with `Package class >> #name` directly:
  407. Smalltalk current packageAt: 'Kernel'
  408. Package named: 'Kernel'
  409. 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.
  410. You can fetch a package from the server:
  411. Package load: 'Additional-Examples'!
  412. !Package methodsFor: 'accessing'!
  413. beClean
  414. dirty := false.
  415. SystemAnnouncer current announce: (PackageClean new
  416. package: self;
  417. yourself)
  418. !
  419. beDirty
  420. dirty := true.
  421. SystemAnnouncer current announce: (PackageDirty new
  422. package: self;
  423. yourself)
  424. !
  425. classTemplate
  426. ^ String streamContents: [ :stream |
  427. stream
  428. nextPutAll: 'Object';
  429. nextPutAll: ' subclass: #NameOfSubclass';
  430. nextPutAll: String lf, String tab;
  431. nextPutAll: 'instanceVariableNames: '''''.
  432. stream
  433. nextPutAll: '''', String lf, String tab;
  434. nextPutAll: 'package: ''';
  435. nextPutAll: self name;
  436. nextPutAll: '''' ]
  437. !
  438. definition
  439. ^ String streamContents: [ :stream |
  440. stream
  441. nextPutAll: self class name;
  442. nextPutAll: String lf, String tab;
  443. nextPutAll: ' named: ';
  444. nextPutAll: '''', self name, '''';
  445. nextPutAll: String lf, String tab;
  446. nextPutAll: ' transport: (';
  447. nextPutAll: self transport definition, ')' ]
  448. !
  449. name
  450. <return self.pkgName>
  451. !
  452. name: aString
  453. self basicName: aString.
  454. self beDirty
  455. !
  456. organization
  457. ^ self basicAt: 'organization'
  458. !
  459. transport
  460. ^ transport ifNil: [
  461. transport := (PackageTransport fromJson: self basicTransport)
  462. package: self;
  463. yourself ]
  464. !
  465. transport: aPackageTransport
  466. transport := aPackageTransport.
  467. aPackageTransport package: self
  468. ! !
  469. !Package methodsFor: 'classes'!
  470. classes
  471. ^ self organization elements
  472. !
  473. setupClasses
  474. self classes
  475. do: [ :each | ClassBuilder new setupClass: each ];
  476. do: [ :each | each initialize ]
  477. !
  478. sortedClasses
  479. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  480. ^ self class sortedClasses: self classes
  481. ! !
  482. !Package methodsFor: 'dependencies'!
  483. loadDependencies
  484. "Returns list of packages that need to be loaded
  485. before loading this package."
  486. | classes packages |
  487. classes := self loadDependencyClasses.
  488. ^ (classes collect: [ :each | each package ]) asSet
  489. remove: self ifAbsent: [];
  490. yourself
  491. !
  492. loadDependencyClasses
  493. "Returns classes needed at the time of loading a package.
  494. These are all that are used to subclass
  495. and to define an extension method"
  496. | starCategoryName |
  497. starCategoryName := '*', self name.
  498. ^ (self classes collect: [ :each | each superclass ]) asSet
  499. remove: nil ifAbsent: [];
  500. addAll: (Smalltalk classes select: [ :each | each protocols, each class protocols includes: starCategoryName ]);
  501. yourself
  502. ! !
  503. !Package methodsFor: 'printing'!
  504. printOn: aStream
  505. super printOn: aStream.
  506. aStream
  507. nextPutAll: ' (';
  508. nextPutAll: self name;
  509. nextPutAll: ')'
  510. ! !
  511. !Package methodsFor: 'private'!
  512. basicName: aString
  513. <self.pkgName = aString>
  514. !
  515. basicTransport
  516. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  517. <return self.transport>
  518. ! !
  519. !Package methodsFor: 'testing'!
  520. isDirty
  521. ^ dirty ifNil: [ false ]
  522. !
  523. isPackage
  524. ^ true
  525. ! !
  526. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  527. !Package class methodsFor: 'accessing'!
  528. named: aPackageName
  529. ^ Smalltalk
  530. packageAt: aPackageName
  531. ifAbsent: [
  532. Smalltalk createPackage: aPackageName ]
  533. !
  534. named: aPackageName ifAbsent: aBlock
  535. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  536. !
  537. named: aPackageName transport: aTransport
  538. | package |
  539. package := self named: aPackageName.
  540. package transport: aTransport.
  541. ^ package
  542. ! !
  543. !Package class methodsFor: 'sorting'!
  544. sortedClasses: classes
  545. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  546. | children others nodes expandedClasses |
  547. children := #().
  548. others := #().
  549. classes do: [ :each |
  550. (classes includes: each superclass)
  551. ifFalse: [ children add: each ]
  552. ifTrue: [ others add: each ]].
  553. nodes := children collect: [ :each |
  554. ClassSorterNode on: each classes: others level: 0 ].
  555. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  556. expandedClasses := Array new.
  557. nodes do: [ :aNode |
  558. aNode traverseClassesWith: expandedClasses ].
  559. ^ expandedClasses
  560. ! !
  561. Object subclass: #PackageStateObserver
  562. instanceVariableNames: ''
  563. package: 'Kernel-Infrastructure'!
  564. !PackageStateObserver commentStamp!
  565. My current instance listens for any changes in the system that might affect the state of a package (being dirty).!
  566. !PackageStateObserver methodsFor: 'accessing'!
  567. announcer
  568. ^ SystemAnnouncer current
  569. ! !
  570. !PackageStateObserver methodsFor: 'actions'!
  571. observeSystem
  572. self announcer
  573. on: PackageAdded
  574. send: #onPackageAdded:
  575. to: self;
  576. on: ClassAnnouncement
  577. send: #onClassModification:
  578. to: self;
  579. on: MethodAnnouncement
  580. send: #onMethodModification:
  581. to: self;
  582. on: ProtocolAnnouncement
  583. send: #onProtocolModification:
  584. to: self
  585. ! !
  586. !PackageStateObserver methodsFor: 'reactions'!
  587. onClassModification: anAnnouncement
  588. anAnnouncement theClass ifNotNil: [ :theClass | theClass package beDirty ]
  589. !
  590. onMethodModification: anAnnouncement
  591. anAnnouncement method package ifNotNil: [ :package | package beDirty ]
  592. !
  593. onPackageAdded: anAnnouncement
  594. anAnnouncement package beDirty
  595. !
  596. onProtocolModification: anAnnouncement
  597. anAnnouncement package ifNotNil: [ :package | package beDirty ]
  598. ! !
  599. PackageStateObserver class instanceVariableNames: 'current'!
  600. !PackageStateObserver class methodsFor: 'accessing'!
  601. current
  602. ^ current ifNil: [ current := self new ]
  603. ! !
  604. !PackageStateObserver class methodsFor: 'initialization'!
  605. initialize
  606. self current observeSystem
  607. ! !
  608. Object subclass: #PlatformInterface
  609. instanceVariableNames: ''
  610. package: 'Kernel-Infrastructure'!
  611. !PlatformInterface commentStamp!
  612. I am single entry point to UI and environment interface.
  613. My `initialize` tries several options (for now, browser environment only) to set myself up.
  614. ## API
  615. PlatformInterface alert: 'Hey, there is a problem'.
  616. PlatformInterface confirm: 'Affirmative?'.
  617. PlatformInterface prompt: 'Your name:'.
  618. PlatformInterface ajax: #{
  619. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  620. }.!
  621. PlatformInterface class instanceVariableNames: 'worker'!
  622. !PlatformInterface class methodsFor: 'accessing'!
  623. globals
  624. <return (new Function('return this'))();>
  625. !
  626. setWorker: anObject
  627. worker := anObject
  628. ! !
  629. !PlatformInterface class methodsFor: 'actions'!
  630. ajax: anObject
  631. ^ worker
  632. ifNotNil: [ worker ajax: anObject ]
  633. ifNil: [ self error: 'ajax: not available' ]
  634. !
  635. alert: aString
  636. ^ worker
  637. ifNotNil: [ worker alert: aString ]
  638. ifNil: [ self error: 'alert: not available' ]
  639. !
  640. confirm: aString
  641. ^ worker
  642. ifNotNil: [ worker confirm: aString ]
  643. ifNil: [ self error: 'confirm: not available' ]
  644. !
  645. existsGlobal: aString
  646. ^ PlatformInterface globals
  647. at: aString
  648. ifPresent: [ true ]
  649. ifAbsent: [ false ]
  650. !
  651. prompt: aString
  652. ^ worker
  653. ifNotNil: [ worker prompt: aString ]
  654. ifNil: [ self error: 'prompt: not available' ]
  655. !
  656. prompt: aString default: defaultString
  657. ^ worker
  658. ifNotNil: [ worker prompt: aString default: defaultString ]
  659. ifNil: [ self error: 'prompt: not available' ]
  660. ! !
  661. !PlatformInterface class methodsFor: 'initialization'!
  662. initialize
  663. | candidate |
  664. super initialize.
  665. BrowserInterface ifNotNil: [
  666. candidate := BrowserInterface new.
  667. candidate isAvailable ifTrue: [ self setWorker: candidate. ^ self ]
  668. ]
  669. ! !
  670. Object subclass: #Service
  671. instanceVariableNames: ''
  672. package: 'Kernel-Infrastructure'!
  673. !Service commentStamp!
  674. I implement the basic behavior for class registration to a service.
  675. See the `Transcript` class for a concrete service.
  676. ## API
  677. Use class-side methods `#register:` and `#registerIfNone:` to register classes to a specific service.!
  678. Service class instanceVariableNames: 'current'!
  679. !Service class methodsFor: 'accessing'!
  680. current
  681. ^ current
  682. ! !
  683. !Service class methodsFor: 'instance creation'!
  684. new
  685. self shouldNotImplement
  686. ! !
  687. !Service class methodsFor: 'registration'!
  688. register: anObject
  689. current := anObject
  690. !
  691. registerIfNone: anObject
  692. self current ifNil: [ self register: anObject ]
  693. ! !
  694. Service subclass: #ErrorHandler
  695. instanceVariableNames: ''
  696. package: 'Kernel-Infrastructure'!
  697. !ErrorHandler commentStamp!
  698. I am the service used to handle Smalltalk errors.
  699. See `boot.js` `handleError()` function.
  700. Registered service instances must implement `#handleError:` to perform an action on the thrown exception.!
  701. !ErrorHandler class methodsFor: 'error handling'!
  702. handleError: anError
  703. self handleUnhandledError: anError
  704. !
  705. handleUnhandledError: anError
  706. anError wasHandled ifTrue: [ ^ self ].
  707. ^ self current handleError: anError
  708. ! !
  709. Service subclass: #Finder
  710. instanceVariableNames: ''
  711. package: 'Kernel-Infrastructure'!
  712. !Finder commentStamp!
  713. I am the service responsible for finding classes/methods.
  714. __There is no default finder.__
  715. ## API
  716. Use `#browse` on an object to find it.!
  717. !Finder class methodsFor: 'finding'!
  718. findClass: aClass
  719. ^ self current findClass: aClass
  720. !
  721. findMethod: aCompiledMethod
  722. ^ self current findMethod: aCompiledMethod
  723. !
  724. findString: aString
  725. ^ self current findString: aString
  726. ! !
  727. Service subclass: #Inspector
  728. instanceVariableNames: ''
  729. package: 'Kernel-Infrastructure'!
  730. !Inspector commentStamp!
  731. I am the service responsible for inspecting objects.
  732. The default inspector object is the transcript.!
  733. !Inspector class methodsFor: 'inspecting'!
  734. inspect: anObject
  735. ^ self current inspect: anObject
  736. ! !
  737. Service subclass: #ProgressHandler
  738. instanceVariableNames: ''
  739. package: 'Kernel-Infrastructure'!
  740. !ProgressHandler commentStamp!
  741. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  742. Registered instances must implement `#do:on:displaying:`.
  743. The default behavior is to simply iterate over the collection, using `NullProgressHandler`.!
  744. !ProgressHandler class methodsFor: 'progress handling'!
  745. do: aBlock on: aCollection displaying: aString
  746. self current do: aBlock on: aCollection displaying: aString
  747. ! !
  748. Service subclass: #Transcript
  749. instanceVariableNames: ''
  750. package: 'Kernel-Infrastructure'!
  751. !Transcript commentStamp!
  752. I am a facade for Transcript actions.
  753. I delegate actions to the currently registered transcript.
  754. ## API
  755. Transcript
  756. show: 'hello world';
  757. cr;
  758. show: anObject.!
  759. !Transcript class methodsFor: 'instance creation'!
  760. open
  761. self current open
  762. ! !
  763. !Transcript class methodsFor: 'printing'!
  764. clear
  765. self current clear
  766. !
  767. cr
  768. self current show: String cr
  769. !
  770. inspect: anObject
  771. self show: anObject
  772. !
  773. show: anObject
  774. self current show: anObject
  775. ! !
  776. Object subclass: #Setting
  777. instanceVariableNames: 'key value defaultValue'
  778. package: 'Kernel-Infrastructure'!
  779. !Setting commentStamp!
  780. I represent a setting accessible via `Smalltalk settings`.
  781. ## API
  782. A `Setting` value can be read using `value` and set using `value:`.
  783. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: 'defaultValue'`.!
  784. !Setting methodsFor: 'accessing'!
  785. defaultValue
  786. ^ defaultValue
  787. !
  788. defaultValue: anObject
  789. defaultValue := anObject
  790. !
  791. key
  792. ^ key
  793. !
  794. key: anObject
  795. key := anObject
  796. !
  797. value
  798. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  799. !
  800. value: aString
  801. ^ Smalltalk settings at: self key put: aString
  802. ! !
  803. !Setting class methodsFor: 'instance creation'!
  804. at: aString ifAbsent: anotherString
  805. ^ super new
  806. key: aString;
  807. defaultValue: anotherString;
  808. yourself
  809. !
  810. new
  811. self shouldNotImplement
  812. ! !
  813. Object subclass: #SmalltalkImage
  814. instanceVariableNames: ''
  815. package: 'Kernel-Infrastructure'!
  816. !SmalltalkImage commentStamp!
  817. I represent the Smalltalk system, wrapping
  818. operations of variable `smalltalk` declared in `support/boot.js`.
  819. ## API
  820. I have only one instance, accessed with global variable `Smalltalk`.
  821. The `smalltalk` object holds all class and packages defined in the system.
  822. ## Classes
  823. Classes can be accessed using the following methods:
  824. - `#classes` answers the full list of Smalltalk classes in the system
  825. - `#at:` answers a specific class or `nil`
  826. ## Packages
  827. Packages can be accessed using the following methods:
  828. - `#packages` answers the full list of packages
  829. - `#packageAt:` answers a specific package or `nil`
  830. ## Parsing
  831. The `#parse:` method is used to parse Amber source code.
  832. It requires the `Compiler` package and the `support/parser.js` parser file in order to work.!
  833. !SmalltalkImage methodsFor: 'accessing'!
  834. at: aString
  835. self deprecatedAPI.
  836. ^ self globals at: aString
  837. !
  838. at: aKey ifAbsent: aBlock
  839. ^ (self includesKey: aKey)
  840. ifTrue: [ self at: aKey ]
  841. ifFalse: [ aBlock value ]
  842. !
  843. at: aString put: anObject
  844. self deprecatedAPI.
  845. ^ self globals at: aString put: anObject
  846. !
  847. current
  848. "Backward compatibility for Smalltalk current ..."
  849. self deprecatedAPI.
  850. ^ self
  851. !
  852. globals
  853. "Future compatibility to be able to use Smalltalk globals at: ..."
  854. <return globals>
  855. !
  856. includesKey: aKey
  857. <return smalltalk.hasOwnProperty(aKey)>
  858. !
  859. parse: aString
  860. | result |
  861. [ result := self basicParse: aString ]
  862. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  863. ^ result
  864. source: aString;
  865. yourself
  866. !
  867. pseudoVariableNames
  868. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  869. !
  870. readJSObject: anObject
  871. <return smalltalk.readJSObject(anObject)>
  872. !
  873. reservedWords
  874. "JavaScript reserved words"
  875. <return smalltalk.reservedWords>
  876. !
  877. settings
  878. ^ SmalltalkSettings
  879. !
  880. version
  881. "Answer the version string of Amber"
  882. ^ '0.13.0-pre'
  883. !
  884. vm
  885. "Future compatibility to be able to use Smalltalk vm ..."
  886. <return smalltalk>
  887. ! !
  888. !SmalltalkImage methodsFor: 'accessing amd'!
  889. amdRequire
  890. ^ self vm at: 'amdRequire'
  891. !
  892. defaultAmdNamespace
  893. ^ 'transport.defaultAmdNamespace' settingValue
  894. !
  895. defaultAmdNamespace: aString
  896. 'transport.defaultAmdNamespace' settingValue: aString
  897. ! !
  898. !SmalltalkImage methodsFor: 'classes'!
  899. classes
  900. <return smalltalk.classes()>
  901. !
  902. removeClass: aClass
  903. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  904. self deleteClass: aClass.
  905. SystemAnnouncer current
  906. announce: (ClassRemoved new
  907. theClass: aClass;
  908. yourself)
  909. ! !
  910. !SmalltalkImage methodsFor: 'error handling'!
  911. asSmalltalkException: anObject
  912. "A JavaScript exception may be thrown.
  913. We then need to convert it back to a Smalltalk object"
  914. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  915. ifTrue: [ anObject ]
  916. ifFalse: [ JavaScriptException on: anObject ]
  917. !
  918. parseError: anException parsing: aString
  919. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  920. ! !
  921. !SmalltalkImage methodsFor: 'globals'!
  922. addGlobalJsVariable: aString
  923. self globalJsVariables add: aString
  924. !
  925. deleteGlobalJsVariable: aString
  926. self globalJsVariables remove: aString ifAbsent:[]
  927. !
  928. globalJsVariables
  929. "Array of global JavaScript variables"
  930. <return smalltalk.globalJsVariables>
  931. ! !
  932. !SmalltalkImage methodsFor: 'packages'!
  933. createPackage: packageName
  934. | package announcement |
  935. package := self basicCreatePackage: packageName.
  936. announcement := PackageAdded new
  937. package: package;
  938. yourself.
  939. SystemAnnouncer current announce: announcement.
  940. ^ package
  941. !
  942. packageAt: packageName
  943. <return smalltalk.packages[packageName]>
  944. !
  945. packageAt: packageName ifAbsent: aBlock
  946. ^ (self packageAt: packageName) ifNil: aBlock
  947. !
  948. packages
  949. "Return all Package instances in the system."
  950. <
  951. return Object.keys(smalltalk.packages).map(function(k) {
  952. return smalltalk.packages[k];
  953. })
  954. >
  955. !
  956. removePackage: packageName
  957. "Removes a package and all its classes."
  958. | pkg |
  959. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  960. pkg classes do: [ :each |
  961. self removeClass: each ].
  962. self deletePackage: packageName
  963. !
  964. renamePackage: packageName to: newName
  965. "Rename a package."
  966. | pkg |
  967. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  968. (self packageAt: newName) ifNotNil: [ self error: 'Already exists a package called: ', newName ].
  969. (self at: 'packages') at: newName put: pkg.
  970. pkg name: newName.
  971. self deletePackage: packageName.
  972. ! !
  973. !SmalltalkImage methodsFor: 'private'!
  974. basicCreatePackage: packageName
  975. "Create and bind a new bare package with given name and return it."
  976. <return smalltalk.addPackage(packageName)>
  977. !
  978. basicParse: aString
  979. ^ SmalltalkParser parse: aString
  980. !
  981. createPackage: packageName properties: aDict
  982. "Needed to import .st files: they begin with this call."
  983. self deprecatedAPI.
  984. aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
  985. ^ self createPackage: packageName
  986. !
  987. deleteClass: aClass
  988. "Deletes a class by deleting its binding only. Use #removeClass instead"
  989. <smalltalk.removeClass(aClass)>
  990. !
  991. deletePackage: packageName
  992. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  993. To remove a package, use #removePackage instead."
  994. <delete smalltalk.packages[packageName]>
  995. ! !
  996. !SmalltalkImage methodsFor: 'testing'!
  997. isSmalltalkObject: anObject
  998. "Consider anObject a Smalltalk object if it has a 'klass' property.
  999. Note that this may be unaccurate"
  1000. <return typeof anObject.klass !!== 'undefined'>
  1001. ! !
  1002. SmalltalkImage class instanceVariableNames: 'current'!
  1003. !SmalltalkImage class methodsFor: 'initialization'!
  1004. initialize
  1005. globals at: 'Smalltalk' put: self current
  1006. ! !
  1007. !SmalltalkImage class methodsFor: 'instance creation'!
  1008. current
  1009. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  1010. !
  1011. new
  1012. self shouldNotImplement
  1013. ! !
  1014. !SequenceableCollection methodsFor: '*Kernel-Infrastructure'!
  1015. do: aBlock displayingProgress: aString
  1016. ProgressHandler
  1017. do: aBlock
  1018. on: self
  1019. displaying: aString
  1020. ! !
  1021. !String methodsFor: '*Kernel-Infrastructure'!
  1022. asJavaScriptSelector
  1023. "Return first keyword of the selector, without trailing colon."
  1024. ^ self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
  1025. !
  1026. asSetting
  1027. ^ Setting at: self ifAbsent: nil
  1028. !
  1029. asSettingIfAbsent: aString
  1030. ^ Setting at: self ifAbsent: aString
  1031. !
  1032. settingValue
  1033. ^ self asSetting value
  1034. !
  1035. settingValue: aString
  1036. ^ self asSetting value: aString
  1037. !
  1038. settingValueIfAbsent: aString
  1039. ^ (self asSettingIfAbsent: aString) value
  1040. ! !