| 1 |
// |jit-test| skip-if: !wasmDebuggingEnabled() |
| 2 |
// Tests that wasm module scripts has inspectable globals and memory. |
| 3 |
|
| 4 |
load(libdir + "wasm.js"); |
| 5 |
load(libdir + 'eqArrayHelper.js'); |
| 6 |
|
| 7 |
function monitorGlobalValues(wast, lib, expected) { |
| 8 |
function setupFrame(frame) { |
| 9 |
var globals = {}; |
| 10 |
framesGlobals.push(globals); |
| 11 |
// Environment with globals follow function scope enviroment |
| 12 |
var globalEnv = frame.environment.parent; |
| 13 |
globalEnv.names().forEach(n => { |
| 14 |
globals[n] = [globalEnv.getVariable(n)]; |
| 15 |
}); |
| 16 |
frame.onStep = function () { |
| 17 |
var globalEnv = frame.environment.parent; |
| 18 |
globalEnv.names().forEach(n => { |
| 19 |
var prevValues = globals[n]; |
| 20 |
if (!prevValues) |
| 21 |
globals[n] = prevValues = [void 0]; |
| 22 |
var value = globalEnv.getVariable(n); |
| 23 |
if (prevValues[prevValues.length - 1] !== value) |
| 24 |
prevValues.push(value); |
| 25 |
}); |
| 26 |
} |
| 27 |
} |
| 28 |
var framesGlobals = []; |
| 29 |
wasmRunWithDebugger(wast, lib, |
| 30 |
function ({dbg}) { |
| 31 |
dbg.onEnterFrame = function(frame) { |
| 32 |
if (frame.type == "wasmcall") |
| 33 |
setupFrame(frame); |
| 34 |
} |
| 35 |
}, |
| 36 |
function ({error}) { |
| 37 |
assertEq(error, undefined); |
| 38 |
} |
| 39 |
); |
| 40 |
assertEq(framesGlobals.length, expected.length); |
| 41 |
for (var i = 0; i < framesGlobals.length; i++) { |
| 42 |
var frameGlobals = framesGlobals[i]; |
| 43 |
var expectedGlobals = expected[i]; |
| 44 |
var globalsNames = Object.keys(frameGlobals); |
| 45 |
assertEq(globalsNames.length, Object.keys(expectedGlobals).length); |
| 46 |
globalsNames.forEach(n => { |
| 47 |
if (typeof expectedGlobals[n][0] === "function") { |
| 48 |
// expectedGlobals are assert functions |
| 49 |
expectedGlobals[n].forEach((assertFn, i) => { |
| 50 |
assertFn(frameGlobals[n][i]); |
| 51 |
}); |
| 52 |
return; |
| 53 |
} |
| 54 |
assertEqArray(frameGlobals[n], expectedGlobals[n]); |
| 55 |
}); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
monitorGlobalValues( |
| 60 |
'(module (func (export "test") (nop)))', |
| 61 |
undefined, |
| 62 |
[{}] |
| 63 |
); |
| 64 |
monitorGlobalValues( |
| 65 |
'(module (memory (export "memory") 1 1) (func (export "test") (nop) (nop)))', |
| 66 |
undefined, |
| 67 |
[{ |
| 68 |
memory0: [ |
| 69 |
function (actual) { |
| 70 |
var bufferProp = actual.proto.getOwnPropertyDescriptor("buffer"); |
| 71 |
assertEq(!!bufferProp, true, "wasm memory buffer property"); |
| 72 |
var buffer = bufferProp.get.call(actual).return; |
| 73 |
var bufferLengthProp = buffer.proto.getOwnPropertyDescriptor("byteLength"); |
| 74 |
var bufferLength = bufferLengthProp.get.call(buffer).return; |
| 75 |
assertEq(bufferLength, 65536, "wasm memory size"); |
| 76 |
} |
| 77 |
] |
| 78 |
}] |
| 79 |
); |
| 80 |
monitorGlobalValues( |
| 81 |
`(module\ |
| 82 |
(global i32 (i32.const 1))(global i64 (i64.const 2))(global f32 (f32.const 3.5))(global f64 (f64.const 42.25))\ |
| 83 |
(global externref (ref.null extern))\ |
| 84 |
(func (export "test") (nop)))`, |
| 85 |
undefined, |
| 86 |
[(function () { |
| 87 |
let x = { |
| 88 |
global0: [1], |
| 89 |
global1: [2], |
| 90 |
global2: [3.5], |
| 91 |
global3: [42.25], |
| 92 |
global4: [ function (x) { assertEq(x.optimizedOut, true); } ], |
| 93 |
}; |
| 94 |
return x; |
| 95 |
})()] |
| 96 |
); |
| 97 |
monitorGlobalValues( |
| 98 |
`(module (global (mut i32) (i32.const 1))(global (mut i64) (i64.const 2))\ |
| 99 |
(global (mut f32) (f32.const 3.5))(global (mut f64) (f64.const 42.25))\ |
| 100 |
(global (mut externref) (ref.null extern))\ |
| 101 |
(func (export "test")\ |
| 102 |
(i32.const 2)(global.set 0)(i64.const 1)(global.set 1)\ |
| 103 |
(f32.const 42.25)(global.set 2)(f64.const 3.5)(global.set 3)\ |
| 104 |
(ref.null extern)(global.set 4)))`, |
| 105 |
undefined, |
| 106 |
[(function () { |
| 107 |
let x = { |
| 108 |
global0: [1, 2], |
| 109 |
global1: [2, 1], |
| 110 |
global2: [3.5, 42.25], |
| 111 |
global3: [42.25, 3.5], |
| 112 |
global4: [ function (x) { assertEq(x.optimizedOut, true); } ], |
| 113 |
}; |
| 114 |
return x; |
| 115 |
})()] |
| 116 |
) |