0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-12 00:54:15 -05:00

Add FunctionTemplate::set_class_name() (#225)

This commit is contained in:
Ben Noordhuis 2020-01-19 00:38:41 +01:00 committed by Bert Belder
parent 7b0269b447
commit 2db5e10b9f
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
3 changed files with 23 additions and 4 deletions

View file

@ -897,6 +897,11 @@ v8::Function* v8__FunctionTemplate__GetFunction(
return maybe_local_to_ptr(self->GetFunction(context));
}
void v8__FunctionTemplate__SetClassName(
v8::Local<v8::FunctionTemplate> self, v8::Local<v8::String> name) {
self->SetClassName(name);
}
v8::Isolate* v8__FunctionCallbackInfo__GetIsolate(
const v8::FunctionCallbackInfo<v8::Value>& self) {
return self.GetIsolate();

View file

@ -10,6 +10,7 @@ use crate::Function;
use crate::FunctionCallback;
use crate::Local;
use crate::Object;
use crate::String;
use crate::ToLocal;
extern "C" {
@ -23,6 +24,10 @@ extern "C" {
fn_template: *mut FunctionTemplate,
context: *mut Context,
) -> *mut Function;
fn v8__FunctionTemplate__SetClassName(
fn_template: *mut FunctionTemplate,
name: Local<String>,
) -> *mut Function;
fn v8__ObjectTemplate__New(
isolate: *mut Isolate,
@ -65,6 +70,13 @@ impl FunctionTemplate {
.to_local(v8__FunctionTemplate__GetFunction(&mut *self, &mut *context))
}
}
/// Set the class name of the FunctionTemplate. This is used for
/// printing objects created with the function created from the
/// FunctionTemplate as its constructor.
pub fn set_class_name(&mut self, name: Local<String>) {
unsafe { v8__FunctionTemplate__SetClassName(&mut *self, name) };
}
}
impl ObjectTemplate {

View file

@ -895,7 +895,10 @@ fn object_template_from_function_template() {
{
let mut hs = v8::HandleScope::new(&mut locker);
let scope = hs.enter();
let function_templ = v8::FunctionTemplate::new(scope, fortytwo_callback);
let mut function_templ =
v8::FunctionTemplate::new(scope, fortytwo_callback);
let expected_class_name = v8_str(scope, "fortytwo");
function_templ.set_class_name(expected_class_name);
let object_templ =
v8::ObjectTemplate::new_from_template(scope, function_templ);
let context = v8::Context::new(scope);
@ -907,9 +910,8 @@ fn object_template_from_function_template() {
context
.global(scope)
.set(context, name.into(), object.into());
let actual = eval(scope, context, "g.constructor.name").unwrap();
let expected = v8::String::new(scope, "").unwrap();
assert!(expected.strict_equals(actual));
let actual_class_name = eval(scope, context, "g.constructor.name").unwrap();
assert!(expected_class_name.strict_equals(actual_class_name));
}
}