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