mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-01 18:01:06 +00:00
Merge pull request #134 from rustwasm/better-formatting
Work a little harder to better format the output JS
This commit is contained in:
commit
f61e12af91
@ -78,13 +78,19 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
|
|
||||||
/// Add extra processing to the prelude of this shim.
|
/// Add extra processing to the prelude of this shim.
|
||||||
pub fn prelude(&mut self, s: &str) -> &mut Self {
|
pub fn prelude(&mut self, s: &str) -> &mut Self {
|
||||||
self.prelude.push_str(s);
|
for line in s.lines() {
|
||||||
|
self.prelude.push_str(line);
|
||||||
|
self.prelude.push_str("\n");
|
||||||
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add extra processing to the finally block of this shim.
|
/// Add extra processing to the finally block of this shim.
|
||||||
pub fn finally(&mut self, s: &str) -> &mut Self {
|
pub fn finally(&mut self, s: &str) -> &mut Self {
|
||||||
self.finally.push_str(s);
|
for line in s.lines() {
|
||||||
|
self.finally.push_str(line);
|
||||||
|
self.finally.push_str("\n");
|
||||||
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,12 +111,12 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
let func = self.cx.pass_to_wasm_function(kind);
|
let func = self.cx.pass_to_wasm_function(kind);
|
||||||
self.cx.expose_set_global_argument();
|
self.cx.expose_set_global_argument();
|
||||||
let global_idx = self.global_idx();
|
let global_idx = self.global_idx();
|
||||||
self.prelude.push_str(&format!("\
|
self.prelude(&format!("\
|
||||||
const [ptr{i}, len{i}] = {func}({arg});
|
const [ptr{i}, len{i}] = {func}({arg});\n\
|
||||||
setGlobalArgument(len{i}, {global_idx});
|
setGlobalArgument(len{i}, {global_idx});\n\
|
||||||
", i = i, func = func, arg = name, global_idx = global_idx));
|
", i = i, func = func, arg = name, global_idx = global_idx));
|
||||||
if arg.is_by_ref() {
|
if arg.is_by_ref() {
|
||||||
self.finally.push_str(&format!("\n\
|
self.finally(&format!("\
|
||||||
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
|
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
|
||||||
", i = i, size = kind.size()));
|
", i = i, size = kind.size()));
|
||||||
self.cx.required_internal_exports.insert(
|
self.cx.required_internal_exports.insert(
|
||||||
@ -126,17 +132,17 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
|
|
||||||
if self.cx.config.debug {
|
if self.cx.config.debug {
|
||||||
self.cx.expose_assert_class();
|
self.cx.expose_assert_class();
|
||||||
self.prelude.push_str(&format!("\
|
self.prelude(&format!("\
|
||||||
_assertClass({arg}, {struct_});
|
_assertClass({arg}, {struct_});\n\
|
||||||
", arg = name, struct_ = s));
|
", arg = name, struct_ = s));
|
||||||
}
|
}
|
||||||
|
|
||||||
if arg.is_by_ref() {
|
if arg.is_by_ref() {
|
||||||
self.rust_arguments.push(format!("{}.ptr", name));
|
self.rust_arguments.push(format!("{}.ptr", name));
|
||||||
} else {
|
} else {
|
||||||
self.prelude.push_str(&format!("\
|
self.prelude(&format!("\
|
||||||
const ptr{i} = {arg}.ptr;
|
const ptr{i} = {arg}.ptr;\n\
|
||||||
{arg}.ptr = 0;
|
{arg}.ptr = 0;\n\
|
||||||
", i = i, arg = name));
|
", i = i, arg = name));
|
||||||
self.rust_arguments.push(format!("ptr{}", i));
|
self.rust_arguments.push(format!("ptr{}", i));
|
||||||
}
|
}
|
||||||
@ -148,7 +154,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
|
|
||||||
if self.cx.config.debug {
|
if self.cx.config.debug {
|
||||||
self.cx.expose_assert_num();
|
self.cx.expose_assert_num();
|
||||||
self.prelude.push_str(&format!("_assertNum({});\n", name));
|
self.prelude(&format!("_assertNum({});", name));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.rust_arguments.push(name);
|
self.rust_arguments.push(name);
|
||||||
@ -158,7 +164,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
if arg.is_ref_anyref() {
|
if arg.is_ref_anyref() {
|
||||||
self.js_arguments.push((name.clone(), "any".to_string()));
|
self.js_arguments.push((name.clone(), "any".to_string()));
|
||||||
self.cx.expose_borrowed_objects();
|
self.cx.expose_borrowed_objects();
|
||||||
self.finally.push_str("stack.pop();\n");
|
self.finally("stack.pop();");
|
||||||
self.rust_arguments.push(format!("addBorrowedObject({})", name));
|
self.rust_arguments.push(format!("addBorrowedObject({})", name));
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -168,8 +174,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
self.js_arguments.push((name.clone(), "boolean".to_string()));
|
self.js_arguments.push((name.clone(), "boolean".to_string()));
|
||||||
if self.cx.config.debug {
|
if self.cx.config.debug {
|
||||||
self.cx.expose_assert_bool();
|
self.cx.expose_assert_bool();
|
||||||
self.prelude.push_str(&format!("\
|
self.prelude(&format!("\
|
||||||
_assertBoolean({name});
|
_assertBoolean({name});\n\
|
||||||
", name = name));
|
", name = name));
|
||||||
}
|
}
|
||||||
self.rust_arguments.push(format!("arg{i} ? 1 : 0", i = i));
|
self.rust_arguments.push(format!("arg{i} ? 1 : 0", i = i));
|
||||||
@ -211,12 +217,12 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
let f = self.cx.expose_get_vector_from_wasm(ty);
|
let f = self.cx.expose_get_vector_from_wasm(ty);
|
||||||
self.cx.expose_get_global_argument();
|
self.cx.expose_get_global_argument();
|
||||||
self.cx.required_internal_exports.insert("__wbindgen_free");
|
self.cx.required_internal_exports.insert("__wbindgen_free");
|
||||||
self.ret_expr = format!("
|
self.ret_expr = format!("\
|
||||||
const ret = RET;
|
const ret = RET;\n\
|
||||||
const len = getGlobalArgument(0);
|
const len = getGlobalArgument(0);\n\
|
||||||
const realRet = {}(ret, len);
|
const realRet = {}(ret, len);\n\
|
||||||
wasm.__wbindgen_free(ret, len * {});
|
wasm.__wbindgen_free(ret, len * {});\n\
|
||||||
return realRet;
|
return realRet;\n\
|
||||||
", f, ty.size());
|
", f, ty.size());
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -263,24 +269,25 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
let mut js = format!("{}({}) {{\n", prefix, js_args);
|
let mut js = format!("{}({}) {{\n", prefix, js_args);
|
||||||
js.push_str(&self.prelude);
|
js.push_str(&indent(&self.prelude));
|
||||||
let rust_args = self.rust_arguments.join(", ");
|
let rust_args = self.rust_arguments.join(", ");
|
||||||
|
|
||||||
let invoc = self.ret_expr.replace("RET", &format!("{}({})", invoc, rust_args));
|
let invoc = self.ret_expr.replace("RET", &format!("{}({})", invoc, rust_args));
|
||||||
if self.finally.len() == 0 {
|
let invoc = if self.finally.len() == 0 {
|
||||||
js.push_str(&invoc);
|
invoc
|
||||||
} else {
|
} else {
|
||||||
js.push_str(&format!("\
|
format!("\
|
||||||
try {{
|
try {{\n\
|
||||||
{}
|
{}\
|
||||||
}} finally {{
|
}} finally {{\n\
|
||||||
{}
|
{}\
|
||||||
}}
|
}}\n\
|
||||||
",
|
",
|
||||||
invoc,
|
indent(&invoc),
|
||||||
self.finally,
|
indent(&self.finally),
|
||||||
));
|
)
|
||||||
}
|
};
|
||||||
|
js.push_str(&indent(&invoc));
|
||||||
js.push_str("}");
|
js.push_str("}");
|
||||||
|
|
||||||
let ts_args = self.js_arguments
|
let ts_args = self.js_arguments
|
||||||
@ -298,3 +305,13 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn indent(s: &str) -> String {
|
||||||
|
let mut ret = String::new();
|
||||||
|
for line in s.lines() {
|
||||||
|
ret.push_str(" ");
|
||||||
|
ret.push_str(line);
|
||||||
|
ret.push_str("\n");
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
@ -56,7 +56,7 @@ impl<'a> Context<'a> {
|
|||||||
format!("export const {} = {};\n", name, contents)
|
format!("export const {} = {};\n", name, contents)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.globals.push_str(&global);
|
self.global(&global);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finalize(&mut self, module_name: &str) -> (String, String) {
|
pub fn finalize(&mut self, module_name: &str) -> (String, String) {
|
||||||
@ -262,7 +262,7 @@ impl<'a> Context<'a> {
|
|||||||
|
|
||||||
self.rewrite_imports(module_name);
|
self.rewrite_imports(module_name);
|
||||||
|
|
||||||
let js = if self.config.no_modules {
|
let mut js = if self.config.no_modules {
|
||||||
format!("
|
format!("
|
||||||
(function() {{
|
(function() {{
|
||||||
var wasm;
|
var wasm;
|
||||||
@ -292,12 +292,12 @@ impl<'a> Context<'a> {
|
|||||||
format!("import * as wasm from './{}_bg';", module_name)
|
format!("import * as wasm from './{}_bg';", module_name)
|
||||||
};
|
};
|
||||||
|
|
||||||
format!("
|
format!("\
|
||||||
/* tslint:disable */
|
/* tslint:disable */\n\
|
||||||
{import_wasm}
|
{import_wasm}\n\
|
||||||
{imports}
|
{imports}\n\
|
||||||
|
|
||||||
{globals}
|
{globals}\n\
|
||||||
{footer}",
|
{footer}",
|
||||||
import_wasm = import_wasm,
|
import_wasm = import_wasm,
|
||||||
globals = self.globals,
|
globals = self.globals,
|
||||||
@ -309,6 +309,10 @@ impl<'a> Context<'a> {
|
|||||||
self.export_table();
|
self.export_table();
|
||||||
self.gc();
|
self.gc();
|
||||||
|
|
||||||
|
while js.contains("\n\n\n") {
|
||||||
|
js = js.replace("\n\n\n", "\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
(js, self.typescript.clone())
|
(js, self.typescript.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -539,7 +543,7 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
")
|
")
|
||||||
};
|
};
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function dropRef(idx) {{
|
function dropRef(idx) {{
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -557,7 +561,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("stack") {
|
if !self.exposed_globals.insert("stack") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let stack = [];
|
let stack = [];
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
@ -566,14 +570,14 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("slab") {
|
if !self.exposed_globals.insert("slab") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("let slab = [];"));
|
self.global(&format!("let slab = [];"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expose_global_slab_next(&mut self) {
|
fn expose_global_slab_next(&mut self) {
|
||||||
if !self.exposed_globals.insert("slab_next") {
|
if !self.exposed_globals.insert("slab_next") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let slab_next = 0;
|
let slab_next = 0;
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
@ -596,7 +600,7 @@ impl<'a> Context<'a> {
|
|||||||
return val.obj;
|
return val.obj;
|
||||||
")
|
")
|
||||||
};
|
};
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getObject(idx) {{
|
function getObject(idx) {{
|
||||||
if ((idx & 1) === 1) {{
|
if ((idx & 1) === 1) {{
|
||||||
return stack[idx >> 1];
|
return stack[idx >> 1];
|
||||||
@ -612,7 +616,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("assert_num") {
|
if !self.exposed_globals.insert("assert_num") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function _assertNum(n) {{
|
function _assertNum(n) {{
|
||||||
if (typeof(n) !== 'number')
|
if (typeof(n) !== 'number')
|
||||||
throw new Error('expected a number argument');
|
throw new Error('expected a number argument');
|
||||||
@ -624,7 +628,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("assert_bool") {
|
if !self.exposed_globals.insert("assert_bool") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function _assertBoolean(n) {{
|
function _assertBoolean(n) {{
|
||||||
if (typeof(n) !== 'boolean')
|
if (typeof(n) !== 'boolean')
|
||||||
throw new Error('expected a boolean argument');
|
throw new Error('expected a boolean argument');
|
||||||
@ -647,7 +651,7 @@ impl<'a> Context<'a> {
|
|||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function passStringToWasm(arg) {{
|
function passStringToWasm(arg) {{
|
||||||
{}
|
{}
|
||||||
const buf = cachedEncoder.encode(arg);
|
const buf = cachedEncoder.encode(arg);
|
||||||
@ -664,7 +668,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.required_internal_exports.insert("__wbindgen_malloc");
|
self.required_internal_exports.insert("__wbindgen_malloc");
|
||||||
self.expose_uint8_memory();
|
self.expose_uint8_memory();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function passArray8ToWasm(arg) {{
|
function passArray8ToWasm(arg) {{
|
||||||
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
||||||
getUint8Memory().set(arg, ptr);
|
getUint8Memory().set(arg, ptr);
|
||||||
@ -679,7 +683,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.required_internal_exports.insert("__wbindgen_malloc");
|
self.required_internal_exports.insert("__wbindgen_malloc");
|
||||||
self.expose_uint16_memory();
|
self.expose_uint16_memory();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function passArray16ToWasm(arg) {{
|
function passArray16ToWasm(arg) {{
|
||||||
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
||||||
getUint16Memory().set(arg, ptr / 2);
|
getUint16Memory().set(arg, ptr / 2);
|
||||||
@ -694,7 +698,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.required_internal_exports.insert("__wbindgen_malloc");
|
self.required_internal_exports.insert("__wbindgen_malloc");
|
||||||
self.expose_uint32_memory();
|
self.expose_uint32_memory();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function passArray32ToWasm(arg) {{
|
function passArray32ToWasm(arg) {{
|
||||||
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
||||||
getUint32Memory().set(arg, ptr / 4);
|
getUint32Memory().set(arg, ptr / 4);
|
||||||
@ -708,7 +712,7 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.required_internal_exports.insert("__wbindgen_malloc");
|
self.required_internal_exports.insert("__wbindgen_malloc");
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function passArrayF32ToWasm(arg) {{
|
function passArrayF32ToWasm(arg) {{
|
||||||
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
||||||
new Float32Array(wasm.memory.buffer).set(arg, ptr / 4);
|
new Float32Array(wasm.memory.buffer).set(arg, ptr / 4);
|
||||||
@ -722,7 +726,7 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.required_internal_exports.insert("__wbindgen_malloc");
|
self.required_internal_exports.insert("__wbindgen_malloc");
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function passArrayF64ToWasm(arg) {{
|
function passArrayF64ToWasm(arg) {{
|
||||||
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
const ptr = wasm.__wbindgen_malloc(arg.byteLength);
|
||||||
new Float64Array(wasm.memory.buffer).set(arg, ptr / 8);
|
new Float64Array(wasm.memory.buffer).set(arg, ptr / 8);
|
||||||
@ -736,17 +740,17 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if self.config.nodejs {
|
if self.config.nodejs {
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
const TextEncoder = require('util').TextEncoder;
|
const TextEncoder = require('util').TextEncoder;
|
||||||
"));
|
"));
|
||||||
} else if !(self.config.browser || self.config.no_modules) {
|
} else if !(self.config.browser || self.config.no_modules) {
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
const TextEncoder = typeof window === 'object' && window.TextEncoder
|
const TextEncoder = typeof window === 'object' && window.TextEncoder
|
||||||
? window.TextEncoder
|
? window.TextEncoder
|
||||||
: require('util').TextEncoder;
|
: require('util').TextEncoder;
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let cachedEncoder = new TextEncoder('utf-8');
|
let cachedEncoder = new TextEncoder('utf-8');
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
@ -756,17 +760,17 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if self.config.nodejs {
|
if self.config.nodejs {
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
const TextDecoder = require('util').TextDecoder;
|
const TextDecoder = require('util').TextDecoder;
|
||||||
"));
|
"));
|
||||||
} else if !(self.config.browser || self.config.no_modules) {
|
} else if !(self.config.browser || self.config.no_modules) {
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
const TextDecoder = typeof window === 'object' && window.TextDecoder
|
const TextDecoder = typeof window === 'object' && window.TextDecoder
|
||||||
? window.TextDecoder
|
? window.TextDecoder
|
||||||
: require('util').TextDecoder;
|
: require('util').TextDecoder;
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let cachedDecoder = new TextDecoder('utf-8');
|
let cachedDecoder = new TextDecoder('utf-8');
|
||||||
"));
|
"));
|
||||||
}
|
}
|
||||||
@ -776,7 +780,7 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.globals.push_str("
|
self.global("
|
||||||
class ConstructorToken {
|
class ConstructorToken {
|
||||||
constructor(ptr) {
|
constructor(ptr) {
|
||||||
this.ptr = ptr;
|
this.ptr = ptr;
|
||||||
@ -791,7 +795,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.expose_text_decoder();
|
self.expose_text_decoder();
|
||||||
self.expose_uint8_memory();
|
self.expose_uint8_memory();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getStringFromWasm(ptr, len) {{
|
function getStringFromWasm(ptr, len) {{
|
||||||
return cachedDecoder.decode(getUint8Memory().slice(ptr, ptr + len));
|
return cachedDecoder.decode(getUint8Memory().slice(ptr, ptr + len));
|
||||||
}}
|
}}
|
||||||
@ -804,7 +808,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.expose_get_array_u32_from_wasm();
|
self.expose_get_array_u32_from_wasm();
|
||||||
self.expose_get_object();
|
self.expose_get_object();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayJsValueFromWasm(ptr, len) {{
|
function getArrayJsValueFromWasm(ptr, len) {{
|
||||||
const mem = getUint32Memory();
|
const mem = getUint32Memory();
|
||||||
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
||||||
@ -822,7 +826,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_i8_from_wasm") {
|
if !self.exposed_globals.insert("get_array_i8_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayI8FromWasm(ptr, len) {{
|
function getArrayI8FromWasm(ptr, len) {{
|
||||||
const mem = getUint8Memory();
|
const mem = getUint8Memory();
|
||||||
const slice = mem.slice(ptr, ptr + len);
|
const slice = mem.slice(ptr, ptr + len);
|
||||||
@ -836,7 +840,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_u8_from_wasm") {
|
if !self.exposed_globals.insert("get_array_u8_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayU8FromWasm(ptr, len) {{
|
function getArrayU8FromWasm(ptr, len) {{
|
||||||
const mem = getUint8Memory();
|
const mem = getUint8Memory();
|
||||||
const slice = mem.slice(ptr, ptr + len);
|
const slice = mem.slice(ptr, ptr + len);
|
||||||
@ -850,7 +854,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_i16_from_wasm") {
|
if !self.exposed_globals.insert("get_array_i16_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayI16FromWasm(ptr, len) {{
|
function getArrayI16FromWasm(ptr, len) {{
|
||||||
const mem = getUint16Memory();
|
const mem = getUint16Memory();
|
||||||
const slice = mem.slice(ptr / 2, ptr / 2 + len);
|
const slice = mem.slice(ptr / 2, ptr / 2 + len);
|
||||||
@ -864,7 +868,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_u16_from_wasm") {
|
if !self.exposed_globals.insert("get_array_u16_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayU16FromWasm(ptr, len) {{
|
function getArrayU16FromWasm(ptr, len) {{
|
||||||
const mem = getUint16Memory();
|
const mem = getUint16Memory();
|
||||||
const slice = mem.slice(ptr / 2, ptr / 2 + len);
|
const slice = mem.slice(ptr / 2, ptr / 2 + len);
|
||||||
@ -878,7 +882,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_i32_from_wasm") {
|
if !self.exposed_globals.insert("get_array_i32_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayI32FromWasm(ptr, len) {{
|
function getArrayI32FromWasm(ptr, len) {{
|
||||||
const mem = getUint32Memory();
|
const mem = getUint32Memory();
|
||||||
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
||||||
@ -892,7 +896,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_u32_from_wasm") {
|
if !self.exposed_globals.insert("get_array_u32_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayU32FromWasm(ptr, len) {{
|
function getArrayU32FromWasm(ptr, len) {{
|
||||||
const mem = getUint32Memory();
|
const mem = getUint32Memory();
|
||||||
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
||||||
@ -905,7 +909,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_f32_from_wasm") {
|
if !self.exposed_globals.insert("get_array_f32_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayF32FromWasm(ptr, len) {{
|
function getArrayF32FromWasm(ptr, len) {{
|
||||||
const mem = new Float32Array(wasm.memory.buffer);
|
const mem = new Float32Array(wasm.memory.buffer);
|
||||||
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
const slice = mem.slice(ptr / 4, ptr / 4 + len);
|
||||||
@ -918,7 +922,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("get_array_f64_from_wasm") {
|
if !self.exposed_globals.insert("get_array_f64_from_wasm") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function getArrayF64FromWasm(ptr, len) {{
|
function getArrayF64FromWasm(ptr, len) {{
|
||||||
const mem = new Float64Array(wasm.memory.buffer);
|
const mem = new Float64Array(wasm.memory.buffer);
|
||||||
const slice = mem.slice(ptr / 8, ptr / 8 + len);
|
const slice = mem.slice(ptr / 8, ptr / 8 + len);
|
||||||
@ -931,7 +935,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("uint8_memory") {
|
if !self.exposed_globals.insert("uint8_memory") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let cachedUint8Memory = null;
|
let cachedUint8Memory = null;
|
||||||
function getUint8Memory() {{
|
function getUint8Memory() {{
|
||||||
if (cachedUint8Memory === null ||
|
if (cachedUint8Memory === null ||
|
||||||
@ -946,7 +950,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("uint16_memory") {
|
if !self.exposed_globals.insert("uint16_memory") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let cachedUint16Memory = null;
|
let cachedUint16Memory = null;
|
||||||
function getUint16Memory() {{
|
function getUint16Memory() {{
|
||||||
if (cachedUint16Memory === null ||
|
if (cachedUint16Memory === null ||
|
||||||
@ -961,7 +965,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("uint32_memory") {
|
if !self.exposed_globals.insert("uint32_memory") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
let cachedUint32Memory = null;
|
let cachedUint32Memory = null;
|
||||||
function getUint32Memory() {{
|
function getUint32Memory() {{
|
||||||
if (cachedUint32Memory === null ||
|
if (cachedUint32Memory === null ||
|
||||||
@ -976,7 +980,7 @@ impl<'a> Context<'a> {
|
|||||||
if !self.exposed_globals.insert("assert_class") {
|
if !self.exposed_globals.insert("assert_class") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function _assertClass(instance, klass) {{
|
function _assertClass(instance, klass) {{
|
||||||
if (!(instance instanceof klass))
|
if (!(instance instanceof klass))
|
||||||
throw new Error(`expected instance of ${{klass.name}}`);
|
throw new Error(`expected instance of ${{klass.name}}`);
|
||||||
@ -990,7 +994,7 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.expose_global_stack();
|
self.expose_global_stack();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function addBorrowedObject(obj) {{
|
function addBorrowedObject(obj) {{
|
||||||
stack.push(obj);
|
stack.push(obj);
|
||||||
return ((stack.length - 1) << 1) | 1;
|
return ((stack.length - 1) << 1) | 1;
|
||||||
@ -1004,7 +1008,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.expose_get_object();
|
self.expose_get_object();
|
||||||
self.expose_drop_ref();
|
self.expose_drop_ref();
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function takeObject(idx) {{
|
function takeObject(idx) {{
|
||||||
const ret = getObject(idx);
|
const ret = getObject(idx);
|
||||||
dropRef(idx);
|
dropRef(idx);
|
||||||
@ -1030,7 +1034,7 @@ impl<'a> Context<'a> {
|
|||||||
slab_next = next;
|
slab_next = next;
|
||||||
")
|
")
|
||||||
};
|
};
|
||||||
self.globals.push_str(&format!("
|
self.global(&format!("
|
||||||
function addHeapObject(obj) {{
|
function addHeapObject(obj) {{
|
||||||
if (slab_next === slab.length)
|
if (slab_next === slab.length)
|
||||||
slab.push(slab.length + 1);
|
slab.push(slab.length + 1);
|
||||||
@ -1140,7 +1144,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.expose_uint32_memory();
|
self.expose_uint32_memory();
|
||||||
self.expose_global_argument_ptr();
|
self.expose_global_argument_ptr();
|
||||||
self.globals.push_str("
|
self.global("
|
||||||
function setGlobalArgument(arg, i) {
|
function setGlobalArgument(arg, i) {
|
||||||
const idx = globalArgumentPtr() / 4 + i;
|
const idx = globalArgumentPtr() / 4 + i;
|
||||||
getUint32Memory()[idx] = arg;
|
getUint32Memory()[idx] = arg;
|
||||||
@ -1154,7 +1158,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
self.expose_uint32_memory();
|
self.expose_uint32_memory();
|
||||||
self.expose_global_argument_ptr();
|
self.expose_global_argument_ptr();
|
||||||
self.globals.push_str("
|
self.global("
|
||||||
function getGlobalArgument(arg) {
|
function getGlobalArgument(arg) {
|
||||||
const idx = globalArgumentPtr() / 4 + arg;
|
const idx = globalArgumentPtr() / 4 + arg;
|
||||||
return getUint32Memory()[idx];
|
return getUint32Memory()[idx];
|
||||||
@ -1167,7 +1171,7 @@ impl<'a> Context<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.required_internal_exports.insert("__wbindgen_global_argument_ptr");
|
self.required_internal_exports.insert("__wbindgen_global_argument_ptr");
|
||||||
self.globals.push_str("
|
self.global("
|
||||||
let cachedGlobalArgumentPtr = null;
|
let cachedGlobalArgumentPtr = null;
|
||||||
function globalArgumentPtr() {
|
function globalArgumentPtr() {
|
||||||
if (cachedGlobalArgumentPtr === null)
|
if (cachedGlobalArgumentPtr === null)
|
||||||
@ -1190,7 +1194,7 @@ impl<'a> Context<'a> {
|
|||||||
// As a result we have a small helper here which will walk the prototype
|
// As a result we have a small helper here which will walk the prototype
|
||||||
// chain looking for a descriptor. For some more information on this see
|
// chain looking for a descriptor. For some more information on this see
|
||||||
// #109
|
// #109
|
||||||
self.globals.push_str("
|
self.global("
|
||||||
function GetOwnOrInheritedPropertyDescriptor(obj, id) {
|
function GetOwnOrInheritedPropertyDescriptor(obj, id) {
|
||||||
while (obj) {
|
while (obj) {
|
||||||
let desc = Object.getOwnPropertyDescriptor(obj, id);
|
let desc = Object.getOwnPropertyDescriptor(obj, id);
|
||||||
@ -1248,6 +1252,20 @@ impl<'a> Context<'a> {
|
|||||||
_ => panic!("unimplemented return from JS to Rust: {:?}", ty),
|
_ => panic!("unimplemented return from JS to Rust: {:?}", ty),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn global(&mut self, s: &str) {
|
||||||
|
let amt_to_strip = s.lines()
|
||||||
|
.filter(|l| !l.trim().is_empty())
|
||||||
|
.map(|s| s.len() - s.trim_left().len())
|
||||||
|
.min()
|
||||||
|
.unwrap_or(0);
|
||||||
|
for line in s.lines() {
|
||||||
|
if !line.trim().is_empty() {
|
||||||
|
self.globals.push_str(&line[amt_to_strip..]);
|
||||||
|
}
|
||||||
|
self.globals.push_str("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> SubContext<'a, 'b> {
|
impl<'a, 'b> SubContext<'a, 'b> {
|
||||||
@ -1529,14 +1547,14 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
format!("{}.prototype.{}", class, function_name)
|
format!("{}.prototype.{}", class, function_name)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.cx.globals.push_str(&format!("
|
self.cx.global(&format!("
|
||||||
const {}_target = {};
|
const {}_target = {};
|
||||||
", import.shim, target));
|
", import.shim, target));
|
||||||
format!("{}_target.call", import.shim)
|
format!("{}_target.call", import.shim)
|
||||||
}
|
}
|
||||||
Some(ref class) => {
|
Some(ref class) => {
|
||||||
let class = self.import_name(info, class);
|
let class = self.import_name(info, class);
|
||||||
self.cx.globals.push_str(&format!("
|
self.cx.global(&format!("
|
||||||
const {}_target = {}.{};
|
const {}_target = {}.{};
|
||||||
", import.shim, class, function_name));
|
", import.shim, class, function_name));
|
||||||
format!("{}_target", import.shim)
|
format!("{}_target", import.shim)
|
||||||
@ -1544,7 +1562,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
None => {
|
None => {
|
||||||
let name = self.import_name(info, function_name);
|
let name = self.import_name(info, function_name);
|
||||||
if name.contains(".") {
|
if name.contains(".") {
|
||||||
self.cx.globals.push_str(&format!("
|
self.cx.global(&format!("
|
||||||
const {}_target = {};
|
const {}_target = {};
|
||||||
", import.shim, name));
|
", import.shim, name));
|
||||||
format!("{}_target", import.shim)
|
format!("{}_target", import.shim)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user