await-call.js 2.4 KB

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