-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[test] Add tentative JS API tests for Exported GC Object #355
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb69ac6
[test] Add JS API tests for Exported GC Objects
takikawa 1779efb
Update getPrototypeOf test
takikawa 15156e0
Address PR feedback
takikawa 920e636
Rebase exported object test
takikawa 25fb7d4
Fix wasm-module-builder for some missed updates
takikawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// META: global=window,dedicatedworker,jsshell | ||
// META: script=/wasm/jsapi/wasm-module-builder.js | ||
|
||
let functions = {}; | ||
setup(() => { | ||
const builder = new WasmModuleBuilder(); | ||
|
||
const structIndex = builder.addStruct([makeField(kWasmI32, true)]); | ||
const arrayIndex = builder.addArray(kWasmI32, true); | ||
const structRef = wasmRefType(structIndex); | ||
const arrayRef = wasmRefType(arrayIndex); | ||
|
||
builder | ||
.addFunction("makeStruct", makeSig_r_v(structRef)) | ||
.addBody([...wasmI32Const(42), | ||
...GCInstr(kExprStructNew), structIndex]) | ||
.exportFunc(); | ||
|
||
builder | ||
.addFunction("makeArray", makeSig_r_v(arrayRef)) | ||
.addBody([...wasmI32Const(5), ...wasmI32Const(42), | ||
...GCInstr(kExprArrayNew), arrayIndex]) | ||
.exportFunc(); | ||
|
||
const buffer = builder.toBuffer(); | ||
const module = new WebAssembly.Module(buffer); | ||
const instance = new WebAssembly.Instance(module, {}); | ||
functions = instance.exports; | ||
}); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_equals(struct.foo, undefined); | ||
assert_equals(struct[0], undefined); | ||
assert_equals(array.foo, undefined); | ||
assert_equals(array[0], undefined); | ||
}, "property access"); | ||
|
||
test(() => { | ||
"use strict"; | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => { struct.foo = 5; }); | ||
assert_throws_js(TypeError, () => { array.foo = 5; }); | ||
assert_throws_js(TypeError, () => { struct[0] = 5; }); | ||
assert_throws_js(TypeError, () => { array[0] = 5; }); | ||
}, "property assignment (strict mode)"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => { struct.foo = 5; }); | ||
assert_throws_js(TypeError, () => { array.foo = 5; }); | ||
assert_throws_js(TypeError, () => { struct[0] = 5; }); | ||
assert_throws_js(TypeError, () => { array[0] = 5; }); | ||
}, "property assignment (non-strict mode)"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_equals(Object.getOwnPropertyNames(struct).length, 0); | ||
assert_equals(Object.getOwnPropertyNames(array).length, 0); | ||
}, "ownPropertyNames"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => Object.defineProperty(struct, "foo", { value: 1 })); | ||
assert_throws_js(TypeError, () => Object.defineProperty(array, "foo", { value: 1 })); | ||
}, "defineProperty"); | ||
|
||
test(() => { | ||
"use strict"; | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => delete struct.foo); | ||
assert_throws_js(TypeError, () => delete struct[0]); | ||
assert_throws_js(TypeError, () => delete array.foo); | ||
assert_throws_js(TypeError, () => delete array[0]); | ||
takikawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, "delete (strict mode)"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => delete struct.foo); | ||
assert_throws_js(TypeError, () => delete struct[0]); | ||
assert_throws_js(TypeError, () => delete array.foo); | ||
assert_throws_js(TypeError, () => delete array[0]); | ||
}, "delete (non-strict mode)"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_equals(Object.getPrototypeOf(struct), null); | ||
assert_equals(Object.getPrototypeOf(array), null); | ||
}, "getPrototypeOf"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => Object.setPrototypeOf(struct, {})); | ||
assert_throws_js(TypeError, () => Object.setPrototypeOf(array, {})); | ||
}, "setPrototypeOf"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_false(Object.isExtensible(struct)); | ||
assert_false(Object.isExtensible(array)); | ||
}, "isExtensible"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => Object.preventExtensions(struct)); | ||
assert_throws_js(TypeError, () => Object.preventExtensions(array)); | ||
}, "preventExtensions"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => Object.seal(struct)); | ||
assert_throws_js(TypeError, () => Object.seal(array)); | ||
}, "sealing"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_equals(typeof struct, "object"); | ||
assert_equals(typeof array, "object"); | ||
}, "typeof"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => struct.toString()); | ||
assert_equals(Object.prototype.toString.call(struct), "[object Object]"); | ||
Ms2ger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_throws_js(TypeError, () => array.toString()); | ||
assert_equals(Object.prototype.toString.call(array), "[object Object]"); | ||
}, "toString"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
assert_throws_js(TypeError, () => struct.valueOf()); | ||
assert_equals(Object.prototype.valueOf.call(struct), struct); | ||
assert_throws_js(TypeError, () => array.valueOf()); | ||
assert_equals(Object.prototype.valueOf.call(array), array); | ||
}, "valueOf"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
const map = new Map(); | ||
map.set(struct, "struct"); | ||
map.set(array, "array"); | ||
assert_equals(map.get(struct), "struct"); | ||
assert_equals(map.get(array), "array"); | ||
}, "GC objects as map keys"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
const set = new Set(); | ||
set.add(struct); | ||
set.add(array); | ||
assert_true(set.has(struct)); | ||
assert_true(set.has(array)); | ||
}, "GC objects as set element"); | ||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
const map = new WeakMap(); | ||
map.set(struct, "struct"); | ||
map.set(array, "array"); | ||
assert_equals(map.get(struct), "struct"); | ||
assert_equals(map.get(array), "array"); | ||
}, "GC objects as weak map keys"); | ||
takikawa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
test(() => { | ||
const struct = functions.makeStruct(); | ||
const array = functions.makeArray(); | ||
const set = new WeakSet(); | ||
set.add(struct); | ||
set.add(array); | ||
assert_true(set.has(struct)); | ||
assert_true(set.has(array)); | ||
}, "GC objects as weak set element"); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now [[Set]] is specified to always throw, which matches the test cases here.
It is arguably a bit inconsistent with the rest of JS though, because usually [[Set]] returns false on failure but doesn't throw. In the usual case, that means that in non-strict mode,
struct.foo = 5
will not add/mutate the field and just silently fail without throwing. With Wasm structs/arrays, it will throw even in non-strict mode. Not sure anyone will care about this difference, but I thought it might be worth mentioning.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, returning false probably makes more sense. In any case, we should probably make sure to test these cases both in strict and loose mode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense. I've changed the test now to have strict and non-strict versions, with no error raised in the non-strict version (and the property shouldn't be added/deleted). We'll have to change the spec to match this though.