Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "wit-bindgen"]
path = wit-bindgen
url = git@github.com:bytecodealliance/wit-bindgen
135 changes: 102 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ env_logger = "0.9.1"
indexmap = "1.9"
wasmtime = { git = 'https://github.com/bytecodealliance/wasmtime', features = ["component-model"] }
wasmtime-environ = { git = 'https://github.com/bytecodealliance/wasmtime' }
wasmprinter = "0.2.44"
wasmparser = "0.95.0"
wasm-encoder = "0.20.0"
wat = "1.0.52"
wasmprinter = "0.2.46"
wasmparser = "0.97.0"
wasm-encoder = "0.21.0"
wat = "1.0.53"
wit-bindgen-guest-rust = { git = 'https://github.com/bytecodealliance/wit-bindgen', default-features = false, features = ['macros'] }
wit-component = "0.3.2"
wit-parser = "0.3.1"
wit-component = "0.4.1"
wit-parser = "0.4.0"
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ Parse a compoment WAT to output a Component binary.

Print the WAT for a Component binary.

#### `componentNew(coreWasm: Uint8Array | null, opts?): Uint8Array`
#### `componentNew(coreWasm: Uint8Array | null, adapters?: [String, Uint8Array][]): Uint8Array`

"WIT Component" Component creation tool.
"WIT Component" Component creation tool, optionally providing a set of named adapter binaries.

#### `componentWit(component: Uint8Array): string`
#### `componentWit(component: Uint8Array, document?: string): string`

Extract the WIT world from a component binary.

Expand All @@ -102,6 +102,14 @@ Commands:
help [command] display help for command
```

## Contributing

Development is based on a standard `npm install && npm run build && npm run test` workflow.

Tests can be run without bundling via `npm run build:dev && npm run test:dev`.

Specific tests can be run adding the mocha `--grep` flag, for example: `npm run test:dev -- --grep exports_only`.

# License

This project is licensed under the Apache 2.0 license with the LLVM exception.
Expand Down
24 changes: 7 additions & 17 deletions api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export interface TranspileOpts {
/**
* Transpile a Component into a JS-executable package
*/
export function transpile(component: Uint8Array, opts?: TranspileOpts): Promise<{ files, imports, exports }>;
export function transpile(component: Uint8Array, opts?: TranspileOpts): Promise<{
files: Record<string, Uint8Array>,
imports: string[],
exports: [string, 'function' | 'instance'][]
}>;

/**
* Parse a WAT string into a Wasm binary
Expand All @@ -51,27 +55,13 @@ export function print(binary: Uint8Array | ArrayBuffer): string;
/**
* WIT Component - create a Component from a Wasm core binary
*/
export function componentNew(binary: Uint8Array | ArrayBuffer | null, opts: ComponentOpts | null): Uint8Array;
export function componentNew(binary: Uint8Array | ArrayBuffer, adapters?: [string, Uint8Array][] | null): Uint8Array;

/**
* Extract the WIT world from a Wasm Component
*/
export function componentWit(binary: Uint8Array | ArrayBuffer): string;
export function componentWit(binary: Uint8Array | ArrayBuffer, document?: string | null): string;

export type StringEncoding = 'utf8' | 'utf16' | 'compact-utf16';

export interface ComponentOpts {
/// wit world for the Component
/// (only needed if not provided in the Component itself,
/// which it usually is)
wit?: string,
/// create a type only Component shell, without implementations
typesOnly?: boolean,
/// adapters to use
adapters?: [string, Uint8Array][],
/// string encoding used by the Component (this should
/// also be picked up from the Component itself)
stringEncoding?: StringEncoding,
}

export const $init: Promise<void>;
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ world js-component-bindgen-component {
valid-lifting-optimization: option<bool>,
}

enum export-type {
function,
instance,
}

record transpiled {
files: files,
imports: list<string>,
exports: list<string>
exports: list<tuple<string, export-type>>
}

/// Generate the file structure for the transpilation of a component
Expand Down
20 changes: 18 additions & 2 deletions crates/js-component-bindgen-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,31 @@ impl js_component_bindgen_component::JsComponentBindgenComponent for JsComponent
let js_component_bindgen::Transpiled {
files,
imports,
exports,
mut exports,
} = transpile(component, opts)
.map_err(|e| format!("{:?}", e))
.map_err(|e| e.to_string())?;

Ok(Transpiled {
files,
imports,
exports,
exports: exports
.drain(..)
.map(|(name, expt)| {
(
name,
match expt {
wasmtime_environ::component::Export::LiftedFunction { .. } => {
ExportType::Function
}
wasmtime_environ::component::Export::Instance(_) => {
ExportType::Instance
}
_ => panic!("Unexpected export type"),
},
)
})
.collect(),
})
}
}
Loading