Platform-ImportExport.st 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. Smalltalk createPackage: 'Platform-ImportExport'!
  2. Object subclass: #AbstractExporter
  3. instanceVariableNames: ''
  4. package: 'Platform-ImportExport'!
  5. !AbstractExporter commentStamp!
  6. I am an abstract exporter for Amber source code.
  7. ## API
  8. Use `#exportPackage:on:` to export a given package on a Stream.!
  9. !AbstractExporter methodsFor: 'accessing'!
  10. extensionMethodsOfPackage: aPackage
  11. | result |
  12. result := OrderedCollection new.
  13. (self extensionProtocolsOfPackage: aPackage) do: [ :each |
  14. result addAll: each ownMethods ].
  15. ^ result
  16. !
  17. extensionProtocolsOfPackage: aPackage
  18. | extensionName result |
  19. extensionName := '*', aPackage name.
  20. result := OrderedCollection new.
  21. "The classes must be loaded since it is extensions only.
  22. Therefore topological sorting (dependency resolution) does not matter here.
  23. Not sorting topologically improves the speed by a number of magnitude.
  24. Not to shuffle diffs, classes are sorted by their name."
  25. (Smalltalk classes asArray sorted: [ :a :b | a name < b name ]) do: [ :each |
  26. ({each. each theMetaClass} copyWithout: nil) do: [ :behavior |
  27. (behavior protocols includes: extensionName) ifTrue: [
  28. result add: (ExportMethodProtocol name: extensionName theClass: behavior) ] ] ].
  29. ^ result
  30. ! !
  31. !AbstractExporter methodsFor: 'output'!
  32. exportPackage: aPackage on: aStream
  33. self subclassResponsibility
  34. ! !
  35. AbstractExporter subclass: #ChunkExporter
  36. instanceVariableNames: ''
  37. package: 'Platform-ImportExport'!
  38. !ChunkExporter commentStamp!
  39. I am an exporter dedicated to outputting Amber source code in the classic Smalltalk chunk format.
  40. I do not output any compiled code.!
  41. !ChunkExporter methodsFor: 'accessing'!
  42. extensionCategoriesOfPackage: aPackage
  43. "Issue #143: sort protocol alphabetically"
  44. | name map result |
  45. name := aPackage name.
  46. result := OrderedCollection new.
  47. (Package sortedClasses: Smalltalk classes) do: [ :each |
  48. {each. each theMetaClass} do: [ :aClass |
  49. map := Dictionary new.
  50. aClass protocolsDo: [ :category :methods |
  51. category = ('*', name) ifTrue: [ map at: category put: methods ] ].
  52. result addAll: ((map keys sorted: [ :a :b | a <= b ]) collect: [ :category |
  53. MethodCategory name: category theClass: aClass methods: (map at: category) ]) ] ].
  54. ^ result
  55. !
  56. ownCategoriesOfClass: aClass
  57. "Answer the protocols of aClass that are not package extensions"
  58. "Issue #143: sort protocol alphabetically"
  59. | map |
  60. map := Dictionary new.
  61. aClass protocolsDo: [ :each :methods |
  62. (each match: '^\*') ifFalse: [ map at: each put: methods ] ].
  63. ^ (map keys sorted: [ :a :b | a <= b ]) collect: [ :each |
  64. MethodCategory name: each theClass: aClass methods: (map at: each) ]
  65. !
  66. ownCategoriesOfMetaClass: aClass
  67. "Issue #143: sort protocol alphabetically"
  68. ^ self ownCategoriesOfClass: aClass theMetaClass
  69. !
  70. ownMethodProtocolsOfClass: aClass
  71. "Answer a collection of ExportMethodProtocol object of aClass that are not package extensions"
  72. ^ aClass ownProtocols collect: [ :each |
  73. ExportMethodProtocol name: each theClass: aClass ]
  74. ! !
  75. !ChunkExporter methodsFor: 'convenience'!
  76. chunkEscape: aString
  77. "Replace all occurrences of !! with !!!! and trim at both ends."
  78. ^ (aString replace: '!!' with: '!!!!') trimBoth
  79. ! !
  80. !ChunkExporter methodsFor: 'output'!
  81. exportBehavior: aBehavior on: aStream
  82. aBehavior exportBehaviorDefinitionTo: aStream using: self.
  83. self
  84. exportProtocols: (self ownMethodProtocolsOfClass: aBehavior)
  85. on: aStream
  86. !
  87. exportCategoryEpilogueOf: aCategory on: aStream
  88. aStream write: ' !!'; lf; lf
  89. !
  90. exportCategoryPrologueOf: aCategory on: aStream
  91. aStream
  92. write: '!!';
  93. print: aCategory theClass;
  94. write: ' methodsFor: ';
  95. print: aCategory;
  96. write: '!!'
  97. !
  98. exportDefinitionOf: aClass on: aStream
  99. "Chunk format."
  100. aStream
  101. print: aClass superclass;
  102. write: ' subclass: ';
  103. printSymbol: aClass name;
  104. lf.
  105. "aClass traitComposition
  106. ifNotEmpty: [ aStream tab; write: {'uses: '. aClass traitCompositionDefinition}; lf ]."
  107. aStream
  108. tab;
  109. write: 'instanceVariableNames: ';
  110. print: (' ' join: aClass instanceVariableNames);
  111. lf;
  112. tab;
  113. write: 'package: ';
  114. print: aClass category;
  115. write: '!!';
  116. lf.
  117. aClass comment ifNotEmpty: [ aStream
  118. write: '!!'; print: aClass; write: ' commentStamp!!'; lf;
  119. write: { self chunkEscape: aClass comment. '!!' }; lf ].
  120. aStream lf
  121. !
  122. exportMetaDefinitionOf: aClass on: aStream
  123. | classIvars classTraitComposition |
  124. classIvars := aClass class instanceVariableNames.
  125. classTraitComposition := aClass class traitComposition.
  126. (classIvars notEmpty "or: [classTraitComposition notEmpty]") ifTrue: [
  127. aStream
  128. print: aClass theMetaClass.
  129. aStream space. "classTraitComposition
  130. ifEmpty: [ aStream space ]
  131. ifNotEmpty: [ aStream lf; tab; write: {'uses: '. aClass class traitCompositionDefinition}; lf; tab ]."
  132. aStream
  133. write: 'instanceVariableNames: ';
  134. print: (' ' join: classIvars);
  135. write: '!!'; lf; lf ]
  136. !
  137. exportMethod: aMethod on: aStream
  138. aStream
  139. lf; lf; write: (self chunkEscape: aMethod source); lf;
  140. write: '!!'
  141. !
  142. exportPackage: aPackage on: aStream
  143. self
  144. exportPackageDefinitionOf: aPackage on: aStream;
  145. exportPackageImportsOf: aPackage on: aStream.
  146. aPackage sortedClasses do: [ :each |
  147. self exportBehavior: each on: aStream.
  148. each theMetaClass ifNotNil: [ :meta | self exportBehavior: meta on: aStream ] ].
  149. self exportPackageTraitCompositionsOf: aPackage on: aStream.
  150. self
  151. exportProtocols: (self extensionProtocolsOfPackage: aPackage)
  152. on: aStream
  153. !
  154. exportPackageDefinitionOf: aPackage on: aStream
  155. aStream
  156. write: 'Smalltalk createPackage: ';
  157. print: aPackage name;
  158. write: '!!';
  159. lf
  160. !
  161. exportPackageImportsOf: aPackage on: aStream
  162. aPackage imports ifNotEmpty: [ :imports | aStream
  163. write: '(Smalltalk packageAt: ';
  164. print: aPackage name;
  165. write: ' ifAbsent: [ self error: ';
  166. print: 'Package not created: ', aPackage name;
  167. write: { ' ]) imports: '. self chunkEscape: aPackage importsDefinition. '!!' };
  168. lf ]
  169. !
  170. exportPackageTraitCompositionsOf: aPackage on: aStream
  171. aPackage traitCompositions ifNotEmpty: [ :traitCompositions |
  172. traitCompositions keysAndValuesDo: [ :key :value | self exportTraitComposition: value of: key on: aStream ].
  173. aStream write: '!! !!'; lf; lf ]
  174. !
  175. exportProtocol: aProtocol on: aStream
  176. aProtocol ownMethods ifNotEmpty: [ :methods |
  177. self exportProtocolPrologueOf: aProtocol on: aStream.
  178. methods do: [ :method |
  179. self exportMethod: method on: aStream ].
  180. self exportProtocolEpilogueOf: aProtocol on: aStream ]
  181. !
  182. exportProtocolEpilogueOf: aProtocol on: aStream
  183. aStream write: ' !!'; lf; lf
  184. !
  185. exportProtocolPrologueOf: aProtocol on: aStream
  186. aStream
  187. write: '!!';
  188. print: aProtocol theClass;
  189. write: ' methodsFor: ';
  190. print: aProtocol name;
  191. write: '!!'
  192. !
  193. exportProtocols: aCollection on: aStream
  194. aCollection do: [ :each |
  195. self exportProtocol: each on: aStream ]
  196. !
  197. exportTraitComposition: aTraitComposition of: aBehavior on: aStream
  198. aStream
  199. print: aBehavior;
  200. write: ' setTraitComposition: ';
  201. write: aBehavior traitCompositionDefinition;
  202. write: ' asTraitComposition!!';
  203. lf
  204. !
  205. exportTraitDefinitionOf: aClass on: aStream
  206. "Chunk format."
  207. aStream
  208. write: 'Trait named: '; printSymbol: aClass name; lf.
  209. "aClass traitComposition
  210. ifNotEmpty: [ aStream tab; write: {'uses: '. aClass traitCompositionDefinition}; lf ]."
  211. aStream
  212. tab; write: 'package: '; print: aClass category; write: '!!'; lf.
  213. aClass comment ifNotEmpty: [
  214. aStream
  215. write: '!!'; print: aClass; write: ' commentStamp!!'; lf;
  216. write: { self chunkEscape: aClass comment. '!!' }; lf ].
  217. aStream lf
  218. ! !
  219. AbstractExporter subclass: #Exporter
  220. instanceVariableNames: ''
  221. package: 'Platform-ImportExport'!
  222. !Exporter commentStamp!
  223. I am responsible for outputting Amber code into a JavaScript string.
  224. The generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.
  225. ## Use case
  226. I am typically used to save code outside of the Amber runtime (committing to disk, etc.).!
  227. !Exporter methodsFor: 'accessing'!
  228. ownMethodsOfClass: aClass
  229. "Issue #143: sort methods alphabetically"
  230. ^ ((aClass methodDictionary values) sorted: [ :a :b | a selector <= b selector ])
  231. reject: [ :each | (each protocol match: '^\*') ]
  232. !
  233. ownMethodsOfMetaClass: aClass
  234. "Issue #143: sort methods alphabetically"
  235. ^ self ownMethodsOfClass: aClass theMetaClass
  236. ! !
  237. !Exporter methodsFor: 'output'!
  238. exportBehavior: aBehavior on: aStream
  239. aBehavior exportBehaviorDefinitionTo: aStream using: self.
  240. aBehavior ownMethods do: [ :method |
  241. self exportMethod: method on: aStream ]
  242. !
  243. exportDefinitionOf: aClass on: aStream
  244. aStream
  245. lf;
  246. write: {
  247. '$core.addClass('.
  248. aClass name asJavaScriptSource. ', '.
  249. aClass superclass ifNil: [ 'null' ] ifNotNil: [ :superclass | superclass asJavaScriptSource ]. ', '.
  250. aClass instanceVariableNames asJavaScriptSource. ', '.
  251. aClass category asJavaScriptSource.
  252. ');' }.
  253. aClass comment ifNotEmpty: [
  254. aStream
  255. lf;
  256. write: '//>>excludeStart("ide", pragmas.excludeIdeData);'; lf;
  257. write: { aClass asJavaScriptSource. '.comment='. aClass comment crlfSanitized asJavaScriptSource. ';' }; lf;
  258. write: '//>>excludeEnd("ide");' ].
  259. aStream lf
  260. !
  261. exportMetaDefinitionOf: aClass on: aStream
  262. aStream lf.
  263. aClass theMetaClass instanceVariableNames ifNotEmpty: [ :classIvars | aStream
  264. write: { aClass theMetaClass asJavaScriptSource. '.iVarNames = '. classIvars asJavaScriptSource. ';' };
  265. lf ]
  266. !
  267. exportMethod: aMethod on: aStream
  268. aStream
  269. write: '$core.addMethod('; lf;
  270. write: '$core.method({'; lf;
  271. write: { 'selector: '. aMethod selector asJavaScriptSource. ',' }; lf;
  272. write: { 'protocol: '. aMethod protocol asJavaScriptSource. ',' }; lf;
  273. write: { 'fn: '. aMethod fn compiledSource. ',' }; lf;
  274. write: '//>>excludeStart("ide", pragmas.excludeIdeData);'; lf;
  275. write: { 'args: '. aMethod arguments asJavaScriptSource. ',' }; lf;
  276. write: { 'source: '. aMethod source asJavaScriptSource. ',' }; lf;
  277. write: { 'referencedClasses: '. aMethod referencedClasses asJavaScriptSource. ',' }; lf;
  278. write: '//>>excludeEnd("ide");'; lf;
  279. write: { 'messageSends: '. aMethod messageSends asJavaScriptSource }; lf;
  280. write: '}),'; lf;
  281. write: { aMethod methodClass asJavaScriptSource. ');' }; lf; lf
  282. !
  283. exportPackage: aPackage on: aStream
  284. self
  285. exportPackagePrologueOf: aPackage on: aStream;
  286. exportPackageDefinitionOf: aPackage on: aStream;
  287. exportPackageContextOf: aPackage on: aStream;
  288. exportPackageImportsOf: aPackage on: aStream;
  289. exportPackageTransportOf: aPackage on: aStream.
  290. aPackage sortedClasses do: [ :each |
  291. self exportBehavior: each on: aStream.
  292. each theMetaClass ifNotNil: [ :meta | self exportBehavior: meta on: aStream ] ].
  293. self exportPackageTraitCompositionsOf: aPackage on: aStream.
  294. (self extensionMethodsOfPackage: aPackage) do: [ :each |
  295. self exportMethod: each on: aStream ].
  296. self exportPackageEpilogueOf: aPackage on: aStream
  297. !
  298. exportPackageBodyBlockPrologueOf: aPackage on: aStream
  299. aStream
  300. write: 'if(!!("nilAsValue" in $boot))$boot.nilAsValue=$boot.nilAsReceiver;'; lf;
  301. write: 'var $core=$boot.api,nil=$boot.nilAsValue,$nil=$boot.nilAsReceiver,$recv=$boot.asReceiver,$globals=$boot.globals;'; lf
  302. !
  303. exportPackageContextOf: aPackage on: aStream
  304. aStream
  305. write: '$pkg.innerEval = function (expr) { return eval(expr); };';
  306. lf
  307. !
  308. exportPackageDefinitionOf: aPackage on: aStream
  309. aStream
  310. write: { 'var $pkg = $core.addPackage('. aPackage name asJavaScriptSource. ');' };
  311. lf
  312. !
  313. exportPackageEpilogueOf: aPackage on: aStream
  314. self subclassResponsibility
  315. !
  316. exportPackageImportsOf: aPackage on: aStream
  317. aPackage importsAsJson ifNotEmpty: [ :imports |
  318. aStream
  319. write: { '$pkg.imports = '. imports asJavaScriptSource. ';' };
  320. lf ]
  321. !
  322. exportPackagePrologueOf: aPackage on: aStream
  323. self subclassResponsibility
  324. !
  325. exportPackageTraitCompositionsOf: aPackage on: aStream
  326. aPackage traitCompositions ifNotEmpty: [ :traitCompositions |
  327. traitCompositions keysAndValuesDo: [ :key :value | self exportTraitComposition: value of: key on: aStream ].
  328. aStream lf ]
  329. !
  330. exportPackageTransportOf: aPackage on: aStream
  331. aStream
  332. write: { '$pkg.transport = '. aPackage transport asJSONString. ';' };
  333. lf
  334. !
  335. exportTraitComposition: aTraitComposition of: aBehavior on: aStream
  336. aStream write: {
  337. '$core.setTraitComposition('.
  338. aTraitComposition asJavaScriptSource.
  339. ', '.
  340. aBehavior asJavaScriptSource.
  341. ');' };
  342. lf
  343. !
  344. exportTraitDefinitionOf: aClass on: aStream
  345. aStream
  346. lf;
  347. write: {
  348. '$core.addTrait('.
  349. aClass name asJavaScriptSource. ', '.
  350. aClass category asJavaScriptSource.
  351. ');' }.
  352. aClass comment ifNotEmpty: [
  353. aStream
  354. lf;
  355. write: '//>>excludeStart("ide", pragmas.excludeIdeData);'; lf;
  356. write: { aClass asJavaScriptSource. '.comment='. aClass comment crlfSanitized asJavaScriptSource. ';' }; lf;
  357. write: '//>>excludeEnd("ide");' ].
  358. aStream lf
  359. ! !
  360. Exporter subclass: #AmdExporter
  361. instanceVariableNames: 'namespace'
  362. package: 'Platform-ImportExport'!
  363. !AmdExporter commentStamp!
  364. I am used to export Packages in an AMD (Asynchronous Module Definition) JavaScript format.!
  365. !AmdExporter methodsFor: 'output'!
  366. exportPackageEpilogueOf: aPackage on: aStream
  367. aStream
  368. write: '});';
  369. lf
  370. !
  371. exportPackagePrologueOf: aPackage on: aStream
  372. | importsForOutput loadDependencies pragmaStart pragmaEnd |
  373. pragmaStart := ''.
  374. pragmaEnd := ''.
  375. importsForOutput := self importsForOutput: aPackage.
  376. loadDependencies := self amdNamesOfPackages: aPackage loadDependencies.
  377. importsForOutput value ifNotEmpty: [
  378. pragmaStart := String lf, '//>>excludeStart("imports", pragmas.excludeImports);', String lf.
  379. pragmaEnd := String lf, '//>>excludeEnd("imports");', String lf ].
  380. aStream
  381. write: {
  382. 'define('.
  383. ((#('amber/boot' ':1:'), importsForOutput value, #(':2:'), loadDependencies asArray sorted) asJavaScriptSource
  384. replace: ',\s*["'']:1:["'']' with: pragmaStart)
  385. replace: ',\s*["'']:2:["'']' with: pragmaEnd.
  386. ', function('.
  387. ((((#('$boot' ':1:'), importsForOutput key, #(':2:')) join: ',')
  388. replace: ',\s*:1:' with: pragmaStart)
  389. replace: ',\s*:2:' with: pragmaEnd).
  390. '){"use strict";' };
  391. lf.
  392. self exportPackageBodyBlockPrologueOf: aPackage on: aStream
  393. ! !
  394. !AmdExporter methodsFor: 'private'!
  395. amdNamesOfPackages: anArray
  396. ^ (anArray
  397. select: [ :each | (self amdNamespaceOfPackage: each) notNil ])
  398. collect: [ :each | (self amdNamespaceOfPackage: each), '/', each name ]
  399. !
  400. amdNamespaceOfPackage: aPackage
  401. ^ (aPackage transport type = 'amd')
  402. ifTrue: [ aPackage transport namespace ]
  403. ifFalse: [ nil ]
  404. !
  405. importsForOutput: aPackage
  406. "Returns an association where key is list of import variables
  407. and value is list of external dependencies, with ones imported as variables
  408. put at the beginning with same order as is in key.
  409. For example imports:{'jQuery'->'jquery'. 'bootstrap'} would yield
  410. #('jQuery') -> #('jquery' 'bootstrap')"
  411. | namedImports anonImports importVarNames |
  412. namedImports := #().
  413. anonImports := #().
  414. importVarNames := #().
  415. aPackage imports do: [ :each | each isString
  416. ifTrue: [ anonImports add: each ]
  417. ifFalse: [ namedImports add: each value.
  418. importVarNames add: each key ]].
  419. ^ importVarNames -> (namedImports, anonImports)
  420. ! !
  421. Object subclass: #ChunkParser
  422. instanceVariableNames: 'stream last'
  423. package: 'Platform-ImportExport'!
  424. !ChunkParser commentStamp!
  425. I am responsible for parsing aStream contents in the chunk format.
  426. ## API
  427. ChunkParser new
  428. stream: aStream;
  429. nextChunk!
  430. !ChunkParser methodsFor: 'accessing'!
  431. last
  432. ^ last
  433. !
  434. stream: aStream
  435. stream := aStream
  436. ! !
  437. !ChunkParser methodsFor: 'reading'!
  438. nextChunk
  439. "The chunk format (Smalltalk Interchange Format or Fileout format)
  440. is a trivial format but can be a bit tricky to understand:
  441. - Uses the exclamation mark as delimiter of chunks.
  442. - Inside a chunk a normal exclamation mark must be doubled.
  443. - A non empty chunk must be a valid Smalltalk expression.
  444. - A chunk on top level with a preceding empty chunk is an instruction chunk:
  445. - The object created by the expression then takes over reading chunks.
  446. This method returns next chunk as a String (trimmed), empty String (all whitespace) or nil."
  447. | char result chunk |
  448. result := '' writeStream.
  449. [ char := stream next.
  450. char notNil ] whileTrue: [
  451. char = '!!' ifTrue: [
  452. stream peek = '!!'
  453. ifTrue: [ stream next "skipping the escape double" ]
  454. ifFalse: [ ^ last := result contents trimBoth "chunk end marker found" ]].
  455. result nextPut: char ].
  456. ^ last := nil "a chunk needs to end with !!"
  457. ! !
  458. !ChunkParser class methodsFor: 'instance creation'!
  459. on: aStream
  460. ^ self new stream: aStream
  461. ! !
  462. Object subclass: #ClassCommentReader
  463. instanceVariableNames: 'class'
  464. package: 'Platform-ImportExport'!
  465. !ClassCommentReader commentStamp!
  466. I provide a mechanism for retrieving class comments stored on a file.
  467. See also `ClassCategoryReader`.!
  468. !ClassCommentReader methodsFor: 'accessing'!
  469. class: aClass
  470. class := aClass
  471. ! !
  472. !ClassCommentReader methodsFor: 'fileIn'!
  473. scanFrom: aChunkParser
  474. | chunk |
  475. chunk := aChunkParser nextChunk.
  476. chunk ifNotEmpty: [
  477. self setComment: chunk ].
  478. ! !
  479. !ClassCommentReader methodsFor: 'initialization'!
  480. initialize
  481. super initialize.
  482. ! !
  483. !ClassCommentReader methodsFor: 'private'!
  484. setComment: aString
  485. class comment: aString
  486. ! !
  487. Object subclass: #ClassProtocolReader
  488. instanceVariableNames: 'class category'
  489. package: 'Platform-ImportExport'!
  490. !ClassProtocolReader commentStamp!
  491. I provide a mechanism for retrieving class descriptions stored on a file in the Smalltalk chunk format.!
  492. !ClassProtocolReader methodsFor: 'accessing'!
  493. class: aClass category: aString
  494. class := aClass.
  495. category := aString
  496. ! !
  497. !ClassProtocolReader methodsFor: 'fileIn'!
  498. scanFrom: aChunkParser
  499. | chunk |
  500. [ chunk := aChunkParser nextChunk.
  501. chunk isEmpty ] whileFalse: [
  502. self compileMethod: chunk ]
  503. ! !
  504. !ClassProtocolReader methodsFor: 'initialization'!
  505. initialize
  506. super initialize.
  507. ! !
  508. !ClassProtocolReader methodsFor: 'private'!
  509. compileMethod: aString
  510. Compiler new install: aString forClass: class protocol: category
  511. ! !
  512. Object subclass: #ExportMethodProtocol
  513. instanceVariableNames: 'name theClass'
  514. package: 'Platform-ImportExport'!
  515. !ExportMethodProtocol commentStamp!
  516. I am an abstraction for a method protocol in a class / metaclass.
  517. I know of my class, name and methods.
  518. I am used when exporting a package.!
  519. !ExportMethodProtocol methodsFor: 'accessing'!
  520. methods
  521. ^ (self theClass methodsInProtocol: self name)
  522. sorted: [ :a :b | a selector <= b selector ]
  523. !
  524. name
  525. ^ name
  526. !
  527. name: aString
  528. name := aString
  529. !
  530. ownMethods
  531. ^ (self theClass ownMethodsInProtocol: self name)
  532. sorted: [ :a :b | a selector <= b selector ]
  533. !
  534. theClass
  535. ^ theClass
  536. !
  537. theClass: aClass
  538. theClass := aClass
  539. ! !
  540. !ExportMethodProtocol class methodsFor: 'instance creation'!
  541. name: aString theClass: aClass
  542. ^ self new
  543. name: aString;
  544. theClass: aClass;
  545. yourself
  546. ! !
  547. Object subclass: #Importer
  548. instanceVariableNames: 'lastSection lastChunk'
  549. package: 'Platform-ImportExport'!
  550. !Importer commentStamp!
  551. I can import Amber code from a string in the chunk format.
  552. ## API
  553. Importer new import: aString!
  554. !Importer methodsFor: 'accessing'!
  555. lastChunk
  556. ^ lastChunk
  557. !
  558. lastSection
  559. ^ lastSection
  560. ! !
  561. !Importer methodsFor: 'fileIn'!
  562. import: aStream
  563. | chunk result parser lastEmpty |
  564. parser := ChunkParser on: aStream.
  565. lastEmpty := false.
  566. lastSection := 'n/a, not started'.
  567. lastChunk := nil.
  568. [
  569. [ chunk := parser nextChunk.
  570. chunk isNil ] whileFalse: [
  571. chunk
  572. ifEmpty: [ lastEmpty := true ]
  573. ifNotEmpty: [
  574. lastSection := chunk.
  575. result := Compiler new evaluateExpression: chunk.
  576. lastEmpty
  577. ifTrue: [
  578. lastEmpty := false.
  579. result scanFrom: parser ]] ].
  580. lastSection := 'n/a, finished'
  581. ] on: Error do: [:e | lastChunk := parser last. e pass ].
  582. ! !
  583. Error subclass: #PackageCommitError
  584. instanceVariableNames: ''
  585. package: 'Platform-ImportExport'!
  586. !PackageCommitError commentStamp!
  587. I get signaled when an attempt to commit a package has failed.!
  588. Object subclass: #PackageHandler
  589. instanceVariableNames: ''
  590. package: 'Platform-ImportExport'!
  591. !PackageHandler commentStamp!
  592. I am responsible for handling package loading and committing.
  593. I should not be used directly. Instead, use the corresponding `Package` methods.!
  594. !PackageHandler methodsFor: 'accessing'!
  595. chunkContentsFor: aPackage
  596. ^ String streamContents: [ :str |
  597. self chunkExporter exportPackage: aPackage on: str ]
  598. !
  599. chunkExporterClass
  600. ^ ChunkExporter
  601. !
  602. commitPathJsFor: aPackage
  603. self subclassResponsibility
  604. !
  605. commitPathStFor: aPackage
  606. self subclassResponsibility
  607. !
  608. contentsFor: aPackage
  609. ^ String streamContents: [ :str |
  610. self exporter exportPackage: aPackage on: str ]
  611. !
  612. exporterClass
  613. self subclassResponsibility
  614. ! !
  615. !PackageHandler methodsFor: 'committing'!
  616. commit: aPackage
  617. self
  618. commit: aPackage
  619. onSuccess: []
  620. onError: [ :error |
  621. PackageCommitError new
  622. messageText: 'Commiting failed with reason: "' , (error responseText) , '"';
  623. signal ]
  624. !
  625. commit: aPackage onSuccess: aBlock onError: anotherBlock
  626. self
  627. commitJsFileFor: aPackage
  628. onSuccess: [
  629. self
  630. commitStFileFor: aPackage
  631. onSuccess: [ aPackage beClean. aBlock value ]
  632. onError: anotherBlock ]
  633. onError: anotherBlock
  634. !
  635. commitJsFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  636. self
  637. ajaxPutAt: (self commitPathJsFor: aPackage), '/', aPackage name, '.js'
  638. data: (self contentsFor: aPackage)
  639. onSuccess: aBlock
  640. onError: anotherBlock
  641. !
  642. commitStFileFor: aPackage onSuccess: aBlock onError: anotherBlock
  643. self
  644. ajaxPutAt: (self commitPathStFor: aPackage), '/', aPackage name, '.st'
  645. data: (self chunkContentsFor: aPackage)
  646. onSuccess: aBlock
  647. onError: anotherBlock
  648. ! !
  649. !PackageHandler methodsFor: 'error handling'!
  650. onCommitError: anError
  651. PackageCommitError new
  652. messageText: 'Commiting failed with reason: "' , (anError responseText) , '"';
  653. signal
  654. ! !
  655. !PackageHandler methodsFor: 'factory'!
  656. chunkExporter
  657. ^ self chunkExporterClass new
  658. !
  659. exporter
  660. ^ self exporterClass new
  661. ! !
  662. !PackageHandler methodsFor: 'loading'!
  663. load: aPackage
  664. "Should return a TThenable"
  665. self subclassResponsibility
  666. ! !
  667. !PackageHandler methodsFor: 'private'!
  668. ajaxPutAt: aURL data: aString onSuccess: aBlock onError: anotherBlock
  669. | xhr |
  670. xhr := Platform newXhr.
  671. xhr open: 'PUT' url: aURL async: true.
  672. xhr onreadystatechange: [
  673. xhr readyState = 4 ifTrue: [
  674. (xhr status >= 200 and: [ xhr status < 300 ])
  675. ifTrue: aBlock
  676. ifFalse: anotherBlock ]].
  677. xhr send: aString
  678. ! !
  679. PackageHandler subclass: #AmdPackageHandler
  680. instanceVariableNames: ''
  681. package: 'Platform-ImportExport'!
  682. !AmdPackageHandler commentStamp!
  683. I am responsible for handling package loading and committing.
  684. I should not be used directly. Instead, use the corresponding `Package` methods.!
  685. !AmdPackageHandler methodsFor: 'accessing'!
  686. commitPathJsFor: aPackage
  687. ^ self toUrl: (self namespaceFor: aPackage)
  688. !
  689. commitPathStFor: aPackage
  690. "If _source is not mapped, .st will be committed to .js path.
  691. It is recommended not to use _source as it can be deprecated."
  692. | path pathWithout |
  693. path := self toUrl: (self namespaceFor: aPackage), '/_source'.
  694. pathWithout := self commitPathJsFor: aPackage.
  695. ^ path = (pathWithout, '/_source') ifTrue: [ pathWithout ] ifFalse: [ path ]
  696. !
  697. exporterClass
  698. ^ AmdExporter
  699. ! !
  700. !AmdPackageHandler methodsFor: 'committing'!
  701. namespaceFor: aPackage
  702. ^ aPackage transport namespace
  703. ! !
  704. !AmdPackageHandler methodsFor: 'loading'!
  705. load: aPackage
  706. ^ Promise new: [ :model |
  707. Smalltalk amdRequire
  708. ifNil: [ self error: 'AMD loader not present' ]
  709. ifNotNil: [ :require |
  710. require
  711. value: { (self namespaceFor: aPackage), '/', aPackage name }
  712. value: [ :result | model value: result ]
  713. value: [ :error | model signal: error ] ] ]
  714. ! !
  715. !AmdPackageHandler methodsFor: 'private'!
  716. toUrl: aString
  717. ^ Smalltalk amdRequire
  718. ifNil: [ self error: 'AMD loader not present' ]
  719. ifNotNil: [ :require | require provided toUrl: aString ]
  720. ! !
  721. !AmdPackageHandler class methodsFor: 'commit paths'!
  722. defaultNamespace
  723. ^ Smalltalk defaultAmdNamespace
  724. !
  725. defaultNamespace: aString
  726. Smalltalk defaultAmdNamespace: aString
  727. ! !
  728. Object subclass: #PackageTransport
  729. instanceVariableNames: 'package'
  730. package: 'Platform-ImportExport'!
  731. !PackageTransport commentStamp!
  732. I represent the transport mechanism used to commit a package.
  733. My concrete subclasses have a `#handler` to which committing is delegated.!
  734. !PackageTransport methodsFor: 'accessing'!
  735. commitHandlerClass
  736. self subclassResponsibility
  737. !
  738. definition
  739. ^ ''
  740. !
  741. package
  742. ^ package
  743. !
  744. package: aPackage
  745. package := aPackage
  746. !
  747. type
  748. ^ self class type
  749. ! !
  750. !PackageTransport methodsFor: 'committing'!
  751. commit
  752. self commitHandler commit: self package
  753. !
  754. commitOnSuccess: aBlock onError: anotherBlock
  755. self commitHandler
  756. commit: self package
  757. onSuccess: aBlock
  758. onError: anotherBlock
  759. ! !
  760. !PackageTransport methodsFor: 'converting'!
  761. asJavaScriptObject
  762. ^ #{ 'type' -> self type }
  763. ! !
  764. !PackageTransport methodsFor: 'factory'!
  765. commitHandler
  766. ^ self commitHandlerClass new
  767. ! !
  768. !PackageTransport methodsFor: 'initialization'!
  769. setupFromJson: anObject
  770. "no op. override if needed in subclasses"
  771. ! !
  772. !PackageTransport methodsFor: 'loading'!
  773. load
  774. ^ (self commitHandler load: self package)
  775. then: [ Smalltalk postLoad ]
  776. ! !
  777. PackageTransport class instanceVariableNames: 'registry'!
  778. !PackageTransport class methodsFor: 'accessing'!
  779. classRegisteredFor: aString
  780. ^ registry at: aString
  781. !
  782. defaultType
  783. ^ AmdPackageTransport type
  784. !
  785. type
  786. "Override in subclasses"
  787. ^ nil
  788. ! !
  789. !PackageTransport class methodsFor: 'initialization'!
  790. initialize
  791. super initialize.
  792. self == PackageTransport
  793. ifTrue: [ registry := #{} ]
  794. ifFalse: [ self register ]
  795. ! !
  796. !PackageTransport class methodsFor: 'instance creation'!
  797. for: aString
  798. ^ (self classRegisteredFor: aString) new
  799. !
  800. fromJson: anObject
  801. anObject ifNil: [ ^ self for: self defaultType ].
  802. ^ (self for: anObject type)
  803. setupFromJson: anObject;
  804. yourself
  805. ! !
  806. !PackageTransport class methodsFor: 'registration'!
  807. register
  808. PackageTransport register: self
  809. !
  810. register: aClass
  811. aClass type ifNotNil: [
  812. registry at: aClass type put: aClass ]
  813. ! !
  814. PackageTransport subclass: #AmdPackageTransport
  815. instanceVariableNames: 'namespace'
  816. package: 'Platform-ImportExport'!
  817. !AmdPackageTransport commentStamp!
  818. I am the default transport for committing packages.
  819. See `AmdExporter` and `AmdPackageHandler`.!
  820. !AmdPackageTransport methodsFor: 'accessing'!
  821. commitHandlerClass
  822. ^ AmdPackageHandler
  823. !
  824. definition
  825. ^ String streamContents: [ :stream | stream
  826. write: { self class name. ' namespace: ' }; print: self namespace ]
  827. !
  828. namespace
  829. ^ namespace ifNil: [ self defaultNamespace ]
  830. !
  831. namespace: aString
  832. namespace := aString
  833. ! !
  834. !AmdPackageTransport methodsFor: 'actions'!
  835. setPath: aString
  836. "Set the path the the receiver's `namespace`"
  837. require provided config: #{
  838. 'paths' -> #{
  839. self namespace -> aString
  840. }
  841. }.
  842. ! !
  843. !AmdPackageTransport methodsFor: 'converting'!
  844. asJavaScriptObject
  845. ^ super asJavaScriptObject
  846. at: 'amdNamespace' put: self namespace;
  847. yourself
  848. ! !
  849. !AmdPackageTransport methodsFor: 'defaults'!
  850. defaultNamespace
  851. ^ Smalltalk defaultAmdNamespace
  852. ! !
  853. !AmdPackageTransport methodsFor: 'initialization'!
  854. setupFromJson: anObject
  855. self namespace: (anObject at: 'amdNamespace')
  856. ! !
  857. !AmdPackageTransport methodsFor: 'printing'!
  858. printOn: aStream
  859. super printOn: aStream.
  860. aStream
  861. nextPutAll: ' (AMD Namespace: ';
  862. nextPutAll: self namespace;
  863. nextPutAll: ')'
  864. ! !
  865. !AmdPackageTransport class methodsFor: 'accessing'!
  866. type
  867. ^ 'amd'
  868. ! !
  869. !AmdPackageTransport class methodsFor: 'instance creation'!
  870. namespace: aString
  871. ^ self new
  872. namespace: aString;
  873. yourself
  874. ! !
  875. !Class methodsFor: '*Platform-ImportExport'!
  876. exportBehaviorDefinitionTo: aStream using: anExporter
  877. anExporter exportDefinitionOf: self on: aStream
  878. ! !
  879. !Metaclass methodsFor: '*Platform-ImportExport'!
  880. exportBehaviorDefinitionTo: aStream using: anExporter
  881. anExporter exportMetaDefinitionOf: self instanceClass on: aStream
  882. ! !
  883. !Package methodsFor: '*Platform-ImportExport'!
  884. commit
  885. ^ self transport commit
  886. !
  887. load
  888. ^ self transport load
  889. !
  890. loadFromNamespace: aString
  891. ^ self transport
  892. namespace: aString;
  893. load
  894. ! !
  895. !Package class methodsFor: '*Platform-ImportExport'!
  896. load: aPackageName
  897. ^ (self named: aPackageName) load
  898. !
  899. load: aPackageName fromNamespace: aString
  900. ^ (self named: aPackageName) loadFromNamespace: aString
  901. ! !
  902. !TBehaviorProvider methodsFor: '*Platform-ImportExport'!
  903. methodsFor: aString
  904. ^ ClassProtocolReader new
  905. class: self category: aString;
  906. yourself
  907. !
  908. methodsFor: aString stamp: aStamp
  909. "Added for file-in compatibility, ignores stamp."
  910. ^ self methodsFor: aString
  911. ! !
  912. !TMasterBehavior methodsFor: '*Platform-ImportExport'!
  913. commentStamp
  914. ^ ClassCommentReader new
  915. class: self;
  916. yourself
  917. !
  918. commentStamp: aStamp prior: prior
  919. ^ self commentStamp
  920. ! !
  921. !Trait methodsFor: '*Platform-ImportExport'!
  922. exportBehaviorDefinitionTo: aStream using: anExporter
  923. anExporter exportTraitDefinitionOf: self on: aStream
  924. ! !