2
0

amberc.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /**
  2. * This is a "compiler" for Amber code.
  3. * Put the following code into compiler.js:
  4. * var amberc = require('amberc');
  5. * var compiler = new amberc.Compiler('path/to/amber');
  6. * var options = amberc.createDefaults();
  7. * // edit options entries
  8. * compiler.main(options);
  9. *
  10. * Execute 'node compiler.js' without arguments or with -h / --help for help.
  11. */
  12. /**
  13. * Map the async filter function onto array and evaluate callback, once all have finished.
  14. * Taken from: http://howtonode.org/control-flow-part-iii
  15. */
  16. function async_map(array, filter, callback) {
  17. if (0 === array.length) {
  18. callback(null, null);
  19. return;
  20. }
  21. var counter = array.length;
  22. var new_array = [];
  23. array.forEach(function (item, index) {
  24. filter(item, function (err, result) {
  25. if (err) { callback(err); return; }
  26. new_array[index] = result;
  27. counter--;
  28. if (counter === 0) {
  29. callback(null, new_array);
  30. }
  31. });
  32. });
  33. }
  34. /**
  35. * Always evaluates the callback parameter.
  36. * Used by Combo blocks to always call the next function,
  37. * even if all of the other functions did not run.
  38. */
  39. function always_resolve(callback) {
  40. callback();
  41. }
  42. /**
  43. * Helper for concatenating Amber generated AMD modules.
  44. * The produced output can be exported and run as an independent program.
  45. *
  46. * var concatenator = createConcatenator();
  47. * concatenator.start(); // write the required AMD define header
  48. * concatenator.add(module1);
  49. * concatenator.addId(module1_ID);
  50. * //...
  51. * concatenator.finish("//some last code");
  52. * var concatenation = concatenator.toString();
  53. * // The variable concatenation contains the concatenated result
  54. * // which can either be stored in a file or interpreted with eval().
  55. */
  56. function createConcatenator () {
  57. return {
  58. elements: [],
  59. ids: [],
  60. add: function () {
  61. this.elements.push.apply(this.elements, arguments);
  62. },
  63. addId: function () {
  64. this.ids.push.apply(this.ids, arguments);
  65. },
  66. forEach: function () {
  67. this.elements.forEach.apply(this.elements, arguments);
  68. },
  69. start: function () {
  70. this.add(
  71. 'var define = (' + require('amdefine') + ')(null, function (id) { throw new Error("Dependency not found: " + id); }), requirejs = define.require;',
  72. 'define("amber_vm/browser-compatibility", [], {});'
  73. );
  74. },
  75. finish: function (realWork) {
  76. this.add(
  77. 'define("amber_vm/_init", ["amber_vm/smalltalk","' + this.ids.join('","') + '"], function (smalltalk) {',
  78. 'smalltalk.initialize();',
  79. realWork,
  80. '});',
  81. 'requirejs("amber_vm/_init");'
  82. );
  83. },
  84. toString: function () {
  85. return this.elements.join('\n');
  86. }
  87. };
  88. }
  89. /**
  90. * Combine several async functions and evaluate callback once all of them have finished.
  91. * Taken from: http://howtonode.org/control-flow
  92. */
  93. function Combo(callback) {
  94. this.callback = callback;
  95. this.items = 0;
  96. this.results = [];
  97. }
  98. Combo.prototype = {
  99. add: function () {
  100. var self = this,
  101. id = this.items;
  102. this.items++;
  103. return function () {
  104. self.check(id, arguments);
  105. };
  106. },
  107. check: function (id, theArguments) {
  108. this.results[id] = Array.prototype.slice.call(theArguments);
  109. this.items--;
  110. if (this.items === 0) {
  111. this.callback.apply(this, this.results);
  112. }
  113. }
  114. };
  115. var path = require('path'),
  116. util = require('util'),
  117. fs = require('fs'),
  118. exec = require('child_process').exec;
  119. /**
  120. * AmberC constructor function.
  121. * amber_dir: points to the location of an amber installation
  122. */
  123. function AmberC(amber_dir) {
  124. if (undefined === amber_dir || !fs.existsSync(amber_dir)) {
  125. throw new Error('amber_dir needs to be a valid directory');
  126. }
  127. this.amber_dir = amber_dir;
  128. this.kernel_libraries = ['boot', 'smalltalk', 'nil', '_st', 'Kernel-Objects', 'Kernel-Classes', 'Kernel-Methods',
  129. 'Kernel-Collections', 'Kernel-Infrastructure', 'Kernel-Exceptions', 'Kernel-Transcript',
  130. 'Kernel-Announcements'];
  131. this.compiler_libraries = this.kernel_libraries.concat(['parser', 'Kernel-ImportExport', 'Compiler-Exceptions',
  132. 'Compiler-Core', 'Compiler-AST', 'Compiler-Exceptions', 'Compiler-IR', 'Compiler-Inlining', 'Compiler-Semantic']);
  133. }
  134. /**
  135. * Default values.
  136. */
  137. var createDefaults = function(finished_callback){
  138. return {
  139. 'load': [],
  140. 'main': undefined,
  141. 'mainfile': undefined,
  142. 'stFiles': [],
  143. 'jsFiles': [],
  144. 'jsGlobals': [],
  145. 'amd_namespace': 'amber_core',
  146. 'suffix': '',
  147. 'loadsuffix': '',
  148. 'suffix_used': '',
  149. 'libraries': [],
  150. 'jsLibraryDirs': [],
  151. 'compile': [],
  152. 'compiled': [],
  153. 'program': undefined,
  154. 'output_dir': undefined,
  155. 'verbose': false,
  156. 'finished_callback': finished_callback
  157. };
  158. };
  159. /**
  160. * Main function for executing the compiler.
  161. * If check_configuration_ok() returns successfully the configuration is set on the current compiler
  162. * instance and check_for_closure_compiler() gets called.
  163. * The last step is to call collect_files().
  164. */
  165. AmberC.prototype.main = function(configuration, finished_callback) {
  166. console.time('Compile Time');
  167. if (undefined !== finished_callback) {
  168. configuration.finished_callback = finished_callback;
  169. }
  170. if (configuration.amd_namespace.length === 0) {
  171. configuration.amd_namespace = 'amber_core';
  172. }
  173. if (undefined !== configuration.jsLibraryDirs) {
  174. configuration.jsLibraryDirs.push(path.join(this.amber_dir, 'js'));
  175. configuration.jsLibraryDirs.push(path.join(this.amber_dir, 'support'));
  176. }
  177. console.ambercLog = console.log;
  178. if (false === configuration.verbose) {
  179. console.log = function() {};
  180. }
  181. if (this.check_configuration_ok(configuration)) {
  182. this.defaults = configuration;
  183. this.defaults.smalltalk = {}; // the evaluated compiler will be stored in this variable (see create_compiler)
  184. this.collect_files(this.defaults.stFiles, this.defaults.jsFiles)
  185. }
  186. };
  187. /**
  188. * Check if the passed in configuration object has sufficient/nonconflicting values
  189. */
  190. AmberC.prototype.check_configuration_ok = function(configuration) {
  191. if (undefined === configuration) {
  192. throw new Error('AmberC.check_configuration_ok(): missing configuration object');
  193. }
  194. if (0 === configuration.jsFiles.length && 0 === configuration.stFiles.length) {
  195. throw new Error('AmberC.check_configuration_ok(): no files to compile/link specified in configuration object');
  196. }
  197. return true;
  198. };
  199. /**
  200. * Check if the file given as parameter exists in any of the following directories:
  201. * 1. current local directory
  202. * 2. defauls.jsLibraryDirs
  203. * 3. $AMBER/js/
  204. * 3. $AMBER/support/
  205. *
  206. * @param filename name of a file without '.js' prefix
  207. * @param callback gets called on success with path to .js file as parameter
  208. */
  209. AmberC.prototype.resolve_js = function(filename, callback) {
  210. var baseName = path.basename(filename, '.js');
  211. var jsFile = baseName + this.defaults.loadsuffix + '.js';
  212. var defaults = this.defaults;
  213. console.log('Resolving: ' + jsFile);
  214. fs.exists(jsFile, function(exists) {
  215. if (exists) {
  216. callback(jsFile);
  217. } else {
  218. var amberJsFile = '';
  219. // check for specified .js file in any of the directories from jsLibraryDirs
  220. var found = defaults.jsLibraryDirs.some(function(directory) {
  221. amberJsFile = path.join(directory, jsFile);
  222. return fs.existsSync(amberJsFile);
  223. });
  224. if (found) {
  225. callback(amberJsFile);
  226. } else {
  227. throw(new Error('JavaScript file not found: ' + jsFile));
  228. }
  229. }
  230. });
  231. };
  232. /**
  233. * Collect libraries and Smalltalk files looking
  234. * both locally and in $AMBER/js and $AMBER/st.
  235. * Followed by resolve_libraries().
  236. */
  237. AmberC.prototype.collect_files = function(stFiles, jsFiles) {
  238. var self = this;
  239. var collected_files = new Combo(function() {
  240. self.resolve_libraries();
  241. });
  242. if (0 !== stFiles.length) {
  243. self.collect_st_files(stFiles, collected_files.add());
  244. }
  245. if (0 !== jsFiles.length) {
  246. self.collect_js_files(jsFiles, collected_files.add());
  247. }
  248. };
  249. /**
  250. * Resolve st files given by stFiles and add them to defaults.compile.
  251. * Respective categories get added to defaults.compile_categories.
  252. * callback is evaluated afterwards.
  253. */
  254. AmberC.prototype.collect_st_files = function(stFiles, callback) {
  255. var defaults = this.defaults;
  256. var self = this;
  257. var collected_st_files = new Combo(function() {
  258. Array.prototype.slice.call(arguments).forEach(function(data) {
  259. var stFile = data[0];
  260. defaults.compile.push(stFile);
  261. });
  262. callback();
  263. });
  264. stFiles.forEach(function(stFile) {
  265. var _callback = collected_st_files.add();
  266. console.log('Checking: ' + stFile);
  267. var amberStFile = path.join(self.amber_dir, 'st', stFile);
  268. fs.exists(stFile, function(exists) {
  269. if (exists) {
  270. _callback(stFile);
  271. } else {
  272. console.log('Checking: ' + amberStFile);
  273. fs.exists(amberStFile, function(exists) {
  274. if (exists) {
  275. _callback(amberStFile);
  276. } else {
  277. throw(new Error('Smalltalk file not found: ' + amberStFile));
  278. }
  279. });
  280. }
  281. });
  282. });
  283. };
  284. /**
  285. * Resolve js files given by jsFiles and add them to defaults.libraries.
  286. * callback is evaluated afterwards.
  287. */
  288. AmberC.prototype.collect_js_files = function(jsFiles, callback) {
  289. var self = this;
  290. var collected_js_files = new Combo(function() {
  291. Array.prototype.slice.call(arguments).forEach(function(file) {
  292. self.defaults.libraries.push(file[0]);
  293. });
  294. callback();
  295. });
  296. jsFiles.forEach(function(jsFile) {
  297. self.resolve_js(jsFile, collected_js_files.add());
  298. });
  299. };
  300. /**
  301. * Resolve kernel and compiler files.
  302. * Followed by resolve_init().
  303. */
  304. AmberC.prototype.resolve_libraries = function() {
  305. // Resolve libraries listed in this.kernel_libraries
  306. var self = this;
  307. var all_resolved = new Combo(function(resolved_kernel_files, resolved_compiler_files) {
  308. self.create_compiler(resolved_compiler_files[0]);
  309. });
  310. this.resolve_kernel(all_resolved.add());
  311. this.resolve_compiler(all_resolved.add());
  312. };
  313. /**
  314. * Resolve .js files needed by kernel
  315. * callback is evaluated afterwards.
  316. */
  317. AmberC.prototype.resolve_kernel = function(callback) {
  318. var self = this;
  319. var kernel_files = this.kernel_libraries.concat(this.defaults.load);
  320. var kernel_resolved = new Combo(function() {
  321. var foundLibraries = [];
  322. Array.prototype.slice.call(arguments).forEach(function(file) {
  323. if (undefined !== file[0]) {
  324. foundLibraries.push(file[0]);
  325. }
  326. });
  327. // boot.js and Kernel files need to be used first
  328. // otherwise the global smalltalk object is undefined
  329. self.defaults.libraries = foundLibraries.concat(self.defaults.libraries);
  330. callback(null);
  331. });
  332. kernel_files.forEach(function(file) {
  333. self.resolve_js(file, kernel_resolved.add());
  334. });
  335. always_resolve(kernel_resolved.add());
  336. };
  337. /**
  338. * Resolve .js files needed by compiler.
  339. * callback is evaluated afterwards with resolved files as argument.
  340. */
  341. AmberC.prototype.resolve_compiler = function(callback) {
  342. // Resolve compiler libraries
  343. var compiler_files = this.compiler_libraries.concat(this.defaults.load);
  344. var compiler_resolved = new Combo(function() {
  345. var compilerFiles = [];
  346. Array.prototype.slice.call(arguments).forEach(function(file) {
  347. if (undefined !== file[0]) {
  348. compilerFiles.push(file[0]);
  349. }
  350. });
  351. callback(compilerFiles);
  352. });
  353. var self = this;
  354. compiler_files.forEach(function(file) {
  355. self.resolve_js(file, compiler_resolved.add());
  356. });
  357. always_resolve(compiler_resolved.add());
  358. };
  359. /**
  360. * Read all .js files needed by compiler and eval() them.
  361. * The finished Compiler gets stored in defaults.smalltalk.
  362. * Followed by compile().
  363. */
  364. AmberC.prototype.create_compiler = function(compilerFilesArray) {
  365. var self = this;
  366. var compiler_files = new Combo(function() {
  367. var builder = createConcatenator();
  368. builder.add('(function() {');
  369. builder.start();
  370. Array.prototype.slice.call(arguments).forEach(function(data) {
  371. // data is an array where index 0 is the error code and index 1 contains the data
  372. builder.add(data[1]);
  373. // matches and returns the "module_id" string in the AMD definition: define("module_id", ...)
  374. var match = ('' + data[1]).match(/^define\("([^"]*)"/);
  375. if (match) {
  376. builder.addId(match[1]);
  377. }
  378. });
  379. // store the generated smalltalk env in self.defaults.smalltalk
  380. builder.finish('self.defaults.smalltalk = smalltalk;');
  381. builder.add('})();');
  382. eval(builder.toString());
  383. console.log('Compiler loaded');
  384. self.defaults.smalltalk.ErrorHandler._setCurrent_(self.defaults.smalltalk.RethrowErrorHandler._new());
  385. if(0 !== self.defaults.jsGlobals.length) {
  386. var jsGlobalVariables = self.defaults.smalltalk.globalJsVariables;
  387. jsGlobalVariables.push.apply(jsGlobalVariables, self.defaults.jsGlobals);
  388. }
  389. self.compile();
  390. });
  391. compilerFilesArray.forEach(function(file) {
  392. console.log('Loading file: ' + file);
  393. fs.readFile(file, compiler_files.add());
  394. });
  395. };
  396. /**
  397. * Compile all given .st files by importing them.
  398. * Followed by category_export().
  399. */
  400. AmberC.prototype.compile = function() {
  401. console.log('Compiling collected .st files');
  402. // import .st files
  403. var self = this;
  404. var imports = new Combo(function() {
  405. Array.prototype.slice.call(arguments).forEach(function(code) {
  406. if (undefined !== code[0]) {
  407. // get element 0 of code since all return values are stored inside an array by Combo
  408. var importer = self.defaults.smalltalk.Importer._new();
  409. try {
  410. importer._import_(code[0]._stream());
  411. } catch (ex) {
  412. throw new Error("Import error in section:\n" +
  413. importer._lastSection() +
  414. "\n\n" +
  415. "while processing chunk:\n" +
  416. importer._lastChunk() +
  417. "\n\n" +
  418. (ex._messageText && ex._messageText() || ex.message || ex));
  419. }
  420. }
  421. });
  422. self.category_export();
  423. });
  424. this.defaults.compile.forEach(function(stFile) {
  425. var callback = imports.add();
  426. if (/\.st/.test(stFile)) {
  427. console.ambercLog('Importing: ' + stFile);
  428. fs.readFile(stFile, 'utf8', function(err, data) {
  429. if (!err)
  430. callback(data);
  431. else
  432. throw new Error('Could not import: ' + stFile);
  433. });
  434. }
  435. });
  436. always_resolve(imports.add());
  437. };
  438. /**
  439. * Export compiled categories to JavaScript files.
  440. * Followed by verify().
  441. */
  442. AmberC.prototype.category_export = function() {
  443. var defaults = this.defaults;
  444. var self = this;
  445. // export categories as .js
  446. async_map(defaults.compile, function(stFile, callback) {
  447. var category = path.basename(stFile, '.st');
  448. var jsFilePath = defaults.output_dir;
  449. if (undefined === jsFilePath) {
  450. jsFilePath = path.dirname(stFile);
  451. }
  452. var jsFile = category + defaults.suffix_used + '.js';
  453. jsFile = path.join(jsFilePath, jsFile);
  454. defaults.compiled.push(jsFile);
  455. var smalltalk = defaults.smalltalk;
  456. var packageObject = smalltalk.Package._named_(category);
  457. packageObject._transport()._namespace_(defaults.amd_namespace);
  458. fs.writeFile(jsFile, smalltalk.String._streamContents_(function (stream) {
  459. smalltalk.AmdExporter._new()._exportPackage_on_(packageObject, stream); }), function(err) {
  460. callback(null, null);
  461. });
  462. }, function(err, result){
  463. self.verify();
  464. });
  465. };
  466. /**
  467. * Verify if all .st files have been compiled.
  468. * Followed by compose_js_files().
  469. */
  470. AmberC.prototype.verify = function() {
  471. console.log('Verifying if all .st files were compiled');
  472. var self = this;
  473. // copy array
  474. var compiledFiles = this.defaults.compiled.slice(0);
  475. async_map(compiledFiles,
  476. function(file, callback) {
  477. fs.exists(file, function(exists) {
  478. if (exists)
  479. callback(null, null);
  480. else
  481. throw(new Error('Compilation failed of: ' + file));
  482. });
  483. }, function(err, result) {
  484. self.compose_js_files();
  485. });
  486. };
  487. /**
  488. * Synchronous function.
  489. * Concatenates compiled JavaScript files into one file in the correct order.
  490. * The name of the produced file is given by defaults.program (set by the last commandline option).
  491. */
  492. AmberC.prototype.compose_js_files = function() {
  493. var defaults = this.defaults;
  494. var programFile = defaults.program;
  495. if (undefined === programFile) {
  496. return;
  497. }
  498. if (undefined !== defaults.output_dir) {
  499. programFile = path.join(defaults.output_dir, programFile);
  500. }
  501. var program_files = [];
  502. if (0 !== defaults.libraries.length) {
  503. console.log('Collecting libraries: ' + defaults.libraries);
  504. program_files.push.apply(program_files, defaults.libraries);
  505. }
  506. if (0 !== defaults.compiled.length) {
  507. var compiledFiles = defaults.compiled.slice(0);
  508. console.log('Collecting compiled files: ' + compiledFiles);
  509. program_files.push.apply(program_files, compiledFiles);
  510. }
  511. console.ambercLog('Writing program file: %s.js', programFile);
  512. var fileStream = fs.createWriteStream(programFile + defaults.suffix_used + '.js');
  513. fileStream.on('error', function(error) {
  514. fileStream.end();
  515. console.ambercLog(error);
  516. });
  517. fileStream.on('close', function(){
  518. return;
  519. });
  520. var builder = createConcatenator();
  521. builder.add('#!/usr/bin/env node');
  522. builder.start();
  523. program_files.forEach(function(file) {
  524. if(fs.existsSync(file)) {
  525. console.log('Adding : ' + file);
  526. var buffer = fs.readFileSync(file);
  527. // matches and returns the "module_id" string in the AMD define: define("module_id", ...)
  528. var match = buffer.toString().match(/^define\("([^"]*)"/);
  529. if (match /*&& match[1].slice(0,9) !== "amber_vm/"*/) {
  530. builder.addId(match[1]);
  531. }
  532. builder.add(buffer);
  533. } else {
  534. fileStream.end();
  535. throw(new Error('Can not find file ' + file));
  536. }
  537. });
  538. var mainFunctionOrFile = '';
  539. if (undefined !== defaults.main) {
  540. console.log('Adding call to: %s>>main', defaults.main);
  541. mainFunctionOrFile += 'smalltalk.' + defaults.main + '._main();';
  542. }
  543. if (undefined !== defaults.mainfile && fs.existsSync(defaults.mainfile)) {
  544. console.log('Adding main file: ' + defaults.mainfile);
  545. mainFunctionOrFile += '\n' + fs.readFileSync(defaults.mainfile);
  546. }
  547. builder.finish(mainFunctionOrFile);
  548. console.log('Writing...');
  549. builder.forEach(function (element) {
  550. fileStream.write(element);
  551. fileStream.write('\n');
  552. });
  553. console.log('Done.');
  554. fileStream.end();
  555. };
  556. module.exports.Compiler = AmberC;
  557. module.exports.createDefaults = createDefaults;
  558. module.exports.Combo = Combo;
  559. module.exports.map = async_map;