mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-18 07:21:24 +00:00
Rename directories to remove "wasm-bindgen-" prefix in sub-crates
This commit is contained in:
1712
crates/cli-support/src/js.rs
Normal file
1712
crates/cli-support/src/js.rs
Normal file
File diff suppressed because it is too large
Load Diff
232
crates/cli-support/src/lib.rs
Normal file
232
crates/cli-support/src/lib.rs
Normal file
@ -0,0 +1,232 @@
|
||||
extern crate parity_wasm;
|
||||
extern crate wasm_bindgen_shared as shared;
|
||||
extern crate serde_json;
|
||||
extern crate wasm_gc;
|
||||
|
||||
use std::char;
|
||||
use std::collections::BTreeSet;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::slice;
|
||||
|
||||
use parity_wasm::elements::*;
|
||||
|
||||
mod js;
|
||||
pub mod wasm2es6js;
|
||||
|
||||
pub struct Bindgen {
|
||||
path: Option<PathBuf>,
|
||||
nodejs: bool,
|
||||
browser: bool,
|
||||
debug: bool,
|
||||
typescript: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error(String);
|
||||
|
||||
impl<E: std::error::Error> From<E> for Error {
|
||||
fn from(e: E) -> Error {
|
||||
Error(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Bindgen {
|
||||
pub fn new() -> Bindgen {
|
||||
Bindgen {
|
||||
path: None,
|
||||
nodejs: false,
|
||||
browser: false,
|
||||
debug: false,
|
||||
typescript: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Bindgen {
|
||||
self.path = Some(path.as_ref().to_path_buf());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn nodejs(&mut self, node: bool) -> &mut Bindgen {
|
||||
self.nodejs = node;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn browser(&mut self, browser: bool) -> &mut Bindgen {
|
||||
self.browser = browser;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
|
||||
self.debug = debug;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn typescript(&mut self, typescript: bool) -> &mut Bindgen {
|
||||
self.typescript = typescript;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
|
||||
self._generate(path.as_ref())
|
||||
}
|
||||
|
||||
fn _generate(&mut self, out_dir: &Path) -> Result<(), Error> {
|
||||
let input = match self.path {
|
||||
Some(ref path) => path,
|
||||
None => panic!("must have a path input for now"),
|
||||
};
|
||||
let stem = input.file_stem().unwrap().to_str().unwrap();
|
||||
let mut module = parity_wasm::deserialize_file(input).map_err(|e| {
|
||||
Error(format!("{:?}", e))
|
||||
})?;
|
||||
let programs = extract_programs(&mut module);
|
||||
|
||||
let (js, ts) = {
|
||||
let mut cx = js::Context {
|
||||
globals: String::new(),
|
||||
imports: String::new(),
|
||||
footer: String::new(),
|
||||
typescript: format!("/* tslint:disable */\n"),
|
||||
exposed_globals: Default::default(),
|
||||
required_internal_exports: Default::default(),
|
||||
custom_type_names: Default::default(),
|
||||
imported_names: Default::default(),
|
||||
exported_classes: Default::default(),
|
||||
config: &self,
|
||||
module: &mut module,
|
||||
};
|
||||
for program in programs.iter() {
|
||||
cx.add_custom_type_names(program);
|
||||
}
|
||||
for program in programs.iter() {
|
||||
js::SubContext {
|
||||
program,
|
||||
cx: &mut cx,
|
||||
}.generate();
|
||||
}
|
||||
cx.finalize(stem)
|
||||
};
|
||||
|
||||
let js_path = out_dir.join(stem).with_extension("js");
|
||||
File::create(&js_path).unwrap()
|
||||
.write_all(js.as_bytes()).unwrap();
|
||||
|
||||
if self.typescript {
|
||||
let ts_path = out_dir.join(stem).with_extension("d.ts");
|
||||
File::create(&ts_path).unwrap()
|
||||
.write_all(ts.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
let wasm_path = out_dir.join(format!("{}_bg", stem)).with_extension("wasm");
|
||||
|
||||
if self.nodejs {
|
||||
let js_path = wasm_path.with_extension("js");
|
||||
let shim = self.generate_node_wasm_import(&module, &wasm_path);
|
||||
File::create(&js_path)?.write_all(shim.as_bytes())?;
|
||||
}
|
||||
|
||||
let wasm_bytes = parity_wasm::serialize(module).map_err(|e| {
|
||||
Error(format!("{:?}", e))
|
||||
})?;
|
||||
let bytes = wasm_gc::Config::new()
|
||||
.demangle(false)
|
||||
.gc(&wasm_bytes)?;
|
||||
File::create(&wasm_path)?.write_all(&bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_node_wasm_import(&self, m: &Module, path: &Path) -> String {
|
||||
let mut imports = BTreeSet::new();
|
||||
if let Some(i) = m.import_section() {
|
||||
for i in i.entries() {
|
||||
imports.insert(i.module());
|
||||
}
|
||||
}
|
||||
|
||||
let mut shim = String::new();
|
||||
shim.push_str("let imports = {};\n");
|
||||
for module in imports {
|
||||
shim.push_str(&format!("imports['{0}'] = require('{0}');\n", module));
|
||||
}
|
||||
|
||||
shim.push_str(&format!("
|
||||
const bytes = require('fs').readFileSync('{}');
|
||||
const wasmModule = new WebAssembly.Module(bytes);
|
||||
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
|
||||
module.exports = wasmInstance.exports;
|
||||
", path.file_name().unwrap().to_str().unwrap()));
|
||||
|
||||
shim
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_programs(module: &mut Module) -> Vec<shared::Program> {
|
||||
let version = shared::version();
|
||||
let mut ret = Vec::new();
|
||||
|
||||
#[repr(packed)]
|
||||
struct Unaligned(u32);
|
||||
|
||||
module.sections_mut().retain(|s| {
|
||||
let custom = match *s {
|
||||
Section::Custom(ref s) => s,
|
||||
_ => return true,
|
||||
};
|
||||
if custom.name() != "__wasm_bindgen_unstable" {
|
||||
return true
|
||||
}
|
||||
|
||||
assert!(custom.payload().len() % 4 == 0);
|
||||
let mut payload = unsafe {
|
||||
slice::from_raw_parts(custom.payload().as_ptr() as *const Unaligned,
|
||||
custom.payload().len() / 4)
|
||||
};
|
||||
|
||||
while payload.len() > 0 {
|
||||
let len = payload[0].0.to_le();
|
||||
assert!(len % 4 == 0);
|
||||
let (a, b) = payload[1..].split_at((len / 4) as usize);
|
||||
payload = b;
|
||||
let json = a.iter()
|
||||
.map(|i| char::from_u32(i.0.to_le()).unwrap())
|
||||
.collect::<String>();
|
||||
let p: shared::Program = match serde_json::from_str(&json) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
panic!("failed to decode what looked like wasm-bindgen data: {}", e)
|
||||
}
|
||||
};
|
||||
if p.schema_version != shared::SCHEMA_VERSION {
|
||||
panic!("
|
||||
|
||||
it looks like the Rust project used to create this wasm file was linked against
|
||||
a different version of wasm-bindgen than this binary:
|
||||
|
||||
rust wasm file: {}
|
||||
this binary: {}
|
||||
|
||||
Currently the bindgen format is unstable enough that these two version must
|
||||
exactly match, so it's required that these two version are kept in sync by
|
||||
either updating the wasm-bindgen dependency or this binary. You should be able
|
||||
to update the wasm-bindgen dependency with:
|
||||
|
||||
cargo update -p wasm-bindgen
|
||||
|
||||
or you can update the binary with
|
||||
|
||||
cargo install -f --git https://github.com/alexcrichton/wasm-bindgen
|
||||
|
||||
if this warning fails to go away though and you're not sure what to do feel free
|
||||
to open an issue at https://github.com/alexcrichton/wasm-bindgen/issues!
|
||||
",
|
||||
p.version, version);
|
||||
}
|
||||
ret.push(p);
|
||||
}
|
||||
|
||||
false
|
||||
});
|
||||
return ret
|
||||
}
|
214
crates/cli-support/src/wasm2es6js.rs
Normal file
214
crates/cli-support/src/wasm2es6js.rs
Normal file
@ -0,0 +1,214 @@
|
||||
extern crate base64;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use parity_wasm::elements::*;
|
||||
|
||||
use super::Error;
|
||||
|
||||
pub struct Config {
|
||||
base64: bool,
|
||||
}
|
||||
|
||||
pub struct Output {
|
||||
module: Module,
|
||||
base64: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Config {
|
||||
Config {
|
||||
base64: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn base64(&mut self, base64: bool) -> &mut Self {
|
||||
self.base64 = base64;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn generate(&mut self, wasm: &[u8]) -> Result<Output, Error> {
|
||||
assert!(self.base64);
|
||||
let module = deserialize_buffer(wasm).map_err(|e| {
|
||||
::Error(format!("{:?}", e))
|
||||
})?;
|
||||
Ok(Output {
|
||||
module,
|
||||
base64: self.base64,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Output {
|
||||
pub fn typescript(&self) -> String {
|
||||
let mut exports = format!("/* tslint:disable */\n");
|
||||
|
||||
if let Some(i) = self.module.export_section() {
|
||||
let imported_functions = self.module.import_section()
|
||||
.map(|m| m.functions() as u32)
|
||||
.unwrap_or(0);
|
||||
for entry in i.entries() {
|
||||
let idx = match *entry.internal() {
|
||||
Internal::Function(i) => i - imported_functions,
|
||||
Internal::Memory(_) => {
|
||||
exports.push_str(&format!("
|
||||
export const {}: WebAssembly.Memory;
|
||||
", entry.field()));
|
||||
continue
|
||||
}
|
||||
Internal::Table(_) => {
|
||||
continue
|
||||
}
|
||||
Internal::Global(_) => {
|
||||
continue
|
||||
}
|
||||
};
|
||||
|
||||
let functions = self.module.function_section()
|
||||
.expect("failed to find function section");
|
||||
let idx = functions.entries()[idx as usize].type_ref();
|
||||
|
||||
let types = self.module.type_section()
|
||||
.expect("failed to find type section");
|
||||
let ty = match types.types()[idx as usize] {
|
||||
Type::Function(ref f) => f,
|
||||
};
|
||||
let mut args = String::new();
|
||||
for (i, _) in ty.params().iter().enumerate() {
|
||||
if i > 0 {
|
||||
args.push_str(", ");
|
||||
}
|
||||
args.push((b'a' + (i as u8)) as char);
|
||||
args.push_str(": number");
|
||||
}
|
||||
|
||||
exports.push_str(&format!("
|
||||
export function {name}({args}): {ret};
|
||||
",
|
||||
name = entry.field(),
|
||||
args = args,
|
||||
ret = if ty.return_type().is_some() { "number" } else { "void" },
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if self.base64 {
|
||||
exports.push_str("export const booted: Promise<boolean>;");
|
||||
}
|
||||
|
||||
return exports
|
||||
}
|
||||
|
||||
pub fn js(self) -> String {
|
||||
let mut js_imports = String::new();
|
||||
let mut exports = String::new();
|
||||
let mut imports = String::new();
|
||||
let mut export_mem = false;
|
||||
|
||||
if let Some(i) = self.module.import_section() {
|
||||
let mut set = HashSet::new();
|
||||
for entry in i.entries() {
|
||||
match *entry.external() {
|
||||
External::Function(_) => {}
|
||||
External::Table(_) => {
|
||||
panic!("wasm imports a table which isn't supported yet");
|
||||
}
|
||||
External::Memory(_) => {
|
||||
panic!("wasm imports memory which isn't supported yet");
|
||||
}
|
||||
External::Global(_) => {
|
||||
panic!("wasm imports globals which aren't supported yet");
|
||||
}
|
||||
}
|
||||
|
||||
if !set.insert(entry.module()) {
|
||||
continue
|
||||
}
|
||||
|
||||
let name = (b'a' + (set.len() as u8)) as char;
|
||||
js_imports.push_str(&format!("import * as import_{} from '{}';",
|
||||
name,
|
||||
entry.module()));
|
||||
imports.push_str(&format!("'{}': import_{}, ", entry.module(), name));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(i) = self.module.export_section() {
|
||||
let imported_functions = self.module.import_section()
|
||||
.map(|m| m.functions() as u32)
|
||||
.unwrap_or(0);
|
||||
for entry in i.entries() {
|
||||
let idx = match *entry.internal() {
|
||||
Internal::Function(i) => i - imported_functions,
|
||||
Internal::Memory(_) => {
|
||||
export_mem = true;
|
||||
continue
|
||||
}
|
||||
Internal::Table(_) => {
|
||||
continue
|
||||
}
|
||||
Internal::Global(_) => {
|
||||
continue
|
||||
}
|
||||
};
|
||||
|
||||
let functions = self.module.function_section()
|
||||
.expect("failed to find function section");
|
||||
let idx = functions.entries()[idx as usize].type_ref();
|
||||
|
||||
let types = self.module.type_section()
|
||||
.expect("failed to find type section");
|
||||
let ty = match types.types()[idx as usize] {
|
||||
Type::Function(ref f) => f,
|
||||
};
|
||||
let mut args = String::new();
|
||||
for (i, _) in ty.params().iter().enumerate() {
|
||||
if i > 0 {
|
||||
args.push_str(", ");
|
||||
}
|
||||
args.push((b'a' + (i as u8)) as char);
|
||||
}
|
||||
|
||||
exports.push_str(&format!("
|
||||
export function {name}({args}) {{
|
||||
{ret} wasm.exports.{name}({args});
|
||||
}}
|
||||
",
|
||||
name = entry.field(),
|
||||
args = args,
|
||||
ret = if ty.return_type().is_some() { "return" } else { "" },
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let wasm = serialize(self.module)
|
||||
.expect("failed to serialize");
|
||||
|
||||
format!("
|
||||
{js_imports}
|
||||
let wasm;
|
||||
let bytes;
|
||||
const base64 = \"{base64}\";
|
||||
if (typeof Buffer === 'undefined') {{
|
||||
bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
|
||||
}} else {{
|
||||
bytes = Buffer.from(base64, 'base64');
|
||||
}}
|
||||
{mem_export}
|
||||
export const booted = WebAssembly.instantiate(bytes, {{ {imports} }})
|
||||
.then(obj => {{
|
||||
wasm = obj.instance;
|
||||
{memory}
|
||||
}});
|
||||
|
||||
{exports}
|
||||
",
|
||||
base64 = base64::encode(&wasm),
|
||||
js_imports = js_imports,
|
||||
imports = imports,
|
||||
exports = exports,
|
||||
mem_export = if export_mem { "export let memory;" } else { "" },
|
||||
memory = if export_mem { "memory = wasm.exports.memory;" } else { "" },
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user