Importer-Exporter.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. smalltalk.addPackage('Importer-Exporter');
  2. smalltalk.addClass('ChunkParser', smalltalk.Object, ['stream'], 'Importer-Exporter');
  3. smalltalk.ChunkParser.comment="I am responsible for parsing aStream contents in the chunk format.\x0a\x0a## API\x0a\x0a ChunkParser new\x0a stream: aStream;\x0a nextChunk";
  4. smalltalk.addMethod(
  5. smalltalk.method({
  6. selector: "nextChunk",
  7. category: 'reading',
  8. fn: function (){
  9. var self=this;
  10. var char,result,chunk;
  11. return smalltalk.withContext(function($ctx1) {
  12. var $1,$2,$3;
  13. var $early={};
  14. try {
  15. result=""._writeStream();
  16. _st((function(){
  17. return smalltalk.withContext(function($ctx2) {
  18. char=_st(self["@stream"])._next();
  19. char;
  20. return _st(char)._notNil();
  21. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileTrue_((function(){
  22. return smalltalk.withContext(function($ctx2) {
  23. $1=_st(char).__eq("!");
  24. if(smalltalk.assert($1)){
  25. $2=_st(_st(self["@stream"])._peek()).__eq("!");
  26. if(smalltalk.assert($2)){
  27. _st(self["@stream"])._next();
  28. } else {
  29. $3=_st(_st(result)._contents())._trimBoth();
  30. throw $early=[$3];
  31. };
  32. };
  33. return _st(result)._nextPut_(char);
  34. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  35. return nil;
  36. }
  37. catch(e) {if(e===$early)return e[0]; throw e}
  38. }, function($ctx1) {$ctx1.fill(self,"nextChunk",{char:char,result:result,chunk:chunk},smalltalk.ChunkParser)})},
  39. args: [],
  40. source: "nextChunk\x0a\x09\x22The chunk format (Smalltalk Interchange Format or Fileout format)\x0a\x09is a trivial format but can be a bit tricky to understand:\x0a\x09\x09- Uses the exclamation mark as delimiter of chunks.\x0a\x09\x09- Inside a chunk a normal exclamation mark must be doubled.\x0a\x09\x09- A non empty chunk must be a valid Smalltalk expression.\x0a\x09\x09- A chunk on top level with a preceding empty chunk is an instruction chunk:\x0a\x09\x09\x09- The object created by the expression then takes over reading chunks.\x0a\x0a\x09This metod returns next chunk as a String (trimmed), empty String (all whitespace) or nil.\x22\x0a\x0a\x09| char result chunk |\x0a\x09result := '' writeStream.\x0a\x09\x09[char := stream next.\x0a\x09\x09char notNil] whileTrue: [\x0a\x09\x09\x09\x09char = '!' ifTrue: [\x0a\x09\x09\x09\x09\x09\x09stream peek = '!'\x0a\x09\x09\x09\x09\x09\x09\x09\x09ifTrue: [stream next \x22skipping the escape double\x22]\x0a\x09\x09\x09\x09\x09\x09\x09\x09ifFalse: [^result contents trimBoth \x22chunk end marker found\x22]].\x0a\x09\x09\x09\x09result nextPut: char].\x0a\x09^nil \x22a chunk needs to end with !\x22",
  41. messageSends: ["writeStream", "whileTrue:", "ifTrue:", "ifTrue:ifFalse:", "next", "trimBoth", "contents", "=", "peek", "nextPut:", "notNil"],
  42. referencedClasses: []
  43. }),
  44. smalltalk.ChunkParser);
  45. smalltalk.addMethod(
  46. smalltalk.method({
  47. selector: "stream:",
  48. category: 'accessing',
  49. fn: function (aStream){
  50. var self=this;
  51. return smalltalk.withContext(function($ctx1) {
  52. self["@stream"]=aStream;
  53. return self}, function($ctx1) {$ctx1.fill(self,"stream:",{aStream:aStream},smalltalk.ChunkParser)})},
  54. args: ["aStream"],
  55. source: "stream: aStream\x0a\x09stream := aStream",
  56. messageSends: [],
  57. referencedClasses: []
  58. }),
  59. smalltalk.ChunkParser);
  60. smalltalk.addMethod(
  61. smalltalk.method({
  62. selector: "on:",
  63. category: 'not yet classified',
  64. fn: function (aStream){
  65. var self=this;
  66. return smalltalk.withContext(function($ctx1) {
  67. var $1;
  68. $1=_st(self._new())._stream_(aStream);
  69. return $1;
  70. }, function($ctx1) {$ctx1.fill(self,"on:",{aStream:aStream},smalltalk.ChunkParser.klass)})},
  71. args: ["aStream"],
  72. source: "on: aStream\x0a\x09^self new stream: aStream",
  73. messageSends: ["stream:", "new"],
  74. referencedClasses: []
  75. }),
  76. smalltalk.ChunkParser.klass);
  77. smalltalk.addClass('Exporter', smalltalk.Object, [], 'Importer-Exporter');
  78. smalltalk.Exporter.comment="I am responsible for outputting Amber code into a JavaScript string.\x0a\x0aThe generated output is enough to reconstruct the exported data, including Smalltalk source code and other metadata.\x0a\x0a## Use case\x0a\x0aI am typically used to save code outside of the Amber runtime (committing to disk, etc.).\x0a\x0a## API\x0a\x0aUse `#exportAll`, `#exportClass:` or `#exportPackage:` methods.";
  79. smalltalk.addMethod(
  80. smalltalk.method({
  81. selector: "classNameFor:",
  82. category: 'private',
  83. fn: function (aClass){
  84. var self=this;
  85. return smalltalk.withContext(function($ctx1) {
  86. var $2,$3,$1;
  87. $2=_st(aClass)._isMetaclass();
  88. if(smalltalk.assert($2)){
  89. $1=_st(_st(_st(aClass)._instanceClass())._name()).__comma(".klass");
  90. } else {
  91. $3=_st(aClass)._isNil();
  92. if(smalltalk.assert($3)){
  93. $1="nil";
  94. } else {
  95. $1=_st(aClass)._name();
  96. };
  97. };
  98. return $1;
  99. }, function($ctx1) {$ctx1.fill(self,"classNameFor:",{aClass:aClass},smalltalk.Exporter)})},
  100. args: ["aClass"],
  101. source: "classNameFor: aClass\x0a\x09^aClass isMetaclass\x0a\x09\x09ifTrue: [aClass instanceClass name, '.klass']\x0a\x09\x09ifFalse: [\x0a\x09\x09aClass isNil\x0a\x09\x09\x09ifTrue: ['nil']\x0a\x09\x09\x09ifFalse: [aClass name]]",
  102. messageSends: ["ifTrue:ifFalse:", ",", "name", "instanceClass", "isNil", "isMetaclass"],
  103. referencedClasses: []
  104. }),
  105. smalltalk.Exporter);
  106. smalltalk.addMethod(
  107. smalltalk.method({
  108. selector: "exportAll",
  109. category: 'fileOut',
  110. fn: function (){
  111. var self=this;
  112. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  113. function $String(){return smalltalk.String||(typeof String=="undefined"?nil:String)}
  114. return smalltalk.withContext(function($ctx1) {
  115. var $1;
  116. $1=_st($String())._streamContents_((function(stream){
  117. return smalltalk.withContext(function($ctx2) {
  118. return _st(_st(_st($Smalltalk())._current())._packages())._do_((function(pkg){
  119. return smalltalk.withContext(function($ctx3) {
  120. return _st(stream)._nextPutAll_(self._exportPackage_(_st(pkg)._name()));
  121. }, function($ctx3) {$ctx3.fillBlock({pkg:pkg},$ctx2)})}));
  122. }, function($ctx2) {$ctx2.fillBlock({stream:stream},$ctx1)})}));
  123. return $1;
  124. }, function($ctx1) {$ctx1.fill(self,"exportAll",{},smalltalk.Exporter)})},
  125. args: [],
  126. source: "exportAll\x0a\x09\x22Export all packages in the system.\x22\x0a\x0a\x09^String streamContents: [:stream |\x0a\x09\x09Smalltalk current packages do: [:pkg |\x0a\x09\x09stream nextPutAll: (self exportPackage: pkg name)]]",
  127. messageSends: ["streamContents:", "do:", "nextPutAll:", "exportPackage:", "name", "packages", "current"],
  128. referencedClasses: ["Smalltalk", "String"]
  129. }),
  130. smalltalk.Exporter);
  131. smalltalk.addMethod(
  132. smalltalk.method({
  133. selector: "exportClass:",
  134. category: 'fileOut',
  135. fn: function (aClass){
  136. var self=this;
  137. function $String(){return smalltalk.String||(typeof String=="undefined"?nil:String)}
  138. return smalltalk.withContext(function($ctx1) {
  139. var $1;
  140. $1=_st($String())._streamContents_((function(stream){
  141. return smalltalk.withContext(function($ctx2) {
  142. self._exportDefinitionOf_on_(aClass,stream);
  143. self._exportMethodsOf_on_(aClass,stream);
  144. self._exportMetaDefinitionOf_on_(aClass,stream);
  145. return self._exportMethodsOf_on_(_st(aClass)._class(),stream);
  146. }, function($ctx2) {$ctx2.fillBlock({stream:stream},$ctx1)})}));
  147. return $1;
  148. }, function($ctx1) {$ctx1.fill(self,"exportClass:",{aClass:aClass},smalltalk.Exporter)})},
  149. args: ["aClass"],
  150. source: "exportClass: aClass\x0a\x09\x22Export a single class. Subclasses override these methods.\x22\x0a\x0a\x09^String streamContents: [:stream |\x0a\x09\x09self exportDefinitionOf: aClass on: stream.\x0a\x09\x09self exportMethodsOf: aClass on: stream.\x0a\x09\x09self exportMetaDefinitionOf: aClass on: stream.\x0a\x09\x09self exportMethodsOf: aClass class on: stream]",
  151. messageSends: ["streamContents:", "exportDefinitionOf:on:", "exportMethodsOf:on:", "exportMetaDefinitionOf:on:", "class"],
  152. referencedClasses: ["String"]
  153. }),
  154. smalltalk.Exporter);
  155. smalltalk.addMethod(
  156. smalltalk.method({
  157. selector: "exportDefinitionOf:on:",
  158. category: 'private',
  159. fn: function (aClass,aStream){
  160. var self=this;
  161. return smalltalk.withContext(function($ctx1) {
  162. var $1,$2,$3,$4,$5,$6,$7;
  163. $1=aStream;
  164. _st($1)._nextPutAll_("smalltalk.addClass(");
  165. _st($1)._nextPutAll_(_st("'".__comma(self._classNameFor_(aClass))).__comma("', "));
  166. _st($1)._nextPutAll_("smalltalk.".__comma(self._classNameFor_(_st(aClass)._superclass())));
  167. $2=_st($1)._nextPutAll_(", [");
  168. _st(_st(aClass)._instanceVariableNames())._do_separatedBy_((function(each){
  169. return smalltalk.withContext(function($ctx2) {
  170. return _st(aStream)._nextPutAll_(_st("'".__comma(each)).__comma("'"));
  171. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  172. return smalltalk.withContext(function($ctx2) {
  173. return _st(aStream)._nextPutAll_(", ");
  174. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  175. $3=aStream;
  176. _st($3)._nextPutAll_("], '");
  177. _st($3)._nextPutAll_(_st(_st(aClass)._category()).__comma("'"));
  178. $4=_st($3)._nextPutAll_(");");
  179. $5=_st(_st(aClass)._comment())._notEmpty();
  180. if(smalltalk.assert($5)){
  181. $6=aStream;
  182. _st($6)._lf();
  183. _st($6)._nextPutAll_("smalltalk.");
  184. _st($6)._nextPutAll_(self._classNameFor_(aClass));
  185. _st($6)._nextPutAll_(".comment=");
  186. _st($6)._nextPutAll_(_st(_st(aClass)._comment())._asJavascript());
  187. $7=_st($6)._nextPutAll_(";");
  188. $7;
  189. };
  190. _st(aStream)._lf();
  191. return self}, function($ctx1) {$ctx1.fill(self,"exportDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  192. args: ["aClass", "aStream"],
  193. source: "exportDefinitionOf: aClass on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.addClass(';\x0a\x09\x09nextPutAll: '''', (self classNameFor: aClass), ''', ';\x0a\x09\x09nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);\x0a\x09\x09nextPutAll: ', ['.\x0a\x09aClass instanceVariableNames\x0a\x09\x09do: [:each | aStream nextPutAll: '''', each, '''']\x0a\x09\x09separatedBy: [aStream nextPutAll: ', '].\x0a\x09aStream\x0a\x09\x09nextPutAll: '], ''';\x0a\x09\x09nextPutAll: aClass category, '''';\x0a\x09\x09nextPutAll: ');'.\x0a\x09aClass comment notEmpty ifTrue: [\x0a\x09\x09aStream\x0a\x09\x09\x09lf;\x0a\x09\x09nextPutAll: 'smalltalk.';\x0a\x09\x09nextPutAll: (self classNameFor: aClass);\x0a\x09\x09nextPutAll: '.comment=';\x0a\x09\x09nextPutAll: aClass comment asJavascript;\x0a\x09\x09nextPutAll: ';'].\x0a\x09aStream lf",
  194. messageSends: ["nextPutAll:", ",", "classNameFor:", "superclass", "do:separatedBy:", "instanceVariableNames", "category", "ifTrue:", "lf", "asJavascript", "comment", "notEmpty"],
  195. referencedClasses: []
  196. }),
  197. smalltalk.Exporter);
  198. smalltalk.addMethod(
  199. smalltalk.method({
  200. selector: "exportMetaDefinitionOf:on:",
  201. category: 'private',
  202. fn: function (aClass,aStream){
  203. var self=this;
  204. function $String(){return smalltalk.String||(typeof String=="undefined"?nil:String)}
  205. return smalltalk.withContext(function($ctx1) {
  206. var $1,$2,$3;
  207. $1=_st(_st(_st(aClass)._class())._instanceVariableNames())._isEmpty();
  208. if(! smalltalk.assert($1)){
  209. $2=aStream;
  210. _st($2)._nextPutAll_("smalltalk.".__comma(self._classNameFor_(_st(aClass)._class())));
  211. $3=_st($2)._nextPutAll_(".iVarNames = [");
  212. $3;
  213. _st(_st(_st(aClass)._class())._instanceVariableNames())._do_separatedBy_((function(each){
  214. return smalltalk.withContext(function($ctx2) {
  215. return _st(aStream)._nextPutAll_(_st("'".__comma(each)).__comma("'"));
  216. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  217. return smalltalk.withContext(function($ctx2) {
  218. return _st(aStream)._nextPutAll_(",");
  219. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  220. _st(aStream)._nextPutAll_("];".__comma(_st($String())._lf()));
  221. };
  222. return self}, function($ctx1) {$ctx1.fill(self,"exportMetaDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  223. args: ["aClass", "aStream"],
  224. source: "exportMetaDefinitionOf: aClass on: aStream\x0a\x09aClass class instanceVariableNames isEmpty ifFalse: [\x0a\x09\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.', (self classNameFor: aClass class);\x0a\x09\x09nextPutAll: '.iVarNames = ['.\x0a\x09\x09aClass class instanceVariableNames\x0a\x09\x09do: [:each | aStream nextPutAll: '''', each, '''']\x0a\x09\x09separatedBy: [aStream nextPutAll: ','].\x0a\x09\x09aStream nextPutAll: '];', String lf]",
  225. messageSends: ["ifFalse:", "nextPutAll:", ",", "classNameFor:", "class", "do:separatedBy:", "instanceVariableNames", "lf", "isEmpty"],
  226. referencedClasses: ["String"]
  227. }),
  228. smalltalk.Exporter);
  229. smalltalk.addMethod(
  230. smalltalk.method({
  231. selector: "exportMethod:of:on:",
  232. category: 'private',
  233. fn: function (aMethod,aClass,aStream){
  234. var self=this;
  235. return smalltalk.withContext(function($ctx1) {
  236. var $1,$2,$3,$4;
  237. $1=aStream;
  238. _st($1)._nextPutAll_("smalltalk.addMethod(");
  239. _st($1)._lf();
  240. _st($1)._nextPutAll_("smalltalk.method({");
  241. _st($1)._lf();
  242. _st($1)._nextPutAll_(_st("selector: ".__comma(_st(_st(aMethod)._selector())._asJavascript())).__comma(","));
  243. _st($1)._lf();
  244. _st($1)._nextPutAll_(_st("category: '".__comma(_st(aMethod)._category())).__comma("',"));
  245. _st($1)._lf();
  246. _st($1)._nextPutAll_(_st("fn: ".__comma(_st(_st(aMethod)._fn())._compiledSource())).__comma(","));
  247. _st($1)._lf();
  248. _st($1)._nextPutAll_(_st("args: ".__comma(_st(_st(aMethod)._arguments())._asJavascript())).__comma(","));
  249. _st($1)._lf();
  250. _st($1)._nextPutAll_(_st("source: ".__comma(_st(_st(aMethod)._source())._asJavascript())).__comma(","));
  251. _st($1)._lf();
  252. _st($1)._nextPutAll_(_st("messageSends: ".__comma(_st(_st(aMethod)._messageSends())._asJavascript())).__comma(","));
  253. _st($1)._lf();
  254. $2=_st($1)._nextPutAll_("referencedClasses: ".__comma(_st(_st(aMethod)._referencedClasses())._asJavascript()));
  255. $3=aStream;
  256. _st($3)._lf();
  257. _st($3)._nextPutAll_("}),");
  258. _st($3)._lf();
  259. _st($3)._nextPutAll_("smalltalk.".__comma(self._classNameFor_(aClass)));
  260. _st($3)._nextPutAll_(");");
  261. _st($3)._lf();
  262. $4=_st($3)._lf();
  263. return self}, function($ctx1) {$ctx1.fill(self,"exportMethod:of:on:",{aMethod:aMethod,aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  264. args: ["aMethod", "aClass", "aStream"],
  265. source: "exportMethod: aMethod of: aClass on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.addMethod(';lf;\x0a\x09\x09\x22nextPutAll: aMethod selector asSelector asJavascript, ',';lf;\x22\x0a\x09\x09nextPutAll: 'smalltalk.method({';lf;\x0a\x09\x09nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;\x0a\x09\x09nextPutAll: 'category: ''', aMethod category, ''',';lf;\x0a\x09\x09nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;\x0a\x09\x09nextPutAll: 'args: ', aMethod arguments asJavascript, ','; lf;\x0a\x09\x09nextPutAll: 'source: ', aMethod source asJavascript, ',';lf;\x0a\x09\x09nextPutAll: 'messageSends: ', aMethod messageSends asJavascript, ',';lf;\x0a\x09\x09nextPutAll: 'referencedClasses: ', aMethod referencedClasses asJavascript.\x0a\x09aStream\x0a\x09\x09lf;\x0a\x09\x09nextPutAll: '}),';lf;\x0a\x09\x09nextPutAll: 'smalltalk.', (self classNameFor: aClass);\x0a\x09\x09nextPutAll: ');';lf;lf",
  266. messageSends: ["nextPutAll:", "lf", ",", "asJavascript", "selector", "category", "compiledSource", "fn", "arguments", "source", "messageSends", "referencedClasses", "classNameFor:"],
  267. referencedClasses: []
  268. }),
  269. smalltalk.Exporter);
  270. smalltalk.addMethod(
  271. smalltalk.method({
  272. selector: "exportMethodsOf:on:",
  273. category: 'private',
  274. fn: function (aClass,aStream){
  275. var self=this;
  276. return smalltalk.withContext(function($ctx1) {
  277. var $1;
  278. _st(_st(_st(_st(aClass)._methodDictionary())._values())._sorted_((function(a,b){
  279. return smalltalk.withContext(function($ctx2) {
  280. return _st(_st(a)._selector()).__lt_eq(_st(b)._selector());
  281. }, function($ctx2) {$ctx2.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(each){
  282. return smalltalk.withContext(function($ctx2) {
  283. $1=_st(_st(each)._category())._match_("^\x5c*");
  284. if(! smalltalk.assert($1)){
  285. return self._exportMethod_of_on_(each,aClass,aStream);
  286. };
  287. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  288. _st(aStream)._lf();
  289. return self}, function($ctx1) {$ctx1.fill(self,"exportMethodsOf:on:",{aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  290. args: ["aClass", "aStream"],
  291. source: "exportMethodsOf: aClass on: aStream\x0a\x09\x22Issue #143: sort methods alphabetically\x22\x0a\x0a\x09((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector]) do: [:each |\x0a\x09\x09(each category match: '^\x5c*') ifFalse: [\x0a\x09\x09\x09self exportMethod: each of: aClass on: aStream]].\x0a\x09aStream lf",
  292. messageSends: ["do:", "ifFalse:", "exportMethod:of:on:", "match:", "category", "sorted:", "<=", "selector", "values", "methodDictionary", "lf"],
  293. referencedClasses: []
  294. }),
  295. smalltalk.Exporter);
  296. smalltalk.addMethod(
  297. smalltalk.method({
  298. selector: "exportPackage:",
  299. category: 'fileOut',
  300. fn: function (packageName){
  301. var self=this;
  302. var package_;
  303. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  304. function $String(){return smalltalk.String||(typeof String=="undefined"?nil:String)}
  305. return smalltalk.withContext(function($ctx1) {
  306. var $1;
  307. $1=_st($String())._streamContents_((function(stream){
  308. return smalltalk.withContext(function($ctx2) {
  309. package_=_st(_st($Smalltalk())._current())._packageAt_(packageName);
  310. package_;
  311. self._exportPackageDefinitionOf_on_(package_,stream);
  312. _st(_st(_st(package_)._sortedClasses())._asSet())._do_((function(each){
  313. return smalltalk.withContext(function($ctx3) {
  314. return _st(stream)._nextPutAll_(self._exportClass_(each));
  315. }, function($ctx3) {$ctx3.fillBlock({each:each},$ctx2)})}));
  316. return self._exportPackageExtensionsOf_on_(package_,stream);
  317. }, function($ctx2) {$ctx2.fillBlock({stream:stream},$ctx1)})}));
  318. return $1;
  319. }, function($ctx1) {$ctx1.fill(self,"exportPackage:",{packageName:packageName,package_:package_},smalltalk.Exporter)})},
  320. args: ["packageName"],
  321. source: "exportPackage: packageName\x0a\x09\x22Export a given package by name.\x22\x0a\x0a\x09| package |\x0a\x09^String streamContents: [:stream |\x0a\x09\x09\x09\x09package := Smalltalk current packageAt: packageName.\x0a\x09\x09\x09\x09self exportPackageDefinitionOf: package on: stream.\x0a\x0a\x09\x09\x22Export classes in dependency order.\x0a\x09\x09Update (issue #171): Remove duplicates for export\x22\x0a\x09\x09\x09package sortedClasses asSet do: [:each |\x0a\x09\x09\x09\x09\x09\x09stream nextPutAll: (self exportClass: each)].\x0a\x09\x09self exportPackageExtensionsOf: package on: stream]",
  322. messageSends: ["streamContents:", "packageAt:", "current", "exportPackageDefinitionOf:on:", "do:", "nextPutAll:", "exportClass:", "asSet", "sortedClasses", "exportPackageExtensionsOf:on:"],
  323. referencedClasses: ["Smalltalk", "String"]
  324. }),
  325. smalltalk.Exporter);
  326. smalltalk.addMethod(
  327. smalltalk.method({
  328. selector: "exportPackageDefinitionOf:on:",
  329. category: 'private',
  330. fn: function (package_,aStream){
  331. var self=this;
  332. return smalltalk.withContext(function($ctx1) {
  333. var $1,$2;
  334. $1=aStream;
  335. _st($1)._nextPutAll_("smalltalk.addPackage(");
  336. _st($1)._nextPutAll_(_st("'".__comma(_st(package_)._name())).__comma("');"));
  337. $2=_st($1)._lf();
  338. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageDefinitionOf:on:",{package_:package_,aStream:aStream},smalltalk.Exporter)})},
  339. args: ["package", "aStream"],
  340. source: "exportPackageDefinitionOf: package on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.addPackage(';\x0a\x09\x09nextPutAll: '''', package name, ''');';\x0a\x09\x09lf",
  341. messageSends: ["nextPutAll:", ",", "name", "lf"],
  342. referencedClasses: []
  343. }),
  344. smalltalk.Exporter);
  345. smalltalk.addMethod(
  346. smalltalk.method({
  347. selector: "exportPackageExtensionsOf:on:",
  348. category: 'private',
  349. fn: function (package_,aStream){
  350. var self=this;
  351. var name;
  352. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  353. function $Package(){return smalltalk.Package||(typeof Package=="undefined"?nil:Package)}
  354. return smalltalk.withContext(function($ctx1) {
  355. var $1;
  356. name=_st(package_)._name();
  357. _st(_st($Package())._sortedClasses_(_st(_st($Smalltalk())._current())._classes()))._do_((function(each){
  358. return smalltalk.withContext(function($ctx2) {
  359. return _st([each,_st(each)._class()])._do_((function(aClass){
  360. return smalltalk.withContext(function($ctx3) {
  361. return _st(_st(_st(_st(aClass)._methodDictionary())._values())._sorted_((function(a,b){
  362. return smalltalk.withContext(function($ctx4) {
  363. return _st(_st(a)._selector()).__lt_eq(_st(b)._selector());
  364. }, function($ctx4) {$ctx4.fillBlock({a:a,b:b},$ctx3)})})))._do_((function(method){
  365. return smalltalk.withContext(function($ctx4) {
  366. $1=_st(_st(method)._category())._match_("^\x5c*".__comma(name));
  367. if(smalltalk.assert($1)){
  368. return self._exportMethod_of_on_(method,aClass,aStream);
  369. };
  370. }, function($ctx4) {$ctx4.fillBlock({method:method},$ctx3)})}));
  371. }, function($ctx3) {$ctx3.fillBlock({aClass:aClass},$ctx2)})}));
  372. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  373. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageExtensionsOf:on:",{package_:package_,aStream:aStream,name:name},smalltalk.Exporter)})},
  374. args: ["package", "aStream"],
  375. source: "exportPackageExtensionsOf: package on: aStream\x0a\x09\x22Issue #143: sort classes and methods alphabetically\x22\x0a\x0a\x09| name |\x0a\x09name := package name.\x0a\x09(Package sortedClasses: Smalltalk current classes) do: [:each |\x0a\x09\x09{each. each class} do: [:aClass |\x0a\x09\x09\x09((aClass methodDictionary values) sorted: [:a :b | a selector <= b selector]) do: [:method |\x0a\x09\x09\x09\x09(method category match: '^\x5c*', name) ifTrue: [\x0a\x09\x09\x09\x09\x09self exportMethod: method of: aClass on: aStream ]]]]",
  376. messageSends: ["name", "do:", "ifTrue:", "exportMethod:of:on:", "match:", ",", "category", "sorted:", "<=", "selector", "values", "methodDictionary", "class", "sortedClasses:", "classes", "current"],
  377. referencedClasses: ["Smalltalk", "Package"]
  378. }),
  379. smalltalk.Exporter);
  380. smalltalk.addClass('ChunkExporter', smalltalk.Exporter, [], 'Importer-Exporter');
  381. smalltalk.ChunkExporter.comment="I am an exporter dedicated to outputting Amber source code in the classic Smalltalk chunk format.\x0a\x0aI do not output any compiled code.";
  382. smalltalk.addMethod(
  383. smalltalk.method({
  384. selector: "chunkEscape:",
  385. category: 'private',
  386. fn: function (aString){
  387. var self=this;
  388. return smalltalk.withContext(function($ctx1) {
  389. var $1;
  390. $1=_st(_st(aString)._replace_with_("!","!!"))._trimBoth();
  391. return $1;
  392. }, function($ctx1) {$ctx1.fill(self,"chunkEscape:",{aString:aString},smalltalk.ChunkExporter)})},
  393. args: ["aString"],
  394. source: "chunkEscape: aString\x0a\x09\x22Replace all occurrences of ! with !! and trim at both ends.\x22\x0a\x0a\x09^(aString replace: '!' with: '!!') trimBoth",
  395. messageSends: ["trimBoth", "replace:with:"],
  396. referencedClasses: []
  397. }),
  398. smalltalk.ChunkExporter);
  399. smalltalk.addMethod(
  400. smalltalk.method({
  401. selector: "classNameFor:",
  402. category: 'private',
  403. fn: function (aClass){
  404. var self=this;
  405. return smalltalk.withContext(function($ctx1) {
  406. var $2,$3,$1;
  407. $2=_st(aClass)._isMetaclass();
  408. if(smalltalk.assert($2)){
  409. $1=_st(_st(_st(aClass)._instanceClass())._name()).__comma(" class");
  410. } else {
  411. $3=_st(aClass)._isNil();
  412. if(smalltalk.assert($3)){
  413. $1="nil";
  414. } else {
  415. $1=_st(aClass)._name();
  416. };
  417. };
  418. return $1;
  419. }, function($ctx1) {$ctx1.fill(self,"classNameFor:",{aClass:aClass},smalltalk.ChunkExporter)})},
  420. args: ["aClass"],
  421. source: "classNameFor: aClass\x0a\x09^aClass isMetaclass\x0a\x09\x09ifTrue: [aClass instanceClass name, ' class']\x0a\x09\x09ifFalse: [\x0a\x09\x09aClass isNil\x0a\x09\x09\x09ifTrue: ['nil']\x0a\x09\x09\x09ifFalse: [aClass name]]",
  422. messageSends: ["ifTrue:ifFalse:", ",", "name", "instanceClass", "isNil", "isMetaclass"],
  423. referencedClasses: []
  424. }),
  425. smalltalk.ChunkExporter);
  426. smalltalk.addMethod(
  427. smalltalk.method({
  428. selector: "exportDefinitionOf:on:",
  429. category: 'private',
  430. fn: function (aClass,aStream){
  431. var self=this;
  432. return smalltalk.withContext(function($ctx1) {
  433. var $1,$2,$3,$4,$5,$6,$7;
  434. $1=aStream;
  435. _st($1)._nextPutAll_(self._classNameFor_(_st(aClass)._superclass()));
  436. _st($1)._nextPutAll_(" subclass: #".__comma(self._classNameFor_(aClass)));
  437. _st($1)._lf();
  438. _st($1)._tab();
  439. $2=_st($1)._nextPutAll_("instanceVariableNames: '");
  440. _st(_st(aClass)._instanceVariableNames())._do_separatedBy_((function(each){
  441. return smalltalk.withContext(function($ctx2) {
  442. return _st(aStream)._nextPutAll_(each);
  443. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  444. return smalltalk.withContext(function($ctx2) {
  445. return _st(aStream)._nextPutAll_(" ");
  446. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  447. $3=aStream;
  448. _st($3)._nextPutAll_("'");
  449. _st($3)._lf();
  450. _st($3)._tab();
  451. _st($3)._nextPutAll_(_st("package: '".__comma(_st(aClass)._category())).__comma("'!"));
  452. $4=_st($3)._lf();
  453. $5=_st(_st(aClass)._comment())._notEmpty();
  454. if(smalltalk.assert($5)){
  455. $6=aStream;
  456. _st($6)._nextPutAll_(_st("!".__comma(self._classNameFor_(aClass))).__comma(" commentStamp!"));
  457. _st($6)._lf();
  458. _st($6)._nextPutAll_(_st(self._chunkEscape_(_st(aClass)._comment())).__comma("!"));
  459. $7=_st($6)._lf();
  460. $7;
  461. };
  462. _st(aStream)._lf();
  463. return self}, function($ctx1) {$ctx1.fill(self,"exportDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  464. args: ["aClass", "aStream"],
  465. source: "exportDefinitionOf: aClass on: aStream\x0a\x09\x22Chunk format.\x22\x0a\x0a\x09aStream\x0a\x09\x09nextPutAll: (self classNameFor: aClass superclass);\x0a\x09\x09nextPutAll: ' subclass: #', (self classNameFor: aClass); lf;\x0a\x09\x09tab; nextPutAll: 'instanceVariableNames: '''.\x0a\x09aClass instanceVariableNames\x0a\x09\x09do: [:each | aStream nextPutAll: each]\x0a\x09\x09separatedBy: [aStream nextPutAll: ' '].\x0a\x09aStream\x0a\x09\x09nextPutAll: ''''; lf;\x0a\x09\x09tab; nextPutAll: 'package: ''', aClass category, '''!'; lf.\x0a\x09aClass comment notEmpty ifTrue: [\x0a\x09\x09aStream\x0a\x09\x09nextPutAll: '!', (self classNameFor: aClass), ' commentStamp!';lf;\x0a\x09\x09nextPutAll: (self chunkEscape: aClass comment), '!';lf].\x0a\x09aStream lf",
  466. messageSends: ["nextPutAll:", "classNameFor:", "superclass", ",", "lf", "tab", "do:separatedBy:", "instanceVariableNames", "category", "ifTrue:", "chunkEscape:", "comment", "notEmpty"],
  467. referencedClasses: []
  468. }),
  469. smalltalk.ChunkExporter);
  470. smalltalk.addMethod(
  471. smalltalk.method({
  472. selector: "exportMetaDefinitionOf:on:",
  473. category: 'private',
  474. fn: function (aClass,aStream){
  475. var self=this;
  476. return smalltalk.withContext(function($ctx1) {
  477. var $1,$2,$3,$4,$5;
  478. $1=_st(_st(_st(aClass)._class())._instanceVariableNames())._isEmpty();
  479. if(! smalltalk.assert($1)){
  480. $2=aStream;
  481. _st($2)._nextPutAll_(self._classNameFor_(_st(aClass)._class()));
  482. $3=_st($2)._nextPutAll_(" instanceVariableNames: '");
  483. $3;
  484. _st(_st(_st(aClass)._class())._instanceVariableNames())._do_separatedBy_((function(each){
  485. return smalltalk.withContext(function($ctx2) {
  486. return _st(aStream)._nextPutAll_(each);
  487. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  488. return smalltalk.withContext(function($ctx2) {
  489. return _st(aStream)._nextPutAll_(" ");
  490. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  491. $4=aStream;
  492. _st($4)._nextPutAll_("'!");
  493. _st($4)._lf();
  494. $5=_st($4)._lf();
  495. $5;
  496. };
  497. return self}, function($ctx1) {$ctx1.fill(self,"exportMetaDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  498. args: ["aClass", "aStream"],
  499. source: "exportMetaDefinitionOf: aClass on: aStream\x0a\x0a\x09aClass class instanceVariableNames isEmpty ifFalse: [\x0a\x09\x09aStream\x0a\x09\x09\x09nextPutAll: (self classNameFor: aClass class);\x0a\x09\x09\x09nextPutAll: ' instanceVariableNames: '''.\x0a\x09\x09aClass class instanceVariableNames\x0a\x09\x09\x09do: [:each | aStream nextPutAll: each]\x0a\x09\x09\x09separatedBy: [aStream nextPutAll: ' '].\x0a\x09\x09aStream\x0a\x09\x09\x09nextPutAll: '''!'; lf; lf]",
  500. messageSends: ["ifFalse:", "nextPutAll:", "classNameFor:", "class", "do:separatedBy:", "instanceVariableNames", "lf", "isEmpty"],
  501. referencedClasses: []
  502. }),
  503. smalltalk.ChunkExporter);
  504. smalltalk.addMethod(
  505. smalltalk.method({
  506. selector: "exportMethod:of:on:",
  507. category: 'private',
  508. fn: function (aMethod,aClass,aStream){
  509. var self=this;
  510. return smalltalk.withContext(function($ctx1) {
  511. var $1,$2;
  512. $1=aStream;
  513. _st($1)._lf();
  514. _st($1)._lf();
  515. _st($1)._nextPutAll_(self._chunkEscape_(_st(aMethod)._source()));
  516. _st($1)._lf();
  517. $2=_st($1)._nextPutAll_("!");
  518. return self}, function($ctx1) {$ctx1.fill(self,"exportMethod:of:on:",{aMethod:aMethod,aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  519. args: ["aMethod", "aClass", "aStream"],
  520. source: "exportMethod: aMethod of: aClass on: aStream\x0a\x09aStream\x0a\x09\x09lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;\x0a\x09\x09nextPutAll: '!'",
  521. messageSends: ["lf", "nextPutAll:", "chunkEscape:", "source"],
  522. referencedClasses: []
  523. }),
  524. smalltalk.ChunkExporter);
  525. smalltalk.addMethod(
  526. smalltalk.method({
  527. selector: "exportMethods:category:of:on:",
  528. category: 'private',
  529. fn: function (methods,category,aClass,aStream){
  530. var self=this;
  531. return smalltalk.withContext(function($ctx1) {
  532. var $1,$2,$3,$4;
  533. $1=aStream;
  534. _st($1)._nextPutAll_("!".__comma(self._classNameFor_(aClass)));
  535. $2=_st($1)._nextPutAll_(_st(" methodsFor: '".__comma(category)).__comma("'!"));
  536. _st(_st(methods)._sorted_((function(a,b){
  537. return smalltalk.withContext(function($ctx2) {
  538. return _st(_st(a)._selector()).__lt_eq(_st(b)._selector());
  539. }, function($ctx2) {$ctx2.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(each){
  540. return smalltalk.withContext(function($ctx2) {
  541. return self._exportMethod_of_on_(each,aClass,aStream);
  542. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  543. $3=aStream;
  544. _st($3)._nextPutAll_(" !");
  545. _st($3)._lf();
  546. $4=_st($3)._lf();
  547. return self}, function($ctx1) {$ctx1.fill(self,"exportMethods:category:of:on:",{methods:methods,category:category,aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  548. args: ["methods", "category", "aClass", "aStream"],
  549. source: "exportMethods: methods category: category of: aClass on: aStream\x0a\x09\x22Issue #143: sort methods alphabetically\x22\x0a\x0a\x09aStream\x0a\x09\x09nextPutAll: '!', (self classNameFor: aClass);\x0a\x09\x09nextPutAll: ' methodsFor: ''', category, '''!'.\x0a\x09\x09(methods sorted: [:a :b | a selector <= b selector]) do: [:each |\x0a\x09\x09\x09\x09self exportMethod: each of: aClass on: aStream].\x0a\x09aStream nextPutAll: ' !'; lf; lf",
  550. messageSends: ["nextPutAll:", ",", "classNameFor:", "do:", "exportMethod:of:on:", "sorted:", "<=", "selector", "lf"],
  551. referencedClasses: []
  552. }),
  553. smalltalk.ChunkExporter);
  554. smalltalk.addMethod(
  555. smalltalk.method({
  556. selector: "exportMethodsOf:on:",
  557. category: 'private',
  558. fn: function (aClass,aStream){
  559. var self=this;
  560. var map;
  561. function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
  562. return smalltalk.withContext(function($ctx1) {
  563. var $1;
  564. map=_st($Dictionary())._new();
  565. _st(aClass)._protocolsDo_((function(category,methods){
  566. return smalltalk.withContext(function($ctx2) {
  567. $1=_st(category)._match_("^\x5c*");
  568. if(! smalltalk.assert($1)){
  569. return _st(map)._at_put_(category,methods);
  570. };
  571. }, function($ctx2) {$ctx2.fillBlock({category:category,methods:methods},$ctx1)})}));
  572. _st(_st(_st(map)._keys())._sorted_((function(a,b){
  573. return smalltalk.withContext(function($ctx2) {
  574. return _st(a).__lt_eq(b);
  575. }, function($ctx2) {$ctx2.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(category){
  576. var methods;
  577. return smalltalk.withContext(function($ctx2) {
  578. methods=_st(map)._at_(category);
  579. methods;
  580. return self._exportMethods_category_of_on_(methods,category,aClass,aStream);
  581. }, function($ctx2) {$ctx2.fillBlock({category:category,methods:methods},$ctx1)})}));
  582. return self}, function($ctx1) {$ctx1.fill(self,"exportMethodsOf:on:",{aClass:aClass,aStream:aStream,map:map},smalltalk.ChunkExporter)})},
  583. args: ["aClass", "aStream"],
  584. source: "exportMethodsOf: aClass on: aStream\x0a\x09\x22Issue #143: sort protocol alphabetically\x22\x0a\x0a\x09| map |\x0a\x09map := Dictionary new.\x0a\x09aClass protocolsDo: [:category :methods |\x0a\x09\x09(category match: '^\x5c*') ifFalse: [ map at: category put: methods ]].\x0a\x09(map keys sorted: [:a :b | a <= b ]) do: [:category | | methods |\x0a\x09\x09methods := map at: category.\x0a\x09\x09self\x0a\x09\x09\x09exportMethods: methods\x0a\x09\x09\x09category: category\x0a\x09\x09\x09of: aClass\x0a\x09\x09\x09on: aStream ]",
  585. messageSends: ["new", "protocolsDo:", "ifFalse:", "at:put:", "match:", "do:", "at:", "exportMethods:category:of:on:", "sorted:", "<=", "keys"],
  586. referencedClasses: ["Dictionary"]
  587. }),
  588. smalltalk.ChunkExporter);
  589. smalltalk.addMethod(
  590. smalltalk.method({
  591. selector: "exportPackageDefinitionOf:on:",
  592. category: 'private',
  593. fn: function (package_,aStream){
  594. var self=this;
  595. return smalltalk.withContext(function($ctx1) {
  596. var $1,$2;
  597. $1=aStream;
  598. _st($1)._nextPutAll_(_st("Smalltalk current createPackage: '".__comma(_st(package_)._name())).__comma("'!"));
  599. $2=_st($1)._lf();
  600. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageDefinitionOf:on:",{package_:package_,aStream:aStream},smalltalk.ChunkExporter)})},
  601. args: ["package", "aStream"],
  602. source: "exportPackageDefinitionOf: package on: aStream\x0a\x09\x22Chunk format.\x22\x0a\x0a\x09aStream\x0a\x09\x09nextPutAll: 'Smalltalk current createPackage: ''', package name, '''!';\x0a\x09\x09lf",
  603. messageSends: ["nextPutAll:", ",", "name", "lf"],
  604. referencedClasses: []
  605. }),
  606. smalltalk.ChunkExporter);
  607. smalltalk.addMethod(
  608. smalltalk.method({
  609. selector: "exportPackageExtensionsOf:on:",
  610. category: 'private',
  611. fn: function (package_,aStream){
  612. var self=this;
  613. var name,map;
  614. function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
  615. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  616. function $Package(){return smalltalk.Package||(typeof Package=="undefined"?nil:Package)}
  617. return smalltalk.withContext(function($ctx1) {
  618. var $1;
  619. name=_st(package_)._name();
  620. _st(_st($Package())._sortedClasses_(_st(_st($Smalltalk())._current())._classes()))._do_((function(each){
  621. return smalltalk.withContext(function($ctx2) {
  622. return _st([each,_st(each)._class()])._do_((function(aClass){
  623. return smalltalk.withContext(function($ctx3) {
  624. map=_st($Dictionary())._new();
  625. map;
  626. _st(aClass)._protocolsDo_((function(category,methods){
  627. return smalltalk.withContext(function($ctx4) {
  628. $1=_st(category)._match_("^\x5c*".__comma(name));
  629. if(smalltalk.assert($1)){
  630. return _st(map)._at_put_(category,methods);
  631. };
  632. }, function($ctx4) {$ctx4.fillBlock({category:category,methods:methods},$ctx3)})}));
  633. return _st(_st(_st(map)._keys())._sorted_((function(a,b){
  634. return smalltalk.withContext(function($ctx4) {
  635. return _st(a).__lt_eq(b);
  636. }, function($ctx4) {$ctx4.fillBlock({a:a,b:b},$ctx3)})})))._do_((function(category){
  637. var methods;
  638. return smalltalk.withContext(function($ctx4) {
  639. methods=_st(map)._at_(category);
  640. methods;
  641. return self._exportMethods_category_of_on_(methods,category,aClass,aStream);
  642. }, function($ctx4) {$ctx4.fillBlock({category:category,methods:methods},$ctx3)})}));
  643. }, function($ctx3) {$ctx3.fillBlock({aClass:aClass},$ctx2)})}));
  644. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  645. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageExtensionsOf:on:",{package_:package_,aStream:aStream,name:name,map:map},smalltalk.ChunkExporter)})},
  646. args: ["package", "aStream"],
  647. source: "exportPackageExtensionsOf: package on: aStream\x0a\x09\x22We need to override this one too since we need to group\x0a\x09all methods in a given protocol under a leading methodsFor: chunk\x0a\x09for that class.\x22\x0a\x0a\x09\x22Issue #143: sort protocol alphabetically\x22\x0a\x0a\x09| name map |\x0a\x09name := package name.\x0a\x09(Package sortedClasses: Smalltalk current classes) do: [:each |\x0a\x09\x09{each. each class} do: [:aClass |\x0a\x09\x09\x09map := Dictionary new.\x0a\x09\x09\x09aClass protocolsDo: [:category :methods |\x0a\x09\x09\x09\x09(category match: '^\x5c*', name) ifTrue: [ map at: category put: methods ]].\x0a\x09\x09\x09(map keys sorted: [:a :b | a <= b ]) do: [:category | | methods |\x0a\x09\x09\x09\x09methods := map at: category.\x0a\x09\x09\x09\x09self exportMethods: methods category: category of: aClass on: aStream ]]]",
  648. messageSends: ["name", "do:", "new", "protocolsDo:", "ifTrue:", "at:put:", "match:", ",", "at:", "exportMethods:category:of:on:", "sorted:", "<=", "keys", "class", "sortedClasses:", "classes", "current"],
  649. referencedClasses: ["Dictionary", "Smalltalk", "Package"]
  650. }),
  651. smalltalk.ChunkExporter);
  652. smalltalk.addClass('StrippedExporter', smalltalk.Exporter, [], 'Importer-Exporter');
  653. smalltalk.StrippedExporter.comment="I export Amber code into a JavaScript string, but without any optional associated data like the Amber source code.";
  654. smalltalk.addMethod(
  655. smalltalk.method({
  656. selector: "exportDefinitionOf:on:",
  657. category: 'private',
  658. fn: function (aClass,aStream){
  659. var self=this;
  660. return smalltalk.withContext(function($ctx1) {
  661. var $1,$2,$3,$4;
  662. $1=aStream;
  663. _st($1)._nextPutAll_("smalltalk.addClass(");
  664. _st($1)._nextPutAll_(_st("'".__comma(self._classNameFor_(aClass))).__comma("', "));
  665. _st($1)._nextPutAll_("smalltalk.".__comma(self._classNameFor_(_st(aClass)._superclass())));
  666. $2=_st($1)._nextPutAll_(", [");
  667. _st(_st(aClass)._instanceVariableNames())._do_separatedBy_((function(each){
  668. return smalltalk.withContext(function($ctx2) {
  669. return _st(aStream)._nextPutAll_(_st("'".__comma(each)).__comma("'"));
  670. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  671. return smalltalk.withContext(function($ctx2) {
  672. return _st(aStream)._nextPutAll_(", ");
  673. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  674. $3=aStream;
  675. _st($3)._nextPutAll_("], '");
  676. _st($3)._nextPutAll_(_st(_st(aClass)._category()).__comma("'"));
  677. $4=_st($3)._nextPutAll_(");");
  678. _st(aStream)._lf();
  679. return self}, function($ctx1) {$ctx1.fill(self,"exportDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.StrippedExporter)})},
  680. args: ["aClass", "aStream"],
  681. source: "exportDefinitionOf: aClass on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.addClass(';\x0a\x09\x09nextPutAll: '''', (self classNameFor: aClass), ''', ';\x0a\x09\x09nextPutAll: 'smalltalk.', (self classNameFor: aClass superclass);\x0a\x09\x09nextPutAll: ', ['.\x0a\x09aClass instanceVariableNames\x0a\x09\x09do: [:each | aStream nextPutAll: '''', each, '''']\x0a\x09\x09separatedBy: [aStream nextPutAll: ', '].\x0a\x09aStream\x0a\x09\x09nextPutAll: '], ''';\x0a\x09\x09nextPutAll: aClass category, '''';\x0a\x09\x09nextPutAll: ');'.\x0a\x09aStream lf",
  682. messageSends: ["nextPutAll:", ",", "classNameFor:", "superclass", "do:separatedBy:", "instanceVariableNames", "category", "lf"],
  683. referencedClasses: []
  684. }),
  685. smalltalk.StrippedExporter);
  686. smalltalk.addMethod(
  687. smalltalk.method({
  688. selector: "exportMethod:of:on:",
  689. category: 'private',
  690. fn: function (aMethod,aClass,aStream){
  691. var self=this;
  692. return smalltalk.withContext(function($ctx1) {
  693. var $1,$2;
  694. $1=aStream;
  695. _st($1)._nextPutAll_("smalltalk.addMethod(");
  696. _st($1)._lf();
  697. _st($1)._nextPutAll_("smalltalk.method({");
  698. _st($1)._lf();
  699. _st($1)._nextPutAll_(_st("selector: ".__comma(_st(_st(aMethod)._selector())._asJavascript())).__comma(","));
  700. _st($1)._lf();
  701. _st($1)._nextPutAll_(_st("fn: ".__comma(_st(_st(aMethod)._fn())._compiledSource())).__comma(","));
  702. _st($1)._lf();
  703. _st($1)._nextPutAll_("messageSends: ".__comma(_st(_st(aMethod)._messageSends())._asJavascript()));
  704. _st($1)._nextPutAll_("}),");
  705. _st($1)._lf();
  706. _st($1)._nextPutAll_("smalltalk.".__comma(self._classNameFor_(aClass)));
  707. _st($1)._nextPutAll_(");");
  708. _st($1)._lf();
  709. $2=_st($1)._lf();
  710. return self}, function($ctx1) {$ctx1.fill(self,"exportMethod:of:on:",{aMethod:aMethod,aClass:aClass,aStream:aStream},smalltalk.StrippedExporter)})},
  711. args: ["aMethod", "aClass", "aStream"],
  712. source: "exportMethod: aMethod of: aClass on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.addMethod(';lf;\x0a\x09\x09\x22nextPutAll: aMethod selector asSelector asJavascript, ',';lf;\x22\x0a\x09\x09nextPutAll: 'smalltalk.method({';lf;\x0a\x09\x09nextPutAll: 'selector: ', aMethod selector asJavascript, ',';lf;\x0a\x09\x09nextPutAll: 'fn: ', aMethod fn compiledSource, ',';lf;\x0a\x09\x09nextPutAll: 'messageSends: ', aMethod messageSends asJavascript;\x0a\x09\x09nextPutAll: '}),';lf;\x0a\x09\x09nextPutAll: 'smalltalk.', (self classNameFor: aClass);\x0a\x09\x09nextPutAll: ');';lf;lf",
  713. messageSends: ["nextPutAll:", "lf", ",", "asJavascript", "selector", "compiledSource", "fn", "messageSends", "classNameFor:"],
  714. referencedClasses: []
  715. }),
  716. smalltalk.StrippedExporter);
  717. smalltalk.addClass('Importer', smalltalk.Object, [], 'Importer-Exporter');
  718. smalltalk.Importer.comment="I can import Amber code from a string in the chunk format.\x0a\x0a## API\x0a\x0a Importer new import: aString";
  719. smalltalk.addMethod(
  720. smalltalk.method({
  721. selector: "import:",
  722. category: 'fileIn',
  723. fn: function (aStream){
  724. var self=this;
  725. var chunk,result,parser,lastEmpty;
  726. function $ChunkParser(){return smalltalk.ChunkParser||(typeof ChunkParser=="undefined"?nil:ChunkParser)}
  727. function $Compiler(){return smalltalk.Compiler||(typeof Compiler=="undefined"?nil:Compiler)}
  728. return smalltalk.withContext(function($ctx1) {
  729. var $1,$2;
  730. parser=_st($ChunkParser())._on_(aStream);
  731. lastEmpty=false;
  732. _st((function(){
  733. return smalltalk.withContext(function($ctx2) {
  734. chunk=_st(parser)._nextChunk();
  735. chunk;
  736. return _st(chunk)._isNil();
  737. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileFalse_((function(){
  738. return smalltalk.withContext(function($ctx2) {
  739. $1=_st(chunk)._isEmpty();
  740. if(smalltalk.assert($1)){
  741. lastEmpty=true;
  742. return lastEmpty;
  743. } else {
  744. result=_st(_st($Compiler())._new())._evaluateExpression_(chunk);
  745. result;
  746. $2=lastEmpty;
  747. if(smalltalk.assert($2)){
  748. lastEmpty=false;
  749. lastEmpty;
  750. return _st(result)._scanFrom_(parser);
  751. };
  752. };
  753. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  754. return self}, function($ctx1) {$ctx1.fill(self,"import:",{aStream:aStream,chunk:chunk,result:result,parser:parser,lastEmpty:lastEmpty},smalltalk.Importer)})},
  755. args: ["aStream"],
  756. source: "import: aStream\x0a\x09| chunk result parser lastEmpty |\x0a\x09parser := ChunkParser on: aStream.\x0a\x09lastEmpty := false.\x0a\x09[chunk := parser nextChunk.\x0a\x09chunk isNil] whileFalse: [\x0a\x09\x09chunk isEmpty\x0a\x09\x09\x09ifTrue: [lastEmpty := true]\x0a\x09\x09\x09ifFalse: [\x0a\x09\x09\x09\x09result := Compiler new evaluateExpression: chunk.\x0a\x09\x09\x09\x09lastEmpty\x0a\x09\x09\x09\x09\x09\x09ifTrue: [\x0a\x09\x09\x09\x09\x09\x09\x09\x09\x09lastEmpty := false.\x0a\x09\x09\x09\x09\x09\x09\x09\x09\x09result scanFrom: parser]]]",
  757. messageSends: ["on:", "whileFalse:", "ifTrue:ifFalse:", "evaluateExpression:", "new", "ifTrue:", "scanFrom:", "isEmpty", "nextChunk", "isNil"],
  758. referencedClasses: ["ChunkParser", "Compiler"]
  759. }),
  760. smalltalk.Importer);
  761. smalltalk.addClass('PackageHandler', smalltalk.Object, [], 'Importer-Exporter');
  762. smalltalk.PackageHandler.comment="I am responsible for handling package loading and committing.\x0a\x0aI should not be used directly. Instead, use the corresponding `Package` methods.";
  763. smalltalk.addMethod(
  764. smalltalk.method({
  765. selector: "ajaxPutAt:data:",
  766. category: 'private',
  767. fn: function (aURL,aString){
  768. var self=this;
  769. return smalltalk.withContext(function($ctx1) {
  770. _st(jQuery)._ajax_options_(aURL,smalltalk.HashedCollection._from_(["type".__minus_gt("PUT"),"data".__minus_gt(aString),"contentType".__minus_gt("text/plain;charset=UTF-8"),"error".__minus_gt((function(xhr){
  771. return smalltalk.withContext(function($ctx2) {
  772. return self._error_(_st(_st(_st("Commiting ".__comma(aURL)).__comma(" failed with reason: \x22")).__comma(_st(xhr)._responseText())).__comma("\x22"));
  773. }, function($ctx2) {$ctx2.fillBlock({xhr:xhr},$ctx1)})}))]));
  774. return self}, function($ctx1) {$ctx1.fill(self,"ajaxPutAt:data:",{aURL:aURL,aString:aString},smalltalk.PackageHandler)})},
  775. args: ["aURL", "aString"],
  776. source: "ajaxPutAt: aURL data: aString\x0a\x09jQuery\x0a\x09\x09ajax: aURL \x0a\x09\x09options: #{ \x0a\x09\x09\x09'type' -> 'PUT'.\x0a\x09\x09\x09'data' -> aString.\x0a\x09\x09\x09'contentType' -> 'text/plain;charset=UTF-8'.\x0a\x09\x09\x09'error' -> [ :xhr | self error: 'Commiting ' , aURL , ' failed with reason: \x22' , (xhr responseText) , '\x22'] }",
  777. messageSends: ["ajax:options:", "->", "error:", ",", "responseText"],
  778. referencedClasses: []
  779. }),
  780. smalltalk.PackageHandler);
  781. smalltalk.addMethod(
  782. smalltalk.method({
  783. selector: "commit:",
  784. category: 'committing',
  785. fn: function (aPackage){
  786. var self=this;
  787. function $Exporter(){return smalltalk.Exporter||(typeof Exporter=="undefined"?nil:Exporter)}
  788. function $StrippedExporter(){return smalltalk.StrippedExporter||(typeof StrippedExporter=="undefined"?nil:StrippedExporter)}
  789. function $ChunkExporter(){return smalltalk.ChunkExporter||(typeof ChunkExporter=="undefined"?nil:ChunkExporter)}
  790. return smalltalk.withContext(function($ctx1) {
  791. _st([_st($Exporter()).__minus_gt(_st(_st(_st(_st(aPackage)._commitPathJs()).__comma("/")).__comma(_st(aPackage)._name())).__comma(".js")),_st($StrippedExporter()).__minus_gt(_st(_st(_st(_st(aPackage)._commitPathJs()).__comma("/")).__comma(_st(aPackage)._name())).__comma(".deploy.js")),_st($ChunkExporter()).__minus_gt(_st(_st(_st(_st(aPackage)._commitPathSt()).__comma("/")).__comma(_st(aPackage)._name())).__comma(".st"))])._do_displayingProgress_((function(commitStrategy){
  792. var fileContents;
  793. return smalltalk.withContext(function($ctx2) {
  794. fileContents=_st(_st(_st(commitStrategy)._key())._new())._exportPackage_(_st(aPackage)._name());
  795. fileContents;
  796. return self._ajaxPutAt_data_(_st(commitStrategy)._value(),fileContents);
  797. }, function($ctx2) {$ctx2.fillBlock({commitStrategy:commitStrategy,fileContents:fileContents},$ctx1)})}),"Committing package ".__comma(_st(aPackage)._name()));
  798. return self}, function($ctx1) {$ctx1.fill(self,"commit:",{aPackage:aPackage},smalltalk.PackageHandler)})},
  799. args: ["aPackage"],
  800. source: "commit: aPackage\x0a\x09{ \x0a\x09\x09Exporter -> (aPackage commitPathJs, '/', aPackage name, '.js').\x0a\x09\x09StrippedExporter -> (aPackage commitPathJs, '/', aPackage name, '.deploy.js').\x0a\x09\x09ChunkExporter -> (aPackage commitPathSt, '/', aPackage name, '.st')\x0a\x09} \x0a\x09\x09do: [ :commitStrategy|| fileContents |\x0a\x09\x09\x09fileContents := (commitStrategy key new exportPackage: aPackage name).\x0a\x09\x09\x09self ajaxPutAt: commitStrategy value data: fileContents ]\x0a\x09\x09displayingProgress: 'Committing package ', aPackage name",
  801. messageSends: ["do:displayingProgress:", "exportPackage:", "name", "new", "key", "ajaxPutAt:data:", "value", ",", "->", "commitPathJs", "commitPathSt"],
  802. referencedClasses: ["Exporter", "StrippedExporter", "ChunkExporter"]
  803. }),
  804. smalltalk.PackageHandler);
  805. smalltalk.addMethod(
  806. smalltalk.method({
  807. selector: "loadPackage:prefix:",
  808. category: 'loading',
  809. fn: function (packageName,aString){
  810. var self=this;
  811. var url;
  812. return smalltalk.withContext(function($ctx1) {
  813. var $1;
  814. url=_st(_st(_st("/".__comma(aString)).__comma("/js/")).__comma(packageName)).__comma(".js");
  815. _st(jQuery)._ajax_options_(url,smalltalk.HashedCollection._from_(["type".__minus_gt("GET"),"dataType".__minus_gt("script"),"complete".__minus_gt((function(jqXHR,textStatus){
  816. return smalltalk.withContext(function($ctx2) {
  817. $1=_st(_st(jqXHR)._readyState()).__eq((4));
  818. if(smalltalk.assert($1)){
  819. return self._setupPackageNamed_prefix_(packageName,aString);
  820. };
  821. }, function($ctx2) {$ctx2.fillBlock({jqXHR:jqXHR,textStatus:textStatus},$ctx1)})})),"error".__minus_gt((function(){
  822. return smalltalk.withContext(function($ctx2) {
  823. return _st(window)._alert_("Could not load package at: ".__comma(url));
  824. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))]));
  825. return self}, function($ctx1) {$ctx1.fill(self,"loadPackage:prefix:",{packageName:packageName,aString:aString,url:url},smalltalk.PackageHandler)})},
  826. args: ["packageName", "aString"],
  827. source: "loadPackage: packageName prefix: aString\x0a\x09| url |\x0a\x09url := '/', aString, '/js/', packageName, '.js'.\x0a\x09jQuery\x0a\x09\x09ajax: url\x0a\x09\x09options: #{\x0a\x09\x09\x09'type' -> 'GET'.\x0a\x09\x09\x09'dataType' -> 'script'.\x0a\x09\x09\x09'complete' -> [ :jqXHR :textStatus |\x0a\x09\x09\x09\x09jqXHR readyState = 4\x0a\x09\x09\x09\x09\x09ifTrue: [ self setupPackageNamed: packageName prefix: aString ] ].\x0a\x09\x09\x09'error' -> [ window alert: 'Could not load package at: ', url ]\x0a\x09\x09}",
  828. messageSends: [",", "ajax:options:", "->", "ifTrue:", "setupPackageNamed:prefix:", "=", "readyState", "alert:"],
  829. referencedClasses: []
  830. }),
  831. smalltalk.PackageHandler);
  832. smalltalk.addMethod(
  833. smalltalk.method({
  834. selector: "loadPackages:prefix:",
  835. category: 'loading',
  836. fn: function (aCollection,aString){
  837. var self=this;
  838. return smalltalk.withContext(function($ctx1) {
  839. _st(aCollection)._do_((function(each){
  840. return smalltalk.withContext(function($ctx2) {
  841. return self._loadPackage_prefix_(each,aString);
  842. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  843. return self}, function($ctx1) {$ctx1.fill(self,"loadPackages:prefix:",{aCollection:aCollection,aString:aString},smalltalk.PackageHandler)})},
  844. args: ["aCollection", "aString"],
  845. source: "loadPackages: aCollection prefix: aString\x0a\x09aCollection do: [ :each |\x0a\x09\x09self loadPackage: each prefix: aString ]",
  846. messageSends: ["do:", "loadPackage:prefix:"],
  847. referencedClasses: []
  848. }),
  849. smalltalk.PackageHandler);
  850. smalltalk.addMethod(
  851. smalltalk.method({
  852. selector: "setupPackageNamed:prefix:",
  853. category: 'private',
  854. fn: function (packageName,aString){
  855. var self=this;
  856. function $Package(){return smalltalk.Package||(typeof Package=="undefined"?nil:Package)}
  857. return smalltalk.withContext(function($ctx1) {
  858. var $1,$2;
  859. $1=_st($Package())._named_(packageName);
  860. _st($1)._setupClasses();
  861. _st($1)._commitPathJs_(_st("/".__comma(aString)).__comma("/js"));
  862. $2=_st($1)._commitPathSt_(_st("/".__comma(aString)).__comma("/st"));
  863. return self}, function($ctx1) {$ctx1.fill(self,"setupPackageNamed:prefix:",{packageName:packageName,aString:aString},smalltalk.PackageHandler)})},
  864. args: ["packageName", "aString"],
  865. source: "setupPackageNamed: packageName prefix: aString\x0a\x0a\x09(Package named: packageName)\x0a\x09\x09setupClasses;\x0a\x09\x09commitPathJs: '/', aString, '/js';\x0a\x09\x09commitPathSt: '/', aString, '/st'",
  866. messageSends: ["setupClasses", "named:", "commitPathJs:", ",", "commitPathSt:"],
  867. referencedClasses: ["Package"]
  868. }),
  869. smalltalk.PackageHandler);
  870. smalltalk.addMethod(
  871. smalltalk.method({
  872. selector: "loadPackages:prefix:",
  873. category: 'loading',
  874. fn: function (aCollection,aString){
  875. var self=this;
  876. return smalltalk.withContext(function($ctx1) {
  877. var $1;
  878. $1=_st(self._new())._loadPackages_prefix_(aCollection,aString);
  879. return $1;
  880. }, function($ctx1) {$ctx1.fill(self,"loadPackages:prefix:",{aCollection:aCollection,aString:aString},smalltalk.PackageHandler.klass)})},
  881. args: ["aCollection", "aString"],
  882. source: "loadPackages: aCollection prefix: aString\x0a\x09^ self new loadPackages: aCollection prefix: aString",
  883. messageSends: ["loadPackages:prefix:", "new"],
  884. referencedClasses: []
  885. }),
  886. smalltalk.PackageHandler.klass);
  887. smalltalk.addMethod(
  888. smalltalk.method({
  889. selector: "commit",
  890. category: '*Importer-Exporter',
  891. fn: function (){
  892. var self=this;
  893. function $PackageHandler(){return smalltalk.PackageHandler||(typeof PackageHandler=="undefined"?nil:PackageHandler)}
  894. return smalltalk.withContext(function($ctx1) {
  895. var $1;
  896. $1=_st(_st($PackageHandler())._new())._commit_(self);
  897. return $1;
  898. }, function($ctx1) {$ctx1.fill(self,"commit",{},smalltalk.Package)})},
  899. args: [],
  900. source: "commit\x0a\x09^ PackageHandler new commit: self",
  901. messageSends: ["commit:", "new"],
  902. referencedClasses: ["PackageHandler"]
  903. }),
  904. smalltalk.Package);