Skip to content

Commit

Permalink
Reuse runtime when compiling source code to bytecode (#781)
Browse files Browse the repository at this point in the history
* Reuse runtime when compiling source code to bytecode

* Add comment about why we're using the initialized runtime

* Revert changes to test runner
  • Loading branch information
jeffcharles authored Oct 21, 2024
1 parent 6eef581 commit deb703e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,20 @@ pub extern "C" fn initialize_runtime() {
/// * `js_src_ptr` must reference a valid array of unsigned bytes of `js_src_len` length
#[export_name = "compile_src"]
pub unsafe extern "C" fn compile_src(js_src_ptr: *const u8, js_src_len: usize) -> *const u32 {
// Use fresh runtime to avoid depending on Wizened runtime
let runtime = runtime::new(Config::default()).unwrap();
// Use initialized runtime when compiling because certain runtime
// configurations can cause different bytecode to be emitted.
//
// For example, given the following JS:
// ```
// function foo() {
// "use math"
// 1234 % 32
// }
// ```
//
// Setting `config.bignum_extension` to `true` will produce different
// bytecode than if it were set to `false`.
let runtime = unsafe { RUNTIME.get().unwrap() };
let js_src = str::from_utf8(slice::from_raw_parts(js_src_ptr, js_src_len)).unwrap();

let bytecode = runtime
Expand Down

0 comments on commit deb703e

Please sign in to comment.