-
Notifications
You must be signed in to change notification settings - Fork 352
Expose ModifyCodeGenerationFromStrings
callback
#1726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12018,6 +12018,52 @@ fn use_counter_callback() { | |
assert_eq!(COUNT.load(Ordering::Relaxed), 1); | ||
} | ||
|
||
#[test] | ||
fn code_generation_from_strings_callback() { | ||
static CODEGEN_ALLOWED: AtomicBool = AtomicBool::new(false); | ||
static COUNT: AtomicUsize = AtomicUsize::new(0); | ||
|
||
#[allow(improper_ctypes_definitions)] | ||
extern "C" fn callback<'s>( | ||
_context: v8::Local<'s, v8::Context>, | ||
_source: v8::Local<'s, v8::Value>, | ||
_is_code_like: bool, | ||
) -> v8::ModifyCodeGenerationFromStringsResult<'s> { | ||
COUNT.fetch_add(1, Ordering::Relaxed); | ||
v8::ModifyCodeGenerationFromStringsResult { | ||
codegen_allowed: CODEGEN_ALLOWED.load(Ordering::Relaxed), | ||
modified_source: None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, sorry for the late reply! All relevant APIs used to interact with V8 strings seem to require a reference to a I'm aware that this is a pretty big limitation. I guess providing a |
||
} | ||
} | ||
|
||
let _setup_guard = setup::parallel_test(); | ||
let mut isolate = v8::Isolate::new(Default::default()); | ||
isolate.set_modify_code_generation_from_strings_callback(callback); | ||
let mut scope = v8::HandleScope::new(&mut isolate); | ||
let context = v8::Context::new(&mut scope, Default::default()); | ||
// Must be set to false, otherwise code generation is unconditionally allowed and the callback is never used | ||
context.set_allow_generation_from_strings(false); | ||
|
||
let scope = &mut v8::ContextScope::new(&mut scope, context); | ||
|
||
// Code generation should be disallowed | ||
{ | ||
let tc = &mut v8::TryCatch::new(scope); | ||
eval(tc, "eval('1 + 1')"); | ||
assert_eq!( | ||
tc.message().unwrap().get(tc).to_rust_string_lossy(tc), | ||
"Uncaught EvalError: Code generation from strings disallowed for this context" | ||
); | ||
rebu-dt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!(COUNT.load(Ordering::Relaxed), 1); | ||
} | ||
|
||
// Enable code generation | ||
CODEGEN_ALLOWED.store(true, Ordering::Relaxed); | ||
let result: Option<v8::Local<'_, v8::Value>> = eval(scope, "eval('1 + 1')"); | ||
assert_eq!(result.unwrap().number_value(scope).unwrap(), 2.0); | ||
rebu-dt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_eq!(COUNT.load(Ordering::Relaxed), 2); | ||
} | ||
|
||
#[test] | ||
fn test_eternals() { | ||
let _setup_guard = setup::parallel_test(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should change how
Local
is defined to avoid this..?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm not sure how to fix this either. It pretty much boils down to a
Option<NonNull<T>>
, which should be safe over FFI, but maybe the type withPhantomData
is too complex for the compiler to detect it as such.