Skip to content

Commit

Permalink
Use the v2 provider to compile (#777)
Browse files Browse the repository at this point in the history
* Use the v2 provider for `compile`

Closes #766

This commit ensures that the soon-to-be-deprecated `compile` command
makes use of the right (v2) provider for compilation. Not ensuring this
has the potential to cause issues upon a new release of QuickJS
containing bytecode incompatibilities.

* Rename param
  • Loading branch information
saulecabrera authored Oct 7, 2024
1 parent 626bb20 commit ef1e806
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
11 changes: 7 additions & 4 deletions crates/cli/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ use wasmtime::{AsContextMut, Engine, Instance, Linker, Memory, Module, Store};
pub const QUICKJS_PROVIDER_MODULE: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/provider.wasm"));

pub fn compile_source(js_source_code: &[u8]) -> Result<Vec<u8>> {
let (mut store, instance, memory) = create_wasm_env()?;
/// Use the legacy provider when using the `compile -d` command.
pub const QUICKJS_PROVIDER_V2_MODULE: &[u8] = include_bytes!("./javy_quickjs_provider_v2.wasm");

pub fn compile_source(provider: &[u8], js_source_code: &[u8]) -> Result<Vec<u8>> {
let (mut store, instance, memory) = create_wasm_env(provider)?;
let (js_src_ptr, js_src_len) =
copy_source_code_into_instance(js_source_code, store.as_context_mut(), &instance, &memory)?;
let ret_ptr = call_compile(js_src_ptr, js_src_len, store.as_context_mut(), &instance)?;
let bytecode = copy_bytecode_from_instance(ret_ptr, store.as_context_mut(), &memory)?;
Ok(bytecode)
}

fn create_wasm_env() -> Result<(Store<WasiCtx>, Instance, Memory)> {
fn create_wasm_env(provider_bytes: &[u8]) -> Result<(Store<WasiCtx>, Instance, Memory)> {
let engine = Engine::default();
let module = Module::new(&engine, QUICKJS_PROVIDER_MODULE)?;
let module = Module::new(&engine, provider_bytes)?;
let mut linker = Linker::new(&engine);
wasi_common::sync::snapshots::preview_1::add_wasi_snapshot_preview1_to_linker(
&mut linker,
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/codegen/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ impl DynamicGenerator {
js: &JS,
imports: &Imports,
) -> Result<BytecodeMetadata> {
let bytecode = js.compile()?;
let bytecode = if self.import_namespace == "javy_quickjs_provider_v2" {
js.compile_legacy()?
} else {
js.compile()?
};
let bytecode_len: i32 = bytecode.len().try_into()?;
let bytecode_data = module.data.add(DataKind::Passive, bytecode);

Expand Down
17 changes: 16 additions & 1 deletion crates/cli/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,23 @@ impl JS {
self.source_code.as_bytes()
}

/// Compiles a JavaScript source to bytecode using the QuickJS provider.
pub fn compile(&self) -> Result<Vec<u8>> {
bytecode::compile_source(self.source_code.as_bytes())
bytecode::compile_source(
bytecode::QUICKJS_PROVIDER_MODULE,
self.source_code.as_bytes(),
)
}

/// Similar to [`Self::compile`]. Instead of using the most up to date
/// provider, it uses the v2 provider.
///
/// NB that this is temporary until the `compile` command is deprecated.
pub fn compile_legacy(&self) -> Result<Vec<u8>> {
bytecode::compile_source(
bytecode::QUICKJS_PROVIDER_V2_MODULE,
self.source_code.as_bytes(),
)
}

pub fn compress(&self) -> Result<Vec<u8>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/test-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ fn expand_cli_tests(test_config: &CliTestConfig, func: syn::ItemFn) -> Result<To
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
builder.preload(
"javy_quickjs_provider_v2".into(),
root.join("tests").join("javy_quickjs_provider_v2.wasm")
root.join("src").join("javy_quickjs_provider_v2.wasm")
);
builder.provider_version(2);
}
Expand Down

0 comments on commit ef1e806

Please sign in to comment.