Kernel-Infrastructure.st 31 KB

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