From 9c0a61c3f9b3f73842b345899cbed97dd96e885e Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 14:32:44 +0200 Subject: [PATCH 1/7] feat: Function::get/set_name() Add bindings for v8::Function::GetName() and v8::Function::SetName() --- src/binding.cc | 8 ++++++++ src/function.rs | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index c344306eef..df98753bd5 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -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 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))); diff --git a/src/function.rs b/src/function.rs index e39bb8b0c7..df66c60733 100644 --- a/src/function.rs +++ b/src/function.rs @@ -15,6 +15,7 @@ use crate::Local; use crate::Name; use crate::Object; use crate::Signature; +use crate::String; use crate::Value; extern "C" { @@ -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, @@ -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) { + unsafe { v8__Function__SetName(self, &*name) } + } } From 938cfa746f8f526bef370b93f87eae02e504f271 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 14:44:15 +0200 Subject: [PATCH 2/7] fix --- src/binding.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index df98753bd5..0511396b06 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1561,7 +1561,7 @@ const v8::Value* v8__Function__GetName(const v8::Function& self) { } void v8__Function_SetName(const v8::Function& self, const v8::String& name) { - return self.SetName(ptr_to_local(name)); + return ptr_to_local(&self)->SetName(ptr_to_local(&name)); } const v8::Signature* v8__Signature__New(v8::Isolate* isolate, From 10765f6f340ef335ff717a194beb02b7437d821d Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 14:55:57 +0200 Subject: [PATCH 3/7] fmt --- src/function.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/function.rs b/src/function.rs index df66c60733..3f6b6e6f39 100644 --- a/src/function.rs +++ b/src/function.rs @@ -451,11 +451,11 @@ 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) { unsafe { v8__Function__SetName(self, &*name) } } From de5b396afae5a9bd45c25010d563a894c9e0aa34 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 15:38:44 +0200 Subject: [PATCH 4/7] dumb typo ... --- src/binding.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index 0511396b06..2923fbd1c5 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1560,7 +1560,7 @@ 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) { +void v8__Function__SetName(const v8::Function& self, const v8::String& name) { return ptr_to_local(&self)->SetName(ptr_to_local(&name)); } From 44ebc60c4fce8c7242a3511272648b8110c6d948 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 19:45:01 +0200 Subject: [PATCH 5/7] add tests --- tests/test_api.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/test_api.rs b/tests/test_api.rs index ffc55ff016..28be83cb83 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5485,3 +5485,71 @@ 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 = eval(scope, "(magicFn() === 42)") + .unwrap() + .try_into() + .unwrap(); + assert!(is_42.is_true()); + let js_str: v8::Local = 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 = + eval(scope, "anonFn() === 42").unwrap().try_into().unwrap(); + assert!(is_42.is_true()); + let js_str: v8::Local = 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), ""); + } +} From a7e7dd9fcceb647ca5c302d8bc5545dbec94adf7 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 19:48:45 +0200 Subject: [PATCH 6/7] tweak --- tests/test_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index 28be83cb83..82e8cc469c 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5513,7 +5513,7 @@ fn function_names() { let global = context.global(scope); global.set(scope, key.into(), func.into()); - let is_42: v8::Local = eval(scope, "(magicFn() === 42)") + let is_42: v8::Local = eval(scope, "magicFn() === 42") .unwrap() .try_into() .unwrap(); From 48126b24ca3df9fc80a0839e15d014dc4a1d2f35 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 1 Oct 2021 20:03:36 +0200 Subject: [PATCH 7/7] fmt --- tests/test_api.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_api.rs b/tests/test_api.rs index 82e8cc469c..1ebecf65d2 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -5513,10 +5513,8 @@ fn function_names() { let global = context.global(scope); global.set(scope, key.into(), func.into()); - let is_42: v8::Local = eval(scope, "magicFn() === 42") - .unwrap() - .try_into() - .unwrap(); + let is_42: v8::Local = + eval(scope, "magicFn() === 42").unwrap().try_into().unwrap(); assert!(is_42.is_true()); let js_str: v8::Local = eval(scope, "magicFn.toString()") .unwrap()