@@ -559,6 +559,22 @@ pub type OomErrorCallback =
559
559
#[ derive( Debug ) ]
560
560
pub struct HeapStatistics ( [ usize ; 16 ] ) ;
561
561
562
+ #[ repr( C ) ]
563
+ pub struct ModifyCodeGenerationFromStringsResult < ' s > {
564
+ pub codegen_allowed : bool ,
565
+ pub modified_source : Option < Local < ' s , String > > ,
566
+ }
567
+
568
+ // We use Option<NonNull<T>> which _is_ FFI-safe.
569
+ // See https://doc.rust-lang.org/nomicon/other-reprs.html
570
+ #[ allow( improper_ctypes_definitions) ]
571
+ pub type ModifyCodeGenerationFromStringsCallback < ' s > =
572
+ extern "C" fn (
573
+ context : Local < ' s , Context > ,
574
+ source : Local < ' s , Value > ,
575
+ is_code_like : bool ,
576
+ ) -> ModifyCodeGenerationFromStringsResult < ' s > ;
577
+
562
578
// Windows x64 ABI: MaybeLocal<Value> returned on the stack.
563
579
#[ cfg( target_os = "windows" ) ]
564
580
pub type PrepareStackTraceCallback < ' s > =
@@ -694,6 +710,13 @@ unsafe extern "C" {
694
710
isolate : * mut Isolate ,
695
711
callback : UseCounterCallback ,
696
712
) ;
713
+ // We use Option<NonNull<T>> which _is_ FFI-safe.
714
+ // See https://doc.rust-lang.org/nomicon/other-reprs.html
715
+ #[ allow( improper_ctypes) ]
716
+ fn v8__Isolate__SetModifyCodeGenerationFromStringsCallback (
717
+ isolate : * mut Isolate ,
718
+ callback : ModifyCodeGenerationFromStringsCallback ,
719
+ ) ;
697
720
fn v8__Isolate__RequestInterrupt (
698
721
isolate : * const Isolate ,
699
722
callback : InterruptCallback ,
@@ -1380,6 +1403,20 @@ impl Isolate {
1380
1403
unsafe { v8__Isolate__RemoveGCPrologueCallback ( self , callback, data) }
1381
1404
}
1382
1405
1406
+ /// This specifies the callback called by v8 when JS is trying to dynamically execute
1407
+ /// code using `eval` or the `Function` constructor.
1408
+ ///
1409
+ /// The callback can decide whether to allow code generation and, if so, modify
1410
+ /// the source code beforehand.
1411
+ pub fn set_modify_code_generation_from_strings_callback (
1412
+ & mut self ,
1413
+ callback : ModifyCodeGenerationFromStringsCallback ,
1414
+ ) {
1415
+ unsafe {
1416
+ v8__Isolate__SetModifyCodeGenerationFromStringsCallback ( self , callback)
1417
+ }
1418
+ }
1419
+
1383
1420
/// Add a callback to invoke in case the heap size is close to the heap limit.
1384
1421
/// If multiple callbacks are added, only the most recently added callback is
1385
1422
/// invoked.
0 commit comments