amberc.js 17 KB

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