mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-17 23:11:23 +00:00
Classes are now working!
This commit is contained in:
@ -4,7 +4,11 @@ use shared;
|
||||
pub struct Js {
|
||||
pub expose_global_memory: bool,
|
||||
pub expose_global_exports: bool,
|
||||
pub expose_get_string_from_wasm: bool,
|
||||
pub expose_pass_string_to_wasm: bool,
|
||||
pub expose_token: bool,
|
||||
pub exports: Vec<(String, String)>,
|
||||
pub classes: Vec<String>,
|
||||
pub nodejs: bool,
|
||||
}
|
||||
|
||||
@ -14,6 +18,9 @@ impl Js {
|
||||
for f in program.free_functions.iter() {
|
||||
self.generate_free_function(f);
|
||||
}
|
||||
for s in program.structs.iter() {
|
||||
self.generate_struct(s);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_free_function(&mut self, func: &shared::Function) {
|
||||
@ -28,12 +35,76 @@ impl Js {
|
||||
return
|
||||
}
|
||||
|
||||
let mut dst = format!("function {}(", func.name);
|
||||
let ret = self.generate_function(&format!("function {}", func.name),
|
||||
&func.name,
|
||||
false,
|
||||
&func.arguments,
|
||||
func.ret.as_ref());
|
||||
|
||||
self.exports.push((func.name.clone(), ret));
|
||||
}
|
||||
|
||||
pub fn generate_struct(&mut self, s: &shared::Struct) {
|
||||
let mut dst = String::new();
|
||||
self.expose_token = true;
|
||||
self.expose_global_exports = true;
|
||||
dst.push_str(&format!("
|
||||
class {} {{
|
||||
constructor(ptr, sym) {{
|
||||
_checkToken(sym);
|
||||
this.__wasmPtr = ptr;
|
||||
}}
|
||||
|
||||
free() {{
|
||||
const ptr = this.__wasmPtr;
|
||||
this.__wasmPtr = 0;
|
||||
exports.{}(ptr);
|
||||
}}
|
||||
", s.name, s.free_function()));
|
||||
|
||||
for function in s.functions.iter() {
|
||||
let f = self.generate_function(
|
||||
&format!("static {}", function.name),
|
||||
&function.struct_function_export_name(&s.name),
|
||||
false,
|
||||
&function.arguments,
|
||||
function.ret.as_ref(),
|
||||
);
|
||||
dst.push_str(&f);
|
||||
dst.push_str("\n");
|
||||
}
|
||||
for method in s.methods.iter() {
|
||||
let f = self.generate_function(
|
||||
&format!("{}", method.function.name),
|
||||
&method.function.struct_function_export_name(&s.name),
|
||||
true,
|
||||
&method.function.arguments,
|
||||
method.function.ret.as_ref(),
|
||||
);
|
||||
dst.push_str(&f);
|
||||
dst.push_str("\n");
|
||||
}
|
||||
dst.push_str("}\n");
|
||||
self.classes.push(dst);
|
||||
self.exports.push((s.name.clone(), s.name.clone()));
|
||||
}
|
||||
|
||||
fn generate_function(&mut self,
|
||||
name: &str,
|
||||
wasm_name: &str,
|
||||
is_method: bool,
|
||||
arguments: &[shared::Type],
|
||||
ret: Option<&shared::Type>) -> String {
|
||||
let mut dst = format!("{}(", name);
|
||||
let mut passed_args = String::new();
|
||||
let mut arg_conversions = String::new();
|
||||
let mut destructors = String::new();
|
||||
|
||||
for (i, arg) in func.arguments.iter().enumerate() {
|
||||
if is_method {
|
||||
passed_args.push_str("this.__wasmPtr");
|
||||
}
|
||||
|
||||
for (i, arg) in arguments.iter().enumerate() {
|
||||
let name = format!("arg{}", i);
|
||||
if i > 0 {
|
||||
dst.push_str(", ");
|
||||
@ -50,23 +121,17 @@ impl Js {
|
||||
shared::Type::Number => pass(&name),
|
||||
shared::Type::BorrowedStr |
|
||||
shared::Type::String => {
|
||||
if self.nodejs {
|
||||
arg_conversions.push_str(&format!("\
|
||||
const buf{i} = Buffer.from({arg});
|
||||
const len{i} = buf{i}.length;
|
||||
const ptr{i} = exports.__wbindgen_malloc(len{i});
|
||||
let memory{i} = new Uint8Array(memory.buffer);
|
||||
buf{i}.copy(memory{i}, ptr{i});
|
||||
", i = i, arg = name));
|
||||
pass(&format!("ptr{}", i));
|
||||
pass(&format!("len{}", i));
|
||||
if let shared::Type::BorrowedStr = *arg {
|
||||
destructors.push_str(&format!("\n\
|
||||
exports.__wbindgen_free(ptr{i}, len{i});\n\
|
||||
", i = i));
|
||||
}
|
||||
} else {
|
||||
panic!("strings not implemented for browser");
|
||||
self.expose_global_exports = true;
|
||||
self.expose_pass_string_to_wasm = true;
|
||||
arg_conversions.push_str(&format!("\
|
||||
const [ptr{i}, len{i}] = passStringToWasm({arg});
|
||||
", i = i, arg = name));
|
||||
pass(&format!("ptr{}", i));
|
||||
pass(&format!("len{}", i));
|
||||
if let shared::Type::BorrowedStr = *arg {
|
||||
destructors.push_str(&format!("\n\
|
||||
exports.__wbindgen_free(ptr{i}, len{i});\n\
|
||||
", i = i));
|
||||
}
|
||||
}
|
||||
shared::Type::ByRef(_) |
|
||||
@ -85,59 +150,107 @@ impl Js {
|
||||
}
|
||||
}
|
||||
}
|
||||
let convert_ret = match func.ret {
|
||||
let convert_ret = match ret {
|
||||
None |
|
||||
Some(shared::Type::Number) => format!("return ret;"),
|
||||
Some(shared::Type::BorrowedStr) |
|
||||
Some(shared::Type::ByMutRef(_)) |
|
||||
Some(shared::Type::ByRef(_)) => panic!(),
|
||||
Some(shared::Type::ByValue(ref name)) => {
|
||||
Some(&shared::Type::Number) => format!("return ret;"),
|
||||
Some(&shared::Type::BorrowedStr) |
|
||||
Some(&shared::Type::ByMutRef(_)) |
|
||||
Some(&shared::Type::ByRef(_)) => panic!(),
|
||||
Some(&shared::Type::ByValue(ref name)) => {
|
||||
format!("\
|
||||
return {name}.__wasmWrap(ret);
|
||||
return new {name}(ret, token);
|
||||
", name = name)
|
||||
}
|
||||
Some(shared::Type::String) => {
|
||||
if self.nodejs {
|
||||
format!("\
|
||||
const mem = new Uint8Array(memory.buffer);
|
||||
const ptr = exports.__wbindgen_boxed_str_ptr(ret);
|
||||
const len = exports.__wbindgen_boxed_str_len(ret);
|
||||
const buf = Buffer.from(mem.slice(ptr, ptr + len));
|
||||
const realRet = buf.toString();
|
||||
exports.__wbindgen_boxed_str_free(ret);
|
||||
return realRet;
|
||||
")
|
||||
} else {
|
||||
panic!("strings not implemented for browser");
|
||||
}
|
||||
Some(&shared::Type::String) => {
|
||||
self.expose_get_string_from_wasm = true;
|
||||
format!("return getStringFromWasm(ret);")
|
||||
}
|
||||
};
|
||||
dst.push_str(") {\n ");
|
||||
dst.push_str(&arg_conversions);
|
||||
dst.push_str(&format!("\
|
||||
try {{
|
||||
self.expose_global_exports = true;
|
||||
if destructors.len() == 0 {
|
||||
dst.push_str(&format!("\
|
||||
const ret = exports.{f}({passed});
|
||||
{convert_ret}
|
||||
}} finally {{
|
||||
{destructors}
|
||||
}}
|
||||
", f = func.name, passed = passed_args, destructors = destructors,
|
||||
convert_ret = convert_ret));
|
||||
dst.push_str("};");
|
||||
|
||||
self.exports.push((func.name.clone(), dst));
|
||||
", f = wasm_name, passed = passed_args, convert_ret = convert_ret));
|
||||
} else {
|
||||
dst.push_str(&format!("\
|
||||
try {{
|
||||
const ret = exports.{f}({passed});
|
||||
{convert_ret}
|
||||
}} finally {{
|
||||
{destructors}
|
||||
}}
|
||||
", f = wasm_name, passed = passed_args, destructors = destructors,
|
||||
convert_ret = convert_ret));
|
||||
}
|
||||
dst.push_str("}");
|
||||
return dst
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
let mut globals = String::new();
|
||||
if self.expose_global_memory {
|
||||
if self.expose_global_memory ||
|
||||
self.expose_pass_string_to_wasm ||
|
||||
self.expose_get_string_from_wasm
|
||||
{
|
||||
globals.push_str("const memory = obj.instance.exports.memory;\n");
|
||||
}
|
||||
if self.expose_global_exports {
|
||||
if self.expose_global_exports ||
|
||||
self.expose_pass_string_to_wasm ||
|
||||
self.expose_get_string_from_wasm
|
||||
{
|
||||
globals.push_str("const exports = obj.instance.exports;\n");
|
||||
}
|
||||
if self.expose_token {
|
||||
globals.push_str("\
|
||||
const token = Symbol('foo');
|
||||
function _checkToken(sym) {
|
||||
if (token != sym)
|
||||
throw new Error('cannot invoke `new` directly');
|
||||
}
|
||||
");
|
||||
}
|
||||
if self.expose_pass_string_to_wasm {
|
||||
if self.nodejs {
|
||||
globals.push_str("
|
||||
function passStringToWasm(arg) {
|
||||
const buf = Buffer.from(arg);
|
||||
const len = buf.length;
|
||||
const ptr = exports.__wbindgen_malloc(len);
|
||||
let array = new Uint8Array(memory.buffer);
|
||||
buf.copy(array, ptr);
|
||||
return [ptr, len];
|
||||
}
|
||||
");
|
||||
} else {
|
||||
panic!("browser strings not implemented yet");
|
||||
}
|
||||
}
|
||||
if self.expose_get_string_from_wasm {
|
||||
if self.nodejs {
|
||||
globals.push_str("
|
||||
function getStringFromWasm(ptr) {
|
||||
const mem = new Uint8Array(memory.buffer);
|
||||
const data = exports.__wbindgen_boxed_str_ptr(ptr);
|
||||
const len = exports.__wbindgen_boxed_str_len(ptr);
|
||||
const buf = Buffer.from(mem.slice(data, data + len));
|
||||
const ret = buf.toString();
|
||||
exports.__wbindgen_boxed_str_free(ptr);
|
||||
return ret;
|
||||
}
|
||||
");
|
||||
} else {
|
||||
panic!("strings not implemented for browser");
|
||||
}
|
||||
}
|
||||
|
||||
let mut exports = String::new();
|
||||
for class in self.classes.iter() {
|
||||
exports.push_str(class);
|
||||
exports.push_str("\n");
|
||||
}
|
||||
for &(ref name, ref body) in self.exports.iter() {
|
||||
exports.push_str("obj.");
|
||||
exports.push_str(name);
|
||||
@ -146,12 +259,12 @@ impl Js {
|
||||
exports.push_str(";\n");
|
||||
}
|
||||
format!("
|
||||
const function xform(obj) {{
|
||||
function xform(obj) {{
|
||||
{}
|
||||
{}
|
||||
return obj;
|
||||
}}
|
||||
export const function instantiate(bytes, imports) {{
|
||||
export function instantiate(bytes, imports) {{
|
||||
return WebAssembly.instantiate(bytes, imports).then(xform);
|
||||
}}
|
||||
", globals, exports)
|
||||
|
@ -88,6 +88,7 @@ impl Object {
|
||||
|
||||
pub fn generate_js(&self) -> String {
|
||||
let mut js = js::Js::default();
|
||||
js.nodejs = self.nodejs;
|
||||
js.generate_program(&self.program);
|
||||
js.to_string()
|
||||
}
|
||||
|
Reference in New Issue
Block a user