Platform-Services.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. <console.log(String($recv(anObject)._asString()))>
  53. ! !
  54. !ConsoleTranscript class methodsFor: 'initialization'!
  55. initialize
  56. Transcript registerIfNone: self new
  57. ! !
  58. Object subclass: #InterfacingObject
  59. instanceVariableNames: ''
  60. package: 'Platform-Services'!
  61. !InterfacingObject commentStamp!
  62. 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`.
  63. ## API
  64. self alert: 'Hey, there is a problem'.
  65. self confirm: 'Affirmative?'.
  66. self prompt: 'Your name:'.
  67. self ajax: #{
  68. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  69. }.!
  70. !InterfacingObject methodsFor: 'actions'!
  71. ajax: anObject
  72. ^ PlatformInterface ajax: anObject
  73. !
  74. alert: aString
  75. ^ PlatformInterface alert: aString
  76. !
  77. confirm: aString
  78. ^ PlatformInterface confirm: aString
  79. !
  80. prompt: aString
  81. ^ PlatformInterface prompt: aString
  82. !
  83. prompt: aString default: defaultString
  84. ^ PlatformInterface prompt: aString default: defaultString
  85. ! !
  86. InterfacingObject subclass: #Environment
  87. instanceVariableNames: ''
  88. package: 'Platform-Services'!
  89. !Environment commentStamp!
  90. I provide an unified entry point to manipulate Amber packages, classes and methods.
  91. Typical use cases include IDEs, remote access and restricting browsing.!
  92. !Environment methodsFor: 'accessing'!
  93. allSelectors
  94. ^ Smalltalk core allSelectors
  95. !
  96. availableClassNames
  97. ^ Smalltalk classes
  98. collect: [ :each | each name ]
  99. !
  100. availablePackageNames
  101. ^ Smalltalk packages
  102. collect: [ :each | each name ]
  103. !
  104. availableProtocolsFor: aClass
  105. | protocols |
  106. protocols := aClass protocols.
  107. aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
  108. ^ protocols asSet asArray sort
  109. !
  110. classBuilder
  111. ^ ClassBuilder new
  112. !
  113. classNamed: aString
  114. ^ (Smalltalk globals at: aString asSymbol)
  115. ifNil: [ self error: 'Invalid class name' ]
  116. !
  117. classes
  118. ^ Smalltalk classes
  119. !
  120. doItReceiver
  121. ^ DoIt new
  122. !
  123. packages
  124. ^ Smalltalk packages
  125. !
  126. systemAnnouncer
  127. ^ (Smalltalk globals at: #SystemAnnouncer) current
  128. ! !
  129. !Environment methodsFor: 'actions'!
  130. commitPackage: aPackage onSuccess: aBlock onError: anotherBlock
  131. aPackage transport
  132. commitOnSuccess: aBlock
  133. onError: anotherBlock
  134. !
  135. copyClass: aClass to: aClassName
  136. (Smalltalk globals at: aClassName)
  137. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  138. ClassBuilder new copyClass: aClass named: aClassName
  139. !
  140. inspect: anObject
  141. Inspector inspect: anObject
  142. !
  143. moveClass: aClass toPackage: aPackageName
  144. | package |
  145. package := Package named: aPackageName.
  146. package ifNil: [ self error: 'Invalid package name' ].
  147. package == aClass package ifTrue: [ ^ self ].
  148. aClass package: package
  149. !
  150. moveMethod: aMethod toClass: aClassName
  151. | destinationClass |
  152. destinationClass := self classNamed: aClassName.
  153. destinationClass == aMethod methodClass ifTrue: [ ^ self ].
  154. aMethod methodClass isMetaclass ifTrue: [
  155. destinationClass := destinationClass class ].
  156. destinationClass
  157. compile: aMethod source
  158. protocol: aMethod protocol.
  159. aMethod methodClass
  160. removeCompiledMethod: aMethod
  161. !
  162. moveMethod: aMethod toProtocol: aProtocol
  163. aMethod protocol: aProtocol
  164. !
  165. removeClass: aClass
  166. Smalltalk removeClass: aClass
  167. !
  168. removeMethod: aMethod
  169. aMethod methodClass removeCompiledMethod: aMethod
  170. !
  171. removeProtocol: aString from: aClass
  172. (aClass methodsInProtocol: aString)
  173. do: [ :each | aClass removeCompiledMethod: each ]
  174. !
  175. renameClass: aClass to: aClassName
  176. (Smalltalk globals at: aClassName)
  177. ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
  178. ClassBuilder new renameClass: aClass to: aClassName
  179. !
  180. renameProtocol: aString to: anotherString in: aClass
  181. (aClass methodsInProtocol: aString)
  182. do: [ :each | each protocol: anotherString ]
  183. !
  184. setClassCommentOf: aClass to: aString
  185. aClass comment: aString
  186. ! !
  187. !Environment methodsFor: 'compiling'!
  188. addInstVarNamed: aString to: aClass
  189. self classBuilder
  190. addSubclassOf: aClass superclass
  191. named: aClass name
  192. instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
  193. package: aClass package name
  194. !
  195. compileClassComment: aString for: aClass
  196. aClass comment: aString
  197. !
  198. compileClassDefinition: aString
  199. [ self evaluate: aString for: DoIt new ]
  200. on: Error
  201. do: [ :error | self alert: error messageText ]
  202. !
  203. compileMethod: sourceCode for: class protocol: protocol
  204. ^ class
  205. compile: sourceCode
  206. protocol: protocol
  207. ! !
  208. !Environment methodsFor: 'error handling'!
  209. evaluate: aBlock on: anErrorClass do: exceptionBlock
  210. "Evaluate a block and catch exceptions happening on the environment stack"
  211. aBlock tryCatch: [ :exception |
  212. (exception isKindOf: (self classNamed: anErrorClass name))
  213. ifTrue: [ exceptionBlock value: exception ]
  214. ifFalse: [ exception resignal ] ]
  215. ! !
  216. !Environment methodsFor: 'evaluating'!
  217. evaluate: aString for: anObject
  218. ^ Evaluator evaluate: aString for: anObject
  219. ! !
  220. !Environment methodsFor: 'services'!
  221. registerErrorHandler: anErrorHandler
  222. ErrorHandler register: anErrorHandler
  223. !
  224. registerFinder: aFinder
  225. Finder register: aFinder
  226. !
  227. registerInspector: anInspector
  228. Inspector register: anInspector
  229. !
  230. registerProgressHandler: aProgressHandler
  231. ProgressHandler register: aProgressHandler
  232. !
  233. registerTranscript: aTranscript
  234. Transcript register: aTranscript
  235. ! !
  236. Object subclass: #NullProgressHandler
  237. instanceVariableNames: ''
  238. package: 'Platform-Services'!
  239. !NullProgressHandler commentStamp!
  240. I am the default progress handler. I do not display any progress, and simply iterate over the collection.!
  241. !NullProgressHandler methodsFor: 'progress handling'!
  242. do: aBlock on: aCollection displaying: aString
  243. aCollection do: aBlock
  244. ! !
  245. NullProgressHandler class instanceVariableNames: 'current'!
  246. !NullProgressHandler class methodsFor: 'initialization'!
  247. initialize
  248. ProgressHandler registerIfNone: self new
  249. ! !
  250. Object subclass: #PlatformInterface
  251. instanceVariableNames: ''
  252. package: 'Platform-Services'!
  253. !PlatformInterface commentStamp!
  254. I am single entry point to UI and environment interface.
  255. My `initialize` tries several options (for now, browser environment only) to set myself up.
  256. ## API
  257. PlatformInterface alert: 'Hey, there is a problem'.
  258. PlatformInterface confirm: 'Affirmative?'.
  259. PlatformInterface prompt: 'Your name:'.
  260. PlatformInterface ajax: #{
  261. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  262. }.!
  263. PlatformInterface class instanceVariableNames: 'worker'!
  264. !PlatformInterface class methodsFor: 'accessing'!
  265. globals
  266. <return (new Function('return this'))();>
  267. !
  268. setWorker: anObject
  269. worker := anObject
  270. ! !
  271. !PlatformInterface class methodsFor: 'actions'!
  272. ajax: anObject
  273. ^ worker
  274. ifNotNil: [ worker ajax: anObject ]
  275. ifNil: [ self error: 'ajax: not available' ]
  276. !
  277. alert: aString
  278. ^ worker
  279. ifNotNil: [ worker alert: aString ]
  280. ifNil: [ self error: 'alert: not available' ]
  281. !
  282. confirm: aString
  283. ^ worker
  284. ifNotNil: [ worker confirm: aString ]
  285. ifNil: [ self error: 'confirm: not available' ]
  286. !
  287. existsGlobal: aString
  288. ^ PlatformInterface globals
  289. at: aString
  290. ifPresent: [ true ]
  291. ifAbsent: [ false ]
  292. !
  293. prompt: aString
  294. ^ worker
  295. ifNotNil: [ worker prompt: aString ]
  296. ifNil: [ self error: 'prompt: not available' ]
  297. !
  298. prompt: aString default: defaultString
  299. ^ worker
  300. ifNotNil: [ worker prompt: aString default: defaultString ]
  301. ifNil: [ self error: 'prompt: not available' ]
  302. ! !
  303. !PlatformInterface class methodsFor: 'initialization'!
  304. initialize
  305. | candidate |
  306. super initialize.
  307. BrowserInterface ifNotNil: [
  308. candidate := BrowserInterface new.
  309. candidate isAvailable ifTrue: [ self setWorker: candidate. ^ self ]
  310. ]
  311. ! !
  312. Object subclass: #Service
  313. instanceVariableNames: ''
  314. package: 'Platform-Services'!
  315. !Service commentStamp!
  316. I implement the basic behavior for class registration to a service.
  317. See the `Transcript` class for a concrete service.
  318. ## API
  319. Use class-side methods `#register:` and `#registerIfNone:` to register classes to a specific service.!
  320. Service class instanceVariableNames: 'current'!
  321. !Service class methodsFor: 'accessing'!
  322. current
  323. ^ current
  324. ! !
  325. !Service class methodsFor: 'instance creation'!
  326. new
  327. self shouldNotImplement
  328. ! !
  329. !Service class methodsFor: 'registration'!
  330. register: anObject
  331. current := anObject
  332. !
  333. registerIfNone: anObject
  334. self current ifNil: [ self register: anObject ]
  335. ! !
  336. Service subclass: #ErrorHandler
  337. instanceVariableNames: ''
  338. package: 'Platform-Services'!
  339. !ErrorHandler commentStamp!
  340. I am the service used to handle Smalltalk errors.
  341. See `boot.js` `handleError()` function.
  342. Registered service instances must implement `#handleError:` to perform an action on the thrown exception.!
  343. !ErrorHandler class methodsFor: 'error handling'!
  344. handleError: anError
  345. self handleUnhandledError: anError
  346. !
  347. handleUnhandledError: anError
  348. anError wasHandled ifTrue: [ ^ self ].
  349. ^ self current handleError: anError
  350. ! !
  351. Service subclass: #Finder
  352. instanceVariableNames: ''
  353. package: 'Platform-Services'!
  354. !Finder commentStamp!
  355. I am the service responsible for finding classes/methods.
  356. __There is no default finder.__
  357. ## API
  358. Use `#browse` on an object to find it.!
  359. !Finder class methodsFor: 'finding'!
  360. findClass: aClass
  361. ^ self current findClass: aClass
  362. !
  363. findMethod: aCompiledMethod
  364. ^ self current findMethod: aCompiledMethod
  365. !
  366. findString: aString
  367. ^ self current findString: aString
  368. ! !
  369. Service subclass: #Inspector
  370. instanceVariableNames: ''
  371. package: 'Platform-Services'!
  372. !Inspector commentStamp!
  373. I am the service responsible for inspecting objects.
  374. The default inspector object is the transcript.!
  375. !Inspector class methodsFor: 'inspecting'!
  376. inspect: anObject
  377. ^ self current inspect: anObject
  378. ! !
  379. Service subclass: #ProgressHandler
  380. instanceVariableNames: ''
  381. package: 'Platform-Services'!
  382. !ProgressHandler commentStamp!
  383. I am used to manage progress in collection iterations, see `SequenceableCollection >> #do:displayingProgress:`.
  384. Registered instances must implement `#do:on:displaying:`.
  385. The default behavior is to simply iterate over the collection, using `NullProgressHandler`.!
  386. !ProgressHandler class methodsFor: 'progress handling'!
  387. do: aBlock on: aCollection displaying: aString
  388. self current do: aBlock on: aCollection displaying: aString
  389. ! !
  390. Service subclass: #Transcript
  391. instanceVariableNames: ''
  392. package: 'Platform-Services'!
  393. !Transcript commentStamp!
  394. I am a facade for Transcript actions.
  395. I delegate actions to the currently registered transcript.
  396. ## API
  397. Transcript
  398. show: 'hello world';
  399. cr;
  400. show: anObject.!
  401. !Transcript class methodsFor: 'instance creation'!
  402. open
  403. self current open
  404. ! !
  405. !Transcript class methodsFor: 'printing'!
  406. clear
  407. self current clear
  408. !
  409. cr
  410. self current show: String cr
  411. !
  412. inspect: anObject
  413. self show: anObject
  414. !
  415. show: anObject
  416. self current show: anObject
  417. ! !
  418. !SequenceableCollection methodsFor: '*Platform-Services'!
  419. do: aBlock displayingProgress: aString
  420. ProgressHandler
  421. do: aBlock
  422. on: self
  423. displaying: aString
  424. ! !