@@ -559,6 +559,22 @@ pub type OomErrorCallback =
559559#[ derive( Debug ) ]
560560pub struct HeapStatistics ( [ usize ; 16 ] ) ;
561561
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+
562578// Windows x64 ABI: MaybeLocal<Value> returned on the stack.
563579#[ cfg( target_os = "windows" ) ]
564580pub type PrepareStackTraceCallback < ' s > =
@@ -694,6 +710,13 @@ unsafe extern "C" {
694710 isolate : * mut Isolate ,
695711 callback : UseCounterCallback ,
696712 ) ;
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+ ) ;
697720 fn v8__Isolate__RequestInterrupt (
698721 isolate : * const Isolate ,
699722 callback : InterruptCallback ,
@@ -1380,6 +1403,20 @@ impl Isolate {
13801403 unsafe { v8__Isolate__RemoveGCPrologueCallback ( self , callback, data) }
13811404 }
13821405
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+
13831420 /// Add a callback to invoke in case the heap size is close to the heap limit.
13841421 /// If multiple callbacks are added, only the most recently added callback is
13851422 /// invoked.
0 commit comments