Skip to content

Commit

Permalink
feat: add ObjectTemplate::set_immutable_proto()
Browse files Browse the repository at this point in the history
This commit exposes SetImmutableProto().
  • Loading branch information
cjihrig committed May 19, 2022
1 parent 356a07c commit 39deaf9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,10 @@ void v8__ObjectTemplate__SetAccessorProperty(const v8::ObjectTemplate& self,
ptr_to_local(&key), ptr_to_local(&getter), ptr_to_local(&setter), attr);
}

void v8__ObjectTemplate__SetImmutableProto(const v8::ObjectTemplate& self) {
return ptr_to_local(&self)->SetImmutableProto();
}

const v8::Object* v8__Object__New(v8::Isolate* isolate) {
return local_to_ptr(v8::Object::New(isolate));
}
Expand Down
7 changes: 7 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extern "C" {
setter: *const FunctionTemplate,
attr: PropertyAttribute,
);
fn v8__ObjectTemplate__SetImmutableProto(this: *const ObjectTemplate);
}

impl Template {
Expand Down Expand Up @@ -362,4 +363,10 @@ impl ObjectTemplate {
)
}
}

/// Makes the ObjectTemplate for an immutable prototype exotic object,
/// with an immutable proto.
pub fn set_immutable_proto(&self) {
unsafe { v8__ObjectTemplate__SetImmutableProto(self) };
}
}
27 changes: 27 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,33 @@ fn object_template_from_function_template() {
}
}

#[test]
fn object_template_immutable_proto() {
let _setup_guard = setup();
let isolate = &mut v8::Isolate::new(Default::default());
{
let scope = &mut v8::HandleScope::new(isolate);
let object_templ = v8::ObjectTemplate::new(scope);
object_templ.set_immutable_proto();
let context = v8::Context::new_from_template(scope, object_templ);
let source = r#"
{
let r = 0;
try {
Object.setPrototypeOf(globalThis, {});
} catch {
r = 42;
}
r;
}
"#;
let actual = eval(scope, source).unwrap();
assert!(actual == 42);
}
}

#[test]
fn function_template_signature() {
let _setup_guard = setup();
Expand Down

0 comments on commit 39deaf9

Please sign in to comment.