Importer-Exporter.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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=_st("")._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(_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_(_st(self)._exportPackage_(_st(pkg)._name()));
  121. }, function($ctx3) {$ctx3.fillBlock({pkg:pkg},$ctx1)})}));
  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. _st(self)._exportDefinitionOf_on_(aClass,stream);
  143. _st(self)._exportMethodsOf_on_(aClass,stream);
  144. _st(self)._exportMetaDefinitionOf_on_(aClass,stream);
  145. return _st(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(_st("'").__comma(_st(self)._classNameFor_(aClass))).__comma("', "));
  166. _st($1)._nextPutAll_(_st("smalltalk.").__comma(_st(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(_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_(_st(self)._classNameFor_(aClass));
  185. _st($6)._nextPutAll_(".comment=");
  186. $7=_st($6)._nextPutAll_(_st(_st(aClass)._comment())._asJavascript());
  187. $7;
  188. };
  189. _st(aStream)._lf();
  190. return self}, function($ctx1) {$ctx1.fill(self,"exportDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  191. args: ["aClass", "aStream"],
  192. 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\x09aStream lf",
  193. messageSends: ["nextPutAll:", ",", "classNameFor:", "superclass", "do:separatedBy:", "instanceVariableNames", "category", "ifTrue:", "lf", "asJavascript", "comment", "notEmpty"],
  194. referencedClasses: []
  195. }),
  196. smalltalk.Exporter);
  197. smalltalk.addMethod(
  198. smalltalk.method({
  199. selector: "exportMetaDefinitionOf:on:",
  200. category: 'private',
  201. fn: function (aClass,aStream){
  202. var self=this;
  203. function $String(){return smalltalk.String||(typeof String=="undefined"?nil:String)}
  204. return smalltalk.withContext(function($ctx1) {
  205. var $1,$2,$3;
  206. $1=_st(_st(_st(aClass)._class())._instanceVariableNames())._isEmpty();
  207. if(! smalltalk.assert($1)){
  208. $2=aStream;
  209. _st($2)._nextPutAll_(_st("smalltalk.").__comma(_st(self)._classNameFor_(_st(aClass)._class())));
  210. $3=_st($2)._nextPutAll_(".iVarNames = [");
  211. $3;
  212. _st(_st(_st(aClass)._class())._instanceVariableNames())._do_separatedBy_((function(each){
  213. return smalltalk.withContext(function($ctx2) {
  214. return _st(aStream)._nextPutAll_(_st(_st("'").__comma(each)).__comma("'"));
  215. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  216. return smalltalk.withContext(function($ctx2) {
  217. return _st(aStream)._nextPutAll_(",");
  218. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  219. _st(aStream)._nextPutAll_(_st("];").__comma(_st($String())._lf()));
  220. };
  221. return self}, function($ctx1) {$ctx1.fill(self,"exportMetaDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  222. args: ["aClass", "aStream"],
  223. 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]",
  224. messageSends: ["ifFalse:", "nextPutAll:", ",", "classNameFor:", "class", "do:separatedBy:", "instanceVariableNames", "lf", "isEmpty"],
  225. referencedClasses: ["String"]
  226. }),
  227. smalltalk.Exporter);
  228. smalltalk.addMethod(
  229. smalltalk.method({
  230. selector: "exportMethod:of:on:",
  231. category: 'private',
  232. fn: function (aMethod,aClass,aStream){
  233. var self=this;
  234. return smalltalk.withContext(function($ctx1) {
  235. var $1,$2,$3,$4;
  236. $1=aStream;
  237. _st($1)._nextPutAll_("smalltalk.addMethod(");
  238. _st($1)._lf();
  239. _st($1)._nextPutAll_("smalltalk.method({");
  240. _st($1)._lf();
  241. _st($1)._nextPutAll_(_st(_st("selector: ").__comma(_st(_st(aMethod)._selector())._asJavascript())).__comma(","));
  242. _st($1)._lf();
  243. _st($1)._nextPutAll_(_st(_st("category: '").__comma(_st(aMethod)._category())).__comma("',"));
  244. _st($1)._lf();
  245. _st($1)._nextPutAll_(_st(_st("fn: ").__comma(_st(_st(aMethod)._fn())._compiledSource())).__comma(","));
  246. _st($1)._lf();
  247. _st($1)._nextPutAll_(_st(_st("args: ").__comma(_st(_st(aMethod)._arguments())._asJavascript())).__comma(","));
  248. _st($1)._lf();
  249. _st($1)._nextPutAll_(_st(_st("source: ").__comma(_st(_st(aMethod)._source())._asJavascript())).__comma(","));
  250. _st($1)._lf();
  251. _st($1)._nextPutAll_(_st(_st("messageSends: ").__comma(_st(_st(aMethod)._messageSends())._asJavascript())).__comma(","));
  252. _st($1)._lf();
  253. $2=_st($1)._nextPutAll_(_st("referencedClasses: ").__comma(_st(_st(aMethod)._referencedClasses())._asJavascript()));
  254. $3=aStream;
  255. _st($3)._lf();
  256. _st($3)._nextPutAll_("}),");
  257. _st($3)._lf();
  258. _st($3)._nextPutAll_(_st("smalltalk.").__comma(_st(self)._classNameFor_(aClass)));
  259. _st($3)._nextPutAll_(");");
  260. _st($3)._lf();
  261. $4=_st($3)._lf();
  262. return self}, function($ctx1) {$ctx1.fill(self,"exportMethod:of:on:",{aMethod:aMethod,aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  263. args: ["aMethod", "aClass", "aStream"],
  264. 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",
  265. messageSends: ["nextPutAll:", "lf", ",", "asJavascript", "selector", "category", "compiledSource", "fn", "arguments", "source", "messageSends", "referencedClasses", "classNameFor:"],
  266. referencedClasses: []
  267. }),
  268. smalltalk.Exporter);
  269. smalltalk.addMethod(
  270. smalltalk.method({
  271. selector: "exportMethodsOf:on:",
  272. category: 'private',
  273. fn: function (aClass,aStream){
  274. var self=this;
  275. return smalltalk.withContext(function($ctx1) {
  276. var $1;
  277. _st(_st(_st(_st(aClass)._methodDictionary())._values())._sorted_((function(a,b){
  278. return smalltalk.withContext(function($ctx2) {
  279. return _st(_st(a)._selector()).__lt_eq(_st(b)._selector());
  280. }, function($ctx2) {$ctx2.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(each){
  281. return smalltalk.withContext(function($ctx2) {
  282. $1=_st(_st(each)._category())._match_("^\x5c*");
  283. if(! smalltalk.assert($1)){
  284. return _st(self)._exportMethod_of_on_(each,aClass,aStream);
  285. };
  286. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  287. _st(aStream)._lf();
  288. return self}, function($ctx1) {$ctx1.fill(self,"exportMethodsOf:on:",{aClass:aClass,aStream:aStream},smalltalk.Exporter)})},
  289. args: ["aClass", "aStream"],
  290. 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",
  291. messageSends: ["do:", "ifFalse:", "exportMethod:of:on:", "match:", "category", "sorted:", "<=", "selector", "values", "methodDictionary", "lf"],
  292. referencedClasses: []
  293. }),
  294. smalltalk.Exporter);
  295. smalltalk.addMethod(
  296. smalltalk.method({
  297. selector: "exportPackage:",
  298. category: 'fileOut',
  299. fn: function (packageName){
  300. var self=this;
  301. var package_;
  302. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  303. function $String(){return smalltalk.String||(typeof String=="undefined"?nil:String)}
  304. return smalltalk.withContext(function($ctx1) {
  305. var $1;
  306. $1=_st($String())._streamContents_((function(stream){
  307. return smalltalk.withContext(function($ctx2) {
  308. package_=_st(_st($Smalltalk())._current())._packageAt_(packageName);
  309. package_;
  310. _st(self)._exportPackageDefinitionOf_on_(package_,stream);
  311. _st(_st(_st(package_)._sortedClasses())._asSet())._do_((function(each){
  312. return smalltalk.withContext(function($ctx3) {
  313. return _st(stream)._nextPutAll_(_st(self)._exportClass_(each));
  314. }, function($ctx3) {$ctx3.fillBlock({each:each},$ctx1)})}));
  315. return _st(self)._exportPackageExtensionsOf_on_(package_,stream);
  316. }, function($ctx2) {$ctx2.fillBlock({stream:stream},$ctx1)})}));
  317. return $1;
  318. }, function($ctx1) {$ctx1.fill(self,"exportPackage:",{packageName:packageName,package_:package_},smalltalk.Exporter)})},
  319. args: ["packageName"],
  320. 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]",
  321. messageSends: ["streamContents:", "packageAt:", "current", "exportPackageDefinitionOf:on:", "do:", "nextPutAll:", "exportClass:", "asSet", "sortedClasses", "exportPackageExtensionsOf:on:"],
  322. referencedClasses: ["Smalltalk", "String"]
  323. }),
  324. smalltalk.Exporter);
  325. smalltalk.addMethod(
  326. smalltalk.method({
  327. selector: "exportPackageDefinitionOf:on:",
  328. category: 'private',
  329. fn: function (package_,aStream){
  330. var self=this;
  331. return smalltalk.withContext(function($ctx1) {
  332. var $1,$2;
  333. $1=aStream;
  334. _st($1)._nextPutAll_("smalltalk.addPackage(");
  335. _st($1)._nextPutAll_(_st(_st("'").__comma(_st(package_)._name())).__comma("');"));
  336. $2=_st($1)._lf();
  337. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageDefinitionOf:on:",{package_:package_,aStream:aStream},smalltalk.Exporter)})},
  338. args: ["package", "aStream"],
  339. source: "exportPackageDefinitionOf: package on: aStream\x0a\x09aStream\x0a\x09\x09nextPutAll: 'smalltalk.addPackage(';\x0a\x09\x09nextPutAll: '''', package name, ''');';\x0a\x09\x09lf",
  340. messageSends: ["nextPutAll:", ",", "name", "lf"],
  341. referencedClasses: []
  342. }),
  343. smalltalk.Exporter);
  344. smalltalk.addMethod(
  345. smalltalk.method({
  346. selector: "exportPackageExtensionsOf:on:",
  347. category: 'private',
  348. fn: function (package_,aStream){
  349. var self=this;
  350. var name;
  351. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  352. function $Package(){return smalltalk.Package||(typeof Package=="undefined"?nil:Package)}
  353. return smalltalk.withContext(function($ctx1) {
  354. var $1;
  355. name=_st(package_)._name();
  356. _st(_st($Package())._sortedClasses_(_st(_st($Smalltalk())._current())._classes()))._do_((function(each){
  357. return smalltalk.withContext(function($ctx2) {
  358. return _st([each,_st(each)._class()])._do_((function(aClass){
  359. return smalltalk.withContext(function($ctx3) {
  360. return _st(_st(_st(_st(aClass)._methodDictionary())._values())._sorted_((function(a,b){
  361. return smalltalk.withContext(function($ctx4) {
  362. return _st(_st(a)._selector()).__lt_eq(_st(b)._selector());
  363. }, function($ctx4) {$ctx4.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(method){
  364. return smalltalk.withContext(function($ctx4) {
  365. $1=_st(_st(method)._category())._match_(_st("^\x5c*").__comma(name));
  366. if(smalltalk.assert($1)){
  367. return _st(self)._exportMethod_of_on_(method,aClass,aStream);
  368. };
  369. }, function($ctx4) {$ctx4.fillBlock({method:method},$ctx1)})}));
  370. }, function($ctx3) {$ctx3.fillBlock({aClass:aClass},$ctx1)})}));
  371. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  372. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageExtensionsOf:on:",{package_:package_,aStream:aStream,name:name},smalltalk.Exporter)})},
  373. args: ["package", "aStream"],
  374. 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 ]]]]",
  375. messageSends: ["name", "do:", "ifTrue:", "exportMethod:of:on:", "match:", ",", "category", "sorted:", "<=", "selector", "values", "methodDictionary", "class", "sortedClasses:", "classes", "current"],
  376. referencedClasses: ["Smalltalk", "Package"]
  377. }),
  378. smalltalk.Exporter);
  379. smalltalk.addClass('ChunkExporter', smalltalk.Exporter, [], 'Importer-Exporter');
  380. 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."
  381. smalltalk.addMethod(
  382. smalltalk.method({
  383. selector: "chunkEscape:",
  384. category: 'private',
  385. fn: function (aString){
  386. var self=this;
  387. return smalltalk.withContext(function($ctx1) {
  388. var $1;
  389. $1=_st(_st(aString)._replace_with_("!","!!"))._trimBoth();
  390. return $1;
  391. }, function($ctx1) {$ctx1.fill(self,"chunkEscape:",{aString:aString},smalltalk.ChunkExporter)})},
  392. args: ["aString"],
  393. source: "chunkEscape: aString\x0a\x09\x22Replace all occurrences of ! with !! and trim at both ends.\x22\x0a\x0a\x09^(aString replace: '!' with: '!!') trimBoth",
  394. messageSends: ["trimBoth", "replace:with:"],
  395. referencedClasses: []
  396. }),
  397. smalltalk.ChunkExporter);
  398. smalltalk.addMethod(
  399. smalltalk.method({
  400. selector: "classNameFor:",
  401. category: 'private',
  402. fn: function (aClass){
  403. var self=this;
  404. return smalltalk.withContext(function($ctx1) {
  405. var $2,$3,$1;
  406. $2=_st(aClass)._isMetaclass();
  407. if(smalltalk.assert($2)){
  408. $1=_st(_st(_st(aClass)._instanceClass())._name()).__comma(" class");
  409. } else {
  410. $3=_st(aClass)._isNil();
  411. if(smalltalk.assert($3)){
  412. $1="nil";
  413. } else {
  414. $1=_st(aClass)._name();
  415. };
  416. };
  417. return $1;
  418. }, function($ctx1) {$ctx1.fill(self,"classNameFor:",{aClass:aClass},smalltalk.ChunkExporter)})},
  419. args: ["aClass"],
  420. 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]]",
  421. messageSends: ["ifTrue:ifFalse:", ",", "name", "instanceClass", "isNil", "isMetaclass"],
  422. referencedClasses: []
  423. }),
  424. smalltalk.ChunkExporter);
  425. smalltalk.addMethod(
  426. smalltalk.method({
  427. selector: "exportDefinitionOf:on:",
  428. category: 'private',
  429. fn: function (aClass,aStream){
  430. var self=this;
  431. return smalltalk.withContext(function($ctx1) {
  432. var $1,$2,$3,$4,$5,$6,$7;
  433. $1=aStream;
  434. _st($1)._nextPutAll_(_st(self)._classNameFor_(_st(aClass)._superclass()));
  435. _st($1)._nextPutAll_(_st(" subclass: #").__comma(_st(self)._classNameFor_(aClass)));
  436. _st($1)._lf();
  437. _st($1)._tab();
  438. $2=_st($1)._nextPutAll_("instanceVariableNames: '");
  439. _st(_st(aClass)._instanceVariableNames())._do_separatedBy_((function(each){
  440. return smalltalk.withContext(function($ctx2) {
  441. return _st(aStream)._nextPutAll_(each);
  442. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  443. return smalltalk.withContext(function($ctx2) {
  444. return _st(aStream)._nextPutAll_(" ");
  445. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  446. $3=aStream;
  447. _st($3)._nextPutAll_("'");
  448. _st($3)._lf();
  449. _st($3)._tab();
  450. _st($3)._nextPutAll_(_st(_st("package: '").__comma(_st(aClass)._category())).__comma("'!"));
  451. $4=_st($3)._lf();
  452. $5=_st(_st(aClass)._comment())._notEmpty();
  453. if(smalltalk.assert($5)){
  454. $6=aStream;
  455. _st($6)._nextPutAll_(_st(_st("!").__comma(_st(self)._classNameFor_(aClass))).__comma(" commentStamp!"));
  456. _st($6)._lf();
  457. _st($6)._nextPutAll_(_st(_st(self)._chunkEscape_(_st(aClass)._comment())).__comma("!"));
  458. $7=_st($6)._lf();
  459. $7;
  460. };
  461. _st(aStream)._lf();
  462. return self}, function($ctx1) {$ctx1.fill(self,"exportDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  463. args: ["aClass", "aStream"],
  464. 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",
  465. messageSends: ["nextPutAll:", "classNameFor:", "superclass", ",", "lf", "tab", "do:separatedBy:", "instanceVariableNames", "category", "ifTrue:", "chunkEscape:", "comment", "notEmpty"],
  466. referencedClasses: []
  467. }),
  468. smalltalk.ChunkExporter);
  469. smalltalk.addMethod(
  470. smalltalk.method({
  471. selector: "exportMetaDefinitionOf:on:",
  472. category: 'private',
  473. fn: function (aClass,aStream){
  474. var self=this;
  475. return smalltalk.withContext(function($ctx1) {
  476. var $1,$2,$3,$4,$5;
  477. $1=_st(_st(_st(aClass)._class())._instanceVariableNames())._isEmpty();
  478. if(! smalltalk.assert($1)){
  479. $2=aStream;
  480. _st($2)._nextPutAll_(_st(self)._classNameFor_(_st(aClass)._class()));
  481. $3=_st($2)._nextPutAll_(" instanceVariableNames: '");
  482. $3;
  483. _st(_st(_st(aClass)._class())._instanceVariableNames())._do_separatedBy_((function(each){
  484. return smalltalk.withContext(function($ctx2) {
  485. return _st(aStream)._nextPutAll_(each);
  486. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  487. return smalltalk.withContext(function($ctx2) {
  488. return _st(aStream)._nextPutAll_(" ");
  489. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  490. $4=aStream;
  491. _st($4)._nextPutAll_("'!");
  492. _st($4)._lf();
  493. $5=_st($4)._lf();
  494. $5;
  495. };
  496. return self}, function($ctx1) {$ctx1.fill(self,"exportMetaDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  497. args: ["aClass", "aStream"],
  498. 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]",
  499. messageSends: ["ifFalse:", "nextPutAll:", "classNameFor:", "class", "do:separatedBy:", "instanceVariableNames", "lf", "isEmpty"],
  500. referencedClasses: []
  501. }),
  502. smalltalk.ChunkExporter);
  503. smalltalk.addMethod(
  504. smalltalk.method({
  505. selector: "exportMethod:of:on:",
  506. category: 'private',
  507. fn: function (aMethod,aClass,aStream){
  508. var self=this;
  509. return smalltalk.withContext(function($ctx1) {
  510. var $1,$2;
  511. $1=aStream;
  512. _st($1)._lf();
  513. _st($1)._lf();
  514. _st($1)._nextPutAll_(_st(self)._chunkEscape_(_st(aMethod)._source()));
  515. _st($1)._lf();
  516. $2=_st($1)._nextPutAll_("!");
  517. return self}, function($ctx1) {$ctx1.fill(self,"exportMethod:of:on:",{aMethod:aMethod,aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  518. args: ["aMethod", "aClass", "aStream"],
  519. source: "exportMethod: aMethod of: aClass on: aStream\x0a\x09aStream\x0a\x09\x09lf; lf; nextPutAll: (self chunkEscape: aMethod source); lf;\x0a\x09\x09nextPutAll: '!'",
  520. messageSends: ["lf", "nextPutAll:", "chunkEscape:", "source"],
  521. referencedClasses: []
  522. }),
  523. smalltalk.ChunkExporter);
  524. smalltalk.addMethod(
  525. smalltalk.method({
  526. selector: "exportMethods:category:of:on:",
  527. category: 'private',
  528. fn: function (methods,category,aClass,aStream){
  529. var self=this;
  530. return smalltalk.withContext(function($ctx1) {
  531. var $1,$2,$3,$4;
  532. $1=aStream;
  533. _st($1)._nextPutAll_(_st("!").__comma(_st(self)._classNameFor_(aClass)));
  534. $2=_st($1)._nextPutAll_(_st(_st(" methodsFor: '").__comma(category)).__comma("'!"));
  535. _st(_st(methods)._sorted_((function(a,b){
  536. return smalltalk.withContext(function($ctx2) {
  537. return _st(_st(a)._selector()).__lt_eq(_st(b)._selector());
  538. }, function($ctx2) {$ctx2.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(each){
  539. return smalltalk.withContext(function($ctx2) {
  540. return _st(self)._exportMethod_of_on_(each,aClass,aStream);
  541. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  542. $3=aStream;
  543. _st($3)._nextPutAll_(" !");
  544. _st($3)._lf();
  545. $4=_st($3)._lf();
  546. return self}, function($ctx1) {$ctx1.fill(self,"exportMethods:category:of:on:",{methods:methods,category:category,aClass:aClass,aStream:aStream},smalltalk.ChunkExporter)})},
  547. args: ["methods", "category", "aClass", "aStream"],
  548. 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",
  549. messageSends: ["nextPutAll:", ",", "classNameFor:", "do:", "exportMethod:of:on:", "sorted:", "<=", "selector", "lf"],
  550. referencedClasses: []
  551. }),
  552. smalltalk.ChunkExporter);
  553. smalltalk.addMethod(
  554. smalltalk.method({
  555. selector: "exportMethodsOf:on:",
  556. category: 'private',
  557. fn: function (aClass,aStream){
  558. var self=this;
  559. var map;
  560. function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
  561. return smalltalk.withContext(function($ctx1) {
  562. var $1;
  563. map=_st($Dictionary())._new();
  564. _st(aClass)._protocolsDo_((function(category,methods){
  565. return smalltalk.withContext(function($ctx2) {
  566. $1=_st(category)._match_("^\x5c*");
  567. if(! smalltalk.assert($1)){
  568. return _st(map)._at_put_(category,methods);
  569. };
  570. }, function($ctx2) {$ctx2.fillBlock({category:category,methods:methods},$ctx1)})}));
  571. _st(_st(_st(map)._keys())._sorted_((function(a,b){
  572. return smalltalk.withContext(function($ctx2) {
  573. return _st(a).__lt_eq(b);
  574. }, function($ctx2) {$ctx2.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(category){
  575. var methods;
  576. return smalltalk.withContext(function($ctx2) {
  577. methods=_st(map)._at_(category);
  578. methods;
  579. return _st(self)._exportMethods_category_of_on_(methods,category,aClass,aStream);
  580. }, function($ctx2) {$ctx2.fillBlock({category:category,methods:methods},$ctx1)})}));
  581. return self}, function($ctx1) {$ctx1.fill(self,"exportMethodsOf:on:",{aClass:aClass,aStream:aStream,map:map},smalltalk.ChunkExporter)})},
  582. args: ["aClass", "aStream"],
  583. 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 ]",
  584. messageSends: ["new", "protocolsDo:", "ifFalse:", "at:put:", "match:", "do:", "at:", "exportMethods:category:of:on:", "sorted:", "<=", "keys"],
  585. referencedClasses: ["Dictionary"]
  586. }),
  587. smalltalk.ChunkExporter);
  588. smalltalk.addMethod(
  589. smalltalk.method({
  590. selector: "exportPackageDefinitionOf:on:",
  591. category: 'private',
  592. fn: function (package_,aStream){
  593. var self=this;
  594. return smalltalk.withContext(function($ctx1) {
  595. var $1,$2;
  596. $1=aStream;
  597. _st($1)._nextPutAll_(_st(_st("Smalltalk current createPackage: '").__comma(_st(package_)._name())).__comma("'!"));
  598. $2=_st($1)._lf();
  599. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageDefinitionOf:on:",{package_:package_,aStream:aStream},smalltalk.ChunkExporter)})},
  600. args: ["package", "aStream"],
  601. source: "exportPackageDefinitionOf: package on: aStream\x0a\x09\x22Chunk format.\x22\x0a\x0a\x09aStream\x0a\x09\x09nextPutAll: 'Smalltalk current createPackage: ''', package name, '''!';\x0a\x09\x09lf",
  602. messageSends: ["nextPutAll:", ",", "name", "lf"],
  603. referencedClasses: []
  604. }),
  605. smalltalk.ChunkExporter);
  606. smalltalk.addMethod(
  607. smalltalk.method({
  608. selector: "exportPackageExtensionsOf:on:",
  609. category: 'private',
  610. fn: function (package_,aStream){
  611. var self=this;
  612. var name,map;
  613. function $Dictionary(){return smalltalk.Dictionary||(typeof Dictionary=="undefined"?nil:Dictionary)}
  614. function $Smalltalk(){return smalltalk.Smalltalk||(typeof Smalltalk=="undefined"?nil:Smalltalk)}
  615. function $Package(){return smalltalk.Package||(typeof Package=="undefined"?nil:Package)}
  616. return smalltalk.withContext(function($ctx1) {
  617. var $1;
  618. name=_st(package_)._name();
  619. _st(_st($Package())._sortedClasses_(_st(_st($Smalltalk())._current())._classes()))._do_((function(each){
  620. return smalltalk.withContext(function($ctx2) {
  621. return _st([each,_st(each)._class()])._do_((function(aClass){
  622. return smalltalk.withContext(function($ctx3) {
  623. map=_st($Dictionary())._new();
  624. map;
  625. _st(aClass)._protocolsDo_((function(category,methods){
  626. return smalltalk.withContext(function($ctx4) {
  627. $1=_st(category)._match_(_st("^\x5c*").__comma(name));
  628. if(smalltalk.assert($1)){
  629. return _st(map)._at_put_(category,methods);
  630. };
  631. }, function($ctx4) {$ctx4.fillBlock({category:category,methods:methods},$ctx1)})}));
  632. return _st(_st(_st(map)._keys())._sorted_((function(a,b){
  633. return smalltalk.withContext(function($ctx4) {
  634. return _st(a).__lt_eq(b);
  635. }, function($ctx4) {$ctx4.fillBlock({a:a,b:b},$ctx1)})})))._do_((function(category){
  636. var methods;
  637. return smalltalk.withContext(function($ctx4) {
  638. methods=_st(map)._at_(category);
  639. methods;
  640. return _st(self)._exportMethods_category_of_on_(methods,category,aClass,aStream);
  641. }, function($ctx4) {$ctx4.fillBlock({category:category,methods:methods},$ctx1)})}));
  642. }, function($ctx3) {$ctx3.fillBlock({aClass:aClass},$ctx1)})}));
  643. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  644. return self}, function($ctx1) {$ctx1.fill(self,"exportPackageExtensionsOf:on:",{package_:package_,aStream:aStream,name:name,map:map},smalltalk.ChunkExporter)})},
  645. args: ["package", "aStream"],
  646. 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 ]]]",
  647. messageSends: ["name", "do:", "new", "protocolsDo:", "ifTrue:", "at:put:", "match:", ",", "at:", "exportMethods:category:of:on:", "sorted:", "<=", "keys", "class", "sortedClasses:", "classes", "current"],
  648. referencedClasses: ["Dictionary", "Smalltalk", "Package"]
  649. }),
  650. smalltalk.ChunkExporter);
  651. smalltalk.addClass('StrippedExporter', smalltalk.Exporter, [], 'Importer-Exporter');
  652. smalltalk.StrippedExporter.comment="I export Amber code into a JavaScript string, but without any optional associated data like the Amber source code."
  653. smalltalk.addMethod(
  654. smalltalk.method({
  655. selector: "exportDefinitionOf:on:",
  656. category: 'private',
  657. fn: function (aClass,aStream){
  658. var self=this;
  659. return smalltalk.withContext(function($ctx1) {
  660. var $1,$2,$3,$4;
  661. $1=aStream;
  662. _st($1)._nextPutAll_("smalltalk.addClass(");
  663. _st($1)._nextPutAll_(_st(_st("'").__comma(_st(self)._classNameFor_(aClass))).__comma("', "));
  664. _st($1)._nextPutAll_(_st("smalltalk.").__comma(_st(self)._classNameFor_(_st(aClass)._superclass())));
  665. $2=_st($1)._nextPutAll_(", [");
  666. _st(_st(aClass)._instanceVariableNames())._do_separatedBy_((function(each){
  667. return smalltalk.withContext(function($ctx2) {
  668. return _st(aStream)._nextPutAll_(_st(_st("'").__comma(each)).__comma("'"));
  669. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}),(function(){
  670. return smalltalk.withContext(function($ctx2) {
  671. return _st(aStream)._nextPutAll_(", ");
  672. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  673. $3=aStream;
  674. _st($3)._nextPutAll_("], '");
  675. _st($3)._nextPutAll_(_st(_st(aClass)._category()).__comma("'"));
  676. $4=_st($3)._nextPutAll_(");");
  677. _st(aStream)._lf();
  678. return self}, function($ctx1) {$ctx1.fill(self,"exportDefinitionOf:on:",{aClass:aClass,aStream:aStream},smalltalk.StrippedExporter)})},
  679. args: ["aClass", "aStream"],
  680. 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",
  681. messageSends: ["nextPutAll:", ",", "classNameFor:", "superclass", "do:separatedBy:", "instanceVariableNames", "category", "lf"],
  682. referencedClasses: []
  683. }),
  684. smalltalk.StrippedExporter);
  685. smalltalk.addMethod(
  686. smalltalk.method({
  687. selector: "exportMethod:of:on:",
  688. category: 'private',
  689. fn: function (aMethod,aClass,aStream){
  690. var self=this;
  691. return smalltalk.withContext(function($ctx1) {
  692. var $1,$2;
  693. $1=aStream;
  694. _st($1)._nextPutAll_("smalltalk.addMethod(");
  695. _st($1)._lf();
  696. _st($1)._nextPutAll_("smalltalk.method({");
  697. _st($1)._lf();
  698. _st($1)._nextPutAll_(_st(_st("selector: ").__comma(_st(_st(aMethod)._selector())._asJavascript())).__comma(","));
  699. _st($1)._lf();
  700. _st($1)._nextPutAll_(_st(_st("fn: ").__comma(_st(_st(aMethod)._fn())._compiledSource())).__comma(","));
  701. _st($1)._lf();
  702. _st($1)._nextPutAll_(_st("messageSends: ").__comma(_st(_st(aMethod)._messageSends())._asJavascript()));
  703. _st($1)._nextPutAll_("}),");
  704. _st($1)._lf();
  705. _st($1)._nextPutAll_(_st("smalltalk.").__comma(_st(self)._classNameFor_(aClass)));
  706. _st($1)._nextPutAll_(");");
  707. _st($1)._lf();
  708. $2=_st($1)._lf();
  709. return self}, function($ctx1) {$ctx1.fill(self,"exportMethod:of:on:",{aMethod:aMethod,aClass:aClass,aStream:aStream},smalltalk.StrippedExporter)})},
  710. args: ["aMethod", "aClass", "aStream"],
  711. 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",
  712. messageSends: ["nextPutAll:", "lf", ",", "asJavascript", "selector", "compiledSource", "fn", "messageSends", "classNameFor:"],
  713. referencedClasses: []
  714. }),
  715. smalltalk.StrippedExporter);
  716. smalltalk.addClass('Importer', smalltalk.Object, [], 'Importer-Exporter');
  717. smalltalk.Importer.comment="I can import Amber code from a string in the chunk format.\x0a\x0a## API\x0a\x0a Importer new import: aString"
  718. smalltalk.addMethod(
  719. smalltalk.method({
  720. selector: "import:",
  721. category: 'fileIn',
  722. fn: function (aStream){
  723. var self=this;
  724. var chunk,result,parser,lastEmpty;
  725. function $ChunkParser(){return smalltalk.ChunkParser||(typeof ChunkParser=="undefined"?nil:ChunkParser)}
  726. function $Compiler(){return smalltalk.Compiler||(typeof Compiler=="undefined"?nil:Compiler)}
  727. return smalltalk.withContext(function($ctx1) {
  728. var $1,$2;
  729. parser=_st($ChunkParser())._on_(aStream);
  730. lastEmpty=false;
  731. _st((function(){
  732. return smalltalk.withContext(function($ctx2) {
  733. chunk=_st(parser)._nextChunk();
  734. chunk;
  735. return _st(chunk)._isNil();
  736. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))._whileFalse_((function(){
  737. return smalltalk.withContext(function($ctx2) {
  738. $1=_st(chunk)._isEmpty();
  739. if(smalltalk.assert($1)){
  740. lastEmpty=true;
  741. return lastEmpty;
  742. } else {
  743. result=_st(_st($Compiler())._new())._evaluateExpression_(chunk);
  744. result;
  745. $2=lastEmpty;
  746. if(smalltalk.assert($2)){
  747. lastEmpty=false;
  748. lastEmpty;
  749. return _st(result)._scanFrom_(parser);
  750. };
  751. };
  752. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
  753. return self}, function($ctx1) {$ctx1.fill(self,"import:",{aStream:aStream,chunk:chunk,result:result,parser:parser,lastEmpty:lastEmpty},smalltalk.Importer)})},
  754. args: ["aStream"],
  755. 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]]]",
  756. messageSends: ["on:", "whileFalse:", "ifTrue:ifFalse:", "evaluateExpression:", "new", "ifTrue:", "scanFrom:", "isEmpty", "nextChunk", "isNil"],
  757. referencedClasses: ["ChunkParser", "Compiler"]
  758. }),
  759. smalltalk.Importer);
  760. smalltalk.addClass('PackageHandler', smalltalk.Object, [], 'Importer-Exporter');
  761. 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."
  762. smalltalk.addMethod(
  763. smalltalk.method({
  764. selector: "ajaxPutAt:data:",
  765. category: 'private',
  766. fn: function (aURL,aString){
  767. var self=this;
  768. return smalltalk.withContext(function($ctx1) {
  769. _st(jQuery)._ajax_options_(aURL,smalltalk.HashedCollection._fromPairs_([_st("type").__minus_gt("PUT"),_st("data").__minus_gt(aString),_st("contentType").__minus_gt("text/plain;charset=UTF-8"),_st("error").__minus_gt((function(xhr){
  770. return smalltalk.withContext(function($ctx2) {
  771. return _st(self)._error_(_st(_st(_st(_st("Commiting ").__comma(aURL)).__comma(" failed with reason: \x22")).__comma(_st(xhr)._responseText())).__comma("\x22"));
  772. }, function($ctx2) {$ctx2.fillBlock({xhr:xhr},$ctx1)})}))]));
  773. return self}, function($ctx1) {$ctx1.fill(self,"ajaxPutAt:data:",{aURL:aURL,aString:aString},smalltalk.PackageHandler)})},
  774. args: ["aURL", "aString"],
  775. 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'] }",
  776. messageSends: ["ajax:options:", "->", "error:", ",", "responseText"],
  777. referencedClasses: []
  778. }),
  779. smalltalk.PackageHandler);
  780. smalltalk.addMethod(
  781. smalltalk.method({
  782. selector: "commit:",
  783. category: 'committing',
  784. fn: function (aPackage){
  785. var self=this;
  786. function $Exporter(){return smalltalk.Exporter||(typeof Exporter=="undefined"?nil:Exporter)}
  787. function $StrippedExporter(){return smalltalk.StrippedExporter||(typeof StrippedExporter=="undefined"?nil:StrippedExporter)}
  788. function $ChunkExporter(){return smalltalk.ChunkExporter||(typeof ChunkExporter=="undefined"?nil:ChunkExporter)}
  789. return smalltalk.withContext(function($ctx1) {
  790. _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){
  791. var fileContents;
  792. return smalltalk.withContext(function($ctx2) {
  793. fileContents=_st(_st(_st(commitStrategy)._key())._new())._exportPackage_(_st(aPackage)._name());
  794. fileContents;
  795. return _st(self)._ajaxPutAt_data_(_st(commitStrategy)._value(),fileContents);
  796. }, function($ctx2) {$ctx2.fillBlock({commitStrategy:commitStrategy,fileContents:fileContents},$ctx1)})}),_st("Committing package ").__comma(_st(aPackage)._name()));
  797. return self}, function($ctx1) {$ctx1.fill(self,"commit:",{aPackage:aPackage},smalltalk.PackageHandler)})},
  798. args: ["aPackage"],
  799. 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",
  800. messageSends: ["do:displayingProgress:", "exportPackage:", "name", "new", "key", "ajaxPutAt:data:", "value", ",", "->", "commitPathJs", "commitPathSt"],
  801. referencedClasses: ["Exporter", "StrippedExporter", "ChunkExporter"]
  802. }),
  803. smalltalk.PackageHandler);
  804. smalltalk.addMethod(
  805. smalltalk.method({
  806. selector: "loadPackage:prefix:",
  807. category: 'loading',
  808. fn: function (packageName,aString){
  809. var self=this;
  810. var url;
  811. return smalltalk.withContext(function($ctx1) {
  812. var $1;
  813. url=_st(_st(_st(_st("/").__comma(aString)).__comma("/js/")).__comma(packageName)).__comma(".js");
  814. _st(jQuery)._ajax_options_(url,smalltalk.HashedCollection._fromPairs_([_st("type").__minus_gt("GET"),_st("dataType").__minus_gt("script"),_st("complete").__minus_gt((function(jqXHR,textStatus){
  815. return smalltalk.withContext(function($ctx2) {
  816. $1=_st(_st(jqXHR)._readyState()).__eq((4));
  817. if(smalltalk.assert($1)){
  818. return _st(self)._setupPackageNamed_prefix_(packageName,aString);
  819. };
  820. }, function($ctx2) {$ctx2.fillBlock({jqXHR:jqXHR,textStatus:textStatus},$ctx1)})})),_st("error").__minus_gt((function(){
  821. return smalltalk.withContext(function($ctx2) {
  822. return _st(window)._alert_(_st("Could not load package at: ").__comma(url));
  823. }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}))]));
  824. return self}, function($ctx1) {$ctx1.fill(self,"loadPackage:prefix:",{packageName:packageName,aString:aString,url:url},smalltalk.PackageHandler)})},
  825. args: ["packageName", "aString"],
  826. 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}",
  827. messageSends: [",", "ajax:options:", "->", "ifTrue:", "setupPackageNamed:prefix:", "=", "readyState", "alert:"],
  828. referencedClasses: []
  829. }),
  830. smalltalk.PackageHandler);
  831. smalltalk.addMethod(
  832. smalltalk.method({
  833. selector: "loadPackages:prefix:",
  834. category: 'loading',
  835. fn: function (aCollection,aString){
  836. var self=this;
  837. return smalltalk.withContext(function($ctx1) {
  838. _st(aCollection)._do_((function(each){
  839. return smalltalk.withContext(function($ctx2) {
  840. return _st(self)._loadPackage_prefix_(each,aString);
  841. }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1)})}));
  842. return self}, function($ctx1) {$ctx1.fill(self,"loadPackages:prefix:",{aCollection:aCollection,aString:aString},smalltalk.PackageHandler)})},
  843. args: ["aCollection", "aString"],
  844. source: "loadPackages: aCollection prefix: aString\x0a\x09aCollection do: [ :each |\x0a\x09\x09self loadPackage: each prefix: aString ]",
  845. messageSends: ["do:", "loadPackage:prefix:"],
  846. referencedClasses: []
  847. }),
  848. smalltalk.PackageHandler);
  849. smalltalk.addMethod(
  850. smalltalk.method({
  851. selector: "setupPackageNamed:prefix:",
  852. category: 'private',
  853. fn: function (packageName,aString){
  854. var self=this;
  855. function $Package(){return smalltalk.Package||(typeof Package=="undefined"?nil:Package)}
  856. return smalltalk.withContext(function($ctx1) {
  857. var $1,$2;
  858. $1=_st($Package())._named_(packageName);
  859. _st($1)._setupClasses();
  860. _st($1)._commitPathJs_(_st(_st("/").__comma(aString)).__comma("/js"));
  861. $2=_st($1)._commitPathSt_(_st(_st("/").__comma(aString)).__comma("/st"));
  862. return self}, function($ctx1) {$ctx1.fill(self,"setupPackageNamed:prefix:",{packageName:packageName,aString:aString},smalltalk.PackageHandler)})},
  863. args: ["packageName", "aString"],
  864. source: "setupPackageNamed: packageName prefix: aString\x0a\x0a\x09(Package named: packageName)\x0a\x09\x09setupClasses;\x0a\x09\x09commitPathJs: '/', aString, '/js';\x0a\x09\x09commitPathSt: '/', aString, '/st'",
  865. messageSends: ["setupClasses", "named:", "commitPathJs:", ",", "commitPathSt:"],
  866. referencedClasses: ["Package"]
  867. }),
  868. smalltalk.PackageHandler);
  869. smalltalk.addMethod(
  870. smalltalk.method({
  871. selector: "loadPackages:prefix:",
  872. category: 'loading',
  873. fn: function (aCollection,aString){
  874. var self=this;
  875. return smalltalk.withContext(function($ctx1) {
  876. var $1;
  877. $1=_st(_st(self)._new())._loadPackages_prefix_(aCollection,aString);
  878. return $1;
  879. }, function($ctx1) {$ctx1.fill(self,"loadPackages:prefix:",{aCollection:aCollection,aString:aString},smalltalk.PackageHandler.klass)})},
  880. args: ["aCollection", "aString"],
  881. source: "loadPackages: aCollection prefix: aString\x0a\x09^ self new loadPackages: aCollection prefix: aString",
  882. messageSends: ["loadPackages:prefix:", "new"],
  883. referencedClasses: []
  884. }),
  885. smalltalk.PackageHandler.klass);
  886. smalltalk.addMethod(
  887. smalltalk.method({
  888. selector: "commit",
  889. category: '*Importer-Exporter',
  890. fn: function (){
  891. var self=this;
  892. function $PackageHandler(){return smalltalk.PackageHandler||(typeof PackageHandler=="undefined"?nil:PackageHandler)}
  893. return smalltalk.withContext(function($ctx1) {
  894. var $1;
  895. $1=_st(_st($PackageHandler())._new())._commit_(self);
  896. return $1;
  897. }, function($ctx1) {$ctx1.fill(self,"commit",{},smalltalk.Package)})},
  898. args: [],
  899. source: "commit\x0a\x09^ PackageHandler new commit: self",
  900. messageSends: ["commit:", "new"],
  901. referencedClasses: ["PackageHandler"]
  902. }),
  903. smalltalk.Package);