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

Add v8::FunctionCallbackInfo::NewTarget bindings #898

Merged
merged 4 commits into from
Feb 11, 2022
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
5 changes: 5 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,11 @@ const v8::Value* v8__FunctionCallbackInfo__Data(
return local_to_ptr(self.Data());
}

const v8::Value* v8__FunctionCallbackInfo__NewTarget(
const v8::FunctionCallbackInfo<v8::Value>& self) {
return local_to_ptr(self.NewTarget());
}

void v8__ReturnValue__Set(v8::ReturnValue<v8::Value>* self,
const v8::Value& value) {
self->Set(ptr_to_local(&value));
Expand Down
10 changes: 10 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extern "C" {
fn v8__FunctionCallbackInfo__Data(
this: *const FunctionCallbackInfo,
) -> *const Value;
fn v8__FunctionCallbackInfo__NewTarget(
this: *const FunctionCallbackInfo,
) -> *const Value;

fn v8__PropertyCallbackInfo__GetReturnValue(
this: *const PropertyCallbackInfo,
Expand Down Expand Up @@ -215,6 +218,13 @@ impl<'s> FunctionCallbackArguments<'s> {
.unwrap()
}
}

/// For construct calls, this returns the "new.target" value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What value does it return for non-construct calls?

Copy link
Member Author

@littledivy littledivy Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v8::undefined

pub fn new_target(&self) -> Local<'s, Value> {
unsafe {
Local::from_raw(v8__FunctionCallbackInfo__NewTarget(self.info)).unwrap()
}
}
}

#[derive(Debug)]
Expand Down
25 changes: 25 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,21 @@ fn fn_callback(
rv.set(s.into());
}

fn fn_callback_new(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut rv: v8::ReturnValue,
) {
assert_eq!(args.length(), 0);
assert!(args.new_target().is_object());
let recv = args.this();
let key = v8::String::new(scope, "works").unwrap();
let value = v8::Boolean::new(scope, true);
assert!(recv.set(scope, key.into(), value.into()).unwrap());
assert!(rv.get(scope).is_undefined());
rv.set(recv.into());
}

fn fn_callback2(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
Expand Down Expand Up @@ -2070,6 +2085,16 @@ fn function() {
let result = eval(scope, "f.prototype").unwrap();
assert!(result.is_undefined());
assert!(eval(scope, "new f()").is_none()); // throws

let function = v8::Function::builder(fn_callback_new).build(scope).unwrap();
let name = v8::String::new(scope, "f2").unwrap();
global.set(scope, name.into(), function.into()).unwrap();
let f2: v8::Local<v8::Object> =
eval(scope, "new f2()").unwrap().try_into().unwrap();
let key = v8::String::new(scope, "works").unwrap();
let value = f2.get(scope, key.into()).unwrap();
assert!(value.is_boolean());
assert!(value.boolean_value(scope));
}
}

Expand Down