Add test for exceptions thrown in v8::Object::Set

This commit is contained in:
Ben Grant
2024-08-29 17:35:44 -07:00
parent 8cb0b5db21
commit 0c91ddf367
3 changed files with 69 additions and 0 deletions

View File

@@ -248,6 +248,22 @@ void test_v8_object(const FunctionCallbackInfo<Value> &info) {
return ok(info);
}
void set_field_from_js(const FunctionCallbackInfo<Value> &info) {
Isolate *isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> obj = info[0].As<Object>();
Local<Value> key = info[1];
Local<Number> value = Number::New(isolate, 321.0);
Maybe<bool> ret = obj->Set(context, key, value);
LOG_EXPR(ret.IsJust());
if (ret.IsJust()) {
LOG_EXPR(ret.ToChecked());
}
return ok(info);
}
void test_v8_array_new(const FunctionCallbackInfo<Value> &info) {
Isolate *isolate = info.GetIsolate();
@@ -483,6 +499,7 @@ void initialize(Local<Object> exports, Local<Value> module,
test_v8_string_write_utf8);
NODE_SET_METHOD(exports, "test_v8_external", test_v8_external);
NODE_SET_METHOD(exports, "test_v8_object", test_v8_object);
NODE_SET_METHOD(exports, "set_field_from_js", set_field_from_js);
NODE_SET_METHOD(exports, "test_v8_array_new", test_v8_array_new);
NODE_SET_METHOD(exports, "test_v8_object_template", test_v8_object_template);
NODE_SET_METHOD(exports, "create_function_with_data",

View File

@@ -2,6 +2,7 @@ module.exports = debugMode => {
const nativeModule = require(`./build/${debugMode ? "Debug" : "Release"}/v8tests`);
return {
...nativeModule,
test_v8_global() {
console.log(nativeModule.global_get());
nativeModule.global_set(123);
@@ -12,6 +13,7 @@ module.exports = debugMode => {
}
console.log(JSON.stringify(nativeModule.global_get()));
},
test_v8_function_template() {
const f = nativeModule.create_function_with_data();
if (process.isBun) {
@@ -19,5 +21,52 @@ module.exports = debugMode => {
}
console.log(f());
},
test_v8_object_set_failure() {
const object = {};
const key = {
toString() {
throw new Error("thrown by key.toString()");
},
};
try {
nativeModule.set_field_from_js(object, key);
console.log("no error while setting with key that throws in toString()");
} catch (e) {
console.log(e.toString());
}
const setterThrows = new Proxy(object, {
set(obj, prop, value) {
throw new Error(`proxy setting ${prop} to ${value}`);
},
});
try {
nativeModule.set_field_from_js(setterThrows, "xyz");
console.log("no error while setting on Proxy that throws");
} catch (e) {
console.log(e.toString());
}
console.log("after setting, object.xyz is", object.xyz);
const onlyGetter = {
get foo() {
return 5;
},
};
try {
nativeModule.set_field_from_js(onlyGetter, "foo");
// apparently this is expected in node
console.log("no error while setting a key that only has a getter");
} catch (e) {
console.log(e.toString());
}
console.log("after setting, onlyGetter.foo is", onlyGetter.foo);
},
};
};

View File

@@ -139,6 +139,9 @@ describe("Object", () => {
it("can create an object and set properties", () => {
checkSameOutput("test_v8_object", []);
});
it("can handle failure in Set()", () => {
checkSameOutput("test_v8_object_set_failure", []);
});
});
describe("Array", () => {
// v8::Array::New is broken as it still tries to reinterpret locals as JSValues