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

Change the encoding of wit definitions #1252

Merged
merged 20 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
277 changes: 236 additions & 41 deletions crates/wit-component/src/decoding.rs

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions crates/wit-component/src/encoding/wit/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anyhow::Result;
use wasm_encoder::{ComponentBuilder, ComponentType};
use wit_parser::{PackageId, Resolve, WorldId};

mod v1;
mod v2;

const ENCODE_V2_BY_DEFAULT: bool = false;

fn use_v2_encoding() -> bool {
match std::env::var("WIT_COMPONENT_ENCODING_V2") {
Ok(s) => s == "1",
Err(_) => ENCODE_V2_BY_DEFAULT,
}
}

/// Encodes the given `package` within `resolve` to a binary WebAssembly
/// representation.
///
/// This function is the root of the implementation of serializing a WIT package
/// into a WebAssembly representation. The wasm representation serves two
/// purposes:
///
/// * One is to be a binary encoding of a WIT document which is ideally more
/// stable than the WIT textual format itself.
/// * Another is to provide a clear mapping of all WIT features into the
/// component model through use of its binary representation.
///
/// The `resolve` provided is a set of packages and types and such and the
/// `package` argument is an ID within the world provided. The documents within
/// `package` will all be encoded into the binary returned.
///
/// The binary returned can be [`decode`d](crate::decode) to recover the WIT
/// package provided.
pub fn encode(use_v2: Option<bool>, resolve: &Resolve, package: PackageId) -> Result<Vec<u8>> {
if use_v2.unwrap_or_else(use_v2_encoding) {
v2::encode(resolve, package)
} else {
v1::encode(resolve, package)
}
}

/// Exactly like `encode`, except gives an unfinished `ComponentBuilder` in case you need
/// to append anything else before finishing.
pub fn encode_component(
use_v2: Option<bool>,
resolve: &Resolve,
package: PackageId,
) -> Result<ComponentBuilder> {
if use_v2.unwrap_or_else(use_v2_encoding) {
v2::encode_component(resolve, package)
} else {
v1::encode_component(resolve, package)
}
}

/// Encodes a `world` as a component type.
pub fn encode_world(
use_v2: Option<bool>,
resolve: &Resolve,
world_id: WorldId,
) -> Result<ComponentType> {
if use_v2.unwrap_or_else(use_v2_encoding) {
v2::encode_world(resolve, world_id)
} else {
v1::encode_world(resolve, world_id)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::encoding::types::{FunctionKey, ValtypeEncoder};
use crate::encoding::{
docs::PackageDocs,
types::{FunctionKey, ValtypeEncoder},
};
use anyhow::Result;
use indexmap::IndexSet;
use std::collections::HashMap;
use std::mem;
use wasm_encoder::*;
use wit_parser::*;

use super::docs::PackageDocs;

/// Encodes the given `package` within `resolve` to a binary WebAssembly
/// representation.
///
Expand Down
Loading
Loading