1
0

amberc.js 18 KB

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