load-async.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // -*- js-indent-level: 4 -*-
  2. var waitFor;
  3. if (typeof drainJobQueue !== 'undefined') {
  4. waitFor = function waitFor(p) { drainJobQueue(); return p; };
  5. } else {
  6. // JSC and V8 will drain promises before exiting and don't require a
  7. // specific waiter.
  8. waitFor = function waitFor(p) { return p; };
  9. }
  10. var args;
  11. if (typeof process !== 'undefined') {
  12. args = process.argv.slice(3);
  13. } else if (typeof scriptArgs !== 'undefined') {
  14. args = scriptArgs;
  15. } else if (typeof arguments !== 'undefined') {
  16. args = arguments;
  17. } else {
  18. // No script arguments available
  19. args = [];
  20. }
  21. var log;
  22. var logErr;
  23. if (typeof print !== 'undefined') {
  24. log = print;
  25. } else if (typeof console !== 'undefined') {
  26. log = console.log.bind(console);
  27. }
  28. if (typeof printErr !== 'undefined') {
  29. logErr = printErr;
  30. } else {
  31. logErr = log;
  32. }
  33. var _exit;
  34. if (typeof quit !== 'undefined') {
  35. _exit = quit.bind(this);
  36. } else if (typeof testRunner !== 'undefined') {
  37. _exit = testRunner.quit.bind(testRunner);
  38. } else if (typeof process !== 'undefined') {
  39. _exit = process.exit.bind(process);
  40. }
  41. var _load;
  42. if (typeof load !== 'undefined') {
  43. _load = load;
  44. } else if (typeof require !== 'undefined') {
  45. _load = require;
  46. }
  47. // V8 treats multiple arguments as files, unless -- is given, but
  48. // SpiderMonkey doesn't treat -- specially. This is a hack to allow
  49. // for -- on SpiderMonkey.
  50. if (args[0] == '--') {
  51. args.shift();
  52. }
  53. if ((args.length < 3) || (args.length > 4)) {
  54. logErr('usage: load.js REFLECT_JS_DIR REFLECT_WASM_DIR FOO.WASM [USER_IMPORTS]');
  55. _exit(1);
  56. }
  57. async function runTest(wasmFile, opts) {
  58. try {
  59. let [proc] = await Scheme.load_main(wasmFile, opts);
  60. log(repr(await proc.call_async()));
  61. } catch (e) {
  62. if (e instanceof hoot.SchemeQuitError) {
  63. _exit(e.status);
  64. } else {
  65. log(`error: ${e} (${e.stack})`);
  66. _exit(1);
  67. }
  68. }
  69. }
  70. var [reflect_js_dir, reflect_wasm_dir, test_wasm, user_imports_file] = args;
  71. var hoot = _load(`${reflect_js_dir}/reflect.js`);
  72. if (typeof hoot !== 'undefined') {
  73. Scheme = hoot.Scheme;
  74. repr = hoot.repr;
  75. }
  76. var user_imports = {};
  77. if (user_imports_file) {
  78. user_imports = _load(user_imports_file).user_imports;
  79. if (typeof user_imports === 'undefined') {
  80. logErr(`user imports file ${user_imports_file} failed to load`);
  81. _exit(1);
  82. }
  83. }
  84. waitFor(runTest(test_wasm, {reflect_wasm_dir, user_imports}));