amberc.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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': [],
  109. 'program': undefined,
  110. 'output_dir': undefined,
  111. 'verbose': false,
  112. 'finished_callback': finished_callback
  113. };
  114. };
  115. /**
  116. * Main function for executing the compiler.
  117. * If check_configuration_ok() returns successfully the configuration is set on the current compiler
  118. * instance and check_for_closure_compiler() gets called.
  119. * The last step is to call collect_files().
  120. */
  121. AmberC.prototype.main = function(configuration, finished_callback) {
  122. console.time('Compile Time');
  123. if (undefined !== finished_callback) {
  124. configuration.finished_callback = finished_callback;
  125. }
  126. if (configuration.closure || configuration.closure_parts || configuration.closure_full) {
  127. configuration.deploy = true;
  128. }
  129. console.ambercLog = console.log;
  130. if (false === configuration.verbose) {
  131. console.log = function() {};
  132. }
  133. if (this.check_configuration_ok(configuration)) {
  134. this.defaults = configuration;
  135. this.defaults.smalltalk = {}; // the evaluated compiler will be stored in this variable (see create_compiler)
  136. var self = this;
  137. this.check_for_closure_compiler(function(){
  138. self.collect_files(self.defaults.stFiles, self.defaults.jsFiles)
  139. });
  140. }
  141. };
  142. /**
  143. * Check if the passed in configuration object has sufficient/nonconflicting values
  144. */
  145. AmberC.prototype.check_configuration_ok = function(configuration) {
  146. if (undefined === configuration) {
  147. throw new Error('AmberC.check_configuration_ok(): missing configuration object');
  148. }
  149. if (undefined === configuration.init) {
  150. throw new Error('AmberC.check_configuration_ok(): init value missing in configuration object');
  151. }
  152. if (0 === configuration.jsFiles.length && 0 === configuration.stFiles.lenght) {
  153. throw new Error('AmberC.check_configuration_ok(): no files to compile/link specified in configuration object');
  154. }
  155. return true;
  156. };
  157. /**
  158. * Checks if the java executable exists and afterwards,
  159. * if compiler.jar exists at the path stored in this.closure_jar.
  160. * All closure related entries are set to false upon failure.
  161. *
  162. * callback gets called in any case.
  163. */
  164. AmberC.prototype.check_for_closure_compiler = function(callback) {
  165. var defaults = this.defaults;
  166. var self = this;
  167. if (defaults.closure) {
  168. exec('which java', function(error, stdout, stderr) {
  169. // stdout contains path to java executable
  170. if (null !== error) {
  171. console.warn('java is not installed but is needed for running the Closure compiler (-O, -A or -o flags).');
  172. defaults.closure = false;
  173. defaults.closure_parts = false;
  174. defaults.closure_full = false;
  175. callback();
  176. return;
  177. }
  178. fs.exists(self.closure_jar, function(exists) {
  179. if (!exists) {
  180. console.warn('Can not find Closure compiler at: ' + self.closure_jar);
  181. defaults.closure = false;
  182. defaults.closure_parts = false;
  183. defaults.closure_full = false;
  184. } else {
  185. console.warn('Closure compiler found at: ' + self.closure_jar);
  186. }
  187. callback();
  188. return;
  189. });
  190. });
  191. } else {
  192. callback();
  193. }
  194. };
  195. /**
  196. * Check if the file given as parameter exists in the local directory or in $AMBER/js/.
  197. * '.js' is appended first.
  198. *
  199. * @param filename name of a file without '.js' prefix
  200. * @param callback gets called on success with path to .js file as parameter
  201. */
  202. AmberC.prototype.resolve_js = function(filename, callback) {
  203. var baseName = path.basename(filename, '.js');
  204. var jsFile = baseName + this.defaults.loadsuffix + '.js';
  205. var amberJsFile = path.join(this.amber_dir, 'js', jsFile);
  206. console.log('Resolving: ' + jsFile);
  207. fs.exists(jsFile, function(exists) {
  208. if (exists) {
  209. callback(jsFile);
  210. } else {
  211. fs.exists(amberJsFile, function(exists) {
  212. if (exists) {
  213. callback(amberJsFile);
  214. } else {
  215. throw(new Error('JavaScript file not found: ' + jsFile));
  216. }
  217. });
  218. }
  219. });
  220. };
  221. /**
  222. * Collect libraries and Smalltalk files looking
  223. * both locally and in $AMBER/js and $AMBER/st.
  224. * Followed by resolve_libraries().
  225. */
  226. AmberC.prototype.collect_files = function(stFiles, jsFiles) {
  227. var self = this;
  228. var collected_files = new Combo(function() {
  229. self.resolve_libraries();
  230. });
  231. if (0 !== stFiles.length) {
  232. self.collect_st_files(stFiles, collected_files.add());
  233. }
  234. if (0 !== jsFiles.length) {
  235. self.collect_js_files(jsFiles, collected_files.add());
  236. }
  237. };
  238. /**
  239. * Resolve st files given by stFiles and add them to defaults.compile.
  240. * Respective categories get added to defaults.compile_categories.
  241. * callback is evaluated afterwards.
  242. */
  243. AmberC.prototype.collect_st_files = function(stFiles, callback) {
  244. var defaults = this.defaults;
  245. var self = this;
  246. var collected_st_files = new Combo(function() {
  247. Array.prototype.slice.call(arguments).forEach(function(data) {
  248. var stFile = data[0];
  249. defaults.compile.push(stFile);
  250. });
  251. callback();
  252. });
  253. stFiles.forEach(function(stFile) {
  254. var _callback = collected_st_files.add();
  255. console.log('Checking: ' + stFile);
  256. var amberStFile = path.join(self.amber_dir, 'st', stFile);
  257. fs.exists(stFile, function(exists) {
  258. if (exists) {
  259. _callback(stFile);
  260. } else {
  261. console.log('Checking: ' + amberStFile);
  262. fs.exists(amberStFile, function(exists) {
  263. if (exists) {
  264. _callback(amberStFile);
  265. } else {
  266. throw(new Error('Smalltalk file not found: ' + amberStFile));
  267. }
  268. });
  269. }
  270. });
  271. });
  272. };
  273. /**
  274. * Resolve js files given by jsFiles and add them to defaults.libraries.
  275. * callback is evaluated afterwards.
  276. */
  277. AmberC.prototype.collect_js_files = function(jsFiles, callback) {
  278. var self = this;
  279. var collected_js_files = new Combo(function() {
  280. Array.prototype.slice.call(arguments).forEach(function(file) {
  281. self.defaults.libraries.push(file[0]);
  282. });
  283. callback();
  284. });
  285. jsFiles.forEach(function(jsFile) {
  286. self.resolve_js(jsFile, collected_js_files.add());
  287. });
  288. };
  289. /**
  290. * Resolve kernel and compiler files.
  291. * Followed by resolve_init().
  292. */
  293. AmberC.prototype.resolve_libraries = function() {
  294. // Resolve libraries listed in this.kernel_libraries
  295. var self = this;
  296. var all_resolved = new Combo(function(resolved_kernel_files, resolved_compiler_files) {
  297. self.resolve_init(resolved_compiler_files[0]);
  298. });
  299. this.resolve_kernel(all_resolved.add());
  300. this.resolve_compiler(all_resolved.add());
  301. };
  302. /**
  303. * Resolve .js files needed by kernel
  304. * callback is evaluated afterwards.
  305. */
  306. AmberC.prototype.resolve_kernel = function(callback) {
  307. var self = this;
  308. var kernel_files = this.kernel_libraries.concat(this.defaults.load);
  309. var kernel_resolved = new Combo(function() {
  310. var foundLibraries = [];
  311. Array.prototype.slice.call(arguments).forEach(function(file) {
  312. if (undefined !== file[0]) {
  313. foundLibraries.push(file[0]);
  314. }
  315. });
  316. // boot.js and Kernel files need to be used first
  317. // otherwise the global smalltalk object is undefined
  318. self.defaults.libraries = foundLibraries.concat(self.defaults.libraries);
  319. callback(null);
  320. });
  321. kernel_files.forEach(function(file) {
  322. self.resolve_js(file, kernel_resolved.add());
  323. });
  324. always_resolve(kernel_resolved.add());
  325. };
  326. /**
  327. * Resolve .js files needed by compiler.
  328. * callback is evaluated afterwards with resolved files as argument.
  329. */
  330. AmberC.prototype.resolve_compiler = function(callback) {
  331. // Resolve compiler libraries
  332. var compiler_files = this.compiler_libraries.concat(this.defaults.load);
  333. var compiler_resolved = new Combo(function() {
  334. var compilerFiles = [];
  335. Array.prototype.slice.call(arguments).forEach(function(file) {
  336. if (undefined !== file[0]) {
  337. compilerFiles.push(file[0]);
  338. }
  339. });
  340. callback(compilerFiles);
  341. });
  342. var self = this
  343. compiler_files.forEach(function(file) {
  344. self.resolve_js(file, compiler_resolved.add());
  345. });
  346. always_resolve(compiler_resolved.add());
  347. };
  348. /**
  349. * Resolves default.init and adds it to compilerFiles.
  350. * Followed by create_compiler().
  351. */
  352. AmberC.prototype.resolve_init = function(compilerFiles) {
  353. // check and add init.js
  354. var initFile = this.defaults.init;
  355. if ('.js' !== path.extname(initFile)) {
  356. initFile = this.resolve_js(initFile);
  357. this.defaults.init = initFile;
  358. }
  359. compilerFiles.push(initFile);
  360. this.create_compiler(compilerFiles);
  361. };
  362. /**
  363. * Read all .js files needed by compiler and eval() them.
  364. * The finished Compiler gets stored in defaults.smalltalk.
  365. * Followed by compile().
  366. */
  367. AmberC.prototype.create_compiler = function(compilerFilesArray) {
  368. var self = this;
  369. var compiler_files = new Combo(function() {
  370. var content = '(function() {';
  371. Array.prototype.slice.call(arguments).forEach(function(data) {
  372. // data is an array where index 0 is the error code and index 1 contains the data
  373. content += data[1];
  374. });
  375. content = content + 'return smalltalk;})();';
  376. self.defaults.smalltalk = eval(content);
  377. console.log('Compiler loaded');
  378. self.defaults.smalltalk.ErrorHandler._setCurrent_(self.defaults.smalltalk.RethrowErrorHandler._new());
  379. self.compile();
  380. });
  381. compilerFilesArray.forEach(function(file) {
  382. console.log('Loading file: ' + file);
  383. fs.readFile(file, compiler_files.add());
  384. });
  385. };
  386. /**
  387. * Compile all given .st files by importing them.
  388. * Followed by category_export().
  389. */
  390. AmberC.prototype.compile = function() {
  391. console.log('Compiling collected .st files')
  392. // import .st files
  393. var self = this;
  394. var imports = new Combo(function() {
  395. Array.prototype.slice.call(arguments).forEach(function(code) {
  396. if (undefined !== code[0]) {
  397. // get element 0 of code since all return values are stored inside an array by Combo
  398. self.defaults.smalltalk.Importer._new()._import_(code[0]._stream());
  399. }
  400. });
  401. self.category_export();
  402. });
  403. this.defaults.compile.forEach(function(stFile) {
  404. var callback = imports.add();
  405. if (/\.st/.test(stFile)) {
  406. console.ambercLog('Importing: ' + stFile);
  407. fs.readFile(stFile, 'utf8', function(err, data) {
  408. if (!err)
  409. callback(data);
  410. else
  411. throw new Error('Could not import: ' + stFile);
  412. });
  413. }
  414. });
  415. always_resolve(imports.add());
  416. };
  417. /**
  418. * Export compiled categories to JavaScript files.
  419. * Followed by verify().
  420. */
  421. AmberC.prototype.category_export = function() {
  422. var defaults = this.defaults;
  423. var self = this;
  424. // export categories as .js
  425. async_map(defaults.compile, function(stFile, callback) {
  426. var category = path.basename(stFile, '.st');
  427. var jsFilePath = defaults.output_dir;
  428. if (undefined === jsFilePath) {
  429. jsFilePath = path.dirname(stFile);
  430. }
  431. var jsFile = category + defaults.suffix_used + '.js';
  432. jsFile = path.join(jsFilePath, jsFile);
  433. defaults.compiled.push(jsFile);
  434. var jsFileDeploy = category + defaults.suffix_used + '.deploy.js';
  435. jsFileDeploy = path.join(jsFilePath, jsFileDeploy);
  436. console.log('Exporting ' + (defaults.deploy ? '(debug + deploy)' : '(debug)')
  437. + ' category ' + category + ' as ' + jsFile
  438. + (defaults.deploy ? ' and ' + jsFileDeploy : ''));
  439. fs.writeFile(jsFile, defaults.smalltalk.Exporter._new()._exportPackage_(category), function(err) {
  440. if (defaults.deploy) {
  441. fs.writeFile(jsFileDeploy, defaults.smalltalk.StrippedExporter._new()._exportPackage_(category), callback);
  442. } else {
  443. callback(null, null);
  444. }
  445. });
  446. }, function(err, result){
  447. self.verify();
  448. });
  449. };
  450. /**
  451. * Verify if all .st files have been compiled.
  452. * Followed by compose_js_files() and optimize().
  453. */
  454. AmberC.prototype.verify = function() {
  455. console.log('Verifying if all .st files were compiled');
  456. var self = this;
  457. // copy array
  458. var compiledFiles = this.defaults.compiled.slice(0);
  459. // append deploy files if necessary
  460. if (true === this.defaults.deploy) {
  461. this.defaults.compiled.forEach(function(file) {
  462. compiledFiles.push(file.replace(/\.js/g, '.deploy.js'));
  463. });
  464. }
  465. async_map(compiledFiles,
  466. function(file, callback) {
  467. fs.exists(file, function(exists) {
  468. if (exists)
  469. callback(null, null);
  470. else
  471. throw(new Error('Compilation failed of: ' + file));
  472. });
  473. }, function(err, result) {
  474. self.compose_js_files();
  475. });
  476. };
  477. /**
  478. * Synchronous function.
  479. * Concatenates compiled JavaScript files into one file in the correct order.
  480. * The name of the produced file is given by defaults.program (set by the last commandline option).
  481. */
  482. AmberC.prototype.compose_js_files = function() {
  483. var defaults = this.defaults;
  484. var self = this;
  485. var programFile = defaults.program;
  486. if (undefined === programFile) {
  487. self.optimize();
  488. return;
  489. }
  490. if (undefined !== defaults.output_dir) {
  491. programFile = path.join(defaults.output_dir, programFile);
  492. }
  493. var program_files = [];
  494. if (0 !== defaults.libraries.length) {
  495. console.log('Collecting libraries: ' + defaults.libraries);
  496. program_files.push.apply(program_files, defaults.libraries);
  497. }
  498. if (0 !== defaults.compiled.length) {
  499. var compiledFiles = defaults.compiled.slice(0);
  500. if (true === defaults.deploy) {
  501. compiledFiles = compiledFiles.map(function(file) {
  502. return file.replace(/\.js$/g, '.deploy.js');
  503. });
  504. }
  505. console.log('Collecting compiled files: ' + compiledFiles);
  506. program_files.push.apply(program_files, compiledFiles);
  507. }
  508. if (undefined !== defaults.init) {
  509. console.log('Adding initializer ' + defaults.init);
  510. program_files.push(defaults.init);
  511. }
  512. console.ambercLog('Writing program file: %s.js', programFile);
  513. var fileStream = fs.createWriteStream(programFile + defaults.suffix_used + '.js');
  514. fileStream.on('error', function(error) {
  515. fileStream.end();
  516. console.ambercLog(error);
  517. });
  518. fileStream.on('close', function(){
  519. self.optimize();
  520. });
  521. program_files.forEach(function(file) {
  522. if(fs.existsSync(file)) {
  523. console.log('Adding : ' + file);
  524. fileStream.write(fs.readFileSync(file));
  525. } else {
  526. fileStream.end();
  527. throw(new Error('Can not find file ' + file));
  528. }
  529. });
  530. if (undefined !== defaults.main) {
  531. console.log('Adding call to: %s>>main', defaults.main);
  532. fileStream.write('smalltalk.' + defaults.main + '._main()');
  533. }
  534. if (undefined !== defaults.mainfile && fs.existsSync(defaults.mainfile)) {
  535. console.log('Adding main file: ' + defaults.mainfile);
  536. fileStream.write(fs.readFileSync(defaults.mainfile));
  537. }
  538. console.log('Done.');
  539. fileStream.end();
  540. };
  541. /**
  542. * Optimize created JavaScript files with Google Closure compiler depending
  543. * on the flags: defaults.closure_parts, defaults.closure_full.
  544. */
  545. AmberC.prototype.optimize = function() {
  546. var defaults = this.defaults;
  547. var self = this;
  548. var optimization_done = new Combo(function() {
  549. console.log = console.ambercLog;
  550. console.timeEnd('Compile Time');
  551. if (undefined !== defaults.finished_callback) {
  552. defaults.finished_callback();
  553. }
  554. });
  555. if (defaults.closure_parts) {
  556. console.log('Compiling all js files using Google closure compiler.');
  557. defaults.compiled.forEach(function(file) {
  558. var deployFile = file.replace(/\.js$/g, '.deploy.js');
  559. console.log('Compiling ' + deployFile + ' file using Google closure compiler.');
  560. var minifiedName = deployFile.replace(/\.js$/g, '.min.js');
  561. self.closure_compile(deployFile, minifiedName, optimization_done.add());
  562. });
  563. }
  564. if (defaults.closure_full && (undefined !== defaults.program)) {
  565. var programFile = defaults.program;
  566. if (undefined !== defaults.output_dir) {
  567. programFile = path.join(defaults.output_dir, programFile);
  568. }
  569. console.log('Compiling ' + programFile + '.js file using Google closure compiler.');
  570. self.closure_compile(programFile + '.js', programFile + '.min.js', optimization_done.add());
  571. }
  572. always_resolve(optimization_done.add());
  573. };
  574. /**
  575. * Compile sourceFile into minifiedFile with Google Closure compiler.
  576. * callback gets executed once finished.
  577. */
  578. AmberC.prototype.closure_compile = function(sourceFile, minifiedFile, callback) {
  579. // exec is asynchronous
  580. var self = this;
  581. exec(
  582. 'java -jar ' +
  583. self.closure_jar + ' ' +
  584. self.defaults.closure_options +
  585. ' --js '+ sourceFile +
  586. ' --js_output_file '+ minifiedFile,
  587. function (error, stdout, stderr) {
  588. if (error) {
  589. console.log(stderr);
  590. } else {
  591. console.log(stdout);
  592. console.log('Minified: '+ minifiedFile);
  593. }
  594. callback();
  595. }
  596. );
  597. };
  598. module.exports.Compiler = AmberC;
  599. module.exports.createDefaults = createDefaults;
  600. module.exports.Combo = Combo;
  601. module.exports.map = async_map;