@@ -554,6 +554,22 @@ pub struct OomDetails {
554554pub type OomErrorCallback =
555555 unsafe extern "C" fn ( location : * const char , details : & OomDetails ) ;
556556
557+ #[ repr( C ) ]
558+ pub struct ModifyCodeGenerationFromStringsResult < ' s > {
559+ pub codegen_allowed : bool ,
560+ pub modified_source : Option < Local < ' s , String > > ,
561+ }
562+
563+ // We use Option<NonNull<T>> which _is_ FFI-safe.
564+ // See https://doc.rust-lang.org/nomicon/other-reprs.html
565+ #[ allow( improper_ctypes_definitions) ]
566+ pub type ModifyCodeGenerationFromStringsCallback < ' s > =
567+ extern "C" fn (
568+ context : Local < ' s , Context > ,
569+ source : Local < ' s , Value > ,
570+ is_code_like : bool ,
571+ ) -> ModifyCodeGenerationFromStringsResult < ' s > ;
572+
557573// Windows x64 ABI: MaybeLocal<Value> returned on the stack.
558574#[ cfg( target_os = "windows" ) ]
559575pub type PrepareStackTraceCallback < ' s > =
@@ -713,6 +729,13 @@ unsafe extern "C" {
713729 isolate : * mut Isolate ,
714730 callback : UseCounterCallback ,
715731 ) ;
732+ // We use Option<NonNull<T>> which _is_ FFI-safe.
733+ // See https://doc.rust-lang.org/nomicon/other-reprs.html
734+ #[ allow( improper_ctypes) ]
735+ fn v8__Isolate__SetModifyCodeGenerationFromStringsCallback (
736+ isolate : * mut Isolate ,
737+ callback : ModifyCodeGenerationFromStringsCallback ,
738+ ) ;
716739 fn v8__Isolate__RequestInterrupt (
717740 isolate : * const Isolate ,
718741 callback : InterruptCallback ,
@@ -1405,6 +1428,20 @@ impl Isolate {
14051428 unsafe { v8__Isolate__RemoveGCEpilogueCallback ( self , callback, data) }
14061429 }
14071430
1431+ /// This specifies the callback called by v8 when JS is trying to dynamically execute
1432+ /// code using `eval` or the `Function` constructor.
1433+ ///
1434+ /// The callback can decide whether to allow code generation and, if so, modify
1435+ /// the source code beforehand.
1436+ pub fn set_modify_code_generation_from_strings_callback (
1437+ & mut self ,
1438+ callback : ModifyCodeGenerationFromStringsCallback ,
1439+ ) {
1440+ unsafe {
1441+ v8__Isolate__SetModifyCodeGenerationFromStringsCallback ( self , callback)
1442+ }
1443+ }
1444+
14081445 /// Add a callback to invoke in case the heap size is close to the heap limit.
14091446 /// If multiple callbacks are added, only the most recently added callback is
14101447 /// invoked.
0 commit comments