-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use a Provider abstraction
- Loading branch information
1 parent
6eef581
commit 447fab5
Showing
6 changed files
with
79 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::bytecode; | ||
use anyhow::Result; | ||
|
||
const QUICKJS_PROVIDER_MODULE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/provider.wasm")); | ||
|
||
/// Use the legacy provider when using the `compile -d` command. | ||
const QUICKJS_PROVIDER_V2_MODULE: &[u8] = include_bytes!("./javy_quickjs_provider_v2.wasm"); | ||
|
||
/// Different providers that are available. | ||
#[derive(Debug)] | ||
pub enum Provider { | ||
/// The default provider. | ||
Default, | ||
/// A provider for use with the `compile` to maintain backward compatibility. | ||
V2, | ||
} | ||
|
||
impl Provider { | ||
/// Returns the provider Wasm module as a byte slice. | ||
pub fn as_bytes(&self) -> &[u8] { | ||
match self { | ||
Self::Default => QUICKJS_PROVIDER_MODULE, | ||
Self::V2 => QUICKJS_PROVIDER_V2_MODULE, | ||
} | ||
} | ||
|
||
/// Uses the provider to generate QuickJS bytecode. | ||
pub fn compile_source(&self, js_source_code: &[u8]) -> Result<Vec<u8>> { | ||
bytecode::compile_source(self, js_source_code) | ||
} | ||
|
||
/// The import namespace to use for this provider. | ||
pub fn import_namespace(&self) -> String { | ||
let prefix = "javy_quickjs_provider_v"; | ||
let version = match self { | ||
Self::Default => 3, | ||
Self::V2 => 2, | ||
}; | ||
format!("{prefix}{version}") | ||
} | ||
} |