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

Add v8::FunctionCallbackInfo::NewTarget bindings (#898)

This commit is contained in:
Divy Srivastava 2022-02-11 20:51:13 +05:30 committed by GitHub
parent 378631e793
commit 41003d097d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 0 deletions

View file

@ -1714,6 +1714,11 @@ const v8::Value* v8__FunctionCallbackInfo__Data(
return local_to_ptr(self.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, void v8__ReturnValue__Set(v8::ReturnValue<v8::Value>* self,
const v8::Value& value) { const v8::Value& value) {
self->Set(ptr_to_local(&value)); self->Set(ptr_to_local(&value));

View file

@ -66,6 +66,9 @@ extern "C" {
fn v8__FunctionCallbackInfo__Data( fn v8__FunctionCallbackInfo__Data(
this: *const FunctionCallbackInfo, this: *const FunctionCallbackInfo,
) -> *const Value; ) -> *const Value;
fn v8__FunctionCallbackInfo__NewTarget(
this: *const FunctionCallbackInfo,
) -> *const Value;
fn v8__PropertyCallbackInfo__GetReturnValue( fn v8__PropertyCallbackInfo__GetReturnValue(
this: *const PropertyCallbackInfo, this: *const PropertyCallbackInfo,
@ -215,6 +218,13 @@ impl<'s> FunctionCallbackArguments<'s> {
.unwrap() .unwrap()
} }
} }
/// For construct calls, this returns the "new.target" value.
pub fn new_target(&self) -> Local<'s, Value> {
unsafe {
Local::from_raw(v8__FunctionCallbackInfo__NewTarget(self.info)).unwrap()
}
}
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -1952,6 +1952,21 @@ fn fn_callback(
rv.set(s.into()); 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( fn fn_callback2(
scope: &mut v8::HandleScope, scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments, args: v8::FunctionCallbackArguments,
@ -2073,6 +2088,16 @@ fn function() {
let result = eval(scope, "f.prototype").unwrap(); let result = eval(scope, "f.prototype").unwrap();
assert!(result.is_undefined()); assert!(result.is_undefined());
assert!(eval(scope, "new f()").is_none()); // throws 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));
} }
} }