global.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. 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 cases = {
  7. basic: 0,
  8. export: 1,
  9. exportMut: 2,
  10. mutable: 3,
  11. import: 4,
  12. impInit: 5,
  13. impInitMut: 6,
  14. count: 7,
  15. };
  16. const casesNames = Object.keys(cases);
  17. const invalidCases = {
  18. i32: [cases.exportMut, cases.impInitMut],
  19. i64: [cases.export, cases.exportMut, cases.import, cases.impInit, cases.impInitMut],
  20. f32: [cases.exportMut, cases.impInitMut],
  21. f64: [cases.exportMut, cases.impInitMut],
  22. };
  23. const mod1 = new WebAssembly.Module(readbuffer("binaries/global.wasm"));
  24. const {exports} = new WebAssembly.Instance(mod1, {test: {
  25. i32: 234,
  26. i64: () => console.log("shouldn't use i64 import"),
  27. f32: 8.47,
  28. f64: 78.145
  29. }});
  30. fixupI64Return(exports, "get-i64");
  31. function printAllGlobals(type) {
  32. console.log(`Print all ${type}`);
  33. const getter = exports[`get-${type}`];
  34. // print exported global
  35. console.log(`exported ${type}: ${exports[type]}`);
  36. for(let iCase = 0; iCase < cases.count; ++iCase) {
  37. const caseName = casesNames[iCase];
  38. try {
  39. const val = getter(iCase);
  40. console.log(`${caseName}: ${val}`);
  41. } catch (e) {
  42. if (!(e instanceof WebAssembly.RuntimeError && invalidCases[type].includes(iCase))) {
  43. console.log(`${caseName}: Unexpected error thrown: ${e}`);
  44. }
  45. }
  46. }
  47. console.log("")
  48. }
  49. ["i32", "i64", "f32", "f64"].forEach(printAllGlobals);
  50. console.log("Modify mutable globals");
  51. exports["set-i32"](456789);
  52. exports["set-i64"]({high: -0xD2A08, low: 0x70000000});
  53. exports["set-f32"](45.78);
  54. exports["set-f64"](65.7895);
  55. ["i32", "i64", "f32", "f64"].forEach(printAllGlobals);
  56. console.log("Invalid cases");
  57. const mod3 = new WebAssembly.Module(readbuffer("binaries/i64_invalid_global_import.wasm"));
  58. try {
  59. new WebAssembly.Instance(mod3, {test: {global: 5}});
  60. console.log("should have trap");
  61. } catch (e) {
  62. if (e instanceof TypeError) {
  63. console.log(`Should be invalid type conversion: ${e.message}`);
  64. } else {
  65. console.log(`Invalid error ${e}`);
  66. }
  67. }
  68. const mod4 = new WebAssembly.Module(readbuffer("binaries/i64_invalid_global_export.wasm"));
  69. try {
  70. new WebAssembly.Instance(mod4, {});
  71. console.log("should have trap");
  72. } catch (e) {
  73. if (e instanceof TypeError) {
  74. console.log(`Should be invalid type conversion: ${e.message}`);
  75. } else {
  76. console.log(`Invalid error ${e}`);
  77. }
  78. }
  79. try {
  80. const mod5 = new WebAssembly.Module(readbuffer("binaries/invalid_global_init.wasm"));
  81. console.log("should have trap");
  82. } catch (e) {
  83. if (e instanceof WebAssembly.CompileError) {
  84. console.log(`Should be invalid init expr: ${e.message}`);
  85. } else {
  86. console.log(`Invalid error ${e}`);
  87. }
  88. }