Skip to content
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

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

Merged
merged 7 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

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::FunctionTemplate* templ) {
return local_to_ptr(v8::Signature::New(isolate, ptr_to_local(templ)));
Expand Down
11 changes: 11 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::Local;
use crate::Name;
use crate::Object;
use crate::Signature;
use crate::String;
use crate::Value;

extern "C" {
Expand All @@ -39,6 +40,8 @@ extern "C" {
argc: int,
argv: *const *const Value,
) -> *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(
info: *const FunctionCallbackInfo,
Expand Down Expand Up @@ -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) }
}
}
66 changes: 66 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5485,3 +5485,69 @@ fn compiled_wasm_module() {
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), "");
}
}