0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-24 08:09:16 -05:00

feat: Function::get/set_name() (#792)

Add bindings for v8::Function::GetName() and v8::Function::SetName()
This commit is contained in:
Aaron O'Mullan 2021-10-01 22:38:05 +02:00 committed by GitHub
parent 4e457758fe
commit 6ccf15c69d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 0 deletions

View file

@ -1556,6 +1556,14 @@ const v8::Object* v8__Function__NewInstance(const v8::Function& self,
ptr_to_local(&context), argc, const_ptr_array_to_local_array(argv))); ptr_to_local(&context), argc, const_ptr_array_to_local_array(argv)));
} }
const v8::Value* v8__Function__GetName(const v8::Function& self) {
return local_to_ptr(self.GetName());
}
void v8__Function__SetName(const v8::Function& self, const v8::String& name) {
return ptr_to_local(&self)->SetName(ptr_to_local(&name));
}
const v8::Signature* v8__Signature__New(v8::Isolate* isolate, const v8::Signature* v8__Signature__New(v8::Isolate* isolate,
const v8::FunctionTemplate* templ) { const v8::FunctionTemplate* templ) {
return local_to_ptr(v8::Signature::New(isolate, ptr_to_local(templ))); return local_to_ptr(v8::Signature::New(isolate, ptr_to_local(templ)));

View file

@ -15,6 +15,7 @@ use crate::Local;
use crate::Name; use crate::Name;
use crate::Object; use crate::Object;
use crate::Signature; use crate::Signature;
use crate::String;
use crate::Value; use crate::Value;
extern "C" { extern "C" {
@ -39,6 +40,8 @@ extern "C" {
argc: int, argc: int,
argv: *const *const Value, argv: *const *const Value,
) -> *const Object; ) -> *const Object;
fn v8__Function__GetName(this: *const Function) -> *const String;
fn v8__Function__SetName(this: *const Function, name: *const String);
fn v8__FunctionCallbackInfo__GetReturnValue( fn v8__FunctionCallbackInfo__GetReturnValue(
info: *const FunctionCallbackInfo, info: *const FunctionCallbackInfo,
@ -448,4 +451,12 @@ impl Function {
}) })
} }
} }
pub fn get_name<'s>(&self, scope: &mut HandleScope<'s>) -> Local<'s, String> {
unsafe { scope.cast_local(|_| v8__Function__GetName(self)).unwrap() }
}
pub fn set_name(&self, name: Local<String>) {
unsafe { v8__Function__SetName(self, &*name) }
}
} }

View file

@ -5485,3 +5485,69 @@ fn compiled_wasm_module() {
assert_eq!(foo_section, b"bar"); assert_eq!(foo_section, b"bar");
} }
} }
#[test]
fn function_names() {
// Setup isolate
let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
// Rust function
fn callback(
scope: &mut v8::HandleScope,
_args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
rv.set(v8::Integer::new(scope, 42).into())
}
// named v8 function
{
let key = v8::String::new(scope, "magicFn").unwrap();
let name = v8::String::new(scope, "fooBar").unwrap();
let tmpl = v8::FunctionTemplate::new(scope, callback);
let func = tmpl.get_function(scope).unwrap();
func.set_name(name);
let global = context.global(scope);
global.set(scope, key.into(), func.into());
let is_42: v8::Local<v8::Boolean> =
eval(scope, "magicFn() === 42").unwrap().try_into().unwrap();
assert!(is_42.is_true());
let js_str: v8::Local<v8::String> = eval(scope, "magicFn.toString()")
.unwrap()
.try_into()
.unwrap();
assert_eq!(
js_str.to_rust_string_lossy(scope),
"function fooBar() { [native code] }"
);
let v8_name = func.get_name(scope);
assert_eq!(v8_name.to_rust_string_lossy(scope), "fooBar");
}
// anon v8 function
{
let key = v8::String::new(scope, "anonFn").unwrap();
let tmpl = v8::FunctionTemplate::new(scope, callback);
let func = tmpl.get_function(scope).unwrap();
let global = context.global(scope);
global.set(scope, key.into(), func.into());
let is_42: v8::Local<v8::Boolean> =
eval(scope, "anonFn() === 42").unwrap().try_into().unwrap();
assert!(is_42.is_true());
let js_str: v8::Local<v8::String> = eval(scope, "anonFn.toString()")
.unwrap()
.try_into()
.unwrap();
assert_eq!(
js_str.to_rust_string_lossy(scope),
"function () { [native code] }"
);
let v8_name = func.get_name(scope);
assert_eq!(v8_name.to_rust_string_lossy(scope), "");
}
}