Skip to content

Commit 32dd15f

Browse files
authored
Revise public documentation for codegen (#906)
1 parent 0455688 commit 32dd15f

7 files changed

+43
-38
lines changed

crates/codegen/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic
77
Versioning](https://semver.org/spec/v2.0.0.html).
88

9-
## [1.0.0] - 2025-2-15
9+
## [1.0.0] - 2025-03-07
1010

1111
Initial release

crates/codegen/src/js.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ use swc_core::{
2626

2727
use crate::plugin::Plugin;
2828

29-
/// A structure representing valid JS code.
29+
/// JS source code.
3030
#[derive(Clone, Debug)]
3131
pub struct JS {
3232
source_code: Rc<String>,
3333
}
3434

3535
impl JS {
36-
/// Generate a valid JS instance from a string containing JS.
36+
/// Create [`JS`] from a string containing JS source code.
3737
pub fn from_string(source_code: String) -> JS {
3838
JS {
3939
source_code: Rc::new(source_code),
4040
}
4141
}
4242

43-
/// Generate a valid JS instance from a file containing JS.
43+
/// Create [`JS`] from a file containing JS.
4444
pub fn from_file(path: &Path) -> Result<JS> {
4545
let mut input_file = File::open(path)
4646
.with_context(|| format!("Failed to open input file {}", path.display()))?;
@@ -49,7 +49,7 @@ impl JS {
4949
Ok(Self::from_string(String::from_utf8(contents)?))
5050
}
5151

52-
/// Get JS source code as bytes.
52+
/// Get source code as bytes.
5353
pub fn as_bytes(&self) -> &[u8] {
5454
self.source_code.as_bytes()
5555
}

crates/codegen/src/lib.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! WebAssembly Code Generation for JavaScript
22
//!
3-
//! This module provides all the functionality to emit Wasm modules for
4-
//! a particular JavaScript program.
3+
//! This module provides functionality to emit Wasm modules which will run
4+
//! JavaScript source code with the QuickJS interpreter.
55
//!
66
//! Javy supports two main code generation paths:
77
//!
@@ -41,32 +41,34 @@
4141
//! use javy_codegen::{Generator, LinkingKind, Plugin, JS};
4242
//!
4343
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
44-
//! // Load your target Javascript.
45-
//! let js = JS::from_file(Path::new("example.js"))?;
44+
//! // Load your target Javascript.
45+
//! let js = JS::from_file(Path::new("example.js"))?;
4646
//!
47-
//! // Load existing pre-initialized Javy plugin.
48-
//! let plugin = Plugin::new_from_path(Path::new("example-plugin.wasm"))?;
47+
//! // Load existing pre-initialized Javy plugin.
48+
//! let plugin = Plugin::new_from_path(Path::new("example-plugin.wasm"))?;
4949
//!
50-
//! // Configure code generator.
51-
//! let mut generator = Generator::new(plugin);
52-
//! generator.linking(LinkingKind::Static);
50+
//! // Configure code generator.
51+
//! let mut generator = Generator::new(plugin);
52+
//! generator.linking(LinkingKind::Static);
5353
//!
54-
//! // Generate your WASM module.
55-
//! let wasm = generator.generate(&js);
54+
//! // Generate your Wasm module.
55+
//! let wasm = generator.generate(&js);
5656
//!
57-
//! Ok(())
57+
//! Ok(())
5858
//! }
5959
//! ```
6060
//!
6161
//! ## Core concepts
6262
//! * [`Generator`] - The main entry point for generating Wasm modules.
63-
//! * [`Plugin`] - Represents a pre-initialized Javy plugin.
64-
//! * [`JS`] - Represents a JavaScript bytecode.
63+
//! * [`Plugin`] - An initialized Javy plugin.
64+
//! * [`JS`] - JavaScript source code.
6565
//!
6666
//! ## Features
6767
//!
68-
//! * `plugin_internal` - Enables additional code generation options for internal use.
69-
//! > Please note that this flag enables an unstable feature. The unstable API's exposed by this future may break in the future without notice.
68+
//! * `plugin_internal` - Enables additional code generation options for
69+
//! internal use. Please note that this flag enables an unstable feature. The
70+
//! unstable API's exposed by this future may break in the future without
71+
//! notice.
7072
7173
use std::{fs, rc::Rc, sync::OnceLock};
7274

@@ -147,8 +149,7 @@ impl BytecodeMetadata {
147149
}
148150
}
149151

150-
/// Generator used to produce valid Wasm
151-
/// binaries from JS.
152+
/// Generator used to produce Wasm binaries from JS source code.
152153
#[derive(Default, Clone)]
153154
pub struct Generator {
154155
/// Plugin to use.
@@ -558,7 +559,7 @@ impl Generator {
558559
// (data (;0;) "\02\05\18function.mjs\06foo\0econsole\06log\06bar\0f\bc\03\00\01\00\00\be\03\00\00\0e\00\06\01\a0\01\00\00\00\03\01\01\1a\00\be\03\00\01\08\ea\05\c0\00\e1)8\e0\00\00\00B\e1\00\00\00\04\e2\00\00\00$\01\00)\bc\03\01\04\01\00\07\0a\0eC\06\01\be\03\00\00\00\03\00\00\13\008\e0\00\00\00B\e1\00\00\00\04\df\00\00\00$\01\00)\bc\03\01\02\03]")
559560
// (data (;1;) "foo")
560561
// )
561-
/// Generate a valid WASM binary from JS.
562+
/// Generate a Wasm module which will run the provided JS source code.
562563
pub fn generate(&mut self, js: &js::JS) -> Result<Vec<u8>> {
563564
if self.wit_opts.defined() {
564565
self.function_exports = exports::process_exports(

crates/codegen/src/plugin.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{borrow::Cow, fs, path::Path, str};
33

44
use super::bytecode;
55

6-
/// Represents the kind of a plugin.
6+
/// The kind of a plugin.
77
// This is an internal detail of this module.
88
#[derive(Default, PartialEq, Copy, Clone)]
99
#[allow(dead_code)] // Suppresses warnings for feature-gated variants
@@ -16,7 +16,6 @@ pub(crate) enum PluginKind {
1616

1717
impl PluginKind {
1818
/// Determine the import namespace of a provided plugin.
19-
// This is an internal detail of this module.
2019
pub(crate) fn import_namespace(self, plugin: &Plugin) -> Result<String> {
2120
match self {
2221
PluginKind::V2 => Ok("javy_quickjs_provider_v2".to_string()),
@@ -41,30 +40,30 @@ impl PluginKind {
4140
}
4241
}
4342

44-
/// Represents any valid Javy plugin.
43+
/// A Javy plugin.
4544
#[derive(Clone, Debug, Default)]
4645
pub struct Plugin {
4746
bytes: Cow<'static, [u8]>,
4847
}
4948

5049
impl Plugin {
51-
/// Constructs a new instance of Plugin.
50+
/// Constructs a new [`Plugin`].
5251
pub fn new(bytes: Cow<'static, [u8]>) -> Self {
5352
Plugin { bytes }
5453
}
5554

56-
/// Constructs a new instance of Plugin from a given path.
55+
/// Constructs a new [`Plugin`] from a given path.
5756
pub fn new_from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
5857
let bytes = fs::read(path)?;
5958
Ok(Self::new(bytes.into()))
6059
}
6160

62-
/// Returns the Plugin as a byte slice.
61+
/// Returns the [`Plugin`] as bytes
6362
pub fn as_bytes(&self) -> &[u8] {
6463
&self.bytes
6564
}
6665

67-
/// Generate valid QuickJS bytecode from Javascript using a Plugin.
66+
/// Generate valid QuickJS bytecode from Javascript source code.
6867
pub(crate) fn compile_source(&self, js_source_code: &[u8]) -> Result<Vec<u8>> {
6968
bytecode::compile_source(self.as_bytes(), js_source_code)
7069
}

docs/docs-contributing-architecture.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ This document is intended to provide an overview of the architecture of `javy`.
66

77
```mermaid
88
flowchart TD
9-
javy-cli --> wasm
9+
javy-cli --> javy-codegen --> wasm
1010
subgraph wasm[plugin.wasm]
1111
javy-plugin --> javy-plugin-api
1212
javy-plugin-api --> javy
1313
javy --> rquickjs
1414
end
1515
```
1616

17-
We anticipate most changes will be to the `javy-cli` and `javy` crates.
17+
We anticipate most changes will be to the `javy-cli`, `javy-codegen`, and
18+
`javy` crates.
1819

1920
### `javy`
2021

@@ -102,9 +103,9 @@ by a Cargo feature on a case-by-case basis.
102103

103104
### `javy-cli`
104105

105-
The CLI for compiling JS to Wasm. This isn't intended to be a CLI that
106-
accommodates all uses for all users but rather to provide a useful base of
107-
functionality.
106+
The CLI that drives the `javy-codegen` crate to compile JS to Wasm. This
107+
isn't intended to be a CLI that accommodates all uses for all users but
108+
rather to provide a useful base of functionality.
108109

109110
#### When to add a `cargo` feature
110111

@@ -119,6 +120,10 @@ You should gate your feature with a cargo feature if your feature/change:
119120
run when the `javy-plugin` crate is built with a non-default configuration (that
120121
is, with different cargo features enabled).
121122

123+
### `javy-codegen`
124+
125+
A Rust crate for compiling JS to Wasm.
126+
122127
### `javy-plugin`
123128

124129
Gets compiled to `plugin.wasm` for use by the CLI and in environments for

docs/docs-contributing-testing-locally.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ cargo +stable install cargo-hack --locked
1515

1616
3. Run tests, eg:
1717
```
18-
CARGO_TARGET_WASM32_WASIP1_RUNNER="wasmtime --dir=." cargo hack test --target=wasm32-wasip1 --workspace --exclude=javy-cli --each-feature -- --nocapture
18+
make tests
1919
```
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Runtime requirements
22

3-
When running the official Javy binary on Linux, `glibc` 2.31 or greater must be
3+
When running the official Javy binary on Linux, `glibc` 2.35 or greater must be
44
available. You may need to update the version of your operating system if you
55
are using an older version of `glibc`.

0 commit comments

Comments
 (0)