table_imports.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var {fixupI64Return} = WScript.LoadScriptFile("./wasmutils.js");
  6. const module = new WebAssembly.Module(readbuffer('binaries/table_imports.wasm'));
  7. function customAdd(a, b) {
  8. print("custom add (+5.42)");
  9. return a + b + 5.42;
  10. }
  11. const types = [{
  12. name: "binopI32",
  13. start: 0,
  14. }, {
  15. name: "binopI64",
  16. start: 2,
  17. trap: [3] // tests that are expected to trap
  18. }, {
  19. name: "binopF32",
  20. start: 4,
  21. }, {
  22. name: "binopF64",
  23. start: 6,
  24. }];
  25. function runTests(exports) {
  26. types.forEach(({name, start, trap = []}) => {
  27. const end = start + 1; // only 2 methods for each types
  28. const isValidRange = i => i >= start && i <= end;
  29. for(let i = 0; i < 8; ++i) {
  30. try {
  31. const val = exports[name](1, 2, i);
  32. print(val);
  33. if (trap.includes(i)) {
  34. print(`${name}[${i}] failed. Expected to trap`);
  35. }
  36. } catch (e) {
  37. if (isValidRange(i) && !trap.includes(i)) {
  38. print(`${name}[${i}] failed. Unexpected error: ${e}`);
  39. }
  40. }
  41. }
  42. });
  43. }
  44. const {exports} = new WebAssembly.Instance(module, {
  45. math: {
  46. addI32: customAdd,
  47. addI64: customAdd,
  48. addF32: customAdd,
  49. addF64: customAdd,
  50. }
  51. });
  52. fixupI64Return(exports, "binopI64");
  53. runTests(exports);
  54. print("\n\n Rerun tests with new instance using previous module's imports");
  55. const {exports: exports2} = new WebAssembly.Instance(module, {math: exports});
  56. // int64 is no longer expected to trap when using a wasm module as import
  57. types[1].trap = [];
  58. fixupI64Return(exports2, "binopI64");
  59. runTests(exports2);