2
0

amberc.js 17 KB

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