amberc.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. /**
  11. * Helper for concatenating Amber generated AMD modules.
  12. * The produced output can be exported and run as an independent program.
  13. *
  14. * var concatenator = createConcatenator();
  15. * concatenator.start(); // write the required AMD define header
  16. * concatenator.add(module1);
  17. * concatenator.addId(module1_ID);
  18. * //...
  19. * concatenator.finish("//some last code");
  20. * var concatenation = concatenator.toString();
  21. * // The variable concatenation contains the concatenated result
  22. * // which can either be stored in a file or interpreted with eval().
  23. */
  24. function createConcatenator () {
  25. return {
  26. elements: [],
  27. ids: [],
  28. add: function () {
  29. this.elements.push.apply(this.elements, arguments);
  30. },
  31. addId: function () {
  32. this.ids.push.apply(this.ids, arguments);
  33. },
  34. forEach: function () {
  35. this.elements.forEach.apply(this.elements, arguments);
  36. },
  37. start: function () {
  38. this.add(
  39. 'var define = (' + require('amdefine') + ')(null, function (id) { throw new Error("Dependency not found: " + id); }), requirejs = define.require;',
  40. 'define("amber_vm/browser-compatibility", [], {});'
  41. );
  42. },
  43. finish: function (realWork) {
  44. this.add(
  45. 'define("amber_vm/_init", ["amber_vm/smalltalk","' + this.ids.join('","') + '"], function (smalltalk) {',
  46. 'smalltalk.initialize();',
  47. realWork,
  48. '});',
  49. 'requirejs("amber_vm/_init");'
  50. );
  51. },
  52. toString: function () {
  53. return this.elements.join('\n');
  54. }
  55. };
  56. }
  57. var path = require('path'),
  58. fs = require('fs'),
  59. Promise = require('es6-promise').Promise;
  60. /**
  61. * AmberC constructor function.
  62. * amber_dir: points to the location of an amber installation
  63. */
  64. function AmberC(amber_dir) {
  65. if (undefined === amber_dir || !fs.existsSync(amber_dir)) {
  66. throw new Error('amber_dir needs to be a valid directory');
  67. }
  68. this.amber_dir = amber_dir;
  69. this.kernel_libraries = ['boot', 'smalltalk', 'nil', '_st', 'Kernel-Objects', 'Kernel-Classes', 'Kernel-Methods',
  70. 'Kernel-Collections', 'Kernel-Infrastructure', 'Kernel-Exceptions', 'Kernel-Transcript',
  71. 'Kernel-Announcements'];
  72. this.compiler_libraries = this.kernel_libraries.concat(['parser', 'Kernel-ImportExport', 'Compiler-Exceptions',
  73. 'Compiler-Core', 'Compiler-AST', 'Compiler-Exceptions', 'Compiler-IR', 'Compiler-Inlining', 'Compiler-Semantic']);
  74. }
  75. /**
  76. * Default values.
  77. */
  78. var createDefaultConfiguration = function() {
  79. return {
  80. 'load': [],
  81. 'main': undefined,
  82. 'mainfile': undefined,
  83. 'stFiles': [],
  84. 'jsFiles': [],
  85. 'jsGlobals': [],
  86. 'amd_namespace': 'amber_core',
  87. 'suffix': '',
  88. 'loadsuffix': '',
  89. 'suffix_used': '',
  90. 'libraries': [],
  91. 'jsLibraryDirs': [],
  92. 'compile': [],
  93. 'compiled': [],
  94. 'program': undefined,
  95. 'output_dir': undefined,
  96. 'verbose': false
  97. };
  98. };
  99. /**
  100. * Main function for executing the compiler.
  101. * If check_configuration_ok() returns successfully
  102. * the configuration is used to trigger the following compilation steps.
  103. */
  104. AmberC.prototype.main = function(configuration, finished_callback) {
  105. console.time('Compile Time');
  106. if (configuration.amd_namespace.length === 0) {
  107. configuration.amd_namespace = 'amber_core';
  108. }
  109. if (undefined !== configuration.jsLibraryDirs) {
  110. configuration.jsLibraryDirs.push(path.join(this.amber_dir, 'js'));
  111. configuration.jsLibraryDirs.push(path.join(this.amber_dir, 'support'));
  112. }
  113. console.ambercLog = console.log;
  114. if (false === configuration.verbose) {
  115. console.log = function() {};
  116. }
  117. // the evaluated compiler will be stored in this variable (see create_compiler)
  118. configuration.smalltalk = {};
  119. configuration.kernel_libraries = this.kernel_libraries;
  120. configuration.compiler_libraries = this.compiler_libraries;
  121. configuration.amber_dir = this.amber_dir;
  122. function logError(error) {
  123. console.log(error);
  124. finished_callback();
  125. };
  126. check_configuration(configuration)
  127. .then(collect_st_files, logError)
  128. .then(collect_js_files, logError)
  129. .then(resolve_kernel, logError)
  130. .then(create_compiler, logError)
  131. .then(compile, logError)
  132. .then(category_export, logError)
  133. .then(verify, logError)
  134. .then(compose_js_files, logError)
  135. .then(function() {
  136. console.log = console.ambercLog;
  137. console.timeEnd('Compile Time');
  138. finished_callback();
  139. });
  140. };
  141. /**
  142. * Check if the passed in configuration object has sufficient/nonconflicting values.
  143. * Returns a Promise which resolves into the configuration object.
  144. */
  145. function check_configuration(configuration) {
  146. return new Promise(function(resolve, reject) {
  147. if (undefined === configuration) {
  148. reject(Error('AmberC.check_configuration_ok(): missing configuration object'));
  149. }
  150. if (0 === configuration.jsFiles.length && 0 === configuration.stFiles.length) {
  151. reject(Error('AmberC.check_configuration_ok(): no files to compile/link specified in configuration object'));
  152. }
  153. resolve(configuration);
  154. });
  155. };
  156. /**
  157. * Check if the file given as parameter exists in any of the following directories:
  158. * 1. current local directory
  159. * 2. configuration.jsLibraryDirs
  160. * 3. $AMBER/js/
  161. * 3. $AMBER/support/
  162. *
  163. * @param filename name of a file without '.js' prefix
  164. * @param configuration the main amberc configuration object
  165. */
  166. function resolve_js(filename, configuration) {
  167. var baseName = path.basename(filename, '.js');
  168. var jsFile = baseName + configuration.loadsuffix + '.js';
  169. return resolve_file(jsFile, configuration.jsLibraryDirs);
  170. };
  171. /**
  172. * Check if the file given as parameter exists in any of the following directories:
  173. * 1. current local directory
  174. * 2. $AMBER/
  175. *
  176. * @param filename name of a .st file
  177. * @param configuration the main amberc configuration object
  178. */
  179. function resolve_st(filename, configuration) {
  180. return resolve_file(filename, [configuration.amber_dir]);
  181. };
  182. /**
  183. * Resolve the location of a file given as parameter filename.
  184. * First check if the file exists at given location,
  185. * then check in each of the directories specified in parameter searchDirectories.
  186. */
  187. function resolve_file(filename, searchDirectories) {
  188. return new Promise(function(resolve, reject) {
  189. console.log('Resolving: ' + filename);
  190. fs.exists(filename, function(exists) {
  191. if (exists) {
  192. resolve(filename);
  193. } else {
  194. var alternativeFile = '';
  195. // check for filename in any of the given searchDirectories
  196. var found = searchDirectories.some(function(directory) {
  197. alternativeFile = path.join(directory, filename);
  198. return fs.existsSync(alternativeFile);
  199. });
  200. if (found) {
  201. resolve(alternativeFile);
  202. } else {
  203. reject(Error('File not found: ' + alternativeFile));
  204. }
  205. }
  206. });
  207. });
  208. };
  209. /**
  210. * Resolve st files given by stFiles and add them to configuration.compile.
  211. * Returns a Promise which resolves into the configuration object.
  212. */
  213. function collect_st_files(configuration) {
  214. return new Promise(function(resolve, reject) {
  215. Promise.all(
  216. configuration.stFiles.map(function(stFile) {
  217. return resolve_st(stFile, configuration);
  218. })
  219. ).then(function(data) {
  220. configuration.compile = configuration.compile.concat(data);
  221. resolve(configuration);
  222. }, function(error) {
  223. reject(error);
  224. });
  225. });
  226. };
  227. /**
  228. * Resolve js files given by jsFiles and add them to configuration.libraries.
  229. * Returns a Promise which resolves into the configuration object.
  230. */
  231. function collect_js_files(configuration) {
  232. return new Promise(function(resolve, reject) {
  233. Promise.all(
  234. configuration.jsFiles.map(function(file) {
  235. return resolve_js(file, configuration);
  236. })
  237. ).then(function(data) {
  238. configuration.libraries = configuration.libraries.concat(data);
  239. resolve(configuration);
  240. }, function(error) {
  241. reject(error);
  242. });
  243. });
  244. };
  245. /**
  246. * Resolve .js files needed by kernel.
  247. * Returns a Promise which resolves into the configuration object.
  248. */
  249. function resolve_kernel(configuration) {
  250. var kernel_files = configuration.kernel_libraries.concat(configuration.load);
  251. return new Promise(function(resolve, reject) {
  252. Promise.all(
  253. kernel_files.map(function(file) {
  254. return resolve_js(file, configuration, resolve);
  255. })
  256. ).then(function(data) {
  257. // boot.js and Kernel files need to be used first
  258. // otherwise the global smalltalk object is undefined
  259. configuration.libraries = data.concat(configuration.libraries);
  260. resolve(configuration);
  261. }, function(error) {
  262. reject(error);
  263. });
  264. });
  265. };
  266. /**
  267. * Resolve .js files needed by compiler, read and eval() them.
  268. * The finished Compiler gets stored in configuration.smalltalk.
  269. * Returns a Promise object which resolves into the configuration object.
  270. */
  271. function create_compiler(configuration) {
  272. return new Promise(function(resolve, reject) {
  273. var compiler_files = configuration.compiler_libraries.concat(configuration.load);
  274. Promise.all(
  275. compiler_files.map(function(file) {
  276. return resolve_js(file, configuration, resolve);
  277. })
  278. )
  279. .then(function(compilerFilesArray) {
  280. return Promise.all(
  281. compilerFilesArray.map(function(file) {
  282. return new Promise(function(resolve, reject) {
  283. console.log('Loading file: ' + file);
  284. fs.readFile(file, function(err, data) {
  285. if (err)
  286. reject(err);
  287. else
  288. resolve(data);
  289. });
  290. });
  291. })
  292. )
  293. }).then(function(files) {
  294. var builder = createConcatenator();
  295. builder.add('(function() {');
  296. builder.start();
  297. files.forEach(function(data) {
  298. // data is an array where index 0 is the error code and index 1 contains the data
  299. builder.add(data);
  300. // matches and returns the "module_id" string in the AMD definition: define("module_id", ...)
  301. var match = ('' + data).match(/^define\("([^"]*)"/);
  302. if (match) {
  303. builder.addId(match[1]);
  304. }
  305. });
  306. // store the generated smalltalk env in configuration.smalltalk
  307. builder.finish('configuration.smalltalk = smalltalk;');
  308. builder.add('})();');
  309. eval(builder.toString());
  310. console.log('Compiler loaded');
  311. configuration.smalltalk.ErrorHandler._setCurrent_(configuration.smalltalk.RethrowErrorHandler._new());
  312. if(0 !== configuration.jsGlobals.length) {
  313. var jsGlobalVariables = configuration.smalltalk.globalJsVariables;
  314. jsGlobalVariables.push.apply(jsGlobalVariables, configuration.jsGlobals);
  315. }
  316. resolve(configuration);
  317. }, function(error) {
  318. reject(Error('Error creating compiler'));
  319. });
  320. });
  321. };
  322. /**
  323. * Compile all given .st files by importing them.
  324. * Returns a Promise object that resolves into the configuration object.
  325. */
  326. function compile(configuration) {
  327. // return function which does the actual work
  328. // and use the compile function to reference the configuration object
  329. return new Promise(function(resolve, reject) {
  330. Promise.all(
  331. configuration.compile.map(function(stFile) {
  332. return new Promise(function(resolve, reject) {
  333. if (/\.st/.test(stFile)) {
  334. console.ambercLog('Importing: ' + stFile);
  335. fs.readFile(stFile, 'utf8', function(err, data) {
  336. if (!err)
  337. resolve(data);
  338. else
  339. reject(Error('Could not import: ' + stFile));
  340. });
  341. }
  342. });
  343. })
  344. )
  345. .then(function(fileContents) {
  346. console.log('Compiling collected .st files');
  347. // import/compile content of .st files
  348. Promise.all(
  349. fileContents.map(function(code) {
  350. return new Promise(function(resolve, reject) {
  351. var importer = configuration.smalltalk.Importer._new();
  352. try {
  353. importer._import_(code._stream());
  354. resolve(true);
  355. } catch (ex) {
  356. reject(Error("Import error in section:\n" +
  357. importer._lastSection() + "\n\n" +
  358. "while processing chunk:\n" +
  359. importer._lastChunk() + "\n\n" +
  360. (ex._messageText && ex._messageText() || ex.message || ex))
  361. );
  362. }
  363. });
  364. })
  365. );
  366. })
  367. .then(function() {
  368. resolve(configuration);
  369. });
  370. });
  371. };
  372. /**
  373. * Export compiled categories to JavaScript files.
  374. * Returns a Promise() that resolves into the configuration object.
  375. */
  376. function category_export(configuration) {
  377. return new Promise(function(resolve, reject) {
  378. Promise.all(
  379. configuration.compile.map(function(stFile) {
  380. return new Promise(function(resolve, reject) {
  381. var category = path.basename(stFile, '.st');
  382. var jsFilePath = configuration.output_dir;
  383. if (undefined === jsFilePath) {
  384. jsFilePath = path.dirname(stFile);
  385. }
  386. var jsFile = category + configuration.suffix_used + '.js';
  387. jsFile = path.join(jsFilePath, jsFile);
  388. configuration.compiled.push(jsFile);
  389. var smalltalk = configuration.smalltalk;
  390. var packageObject = smalltalk.Package._named_(category);
  391. packageObject._transport()._namespace_(configuration.amd_namespace);
  392. fs.writeFile(jsFile, smalltalk.String._streamContents_(function (stream) {
  393. smalltalk.AmdExporter._new()._exportPackage_on_(packageObject, stream);
  394. }), function(err) {
  395. if (err)
  396. reject(err);
  397. else
  398. resolve(true);
  399. });
  400. });
  401. })
  402. ).then(function() {
  403. resolve(configuration);
  404. });
  405. });
  406. };
  407. /**
  408. * Verify if all .st files have been compiled.
  409. * Returns a Promise() that resolves into the configuration object.
  410. */
  411. function verify(configuration) {
  412. console.log('Verifying if all .st files were compiled');
  413. return new Promise(function(resolve, reject) {
  414. Promise.all(
  415. configuration.compiled.map(function(file) {
  416. return new Promise(function(resolve, reject) {
  417. fs.exists(file, function(exists) {
  418. if (exists)
  419. resolve(true);
  420. else
  421. reject(Error('Compilation failed of: ' + file));
  422. });
  423. });
  424. })
  425. ).then(function() {
  426. resolve(configuration);
  427. });
  428. });
  429. };
  430. /**
  431. * Synchronous function.
  432. * Concatenates compiled JavaScript files into one file in the correct order.
  433. * The name of the produced file is given by configuration.program.
  434. * Returns a Promise which resolves into the configuration object.
  435. */
  436. function compose_js_files(configuration) {
  437. return new Promise(function(resolve, reject) {
  438. var programFile = configuration.program;
  439. if (undefined === programFile) {
  440. return;
  441. }
  442. if (undefined !== configuration.output_dir) {
  443. programFile = path.join(configuration.output_dir, programFile);
  444. }
  445. var program_files = [];
  446. if (0 !== configuration.libraries.length) {
  447. console.log('Collecting libraries: ' + configuration.libraries);
  448. program_files.push.apply(program_files, configuration.libraries);
  449. }
  450. if (0 !== configuration.compiled.length) {
  451. var compiledFiles = configuration.compiled.slice(0);
  452. console.log('Collecting compiled files: ' + compiledFiles);
  453. program_files.push.apply(program_files, compiledFiles);
  454. }
  455. console.ambercLog('Writing program file: %s.js', programFile);
  456. var fileStream = fs.createWriteStream(programFile + configuration.suffix_used + '.js');
  457. fileStream.on('error', function(error) {
  458. fileStream.end();
  459. console.ambercLog(error);
  460. });
  461. fileStream.on('close', function(){
  462. return;
  463. });
  464. var builder = createConcatenator();
  465. builder.add('#!/usr/bin/env node');
  466. builder.start();
  467. program_files.forEach(function(file) {
  468. if(fs.existsSync(file)) {
  469. console.log('Adding : ' + file);
  470. var buffer = fs.readFileSync(file);
  471. // matches and returns the "module_id" string in the AMD define: define("module_id", ...)
  472. var match = buffer.toString().match(/^define\("([^"]*)"/);
  473. if (match /*&& match[1].slice(0,9) !== "amber_vm/"*/) {
  474. builder.addId(match[1]);
  475. }
  476. builder.add(buffer);
  477. } else {
  478. fileStream.end();
  479. reject(Error('Can not find file ' + file));
  480. }
  481. });
  482. var mainFunctionOrFile = '';
  483. if (undefined !== configuration.main) {
  484. console.log('Adding call to: %s>>main', configuration.main);
  485. mainFunctionOrFile += 'smalltalk.' + configuration.main + '._main();';
  486. }
  487. if (undefined !== configuration.mainfile && fs.existsSync(configuration.mainfile)) {
  488. console.log('Adding main file: ' + configuration.mainfile);
  489. mainFunctionOrFile += '\n' + fs.readFileSync(configuration.mainfile);
  490. }
  491. builder.finish(mainFunctionOrFile);
  492. console.log('Writing...');
  493. builder.forEach(function (element) {
  494. fileStream.write(element);
  495. fileStream.write('\n');
  496. });
  497. console.log('Done.');
  498. fileStream.end();
  499. resolve(configuration);
  500. });
  501. };
  502. module.exports.Compiler = AmberC;
  503. module.exports.createDefaultConfiguration = createDefaultConfiguration;