Skip to content

Commit

Permalink
docs: Improve READMEs for apis and javy crates (#640)
Browse files Browse the repository at this point in the history
* docs: Slight improvement to `javy-apis`

In preparation to adding more APIs I decided to do a very light cleanup of some of the documentation in this crate. I opted to remove the documentation in the README to reduce the overhead of having to maintain the same documentation in multiple places.

* Review comments

* Bring example to README and fix error

Brings the APIs example to the README and fixes a dangling statement.

* Use the right example

* Standardize the READMEs even more

Adds crates.io and docs.rs badges and updates the  `javy` crate with similar updates. 

Includes a link to the documentation to ensure that there's a direct navigation from the README to the crate-level docs.
  • Loading branch information
saulecabrera authored May 7, 2024
1 parent d5866dc commit 8877be4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 58 deletions.
31 changes: 15 additions & 16 deletions crates/apis/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# javy-apis

A collection of APIs that can be added to a Javy runtime.

APIs are registered by enabling crate features.

## Example usage
<div align="center">
<h1><code>Javy APIs</code></h1>
<p>
<strong>A collection of APIs for Javy</strong>
</p>

<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;
Expand All @@ -24,14 +31,6 @@ fn main() -> Result<()> {
}
```

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` - Registers an implementation of the `console` API.
* `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 Wizer to snapshot a Javy 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` - Registers implementations of `Javy.IO.readSync` and `Javy.IO.writeSync`.

## Publishing to crates.io

To publish this crate to crates.io, run `./publish.sh`.
60 changes: 29 additions & 31 deletions crates/apis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
//! JS APIs for Javy.
//! A collection of APIs for Javy.
//!
//! This crate provides JS APIs you can add to Javy.
//! APIs are enabled through cargo features.
//!
//! Example usage:
//! ```
//! # use anyhow::{anyhow, Error, Result};
//! use javy::{quickjs::JSValue, Runtime};
//! ```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(())
//! }
//!
//! let runtime = Runtime::new_with_defaults()?;
//! let context = runtime.context();
//! context.global_object()?.set_property(
//! "print",
//! context.wrap_callback(move |_ctx, _this, args| {
//! let str = args
//! .first()
//! .ok_or(anyhow!("Need to pass an argument"))?
//! .to_string();
//! println!("{str}");
//! Ok(JSValue::Undefined)
//! })?,
//! )?;
//! context.eval_global("hello.js", "print('hello!');")?;
//! # Ok::<(), Error>(())
//! ```
//!
//! 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` - Registers an implementation of the `console` API.
//! * `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 Wizer to snapshot a [`javy::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` - Registers implementations of `Javy.IO.readSync` and `Javy.IO.writeSync`.
//! * `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;
Expand Down
24 changes: 13 additions & 11 deletions crates/javy/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# javy
<div align="center">
<h1><code>Javy</code></h1>
<p>
<strong>A configurable JavaScript runtime for WebAssembly</strong>
</p>
<p>
<a href="https://docs.rs/javy"><img src="https://docs.rs/javy/badge.svg" alt="Documentation Status" /></a>
<a href="https://crates.io/crates/javy"><img src="https://img.shields.io/crates/v/javy.svg" alt="crates.io status" /></a>
</p>
</div>

A configurable JavaScript runtime for WebAssembly.

Uses QuickJS through the [`rquickjs`](https://docs.rs/rquickjs/latest/rquickjs/)
crate to evalulate JavaScript source code or QuickJS bytecode.

## Example usage
Refer to the [crate level documentation](https://docs.rs/javy) to learn more.

Example usage:

```rust
use anyhow::anyhow;
Expand Down Expand Up @@ -33,14 +43,6 @@ context.with(|cx| {
});
```

Create a `Runtime` and use the reference returned by `context()` to add functions and evaluate source code.

## Features

- `export_alloc_fns` - exports `canonical_abi_realloc` and `canonical_abi_free` from generated WebAssembly for allocating and freeing memory
- `json` - transcoding functions for converting between `JSValueRef` and JSON
- `messagepack` - transcoding functions for converting between `JSValueRef` and MessagePack

## Publishing to crates.io

To publish this crate to crates.io, run `./publish.sh`.

0 comments on commit 8877be4

Please sign in to comment.