Platform-ImportExport.st 26 KB

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