Platform-Services.st 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. Smalltalk createPackage: 'Platform-Services'!
  2. Object subclass: #ConsoleErrorHandler
  3. instanceVariableNames: ''
  4. package: 'Platform-Services'!
  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: #ConsoleTranscript
  35. instanceVariableNames: 'textarea'
  36. package: 'Platform-Services'!
  37. !ConsoleTranscript commentStamp!
  38. I am a specific transcript emitting to the JavaScript console.
  39. If no other transcript is registered, I am the default.!
  40. !ConsoleTranscript methodsFor: 'actions'!
  41. open
  42. ! !
  43. !ConsoleTranscript methodsFor: 'printing'!
  44. clear
  45. "no op"
  46. !
  47. cr
  48. "no op"
  49. !
  50. show: anObject
  51. "Smalltalk objects should have no trouble displaying themselves on the Transcript; Javascript objects don't know how, so must be wrapped in a JSObectProxy."
  52. <inlineJS: 'console.log(String($recv(anObject)._asString()))'>
  53. ! !
  54. !ConsoleTranscript class methodsFor: 'initialization'!
  55. initialize
  56. Transcript registerIfNone: self new
  57. ! !
  58. Object subclass: #Environment
  59. instanceVariableNames: ''
  60. package: 'Platform-Services'!
  61. !Environment commentStamp!
  62. I provide an unified entry point to manipulate Amber packages, classes and methods.
  63. Typical use cases include IDEs, remote access and restricting browsing.!
  64. !Environment methodsFor: 'accessing'!
  65. allSelectors
  66. ^ Smalltalk core allSelectors
  67. !
  68. availableClassNames
  69. ^ Smalltalk classes
  70. collect: [ :each | each name ]
  71. !
  72. availablePackageNames
  73. ^ Smalltalk packages
  74. collect: [ :each | each name ]
  75. !
  76. availableProtocolsFor: aClass
  77. | protocols |
  78. protocols := aClass protocols.
  79. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  80. ^ protocols asSet asArray sort
  81. !
  82. classBuilder
  83. ^ ClassBuilder new
  84. !
  85. classNamed: aString
  86. ^ (Smalltalk globals at: aString asSymbol)
  87. ifNil: [ self error: 'Invalid class name' ]
  88. !
  89. classes
  90. ^ Smalltalk classes
  91. !
  92. doItReceiver
  93. ^ DoIt new
  94. !
  95. packages
  96. ^ Smalltalk packages
  97. !
  98. systemAnnouncer
  99. ^ (Smalltalk globals at: #SystemAnnouncer) current
  100. ! !
  101. !Environment methodsFor: 'actions'!
  102. commitPackage: aPackage onSuccess: aBlock onError: anotherBlock
  103. aPackage transport
  104. commitOnSuccess: aBlock
  105. onError: anotherBlock
  106. !
  107. copyClass: aClass to: aClassName
  108. (Smalltalk globals at: aClassName)
  109. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  110. ClassBuilder new copyClass: aClass named: aClassName
  111. !
  112. inspect: anObject
  113. Inspector inspect: anObject
  114. !
  115. moveClass: aClass toPackage: aPackageName
  116. | package |
  117. package := Package named: aPackageName.
  118. package ifNil: [ self error: 'Invalid package name' ].
  119. package == aClass package ifTrue: [ ^ self ].
  120. aClass package: package
  121. !
  122. moveMethod: aMethod toClass: aClassName
  123. | destinationClass |
  124. destinationClass := self classNamed: aClassName.
  125. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  126. aMethod methodClass isMetaclass ifTrue: [
  127. destinationClass := destinationClass theMetaClass ].
  128. destinationClass
  129. compile: aMethod source
  130. protocol: aMethod protocol.
  131. aMethod methodClass
  132. removeCompiledMethod: aMethod
  133. !
  134. moveMethod: aMethod toProtocol: aProtocol
  135. aMethod protocol: aProtocol.
  136. aMethod methodClass
  137. compile: aMethod source
  138. protocol: aMethod protocol
  139. !
  140. removeClass: aClass
  141. Smalltalk removeClass: aClass
  142. !
  143. removeMethod: aMethod
  144. aMethod methodClass removeCompiledMethod: aMethod
  145. !
  146. removeProtocol: aString from: aClass
  147. (aClass methodsInProtocol: aString)
  148. do: [ :each | aClass removeCompiledMethod: each ]
  149. !
  150. renameClass: aClass to: aClassName
  151. (Smalltalk globals at: aClassName)
  152. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  153. ClassBuilder new renameClass: aClass to: aClassName
  154. !
  155. renamePackage: aPackageName to: aNewPackageName
  156. (Smalltalk globals at: aNewPackageName)
  157. ifNotNil: [ self error: 'A package named ', aNewPackageName, ' already exists' ].
  158. Smalltalk renamePackage: aPackageName to: aNewPackageName
  159. !
  160. renameProtocol: aString to: anotherString in: aClass
  161. (aClass methodsInProtocol: aString)
  162. do: [ :each | each protocol: anotherString ]
  163. !
  164. setClassCommentOf: aClass to: aString
  165. aClass comment: aString
  166. ! !
  167. !Environment methodsFor: 'compiling'!
  168. addInstVarNamed: aString to: aClass
  169. self classBuilder
  170. addSubclassOf: aClass superclass
  171. named: aClass name
  172. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  173. package: aClass package name
  174. !
  175. compileClassComment: aString for: aClass
  176. aClass comment: aString
  177. !
  178. compileClassDefinition: aString
  179. [ self evaluate: aString for: DoIt new ]
  180. on: Error
  181. do: [ :error | Terminal alert: error messageText ]
  182. !
  183. compileMethod: sourceCode for: class protocol: protocol
  184. ^ class
  185. compile: sourceCode
  186. protocol: protocol
  187. ! !
  188. !Environment methodsFor: 'error handling'!
  189. evaluate: aBlock on: anErrorClass do: exceptionBlock
  190. "Evaluate a block and catch exceptions happening on the environment stack"
  191. aBlock tryCatch: [ :exception |
  192. (exception isKindOf: (self classNamed: anErrorClass name))
  193. ifTrue: [ exceptionBlock value: exception ]
  194. ifFalse: [ exception resignal ] ]
  195. ! !
  196. !Environment methodsFor: 'evaluating'!
  197. evaluate: aString for: anObject
  198. ^ Evaluator evaluate: aString for: anObject
  199. ! !
  200. !Environment methodsFor: 'services'!
  201. registerErrorHandler: anErrorHandler
  202. ErrorHandler register: anErrorHandler
  203. !
  204. registerFinder: aFinder
  205. Finder register: aFinder
  206. !
  207. registerInspector: anInspector
  208. Inspector register: anInspector
  209. !
  210. registerProgressHandler: aProgressHandler
  211. ProgressHandler register: aProgressHandler
  212. !
  213. registerTranscript: aTranscript
  214. Transcript register: aTranscript
  215. ! !
  216. Object subclass: #NullProgressHandler
  217. instanceVariableNames: ''
  218. package: 'Platform-Services'!
  219. !NullProgressHandler commentStamp!
  220. I am the default progress handler. I do not display any progress, and simply iterate over the collection.!
  221. !NullProgressHandler methodsFor: 'progress handling'!
  222. do: aBlock on: aCollection displaying: aString
  223. aCollection do: aBlock
  224. ! !
  225. NullProgressHandler class instanceVariableNames: 'current'!
  226. !NullProgressHandler class methodsFor: 'initialization'!
  227. initialize
  228. ProgressHandler registerIfNone: self new
  229. ! !
  230. Object subclass: #Service
  231. instanceVariableNames: ''
  232. package: 'Platform-Services'!
  233. !Service commentStamp!
  234. I implement the basic behavior for class registration to a service.
  235. See the `Transcript` class for a concrete service.
  236. ## API
  237. Use class-side methods `#register:` and `#registerIfNone:` to register classes to a specific service.!
  238. Service class instanceVariableNames: 'current'!
  239. !Service class methodsFor: 'accessing'!
  240. current
  241. ^ current
  242. ! !
  243. !Service class methodsFor: 'instance creation'!
  244. new
  245. self shouldNotImplement
  246. ! !
  247. !Service class methodsFor: 'registration'!
  248. register: anObject
  249. current := anObject
  250. !
  251. registerIfNone: anObject
  252. self current ifNil: [ self register: anObject ]
  253. ! !
  254. Service subclass: #ErrorHandler
  255. instanceVariableNames: ''
  256. package: 'Platform-Services'!
  257. !ErrorHandler commentStamp!
  258. I am the service used to handle Smalltalk errors.
  259. See `boot.js` `handleError()` function.
  260. Registered service instances must implement `#handleError:` to perform an action on the thrown exception.!
  261. !ErrorHandler class methodsFor: 'error handling'!
  262. handleError: anError
  263. self handleUnhandledError: anError
  264. !
  265. handleUnhandledError: anError
  266. anError wasHandled ifTrue: [ ^ self ].
  267. ^ self current handleError: anError
  268. ! !
  269. Service subclass: #Finder
  270. instanceVariableNames: ''
  271. package: 'Platform-Services'!
  272. !Finder commentStamp!
  273. I am the service responsible for finding classes/methods.
  274. __There is no default finder.__
  275. ## API
  276. Use `#browse` on an object to find it.!
  277. !Finder class methodsFor: 'finding'!
  278. findClass: aClass
  279. ^ self current findClass: aClass
  280. !
  281. findMethod: aCompiledMethod
  282. ^ self current findMethod: aCompiledMethod
  283. !
  284. findString: aString
  285. ^ self current findString: aString
  286. ! !
  287. Service subclass: #Inspector
  288. instanceVariableNames: ''
  289. package: 'Platform-Services'!
  290. !Inspector commentStamp!
  291. I am the service responsible for inspecting objects.
  292. The default inspector object is the transcript.!
  293. !Inspector class methodsFor: 'inspecting'!
  294. inspect: anObject
  295. ^ self current inspect: anObject
  296. ! !
  297. Service subclass: #Platform
  298. instanceVariableNames: ''
  299. package: 'Platform-Services'!
  300. !Platform commentStamp!
  301. I am bridge to JS environment.
  302. ## API
  303. Platform globals. "JS global object"
  304. Platform newXHR "new XMLHttpRequest() or its shim"!
  305. !Platform class methodsFor: 'accessing'!
  306. globals
  307. ^ self current globals
  308. !
  309. newXhr
  310. ^ self current newXhr
  311. ! !
  312. Service subclass: #ProgressHandler
  313. instanceVariableNames: ''
  314. package: 'Platform-Services'!
  315. !ProgressHandler commentStamp!
  316. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  317. Registered instances must implement `#do:on:displaying:`.
  318. The default behavior is to simply iterate over the collection, using `NullProgressHandler`.!
  319. !ProgressHandler class methodsFor: 'progress handling'!
  320. do: aBlock on: aCollection displaying: aString
  321. self current do: aBlock on: aCollection displaying: aString
  322. ! !
  323. Service subclass: #Terminal
  324. instanceVariableNames: ''
  325. package: 'Platform-Services'!
  326. !Terminal commentStamp!
  327. I am UI interface service.
  328. ## API
  329. Terminal alert: 'Hey, there is a problem'.
  330. Terminal confirm: 'Affirmative?'.
  331. Terminal prompt: 'Your name:'.!
  332. !Terminal class methodsFor: 'dialogs'!
  333. alert: aString
  334. ^ self current alert: aString
  335. !
  336. confirm: aString
  337. ^ self current confirm: aString
  338. !
  339. prompt: aString
  340. ^ self current prompt: aString
  341. !
  342. prompt: aString default: defaultString
  343. ^ self current prompt: aString default: defaultString
  344. ! !
  345. Service subclass: #Transcript
  346. instanceVariableNames: ''
  347. package: 'Platform-Services'!
  348. !Transcript commentStamp!
  349. I am a facade for Transcript actions.
  350. I delegate actions to the currently registered transcript.
  351. ## API
  352. Transcript
  353. show: 'hello world';
  354. cr;
  355. show: anObject.!
  356. !Transcript class methodsFor: 'instance creation'!
  357. open
  358. self current open
  359. ! !
  360. !Transcript class methodsFor: 'printing'!
  361. clear
  362. self current clear
  363. !
  364. cr
  365. self current show: String cr
  366. !
  367. inspect: anObject
  368. self show: anObject
  369. !
  370. show: anObject
  371. self current show: anObject
  372. ! !
  373. !AssociativeCollection methodsFor: '*Platform-Services'!
  374. inspectOn: anInspector
  375. | variables |
  376. variables := Dictionary new.
  377. variables at: '#self' put: self.
  378. variables at: '#keys' put: self keys.
  379. self keysAndValuesDo: [ :key :value |
  380. variables at: key put: value ].
  381. anInspector
  382. setLabel: self printString;
  383. setVariables: variables
  384. ! !
  385. !Collection methodsFor: '*Platform-Services'!
  386. inspectOn: anInspector
  387. | variables |
  388. variables := Dictionary new.
  389. variables at: '#self' put: self.
  390. self withIndexDo: [ :each :i |
  391. variables at: i put: each ].
  392. anInspector
  393. setLabel: self printString;
  394. setVariables: variables
  395. ! !
  396. !Date methodsFor: '*Platform-Services'!
  397. inspectOn: anInspector
  398. | variables |
  399. variables := Dictionary new.
  400. variables at: '#self' put: self.
  401. variables at: '#year' put: self year.
  402. variables at: '#month' put: self month.
  403. variables at: '#day' put: self day.
  404. variables at: '#hours' put: self hours.
  405. variables at: '#minutes' put: self minutes.
  406. variables at: '#seconds' put: self seconds.
  407. variables at: '#milliseconds' put: self milliseconds.
  408. anInspector
  409. setLabel: self printString;
  410. setVariables: variables
  411. ! !
  412. !JSObjectProxy methodsFor: '*Platform-Services'!
  413. inspectOn: anInspector
  414. | variables |
  415. variables := Dictionary new.
  416. variables at: '#self' put: self jsObject.
  417. anInspector setLabel: self printString.
  418. JSObjectProxy addObjectVariablesTo: variables ofProxy: self.
  419. anInspector setVariables: variables
  420. ! !
  421. !MethodContext methodsFor: '*Platform-Services'!
  422. inspectOn: anInspector
  423. | variables |
  424. variables := Dictionary new.
  425. variables at: '#self' put: self.
  426. variables at: '#home' put: self home.
  427. variables at: '#receiver' put: self receiver.
  428. variables at: '#selector' put: self selector.
  429. variables at: '#locals' put: self locals.
  430. self class instanceVariableNames do: [ :each |
  431. variables at: each put: (self instVarAt: each) ].
  432. anInspector
  433. setLabel: self printString;
  434. setVariables: variables
  435. ! !
  436. !Object methodsFor: '*Platform-Services'!
  437. inspectOn: anInspector
  438. | variables |
  439. variables := Dictionary new.
  440. variables at: '#self' put: self.
  441. self class allInstanceVariableNames do: [ :each |
  442. variables at: each put: (self instVarAt: each) ].
  443. anInspector
  444. setLabel: self printString;
  445. setVariables: variables
  446. ! !
  447. !SequenceableCollection methodsFor: '*Platform-Services'!
  448. do: aBlock displayingProgress: aString
  449. ProgressHandler
  450. do: aBlock
  451. on: self
  452. displaying: aString
  453. ! !
  454. !Set methodsFor: '*Platform-Services'!
  455. inspectOn: anInspector
  456. | variables i |
  457. variables := Dictionary new.
  458. variables at: '#self' put: self.
  459. i := 1.
  460. self do: [ :each |
  461. variables at: i put: each.
  462. i := i + 1 ].
  463. anInspector
  464. setLabel: self printString;
  465. setVariables: variables
  466. ! !
  467. !String methodsFor: '*Platform-Services'!
  468. inspectOn: anInspector
  469. | label |
  470. super inspectOn: anInspector.
  471. self printString size > 30
  472. ifTrue: [ label := (self printString copyFrom: 1 to: 30), '...''' ]
  473. ifFalse: [ label := self printString ].
  474. anInspector setLabel: label
  475. ! !