await-call.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. log(`error: ${e} (${e.stack})`);
  70. _exit(1);
  71. }
  72. }
  73. var [reflect_js_dir, reflect_wasm_dir, ...test_call] = args;
  74. var hoot = _load(`${reflect_js_dir}/reflect.js`);
  75. if (typeof hoot !== 'undefined') {
  76. Scheme = hoot.Scheme;
  77. repr = hoot.repr;
  78. }
  79. waitFor(runTest(test_call, {reflect_wasm_dir}));