Skip to content
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

Use the v2 provider to compile #777

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(bytes: &[u8]) -> Result<(Store<WasiCtx>, Instance, Memory)> {
saulecabrera marked this conversation as resolved.
Show resolved Hide resolved
let engine = Engine::default();
let module = Module::new(&engine, QUICKJS_PROVIDER_MODULE)?;
let module = Module::new(&engine, 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
Loading