1
0

amberc.js 19 KB

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