amberc.js 17 KB

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