reload.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. function test(shared) {
  6. if (shared) {
  7. WScript.Flag("-WasmThreads");
  8. }
  9. const memPrefix = shared ? "i32.atomic" : "i32";
  10. const buf = WebAssembly.wabt.convertWast2Wasm(`
  11. (module
  12. (import "test" "grow" (func $grow_import))
  13. (memory (export "mem") 1 100 ${shared ? "shared" : ""})
  14. (func $grow (memory.grow (i32.const 1)) drop)
  15. (func $empty)
  16. (func (export "test") (param $i i32) (result i32)
  17. (local $v i32)
  18. (i32.ge_s (get_local $i) (i32.const 10))
  19. (if (result i32)
  20. ;; not growing scenario
  21. (then
  22. (${memPrefix}.store (i32.const 0) (get_local $i))
  23. (call $empty)
  24. (i32.load (i32.const 0))
  25. (set_local $v (i32.add (i32.const 1)))
  26. (i32.store (i32.const 2) (get_local $v))
  27. (call $empty)
  28. (${memPrefix}.load (i32.const 0))
  29. )
  30. ;; growing scenario
  31. (else
  32. (${memPrefix}.store (i32.const 0) (get_local $i))
  33. (call $grow)
  34. (${memPrefix}.load (i32.const 0))
  35. (set_local $v (i32.add (get_local $i)))
  36. (i32.store (i32.const 2) (get_local $v))
  37. (call $grow_import)
  38. (${memPrefix}.load (i32.const 0))
  39. )
  40. )
  41. )
  42. )`);
  43. const hex = v => "0x" + v.toString(16);
  44. function validate(res, val) {
  45. const growing = val < 10;
  46. const expected = !growing
  47. ? (val & 0xFFFF) + (((val + 1) << 16) & 0xFFFFFFFF)
  48. : (val & 0xFFFF) + (((val + val) << 16) & 0xFFFFFFFF);
  49. if (res !== expected) {
  50. const testType = growing ? ".grow" : "";
  51. const sharing = shared ? "shared memory" : "normal memory";
  52. console.log(`${sharing}: test${testType}(${val}) Invalid value. Expected ${hex(expected)}, got ${hex(res)}`);
  53. }
  54. }
  55. const mod = new WebAssembly.Module(buf);
  56. const {exports} = new WebAssembly.Instance(mod, {
  57. test: {grow() {
  58. try {
  59. // try to detach, should not be possible
  60. ArrayBuffer.detach(exports.mem.buffer);
  61. console.log("ArrayBuffer.detach should not have succeeded");
  62. } catch (e) {
  63. if (
  64. // WebAssemblySharedArrayBuffer are not classic ArrayBuffer
  65. (shared && !e.message.includes("ArrayBuffer object expected")) ||
  66. (!shared && !e.message.includes("Not allowed to detach WebAssembly.Memory buffer"))
  67. ) {
  68. console.log(e);
  69. }
  70. }
  71. exports.mem.grow(1);
  72. }}
  73. });
  74. validate(exports.test(5), 5);
  75. for (let i = 0; i < 100; ++i) {
  76. // Warm up with empty calls
  77. const val = ((i % 20) + 15)|0;
  78. validate(exports.test(val), val);
  79. }
  80. validate(exports.test(5), 5);
  81. }
  82. test(false);
  83. test(true);
  84. console.log("pass");