-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the structure of the
Config
/APIs (#646)
* Refactor the structure of the `Config`/APIs This change primarily rewrites the current APIs using "Intrinsics" as defined by rquickjs. This change has several objectives: Refactor the structure of the Config and its relationship with the Runtime to pave the way for a truly extensible and configurable engine. The way Intrinsics are exposed in rquickjs simplifies (i) enabling a granular configuration experience of the native intrinsics, which was not entirely clear how to achieve before, and (ii) adding custom APIs as intrinsics. Enhance how our configuration operates: the previous model assumed an almost static configuration setup, allowing only limited, non-straightforward customizability from the CLI in the future. This was mostly modeled per API and not as a global aspect of the runtime. Our recommended approach for users needing functionality beyond what was provided was to fork or recreate their own version of the CLI/APIs. A telling sign of this issue is that the configuration parameters were almost never used in any of the APIs. This PR paves the way for greater extensibility, which will be facilitated by threading configuration options from the CLI in a follow-up PR; this method will also allow for opting-in to non-standard functionality via a CLI flag, helping us avoid issues like the one noted here: #628. Along the way, I had to make several changes to make all this work; the most relevant ones are: (i) deprecating the `javy-apis` crate (ii) enabling all the APIs by default and (iii) reworking a bit the `Console` implementation. The reasoning to deprecate the `javy-apis` crate is mostly around: (i) the fact that we're revamping our extensilbity model in the near fuiture and (ii) the complexity around cyclical dependencies: `javy` exposes rquickjs and given that the cofiguration is tied to the runtime, `javy` needed to depend on `javy-apis` but the other way around was also true. I decided not to spend too much cycles on this mostly given point (i) above. There still things to follow-up on after this PR, namely - [ ] Update all the documentation under `docs` - [ ] A PR to thread the engine configuration from the CLI. NB that the engine behaviour is not altered in this PR. * Fix typos * Add a comment in the apis/lib.rs This makes `cargo fmt -- --check` happy * Review comments
- Loading branch information
1 parent
821b15f
commit 30c21d0
Showing
22 changed files
with
489 additions
and
414 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,36 +1,3 @@ | ||
<div align="center"> | ||
<h1><code>Javy APIs</code></h1> | ||
<p> | ||
<strong>A collection of APIs for Javy</strong> | ||
</p> | ||
# This crate is deprecated. | ||
|
||
<p> | ||
<a href="https://docs.rs/javy-apis"><img src="https://docs.rs/javy-apis/badge.svg" alt="Documentation Status" /></a> | ||
<a href="https://crates.io/crates/javy-apis"><img src="https://img.shields.io/crates/v/javy-apis.svg" alt="crates.io status" /></a> | ||
</p> | ||
</div> | ||
|
||
Refer to the [crate level documentation](https://docs.rs/javy-apis) to learn more. | ||
|
||
Example usage: | ||
|
||
```rust | ||
// With the `console` feature enabled. | ||
use javy::{Runtime, from_js_error}; | ||
use javy_apis::RuntimeExt; | ||
use anyhow::Result; | ||
|
||
fn main() -> Result<()> { | ||
let runtime = Runtime::new_with_defaults()?; | ||
let context = runtime.context(); | ||
context.with(|cx| { | ||
cx.eval_with_options(Default::default(), "console.log('hello!');") | ||
.map_err(|e| to_js_error(cx.clone(), e))? | ||
}); | ||
Ok(()) | ||
} | ||
``` | ||
|
||
## Publishing to crates.io | ||
|
||
To publish this crate to crates.io, run `./publish.sh`. | ||
Javy APIs have been folded into the [Javy crate](https://crates.io/crates/javy) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,87 +1,2 @@ | ||
//! A collection of APIs for Javy. | ||
//! | ||
//! APIs are enabled through cargo features. | ||
//! | ||
//! Example usage: | ||
//! ```rust | ||
//! | ||
//! //With the `console` feature enabled. | ||
//! use javy::{Runtime, from_js_error}; | ||
//! use javy_apis::RuntimeExt; | ||
//! use anyhow::Result; | ||
//! | ||
//! fn main() -> Result<()> { | ||
//! let runtime = Runtime::new_with_defaults()?; | ||
//! let context = runtime.context(); | ||
//! context.with(|cx| { | ||
//! cx.eval_with_options(Default::default(), "console.log('hello!');") | ||
//! .map_err(|e| to_js_error(cx.clone(), e))? | ||
//! }); | ||
//! Ok(()) | ||
//! } | ||
//! | ||
//! ``` | ||
//! | ||
//! If you want to customize the runtime or the APIs, you can use the | ||
//! [`Runtime::new_with_apis`] method instead to provide a [`javy::Config`] | ||
//! for the underlying [`Runtime`] or an [`APIConfig`] for the APIs. | ||
//! | ||
//! ## Features | ||
//! * `console`: Adds an implementation of the `console.log` and `console.error`, | ||
//! enabling the configuration of the standard streams. | ||
//! * `text_encoding`: Registers implementations of `TextEncoder` and `TextDecoder`. | ||
//! * `random`: Overrides the implementation of `Math.random` to one that seeds | ||
//! the RNG on first call to `Math.random`. This is helpful to enable when using | ||
//! using a tool like Wizer to snapshot a [`Runtime`] so that the output of | ||
//! `Math.random` relies on the WASI context used at runtime and not the WASI | ||
//! context used when Wizening. Enabling this feature will increase the size of | ||
//! the Wasm module that includes the Javy Runtime and will introduce an | ||
//! additional hostcall invocation when `Math.random` is invoked for the first | ||
//! time. | ||
//! * `stream_io`: Adds an implementation of `Javy.IO.readSync` and `Javy.IO.writeSync`. | ||
use anyhow::Result; | ||
use javy::Runtime; | ||
|
||
pub use api_config::APIConfig; | ||
#[cfg(feature = "console")] | ||
pub use console::LogStream; | ||
pub use runtime_ext::RuntimeExt; | ||
|
||
mod api_config; | ||
#[cfg(feature = "console")] | ||
mod console; | ||
#[cfg(feature = "random")] | ||
mod random; | ||
mod runtime_ext; | ||
#[cfg(feature = "stream_io")] | ||
mod stream_io; | ||
#[cfg(feature = "text_encoding")] | ||
mod text_encoding; | ||
|
||
pub(crate) trait JSApiSet { | ||
fn register(&self, runtime: &Runtime, config: &APIConfig) -> Result<()>; | ||
} | ||
|
||
/// Adds enabled JS APIs to the provided [`Runtime`]. | ||
/// | ||
/// ## Example | ||
/// ``` | ||
/// # use anyhow::Error; | ||
/// # use javy::Runtime; | ||
/// # use javy_apis::APIConfig; | ||
/// let runtime = Runtime::default(); | ||
/// javy_apis::add_to_runtime(&runtime, APIConfig::default())?; | ||
/// # Ok::<(), Error>(()) | ||
/// ``` | ||
pub fn add_to_runtime(runtime: &Runtime, config: APIConfig) -> Result<()> { | ||
#[cfg(feature = "console")] | ||
console::Console::new().register(runtime, &config)?; | ||
#[cfg(feature = "random")] | ||
random::Random.register(runtime, &config)?; | ||
#[cfg(feature = "stream_io")] | ||
stream_io::StreamIO.register(runtime, &config)?; | ||
#[cfg(feature = "text_encoding")] | ||
text_encoding::TextEncoding.register(runtime, &config)?; | ||
Ok(()) | ||
} | ||
//! Javy APIs -- Deprecated | ||
//! Javy APIs moved to the main Javy crate. |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
use anyhow::Result; | ||
use javy::{Config, Runtime}; | ||
use javy_apis::{APIConfig, LogStream, RuntimeExt}; | ||
|
||
pub(crate) fn new_runtime() -> Result<Runtime> { | ||
let mut api_config = APIConfig::default(); | ||
api_config.log_stream(LogStream::StdErr); | ||
Runtime::new_with_apis(Config::default(), api_config) | ||
let mut config = Config::default(); | ||
let config = config | ||
.text_encoding(true) | ||
.redirect_stdout_to_stderr(true) | ||
.javy_stream_io(true); | ||
|
||
Runtime::new(std::mem::take(config)) | ||
} |
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
Oops, something went wrong.