Kernel-Infrastructure.st 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 core 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 asJavaScriptPropertyName)
  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 $core.accessJavaScript(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 imports 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: 'imports: ';
  447. nextPutAll: self importsDefinition;
  448. nextPutAll: String lf, String tab;
  449. nextPutAll: 'transport: (';
  450. nextPutAll: self transport definition, ')' ]
  451. !
  452. imports
  453. ^ imports ifNil: [
  454. | parsed |
  455. parsed := self importsFromJson: self basicImports.
  456. self imports: parsed.
  457. imports ]
  458. !
  459. imports: anArray
  460. self validateImports: anArray.
  461. imports := anArray asSet
  462. !
  463. importsDefinition
  464. ^ String streamContents: [ :stream |
  465. stream nextPutAll: '{'.
  466. self sortedImportsAsArray
  467. do: [ :each | stream nextPutAll: each importsString ]
  468. separatedBy: [ stream nextPutAll: '. ' ].
  469. stream nextPutAll: '}' ]
  470. !
  471. name
  472. <return self.pkgName>
  473. !
  474. name: aString
  475. self basicName: aString.
  476. self beDirty
  477. !
  478. organization
  479. ^ self basicAt: 'organization'
  480. !
  481. transport
  482. ^ transport ifNil: [
  483. transport := (PackageTransport fromJson: self basicTransport)
  484. package: self;
  485. yourself ]
  486. !
  487. transport: aPackageTransport
  488. transport := aPackageTransport.
  489. aPackageTransport package: self
  490. ! !
  491. !Package methodsFor: 'classes'!
  492. classes
  493. ^ self organization elements
  494. !
  495. setupClasses
  496. self classes
  497. do: [ :each | ClassBuilder new setupClass: each ];
  498. do: [ :each | each initialize ]
  499. !
  500. sortedClasses
  501. "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
  502. ^ self class sortedClasses: self classes
  503. ! !
  504. !Package methodsFor: 'converting'!
  505. importsAsJson
  506. ^ self sortedImportsAsArray collect: [ :each |
  507. each isString
  508. ifTrue: [ each ]
  509. ifFalse: [ each key, '=', each value ]]
  510. !
  511. importsFromJson: anArray
  512. "Parses array of string, eg. #('asdf' 'qwer=tyuo')
  513. into array of Strings and Associations,
  514. eg. {'asdf'. 'qwer'->'tyuo'}"
  515. ^ anArray collect: [ :each |
  516. | split |
  517. split := each tokenize: '='.
  518. split size = 1
  519. ifTrue: [ split first ]
  520. ifFalse: [ split first -> split second ]]
  521. ! !
  522. !Package methodsFor: 'dependencies'!
  523. loadDependencies
  524. "Returns list of packages that need to be loaded
  525. before loading this package."
  526. | classes packages |
  527. classes := self loadDependencyClasses.
  528. ^ (classes collect: [ :each | each package ]) asSet
  529. remove: self ifAbsent: [];
  530. yourself
  531. !
  532. loadDependencyClasses
  533. "Returns classes needed at the time of loading a package.
  534. These are all that are used to subclass
  535. and to define an extension method"
  536. | starCategoryName |
  537. starCategoryName := '*', self name.
  538. ^ (self classes collect: [ :each | each superclass ]) asSet
  539. remove: nil ifAbsent: [];
  540. addAll: (Smalltalk classes select: [ :each | each protocols, each class protocols includes: starCategoryName ]);
  541. yourself
  542. ! !
  543. !Package methodsFor: 'printing'!
  544. printOn: aStream
  545. super printOn: aStream.
  546. aStream
  547. nextPutAll: ' (';
  548. nextPutAll: self name;
  549. nextPutAll: ')'
  550. ! !
  551. !Package methodsFor: 'private'!
  552. basicImports
  553. "Answer the imports literal JavaScript object as setup in the JavaScript file, if any"
  554. <return self.imports || []>
  555. !
  556. basicName: aString
  557. <self.pkgName = aString>
  558. !
  559. basicTransport
  560. "Answer the transport literal JavaScript object as setup in the JavaScript file, if any"
  561. <return self.transport>
  562. !
  563. sortedImportsAsArray
  564. "Answer imports sorted first by type (associations first),
  565. then by value"
  566. ^ self imports asArray
  567. sorted: [ :a :b |
  568. a isString not & b isString or: [
  569. a isString = b isString and: [
  570. a value <= b value ]]]
  571. ! !
  572. !Package methodsFor: 'testing'!
  573. isDirty
  574. ^ dirty ifNil: [ false ]
  575. !
  576. isPackage
  577. ^ true
  578. ! !
  579. !Package methodsFor: 'validation'!
  580. validateImports: aCollection
  581. aCollection do: [ :import |
  582. import isString ifFalse: [
  583. (import respondsTo: #key) ifFalse: [
  584. self error: 'Imports must be Strings or Associations' ].
  585. import key isString & import value isString ifFalse: [
  586. self error: 'Key and value must be Strings' ].
  587. (import key match: '^[a-zA-Z][a-zA-Z0-9]*$') ifFalse: [
  588. self error: 'Keys must be identifiers' ]]]
  589. ! !
  590. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  591. !Package class methodsFor: 'accessing'!
  592. named: aPackageName
  593. ^ Smalltalk
  594. packageAt: aPackageName
  595. ifAbsent: [
  596. Smalltalk createPackage: aPackageName ]
  597. !
  598. named: aPackageName ifAbsent: aBlock
  599. ^ Smalltalk packageAt: aPackageName ifAbsent: aBlock
  600. !
  601. named: aPackageName imports: anArray transport: aTransport
  602. | package |
  603. package := self named: aPackageName.
  604. package imports: anArray.
  605. package transport: aTransport.
  606. ^ package
  607. !
  608. named: aPackageName transport: aTransport
  609. | package |
  610. package := self named: aPackageName.
  611. package transport: aTransport.
  612. ^ package
  613. ! !
  614. !Package class methodsFor: 'sorting'!
  615. sortedClasses: classes
  616. "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
  617. | children others nodes expandedClasses |
  618. children := #().
  619. others := #().
  620. classes do: [ :each |
  621. (classes includes: each superclass)
  622. ifFalse: [ children add: each ]
  623. ifTrue: [ others add: each ]].
  624. nodes := children collect: [ :each |
  625. ClassSorterNode on: each classes: others level: 0 ].
  626. nodes := nodes sorted: [ :a :b | a theClass name <= b theClass name ].
  627. expandedClasses := Array new.
  628. nodes do: [ :aNode |
  629. aNode traverseClassesWith: expandedClasses ].
  630. ^ expandedClasses
  631. ! !
  632. Object subclass: #PackageStateObserver
  633. instanceVariableNames: ''
  634. package: 'Kernel-Infrastructure'!
  635. !PackageStateObserver commentStamp!
  636. My current instance listens for any changes in the system that might affect the state of a package (being dirty).!
  637. !PackageStateObserver methodsFor: 'accessing'!
  638. announcer
  639. ^ SystemAnnouncer current
  640. ! !
  641. !PackageStateObserver methodsFor: 'actions'!
  642. observeSystem
  643. self announcer
  644. on: PackageAdded
  645. send: #onPackageAdded:
  646. to: self;
  647. on: ClassAnnouncement
  648. send: #onClassModification:
  649. to: self;
  650. on: MethodAnnouncement
  651. send: #onMethodModification:
  652. to: self;
  653. on: ProtocolAnnouncement
  654. send: #onProtocolModification:
  655. to: self
  656. ! !
  657. !PackageStateObserver methodsFor: 'reactions'!
  658. onClassModification: anAnnouncement
  659. anAnnouncement theClass ifNotNil: [ :theClass | theClass package beDirty ]
  660. !
  661. onMethodModification: anAnnouncement
  662. anAnnouncement method package ifNotNil: [ :package | package beDirty ]
  663. !
  664. onPackageAdded: anAnnouncement
  665. anAnnouncement package beDirty
  666. !
  667. onProtocolModification: anAnnouncement
  668. anAnnouncement package ifNotNil: [ :package | package beDirty ]
  669. ! !
  670. PackageStateObserver class instanceVariableNames: 'current'!
  671. !PackageStateObserver class methodsFor: 'accessing'!
  672. current
  673. ^ current ifNil: [ current := self new ]
  674. ! !
  675. !PackageStateObserver class methodsFor: 'initialization'!
  676. initialize
  677. self current observeSystem
  678. ! !
  679. Object subclass: #PlatformInterface
  680. instanceVariableNames: ''
  681. package: 'Kernel-Infrastructure'!
  682. !PlatformInterface commentStamp!
  683. I am single entry point to UI and environment interface.
  684. My `initialize` tries several options (for now, browser environment only) to set myself up.
  685. ## API
  686. PlatformInterface alert: 'Hey, there is a problem'.
  687. PlatformInterface confirm: 'Affirmative?'.
  688. PlatformInterface prompt: 'Your name:'.
  689. PlatformInterface ajax: #{
  690. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  691. }.!
  692. PlatformInterface class instanceVariableNames: 'worker'!
  693. !PlatformInterface class methodsFor: 'accessing'!
  694. globals
  695. <return (new Function('return this'))();>
  696. !
  697. setWorker: anObject
  698. worker := anObject
  699. ! !
  700. !PlatformInterface class methodsFor: 'actions'!
  701. ajax: anObject
  702. ^ worker
  703. ifNotNil: [ worker ajax: anObject ]
  704. ifNil: [ self error: 'ajax: not available' ]
  705. !
  706. alert: aString
  707. ^ worker
  708. ifNotNil: [ worker alert: aString ]
  709. ifNil: [ self error: 'alert: not available' ]
  710. !
  711. confirm: aString
  712. ^ worker
  713. ifNotNil: [ worker confirm: aString ]
  714. ifNil: [ self error: 'confirm: not available' ]
  715. !
  716. existsGlobal: aString
  717. ^ PlatformInterface globals
  718. at: aString
  719. ifPresent: [ true ]
  720. ifAbsent: [ false ]
  721. !
  722. prompt: aString
  723. ^ worker
  724. ifNotNil: [ worker prompt: aString ]
  725. ifNil: [ self error: 'prompt: not available' ]
  726. !
  727. prompt: aString default: defaultString
  728. ^ worker
  729. ifNotNil: [ worker prompt: aString default: defaultString ]
  730. ifNil: [ self error: 'prompt: not available' ]
  731. ! !
  732. !PlatformInterface class methodsFor: 'initialization'!
  733. initialize
  734. | candidate |
  735. super initialize.
  736. BrowserInterface ifNotNil: [
  737. candidate := BrowserInterface new.
  738. candidate isAvailable ifTrue: [ self setWorker: candidate. ^ self ]
  739. ]
  740. ! !
  741. Object subclass: #Service
  742. instanceVariableNames: ''
  743. package: 'Kernel-Infrastructure'!
  744. !Service commentStamp!
  745. I implement the basic behavior for class registration to a service.
  746. See the `Transcript` class for a concrete service.
  747. ## API
  748. Use class-side methods `#register:` and `#registerIfNone:` to register classes to a specific service.!
  749. Service class instanceVariableNames: 'current'!
  750. !Service class methodsFor: 'accessing'!
  751. current
  752. ^ current
  753. ! !
  754. !Service class methodsFor: 'instance creation'!
  755. new
  756. self shouldNotImplement
  757. ! !
  758. !Service class methodsFor: 'registration'!
  759. register: anObject
  760. current := anObject
  761. !
  762. registerIfNone: anObject
  763. self current ifNil: [ self register: anObject ]
  764. ! !
  765. Service subclass: #ErrorHandler
  766. instanceVariableNames: ''
  767. package: 'Kernel-Infrastructure'!
  768. !ErrorHandler commentStamp!
  769. I am the service used to handle Smalltalk errors.
  770. See `boot.js` `handleError()` function.
  771. Registered service instances must implement `#handleError:` to perform an action on the thrown exception.!
  772. !ErrorHandler class methodsFor: 'error handling'!
  773. handleError: anError
  774. self handleUnhandledError: anError
  775. !
  776. handleUnhandledError: anError
  777. anError wasHandled ifTrue: [ ^ self ].
  778. ^ self current handleError: anError
  779. ! !
  780. Service subclass: #Finder
  781. instanceVariableNames: ''
  782. package: 'Kernel-Infrastructure'!
  783. !Finder commentStamp!
  784. I am the service responsible for finding classes/methods.
  785. __There is no default finder.__
  786. ## API
  787. Use `#browse` on an object to find it.!
  788. !Finder class methodsFor: 'finding'!
  789. findClass: aClass
  790. ^ self current findClass: aClass
  791. !
  792. findMethod: aCompiledMethod
  793. ^ self current findMethod: aCompiledMethod
  794. !
  795. findString: aString
  796. ^ self current findString: aString
  797. ! !
  798. Service subclass: #Inspector
  799. instanceVariableNames: ''
  800. package: 'Kernel-Infrastructure'!
  801. !Inspector commentStamp!
  802. I am the service responsible for inspecting objects.
  803. The default inspector object is the transcript.!
  804. !Inspector class methodsFor: 'inspecting'!
  805. inspect: anObject
  806. ^ self current inspect: anObject
  807. ! !
  808. Service subclass: #ProgressHandler
  809. instanceVariableNames: ''
  810. package: 'Kernel-Infrastructure'!
  811. !ProgressHandler commentStamp!
  812. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  813. Registered instances must implement `#do:on:displaying:`.
  814. The default behavior is to simply iterate over the collection, using `NullProgressHandler`.!
  815. !ProgressHandler class methodsFor: 'progress handling'!
  816. do: aBlock on: aCollection displaying: aString
  817. self current do: aBlock on: aCollection displaying: aString
  818. ! !
  819. Service subclass: #Transcript
  820. instanceVariableNames: ''
  821. package: 'Kernel-Infrastructure'!
  822. !Transcript commentStamp!
  823. I am a facade for Transcript actions.
  824. I delegate actions to the currently registered transcript.
  825. ## API
  826. Transcript
  827. show: 'hello world';
  828. cr;
  829. show: anObject.!
  830. !Transcript class methodsFor: 'instance creation'!
  831. open
  832. self current open
  833. ! !
  834. !Transcript class methodsFor: 'printing'!
  835. clear
  836. self current clear
  837. !
  838. cr
  839. self current show: String cr
  840. !
  841. inspect: anObject
  842. self show: anObject
  843. !
  844. show: anObject
  845. self current show: anObject
  846. ! !
  847. Object subclass: #Setting
  848. instanceVariableNames: 'key value defaultValue'
  849. package: 'Kernel-Infrastructure'!
  850. !Setting commentStamp!
  851. I represent a setting **stored** at `Smalltalk settings`.
  852. In the current implementation, `Smalltalk settings` is an object persisted in the localStorage.
  853. ## API
  854. A `Setting` value can be read using `value` and set using `value:`.
  855. Settings are accessed with `'key' asSetting` or `'key' asSettingIfAbsent: aDefaultValue`.
  856. To read the value of a setting you can also use the convenience:
  857. `theValueSet := 'any.characteristic' settingValue`
  858. or with a default using:
  859. `theEnsuredValueSet := 'any.characteristic' settingValueIfAbsent: true`!
  860. !Setting methodsFor: 'accessing'!
  861. defaultValue
  862. ^ defaultValue
  863. !
  864. defaultValue: aStringifiableObject
  865. defaultValue := aStringifiableObject
  866. !
  867. key
  868. ^ key
  869. !
  870. key: aString
  871. key := aString
  872. !
  873. value
  874. ^ Smalltalk settings at: self key ifAbsent: [ self defaultValue ]
  875. !
  876. value: aStringifiableObject
  877. ^ Smalltalk settings at: self key put: aStringifiableObject
  878. ! !
  879. !Setting class methodsFor: 'instance creation'!
  880. at: aString ifAbsent: aDefaultValue
  881. ^ super new
  882. key: aString;
  883. defaultValue: aDefaultValue;
  884. yourself
  885. !
  886. new
  887. self shouldNotImplement
  888. ! !
  889. Object subclass: #SmalltalkImage
  890. instanceVariableNames: ''
  891. package: 'Kernel-Infrastructure'!
  892. !SmalltalkImage commentStamp!
  893. I represent the Smalltalk system, wrapping
  894. operations of variable `$core` declared in `support/boot.js`.
  895. ## API
  896. I have only one instance, accessed with global variable `Smalltalk`.
  897. ## Classes
  898. Classes can be accessed using the following methods:
  899. - `#classes` answers the full list of Smalltalk classes in the system
  900. - `#globals #at:` answers a specific global (usually, a class) or `nil`
  901. ## Packages
  902. Packages can be accessed using the following methods:
  903. - `#packages` answers the full list of packages
  904. - `#packageAt:` answers a specific package or `nil`
  905. ## Parsing
  906. The `#parse:` method is used to parse Amber source code.
  907. It requires the `Compiler` package and the `support/parser.js` parser file in order to work.!
  908. !SmalltalkImage methodsFor: 'accessing'!
  909. cancelOptOut: anObject
  910. "A Smalltalk object has a 'klass' property.
  911. If this property is shadowed for anObject by optOut:,
  912. the object is treated as plain JS object.
  913. This removes the shadow and anObject is Smalltalk object
  914. again if it was before."
  915. <delete anObject.klass>
  916. !
  917. core
  918. <return $core>
  919. !
  920. globals
  921. <return $globals>
  922. !
  923. includesKey: aKey
  924. <return $core.hasOwnProperty(aKey)>
  925. !
  926. optOut: anObject
  927. "A Smalltalk object has a 'klass' property.
  928. This shadows the property for anObject.
  929. The object is treated as plain JS object following this."
  930. <anObject.klass = null>
  931. !
  932. parse: aString
  933. | result |
  934. [ result := self basicParse: aString ]
  935. tryCatch: [ :ex | (self parseError: ex parsing: aString) signal ].
  936. ^ result
  937. source: aString;
  938. yourself
  939. !
  940. pseudoVariableNames
  941. ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
  942. !
  943. readJSObject: anObject
  944. <return $core.readJSObject(anObject)>
  945. !
  946. reservedWords
  947. "JavaScript reserved words"
  948. <return $core.reservedWords>
  949. !
  950. settings
  951. ^ SmalltalkSettings
  952. !
  953. version
  954. "Answer the version string of Amber"
  955. ^ '0.15.0-pre'
  956. ! !
  957. !SmalltalkImage methodsFor: 'accessing amd'!
  958. amdRequire
  959. ^ self core at: 'amdRequire'
  960. !
  961. defaultAmdNamespace
  962. ^ 'transport.defaultAmdNamespace' settingValue
  963. !
  964. defaultAmdNamespace: aString
  965. 'transport.defaultAmdNamespace' settingValue: aString
  966. ! !
  967. !SmalltalkImage methodsFor: 'classes'!
  968. classes
  969. <return $core.classes()>
  970. !
  971. removeClass: aClass
  972. aClass isMetaclass ifTrue: [ self error: aClass asString, ' is a Metaclass and cannot be removed!!' ].
  973. self deleteClass: aClass.
  974. SystemAnnouncer current
  975. announce: (ClassRemoved new
  976. theClass: aClass;
  977. yourself)
  978. ! !
  979. !SmalltalkImage methodsFor: 'error handling'!
  980. asSmalltalkException: anObject
  981. "A JavaScript exception may be thrown.
  982. We then need to convert it back to a Smalltalk object"
  983. ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
  984. ifTrue: [ anObject ]
  985. ifFalse: [ JavaScriptException on: anObject ]
  986. !
  987. parseError: anException parsing: aString
  988. ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
  989. ! !
  990. !SmalltalkImage methodsFor: 'globals'!
  991. addGlobalJsVariable: aString
  992. self globalJsVariables add: aString
  993. !
  994. deleteGlobalJsVariable: aString
  995. self globalJsVariables remove: aString ifAbsent:[]
  996. !
  997. globalJsVariables
  998. "Array of global JavaScript variables"
  999. <return $core.globalJsVariables>
  1000. ! !
  1001. !SmalltalkImage methodsFor: 'packages'!
  1002. createPackage: packageName
  1003. | package announcement |
  1004. package := self basicCreatePackage: packageName.
  1005. announcement := PackageAdded new
  1006. package: package;
  1007. yourself.
  1008. SystemAnnouncer current announce: announcement.
  1009. ^ package
  1010. !
  1011. packageAt: packageName
  1012. <return $core.packages[packageName]>
  1013. !
  1014. packageAt: packageName ifAbsent: aBlock
  1015. ^ (self packageAt: packageName) ifNil: aBlock
  1016. !
  1017. packages
  1018. "Return all Package instances in the system."
  1019. <
  1020. return Object.keys($core.packages).map(function(k) {
  1021. return $core.packages[k];
  1022. })
  1023. >
  1024. !
  1025. removePackage: packageName
  1026. "Removes a package and all its classes."
  1027. | pkg |
  1028. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  1029. pkg classes do: [ :each |
  1030. self removeClass: each ].
  1031. self deletePackage: packageName
  1032. !
  1033. renamePackage: packageName to: newName
  1034. "Rename a package."
  1035. | pkg |
  1036. pkg := self packageAt: packageName ifAbsent: [ self error: 'Missing package: ', packageName ].
  1037. (self packageAt: newName) ifNotNil: [ self error: 'Already exists a package called: ', newName ].
  1038. pkg name: newName.
  1039. self basicRegisterPackage: pkg.
  1040. self deletePackage: packageName.
  1041. ! !
  1042. !SmalltalkImage methodsFor: 'private'!
  1043. basicCreatePackage: packageName
  1044. "Create and bind a new bare package with given name and return it."
  1045. <return $core.addPackage(packageName)>
  1046. !
  1047. basicParse: aString
  1048. ^ SmalltalkParser parse: aString
  1049. !
  1050. basicRegisterPackage: aPackage
  1051. "Put aPackage in $core.packages object."
  1052. <$core.packages[aPackage.pkgName]=aPackage>
  1053. !
  1054. deleteClass: aClass
  1055. "Deletes a class by deleting its binding only. Use #removeClass instead"
  1056. <$core.removeClass(aClass)>
  1057. !
  1058. deletePackage: packageName
  1059. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  1060. To remove a package, use #removePackage instead."
  1061. <delete $core.packages[packageName]>
  1062. ! !
  1063. !SmalltalkImage methodsFor: 'testing'!
  1064. isSmalltalkObject: anObject
  1065. "Consider anObject a Smalltalk object if it has a 'klass' property.
  1066. Note that this may be unaccurate"
  1067. <return typeof anObject.klass !!== 'undefined'>
  1068. ! !
  1069. SmalltalkImage class instanceVariableNames: 'current'!
  1070. !SmalltalkImage class methodsFor: 'initialization'!
  1071. initialize
  1072. | st |
  1073. st := self current.
  1074. st globals at: 'Smalltalk' put: st
  1075. ! !
  1076. !SmalltalkImage class methodsFor: 'instance creation'!
  1077. current
  1078. ^ current ifNil: [ current := super new ] ifNotNil: [ self deprecatedAPI. current ]
  1079. !
  1080. new
  1081. self shouldNotImplement
  1082. ! !
  1083. !Association methodsFor: '*Kernel-Infrastructure'!
  1084. importsString
  1085. "This is for use by package exporter.
  1086. It can fail for non-string keys and values."
  1087. ^ self key importsString, ' -> ', self value importsString
  1088. ! !
  1089. !SequenceableCollection methodsFor: '*Kernel-Infrastructure'!
  1090. do: aBlock displayingProgress: aString
  1091. ProgressHandler
  1092. do: aBlock
  1093. on: self
  1094. displaying: aString
  1095. ! !
  1096. !String methodsFor: '*Kernel-Infrastructure'!
  1097. asJavaScriptPropertyName
  1098. <return $core.st2prop(self)>
  1099. !
  1100. asSetting
  1101. "Answer aSetting dedicated to locally store a value using this string as key.
  1102. Nil will be the default value."
  1103. ^ Setting at: self ifAbsent: nil
  1104. !
  1105. asSettingIfAbsent: aDefaultValue
  1106. "Answer aSetting dedicated to locally store a value using this string as key.
  1107. Make this setting to have aDefaultValue."
  1108. ^ Setting at: self ifAbsent: aDefaultValue
  1109. !
  1110. importsString
  1111. "Answer receiver as Smalltalk expression"
  1112. ^ '''', (self replace: '''' with: ''''''), ''''
  1113. !
  1114. settingValue
  1115. ^ self asSetting value
  1116. !
  1117. settingValue: aValue
  1118. "Sets the value of the setting that will be locally stored using this string as key.
  1119. Note that aValue can be any object that can be stringifyed"
  1120. ^ self asSetting value: aValue
  1121. !
  1122. settingValueIfAbsent: aDefaultValue
  1123. "Answer the value of the locally stored setting using this string as key.
  1124. Use aDefaultValue in case no setting is found"
  1125. ^ (self asSettingIfAbsent: aDefaultValue) value
  1126. ! !