1
0

amberc.js 17 KB

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