-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathfunction.rs
1123 lines (1021 loc) · 32.6 KB
/
function.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::convert::TryFrom;
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::ptr::null;
use crate::Array;
use crate::Boolean;
use crate::Context;
use crate::Function;
use crate::HandleScope;
use crate::Integer;
use crate::Isolate;
use crate::Local;
use crate::Name;
use crate::Object;
use crate::PropertyDescriptor;
use crate::Signature;
use crate::String;
use crate::UniqueRef;
use crate::Value;
use crate::scope::CallbackScope;
use crate::script_compiler::CachedData;
use crate::support::MapFnFrom;
use crate::support::MapFnTo;
use crate::support::ToCFn;
use crate::support::UnitType;
use crate::support::{Opaque, int};
use crate::template::Intercepted;
use crate::{ScriptOrigin, undefined};
unsafe extern "C" {
fn v8__Function__New(
context: *const Context,
callback: FunctionCallback,
data_or_null: *const Value,
length: i32,
constructor_behavior: ConstructorBehavior,
side_effect_type: SideEffectType,
) -> *const Function;
fn v8__Function__Call(
this: *const Function,
context: *const Context,
recv: *const Value,
argc: int,
argv: *const *const Value,
) -> *const Value;
fn v8__Function__NewInstance(
this: *const Function,
context: *const Context,
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__Function__GetScriptColumnNumber(this: *const Function) -> int;
fn v8__Function__GetScriptLineNumber(this: *const Function) -> int;
fn v8__Function__ScriptId(this: *const Function) -> int;
fn v8__Function__GetScriptOrigin(
this: *const Function,
) -> *const ScriptOrigin<'static>;
fn v8__Function__CreateCodeCache(
script: *const Function,
) -> *mut CachedData<'static>;
static v8__FunctionCallbackInfo__kArgsLength: int;
fn v8__FunctionCallbackInfo__Data(
this: *const FunctionCallbackInfo,
) -> *const Value;
fn v8__PropertyCallbackInfo__GetIsolate(
this: *const RawPropertyCallbackInfo,
) -> *mut Isolate;
fn v8__PropertyCallbackInfo__Data(
this: *const RawPropertyCallbackInfo,
) -> *const Value;
fn v8__PropertyCallbackInfo__This(
this: *const RawPropertyCallbackInfo,
) -> *const Object;
fn v8__PropertyCallbackInfo__Holder(
this: *const RawPropertyCallbackInfo,
) -> *const Object;
fn v8__PropertyCallbackInfo__GetReturnValue(
this: *const RawPropertyCallbackInfo,
) -> usize;
fn v8__PropertyCallbackInfo__ShouldThrowOnError(
this: *const RawPropertyCallbackInfo,
) -> bool;
fn v8__ReturnValue__Value__Set(
this: *mut RawReturnValue,
value: *const Value,
);
fn v8__ReturnValue__Value__Set__Bool(this: *mut RawReturnValue, value: bool);
fn v8__ReturnValue__Value__Set__Int32(this: *mut RawReturnValue, value: i32);
fn v8__ReturnValue__Value__Set__Uint32(this: *mut RawReturnValue, value: u32);
fn v8__ReturnValue__Value__Set__Double(this: *mut RawReturnValue, value: f64);
fn v8__ReturnValue__Value__SetNull(this: *mut RawReturnValue);
fn v8__ReturnValue__Value__SetUndefined(this: *mut RawReturnValue);
fn v8__ReturnValue__Value__SetEmptyString(this: *mut RawReturnValue);
fn v8__ReturnValue__Value__Get(this: *const RawReturnValue) -> *const Value;
}
// Ad-libbed - V8 does not document ConstructorBehavior.
/// ConstructorBehavior::Allow creates a regular API function.
///
/// ConstructorBehavior::Throw creates a "concise" API function, a function
/// without a ".prototype" property, that is somewhat faster to create and has
/// a smaller footprint. Functionally equivalent to ConstructorBehavior::Allow
/// followed by a call to FunctionTemplate::RemovePrototype().
#[repr(C)]
pub enum ConstructorBehavior {
Throw,
Allow,
}
/// Options for marking whether callbacks may trigger JS-observable side
/// effects. Side-effect-free callbacks are allowlisted during debug evaluation
/// with throwOnSideEffect. It applies when calling a Function,
/// FunctionTemplate, or an Accessor callback. For Interceptors, please see
/// PropertyHandlerFlags's kHasNoSideEffect.
/// Callbacks that only cause side effects to the receiver are allowlisted if
/// invoked on receiver objects that are created within the same debug-evaluate
/// call, as these objects are temporary and the side effect does not escape.
#[repr(C)]
pub enum SideEffectType {
HasSideEffect,
HasNoSideEffect,
HasSideEffectToReceiver,
}
#[repr(C)]
#[derive(Debug)]
struct RawReturnValue(usize);
// Note: the 'cb lifetime is required because the ReturnValue object must not
// outlive the FunctionCallbackInfo/PropertyCallbackInfo object from which it
// is derived.
#[derive(Debug)]
pub struct ReturnValue<'cb, T = Value>(RawReturnValue, PhantomData<&'cb T>);
impl<'cb, T> ReturnValue<'cb, T> {
#[inline(always)]
pub fn from_property_callback_info(
info: &'cb PropertyCallbackInfo<T>,
) -> Self {
Self(
unsafe {
RawReturnValue(v8__PropertyCallbackInfo__GetReturnValue(&info.0))
},
PhantomData,
)
}
}
impl<'cb> ReturnValue<'cb, Value> {
#[inline(always)]
pub fn from_function_callback_info(info: &'cb FunctionCallbackInfo) -> Self {
let nn = info.get_return_value_non_null();
Self(RawReturnValue(nn.as_ptr() as _), PhantomData)
}
}
impl ReturnValue<'_, ()> {
#[inline(always)]
pub fn set_bool(&mut self, value: bool) {
unsafe { v8__ReturnValue__Value__Set__Bool(&mut self.0, value) }
}
}
impl<T> ReturnValue<'_, T>
where
for<'s> Local<'s, T>: Into<Local<'s, Value>>,
{
#[inline(always)]
pub fn set(&mut self, value: Local<T>) {
unsafe { v8__ReturnValue__Value__Set(&mut self.0, &*value.into()) }
}
#[inline(always)]
pub fn set_bool(&mut self, value: bool) {
unsafe { v8__ReturnValue__Value__Set__Bool(&mut self.0, value) }
}
#[inline(always)]
pub fn set_int32(&mut self, value: i32) {
unsafe { v8__ReturnValue__Value__Set__Int32(&mut self.0, value) }
}
#[inline(always)]
pub fn set_uint32(&mut self, value: u32) {
unsafe { v8__ReturnValue__Value__Set__Uint32(&mut self.0, value) }
}
#[inline(always)]
pub fn set_double(&mut self, value: f64) {
unsafe { v8__ReturnValue__Value__Set__Double(&mut self.0, value) }
}
#[inline(always)]
pub fn set_null(&mut self) {
unsafe { v8__ReturnValue__Value__SetNull(&mut self.0) }
}
#[inline(always)]
pub fn set_undefined(&mut self) {
unsafe { v8__ReturnValue__Value__SetUndefined(&mut self.0) }
}
#[inline(always)]
pub fn set_empty_string(&mut self) {
unsafe { v8__ReturnValue__Value__SetEmptyString(&mut self.0) }
}
/// Getter. Creates a new Local<> so it comes with a certain performance
/// hit. If the ReturnValue was not yet set, this will return the undefined
/// value.
#[inline(always)]
pub fn get<'s>(&self, scope: &mut HandleScope<'s>) -> Local<'s, Value> {
unsafe { scope.cast_local(|_| v8__ReturnValue__Value__Get(&self.0)) }
.unwrap()
}
}
/// The argument information given to function call callbacks. This
/// class provides access to information about the context of the call,
/// including the receiver, the number and values of arguments, and
/// the holder of the function.
#[repr(C)]
#[derive(Debug)]
pub struct FunctionCallbackInfo {
// The layout of this struct must match that of `class FunctionCallbackInfo`
// as defined in v8.h.
implicit_args: *mut *const Opaque,
values: *mut *const Opaque,
length: int,
}
// These constants must match those defined on `class FunctionCallbackInfo` in
// v8-function-callback.h.
#[allow(dead_code, non_upper_case_globals)]
impl FunctionCallbackInfo {
const kHolderIndex: i32 = 0;
const kIsolateIndex: i32 = 1;
const kContextIndex: i32 = 2;
const kReturnValueIndex: i32 = 3;
const kTargetIndex: i32 = 4;
const kNewTargetIndex: i32 = 5;
const kArgsLength: i32 = 6;
}
impl FunctionCallbackInfo {
#[inline(always)]
pub(crate) fn get_isolate_ptr(&self) -> *mut Isolate {
let arg_nn =
self.get_implicit_arg_non_null::<*mut Isolate>(Self::kIsolateIndex);
*unsafe { arg_nn.as_ref() }
}
#[inline(always)]
pub(crate) fn get_return_value_non_null(&self) -> NonNull<Value> {
self.get_implicit_arg_non_null::<Value>(Self::kReturnValueIndex)
}
#[inline(always)]
pub(crate) fn new_target(&self) -> Local<Value> {
unsafe { self.get_implicit_arg_local(Self::kNewTargetIndex) }
}
#[inline(always)]
pub(crate) fn this(&self) -> Local<Object> {
unsafe { self.get_arg_local(-1) }
}
#[inline(always)]
pub(crate) fn data(&self) -> Local<Value> {
unsafe {
let ptr = v8__FunctionCallbackInfo__Data(self);
let nn = NonNull::new_unchecked(ptr as *mut Value);
Local::from_non_null(nn)
}
}
#[inline(always)]
pub(crate) fn length(&self) -> i32 {
self.length
}
#[inline(always)]
pub(crate) fn get(&self, index: int) -> Local<Value> {
if index >= 0 && index < self.length {
unsafe { self.get_arg_local(index) }
} else {
let isolate = unsafe { &mut *self.get_isolate_ptr() };
undefined(isolate).into()
}
}
#[inline(always)]
fn get_implicit_arg_non_null<T>(&self, index: i32) -> NonNull<T> {
// In debug builds, check that `FunctionCallbackInfo::kArgsLength` matches
// the C++ definition. Unfortunately we can't check the other constants
// because they are declared protected in the C++ header.
debug_assert_eq!(
unsafe { v8__FunctionCallbackInfo__kArgsLength },
Self::kArgsLength
);
// Assert that `index` is in bounds.
assert!(index >= 0);
assert!(index < Self::kArgsLength);
// Compute the address of the implicit argument and cast to `NonNull<T>`.
let ptr = unsafe { self.implicit_args.offset(index as isize) as *mut T };
debug_assert!(!ptr.is_null());
unsafe { NonNull::new_unchecked(ptr) }
}
// SAFETY: caller must guarantee that the implicit argument at `index`
// contains a valid V8 handle.
#[inline(always)]
unsafe fn get_implicit_arg_local<T>(&self, index: i32) -> Local<T> {
let nn = self.get_implicit_arg_non_null::<T>(index);
unsafe { Local::from_non_null(nn) }
}
// SAFETY: caller must guarantee that the `index` value lies between -1 and
// self.length.
#[inline(always)]
unsafe fn get_arg_local<T>(&self, index: i32) -> Local<T> {
let ptr = unsafe { self.values.offset(index as _) } as *mut T;
debug_assert!(!ptr.is_null());
let nn = unsafe { NonNull::new_unchecked(ptr) };
unsafe { Local::from_non_null(nn) }
}
}
#[repr(C)]
#[derive(Debug)]
struct RawPropertyCallbackInfo(Opaque);
/// The information passed to a property callback about the context
/// of the property access.
#[repr(C)]
#[derive(Debug)]
pub struct PropertyCallbackInfo<T>(RawPropertyCallbackInfo, PhantomData<T>);
impl<T> PropertyCallbackInfo<T> {
#[inline(always)]
pub(crate) fn get_isolate_ptr(&self) -> *mut Isolate {
unsafe { v8__PropertyCallbackInfo__GetIsolate(&self.0) }
}
}
#[derive(Debug)]
pub struct FunctionCallbackArguments<'s>(&'s FunctionCallbackInfo);
impl<'s> FunctionCallbackArguments<'s> {
#[inline(always)]
pub fn from_function_callback_info(info: &'s FunctionCallbackInfo) -> Self {
Self(info)
}
/// SAFETY: caller must guarantee that no other references to the isolate are
/// accessible. Specifically, if an open CallbackScope or HandleScope exists
/// in the current function, `FunctionCallbackArguments::get_isolate()` should
/// not be called.
#[inline(always)]
pub unsafe fn get_isolate(&mut self) -> &mut Isolate {
unsafe { &mut *self.0.get_isolate_ptr() }
}
/// For construct calls, this returns the "new.target" value.
#[inline(always)]
pub fn new_target(&self) -> Local<'s, Value> {
self.0.new_target()
}
/// Returns the receiver. This corresponds to the "this" value.
#[inline(always)]
pub fn this(&self) -> Local<'s, Object> {
self.0.this()
}
/// Returns the data argument specified when creating the callback.
#[inline(always)]
pub fn data(&self) -> Local<'s, Value> {
self.0.data()
}
/// The number of available arguments.
#[inline(always)]
pub fn length(&self) -> int {
self.0.length()
}
/// Accessor for the available arguments. Returns `undefined` if the index is
/// out of bounds.
#[inline(always)]
pub fn get(&self, i: int) -> Local<'s, Value> {
self.0.get(i)
}
}
#[derive(Debug)]
pub struct PropertyCallbackArguments<'s>(&'s RawPropertyCallbackInfo);
impl<'s> PropertyCallbackArguments<'s> {
#[inline(always)]
pub(crate) fn from_property_callback_info<T>(
info: &'s PropertyCallbackInfo<T>,
) -> Self {
Self(&info.0)
}
/// Returns the object in the prototype chain of the receiver that has the
/// interceptor. Suppose you have `x` and its prototype is `y`, and `y`
/// has an interceptor. Then `info.This()` is `x` and `info.Holder()` is `y`.
/// In case the property is installed on the global object the Holder()
/// would return the global proxy.
#[inline(always)]
pub fn holder(&self) -> Local<'s, Object> {
unsafe {
Local::from_raw(v8__PropertyCallbackInfo__Holder(self.0))
.unwrap_unchecked()
}
}
/// Returns the receiver. In many cases, this is the object on which the
/// property access was intercepted. When using
/// `Reflect.get`, `Function.prototype.call`, or similar functions, it is the
/// object passed in as receiver or thisArg.
///
/// ```c++
/// void GetterCallback(Local<Name> name,
/// const v8::PropertyCallbackInfo<v8::Value>& info) {
/// auto context = info.GetIsolate()->GetCurrentContext();
///
/// v8::Local<v8::Value> a_this =
/// info.This()
/// ->GetRealNamedProperty(context, v8_str("a"))
/// .ToLocalChecked();
/// v8::Local<v8::Value> a_holder =
/// info.Holder()
/// ->GetRealNamedProperty(context, v8_str("a"))
/// .ToLocalChecked();
///
/// CHECK(v8_str("r")->Equals(context, a_this).FromJust());
/// CHECK(v8_str("obj")->Equals(context, a_holder).FromJust());
///
/// info.GetReturnValue().Set(name);
/// }
///
/// v8::Local<v8::FunctionTemplate> templ =
/// v8::FunctionTemplate::New(isolate);
/// templ->InstanceTemplate()->SetHandler(
/// v8::NamedPropertyHandlerConfiguration(GetterCallback));
/// LocalContext env;
/// env->Global()
/// ->Set(env.local(), v8_str("obj"), templ->GetFunction(env.local())
/// .ToLocalChecked()
/// ->NewInstance(env.local())
/// .ToLocalChecked())
/// .FromJust();
///
/// CompileRun("obj.a = 'obj'; var r = {a: 'r'}; Reflect.get(obj, 'x', r)");
/// ```
#[inline(always)]
pub fn this(&self) -> Local<'s, Object> {
unsafe {
Local::from_raw(v8__PropertyCallbackInfo__This(self.0)).unwrap_unchecked()
}
}
/// Returns the data set in the configuration, i.e., in
/// `NamedPropertyHandlerConfiguration` or
/// `IndexedPropertyHandlerConfiguration.`
#[inline(always)]
pub fn data(&self) -> Local<'s, Value> {
unsafe {
Local::from_raw(v8__PropertyCallbackInfo__Data(self.0)).unwrap_unchecked()
}
}
/// Returns `true` if the intercepted function should throw if an error
/// occurs. Usually, `true` corresponds to `'use strict'`.
///
/// Always `false` when intercepting `Reflect.set()` independent of the
/// language mode.
#[inline(always)]
pub fn should_throw_on_error(&self) -> bool {
unsafe { v8__PropertyCallbackInfo__ShouldThrowOnError(self.0) }
}
}
pub type FunctionCallback = unsafe extern "C" fn(*const FunctionCallbackInfo);
impl<F> MapFnFrom<F> for FunctionCallback
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
FunctionCallbackArguments<'s>,
ReturnValue<Value>,
),
{
fn mapping() -> Self {
let f = |info: *const FunctionCallbackInfo| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = FunctionCallbackArguments::from_function_callback_info(info);
let rv = ReturnValue::from_function_callback_info(info);
(F::get())(scope, args, rv);
};
f.to_c_fn()
}
}
pub(crate) type NamedGetterCallbackForAccessor<'s> =
unsafe extern "C" fn(Local<'s, Name>, *const PropertyCallbackInfo<Value>);
impl<F> MapFnFrom<F> for NamedGetterCallbackForAccessor<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
PropertyCallbackArguments<'s>,
ReturnValue<Value>,
),
{
fn mapping() -> Self {
let f = |key: Local<Name>, info: *const PropertyCallbackInfo<Value>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, args, rv);
};
f.to_c_fn()
}
}
pub(crate) type NamedGetterCallback<'s> = unsafe extern "C" fn(
Local<'s, Name>,
*const PropertyCallbackInfo<Value>,
) -> Intercepted;
impl<F> MapFnFrom<F> for NamedGetterCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
PropertyCallbackArguments<'s>,
ReturnValue<Value>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |key: Local<Name>, info: *const PropertyCallbackInfo<Value>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type NamedQueryCallback<'s> = unsafe extern "C" fn(
Local<'s, Name>,
*const PropertyCallbackInfo<Integer>,
) -> Intercepted;
impl<F> MapFnFrom<F> for NamedQueryCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
PropertyCallbackArguments<'s>,
ReturnValue<Integer>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |key: Local<Name>, info: *const PropertyCallbackInfo<Integer>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type NamedSetterCallbackForAccessor<'s> = unsafe extern "C" fn(
Local<'s, Name>,
Local<'s, Value>,
*const PropertyCallbackInfo<()>,
);
impl<F> MapFnFrom<F> for NamedSetterCallbackForAccessor<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
Local<'s, Value>,
PropertyCallbackArguments<'s>,
ReturnValue<()>,
),
{
fn mapping() -> Self {
let f = |key: Local<Name>,
value: Local<Value>,
info: *const PropertyCallbackInfo<()>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, value, args, rv);
};
f.to_c_fn()
}
}
pub(crate) type NamedSetterCallback<'s> = unsafe extern "C" fn(
Local<'s, Name>,
Local<'s, Value>,
*const PropertyCallbackInfo<()>,
) -> Intercepted;
impl<F> MapFnFrom<F> for NamedSetterCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
Local<'s, Value>,
PropertyCallbackArguments<'s>,
ReturnValue<()>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |key: Local<Name>,
value: Local<Value>,
info: *const PropertyCallbackInfo<()>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, value, args, rv)
};
f.to_c_fn()
}
}
// Should return an Array in Return Value
pub(crate) type PropertyEnumeratorCallback<'s> =
unsafe extern "C" fn(*const PropertyCallbackInfo<Array>);
impl<F> MapFnFrom<F> for PropertyEnumeratorCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
PropertyCallbackArguments<'s>,
ReturnValue<Array>,
),
{
fn mapping() -> Self {
let f = |info: *const PropertyCallbackInfo<Array>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, args, rv);
};
f.to_c_fn()
}
}
pub(crate) type NamedDefinerCallback<'s> = unsafe extern "C" fn(
Local<'s, Name>,
*const PropertyDescriptor,
*const PropertyCallbackInfo<()>,
) -> Intercepted;
impl<F> MapFnFrom<F> for NamedDefinerCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
&PropertyDescriptor,
PropertyCallbackArguments<'s>,
ReturnValue<()>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |key: Local<Name>,
desc: *const PropertyDescriptor,
info: *const PropertyCallbackInfo<()>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let desc = unsafe { &*desc };
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, desc, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type NamedDeleterCallback<'s> = unsafe extern "C" fn(
Local<'s, Name>,
*const PropertyCallbackInfo<Boolean>,
) -> Intercepted;
impl<F> MapFnFrom<F> for NamedDeleterCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
Local<'s, Name>,
PropertyCallbackArguments<'s>,
ReturnValue<Boolean>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |key: Local<Name>, info: *const PropertyCallbackInfo<Boolean>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type IndexedGetterCallback<'s> =
unsafe extern "C" fn(u32, *const PropertyCallbackInfo<Value>) -> Intercepted;
impl<F> MapFnFrom<F> for IndexedGetterCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
u32,
PropertyCallbackArguments<'s>,
ReturnValue<Value>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |index: u32, info: *const PropertyCallbackInfo<Value>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, index, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type IndexedQueryCallback<'s> = unsafe extern "C" fn(
u32,
*const PropertyCallbackInfo<Integer>,
) -> Intercepted;
impl<F> MapFnFrom<F> for IndexedQueryCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
u32,
PropertyCallbackArguments<'s>,
ReturnValue<Integer>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |key: u32, info: *const PropertyCallbackInfo<Integer>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, key, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type IndexedSetterCallback<'s> = unsafe extern "C" fn(
u32,
Local<'s, Value>,
*const PropertyCallbackInfo<()>,
)
-> Intercepted;
impl<F> MapFnFrom<F> for IndexedSetterCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
u32,
Local<'s, Value>,
PropertyCallbackArguments<'s>,
ReturnValue<()>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |index: u32,
value: Local<Value>,
info: *const PropertyCallbackInfo<()>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, index, value, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type IndexedDefinerCallback<'s> =
unsafe extern "C" fn(
u32,
*const PropertyDescriptor,
*const PropertyCallbackInfo<()>,
) -> Intercepted;
impl<F> MapFnFrom<F> for IndexedDefinerCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
u32,
&PropertyDescriptor,
PropertyCallbackArguments<'s>,
ReturnValue<()>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |index: u32,
desc: *const PropertyDescriptor,
info: *const PropertyCallbackInfo<()>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
let desc = unsafe { &*desc };
(F::get())(scope, index, desc, args, rv)
};
f.to_c_fn()
}
}
pub(crate) type IndexedDeleterCallback<'s> =
unsafe extern "C" fn(
u32,
*const PropertyCallbackInfo<Boolean>,
) -> Intercepted;
impl<F> MapFnFrom<F> for IndexedDeleterCallback<'_>
where
F: UnitType
+ for<'s> Fn(
&mut HandleScope<'s>,
u32,
PropertyCallbackArguments<'s>,
ReturnValue<Boolean>,
) -> Intercepted,
{
fn mapping() -> Self {
let f = |index: u32, info: *const PropertyCallbackInfo<Boolean>| {
let info = unsafe { &*info };
let scope = &mut unsafe { CallbackScope::new(info) };
let args = PropertyCallbackArguments::from_property_callback_info(info);
let rv = ReturnValue::from_property_callback_info(info);
(F::get())(scope, index, args, rv)
};
f.to_c_fn()
}
}
/// A builder to construct the properties of a Function or FunctionTemplate.
pub struct FunctionBuilder<'s, T> {
pub(crate) callback: FunctionCallback,
pub(crate) data: Option<Local<'s, Value>>,
pub(crate) signature: Option<Local<'s, Signature>>,
pub(crate) length: i32,
pub(crate) constructor_behavior: ConstructorBehavior,
pub(crate) side_effect_type: SideEffectType,
phantom: PhantomData<T>,
}
impl<'s, T> FunctionBuilder<'s, T> {
/// Create a new FunctionBuilder.
#[inline(always)]
pub fn new(callback: impl MapFnTo<FunctionCallback>) -> Self {
Self::new_raw(callback.map_fn_to())
}
#[inline(always)]
pub fn new_raw(callback: FunctionCallback) -> Self {
Self {
callback,
data: None,
signature: None,
length: 0,
constructor_behavior: ConstructorBehavior::Allow,
side_effect_type: SideEffectType::HasSideEffect,
phantom: PhantomData,
}
}
/// Set the associated data. The default is no associated data.
#[inline(always)]
pub fn data(mut self, data: Local<'s, Value>) -> Self {
self.data = Some(data);
self
}
/// Set the function length. The default is 0.
#[inline(always)]
pub fn length(mut self, length: i32) -> Self {
self.length = length;
self
}
/// Set the constructor behavior. The default is ConstructorBehavior::Allow.
#[inline(always)]
pub fn constructor_behavior(
mut self,
constructor_behavior: ConstructorBehavior,
) -> Self {
self.constructor_behavior = constructor_behavior;
self
}
/// Set the side effect type. The default is SideEffectType::HasSideEffect.
#[inline(always)]
pub fn side_effect_type(mut self, side_effect_type: SideEffectType) -> Self {
self.side_effect_type = side_effect_type;
self
}
}
impl<'s> FunctionBuilder<'s, Function> {
/// Create the function in the current execution context.
#[inline(always)]
pub fn build(
self,
scope: &mut HandleScope<'s>,
) -> Option<Local<'s, Function>> {
unsafe {
scope.cast_local(|sd| {
v8__Function__New(
sd.get_current_context(),
self.callback,
self.data.map_or_else(null, |p| &*p),
self.length,
self.constructor_behavior,
self.side_effect_type,
)
})
}
}
}
impl Function {
/// Create a FunctionBuilder to configure a Function.
/// This is the same as FunctionBuilder::<Function>::new().
#[inline(always)]
pub fn builder<'s>(
callback: impl MapFnTo<FunctionCallback>,
) -> FunctionBuilder<'s, Self> {
FunctionBuilder::new(callback)
}
#[inline(always)]
pub fn builder_raw<'s>(
callback: FunctionCallback,
) -> FunctionBuilder<'s, Self> {
FunctionBuilder::new_raw(callback)
}
/// Create a function in the current execution context
/// for a given FunctionCallback.
#[inline(always)]
pub fn new<'s>(
scope: &mut HandleScope<'s>,
callback: impl MapFnTo<FunctionCallback>,
) -> Option<Local<'s, Function>> {
Self::builder(callback).build(scope)
}
#[inline(always)]