mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-22 20:52:13 +00:00
Reduce node/browser duplication
Centralize the export of a function to one location to reduce duplication in various other locations checking whether Node is being targeted or not.
This commit is contained in:
parent
cf9ef7b570
commit
7f614c3c3a
@ -43,6 +43,20 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn export(&mut self, name: &str, contents: &str) {
|
||||||
|
let contents = contents.trim();
|
||||||
|
let global = if self.config.nodejs {
|
||||||
|
format!("module.exports.{} = {};\n", name, contents)
|
||||||
|
} else {
|
||||||
|
if contents.starts_with("function") {
|
||||||
|
format!("export function {} {}\n", name, &contents[8..])
|
||||||
|
} else {
|
||||||
|
format!("export const {} = {};\n", name, contents)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.globals.push_str(&global);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn finalize(&mut self, module_name: &str) -> (String, String) {
|
pub fn finalize(&mut self, module_name: &str) -> (String, String) {
|
||||||
self.write_classes();
|
self.write_classes();
|
||||||
{
|
{
|
||||||
@ -52,16 +66,7 @@ impl<'a> Context<'a> {
|
|||||||
}
|
}
|
||||||
let contents = f(self);
|
let contents = f(self);
|
||||||
let contents = contents.trim();
|
let contents = contents.trim();
|
||||||
let global = if self.config.nodejs {
|
self.export(name, contents);
|
||||||
format!("module.exports.{} = {};\n", name, contents)
|
|
||||||
} else {
|
|
||||||
if contents.starts_with("function") {
|
|
||||||
format!("export function {} {}\n", name, &contents[8..])
|
|
||||||
} else {
|
|
||||||
format!("export const {} = {};\n", name, contents)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
self.globals.push_str(&global);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bind("__wbindgen_object_clone_ref", &|me| {
|
bind("__wbindgen_object_clone_ref", &|me| {
|
||||||
@ -269,11 +274,11 @@ impl<'a> Context<'a> {
|
|||||||
|
|
||||||
let new_name = shared::new_function(&class);
|
let new_name = shared::new_function(&class);
|
||||||
if self.wasm_import_needed(&new_name) {
|
if self.wasm_import_needed(&new_name) {
|
||||||
self.globals.push_str(&format!("
|
self.export(&new_name, &format!("
|
||||||
export function {new_name}(ptr) {{
|
function(ptr) {{
|
||||||
return addHeapObject(new {class}(ptr, token));
|
return addHeapObject(new {class}(ptr, token));
|
||||||
}}
|
}}
|
||||||
", new_name = new_name, class = class));
|
", class = class));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dst.push_str(&format!("
|
dst.push_str(&format!("
|
||||||
@ -285,11 +290,11 @@ impl<'a> Context<'a> {
|
|||||||
|
|
||||||
let new_name = shared::new_function(&class);
|
let new_name = shared::new_function(&class);
|
||||||
if self.wasm_import_needed(&new_name) {
|
if self.wasm_import_needed(&new_name) {
|
||||||
self.globals.push_str(&format!("
|
self.export(&new_name, &format!("
|
||||||
export function {new_name}(ptr) {{
|
function(ptr) {{
|
||||||
return addHeapObject(new {class}(ptr));
|
return addHeapObject(new {class}(ptr));
|
||||||
}}
|
}}
|
||||||
", new_name = new_name, class = class));
|
", class = class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -566,7 +571,7 @@ impl<'a> Context<'a> {
|
|||||||
const buf = textEncoder().encode(arg);
|
const buf = textEncoder().encode(arg);
|
||||||
const ptr = wasm.__wbindgen_malloc(buf.length);
|
const ptr = wasm.__wbindgen_malloc(buf.length);
|
||||||
getUint8Memory().set(buf, buf.length);
|
getUint8Memory().set(buf, buf.length);
|
||||||
return [ptr, len];
|
return [ptr, buf.length];
|
||||||
}}
|
}}
|
||||||
", debug));
|
", debug));
|
||||||
}
|
}
|
||||||
@ -1110,17 +1115,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
&export.function.name,
|
&export.function.name,
|
||||||
false,
|
false,
|
||||||
&export.function);
|
&export.function);
|
||||||
if self.cx.config.nodejs {
|
self.cx.export(&export.function.name, &js);
|
||||||
self.cx.globals.push_str("module.exports.");
|
|
||||||
self.cx.globals.push_str(&export.function.name);
|
|
||||||
self.cx.globals.push_str(" = ");
|
|
||||||
} else {
|
|
||||||
self.cx.globals.push_str("export ");
|
|
||||||
}
|
|
||||||
self.cx.globals.push_str(&js);
|
|
||||||
if self.cx.config.nodejs {
|
|
||||||
self.cx.globals.push_str(";");
|
|
||||||
}
|
|
||||||
self.cx.globals.push_str("\n");
|
self.cx.globals.push_str("\n");
|
||||||
self.cx.typescript.push_str("export ");
|
self.cx.typescript.push_str("export ");
|
||||||
self.cx.typescript.push_str(&ts);
|
self.cx.typescript.push_str(&ts);
|
||||||
@ -1156,7 +1151,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
wasm_name: &str,
|
wasm_name: &str,
|
||||||
is_method: bool,
|
is_method: bool,
|
||||||
function: &shared::Function) -> (String, String) {
|
function: &shared::Function) -> (String, String) {
|
||||||
let mut dst = format!("{}(", function.name);
|
let mut dst = String::from("(");
|
||||||
let mut dst_ts = format!("{}(", function.name);
|
let mut dst_ts = format!("{}(", function.name);
|
||||||
let mut passed_args = String::new();
|
let mut passed_args = String::new();
|
||||||
let mut arg_conversions = String::new();
|
let mut arg_conversions = String::new();
|
||||||
@ -1383,11 +1378,11 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
// TODO: should support more types to import here
|
// TODO: should support more types to import here
|
||||||
let obj = self.import_name(info, &import.name);
|
let obj = self.import_name(info, &import.name);
|
||||||
self.cx.expose_add_heap_object();
|
self.cx.expose_add_heap_object();
|
||||||
self.cx.globals.push_str(&format!("
|
self.cx.export(&import.shim, &format!("
|
||||||
export function {}() {{
|
function() {{
|
||||||
return addHeapObject({});
|
return addHeapObject({});
|
||||||
}}
|
}}
|
||||||
", import.shim, obj));
|
", obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_import_function(&mut self,
|
pub fn generate_import_function(&mut self,
|
||||||
@ -1395,7 +1390,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
import: &shared::ImportFunction) {
|
import: &shared::ImportFunction) {
|
||||||
let mut dst = String::new();
|
let mut dst = String::new();
|
||||||
|
|
||||||
dst.push_str(&format!("function {}(", import.shim));
|
dst.push_str("function(");
|
||||||
let mut invoc_args = Vec::new();
|
let mut invoc_args = Vec::new();
|
||||||
let mut abi_args = Vec::new();
|
let mut abi_args = Vec::new();
|
||||||
|
|
||||||
@ -1584,18 +1579,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
dst.push_str(") {\n");
|
dst.push_str(") {\n");
|
||||||
dst.push_str(&extra);
|
dst.push_str(&extra);
|
||||||
dst.push_str(&format!("{}\n}}", invoc));
|
dst.push_str(&format!("{}\n}}", invoc));
|
||||||
|
self.cx.export(&import.shim, &dst);
|
||||||
if self.cx.config.nodejs {
|
|
||||||
self.cx.globals.push_str("module.exports.");
|
|
||||||
self.cx.globals.push_str(&import.shim);
|
|
||||||
self.cx.globals.push_str(" = ");
|
|
||||||
self.cx.globals.push_str(&dst);
|
|
||||||
self.cx.globals.push_str(";\n");
|
|
||||||
} else {
|
|
||||||
self.cx.globals.push_str("export ");
|
|
||||||
self.cx.globals.push_str(&dst);
|
|
||||||
self.cx.globals.push_str("\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_enum(&mut self, enum_: &shared::Enum) {
|
pub fn generate_enum(&mut self, enum_: &shared::Enum) {
|
||||||
@ -1604,21 +1588,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
for variant in enum_.variants.iter() {
|
for variant in enum_.variants.iter() {
|
||||||
variants.push_str(&format!("{}:{},", variant.name, variant.value));
|
variants.push_str(&format!("{}:{},", variant.name, variant.value));
|
||||||
}
|
}
|
||||||
let global_export = if self.cx.config.nodejs {
|
self.cx.export(&enum_.name, &format!("Object.freeze({{ {} }})", variants));
|
||||||
let mut enum_string = format!("const {} = Object.freeze({{", enum_.name);
|
|
||||||
enum_string.push_str(&variants);
|
|
||||||
enum_string.push_str("})\n");
|
|
||||||
let export = format!("module.exports.{} = {};\n", enum_.name, enum_.name);
|
|
||||||
enum_string.push_str(&export);
|
|
||||||
enum_string
|
|
||||||
} else {
|
|
||||||
let mut enum_string = format!("export const {} = Object.freeze({{", enum_.name);
|
|
||||||
enum_string.push_str(&variants);
|
|
||||||
enum_string.push_str("})\n");
|
|
||||||
enum_string
|
|
||||||
};
|
|
||||||
self.cx.globals.push_str(&global_export);
|
|
||||||
|
|
||||||
self.cx.typescript.push_str(&format!("export enum {} {{", enum_.name));
|
self.cx.typescript.push_str(&format!("export enum {} {{", enum_.name));
|
||||||
|
|
||||||
variants.clear();
|
variants.clear();
|
||||||
|
@ -13,12 +13,14 @@ fn works() {
|
|||||||
|
|
||||||
#[wasm_bindgen(module = "./test")]
|
#[wasm_bindgen(module = "./test")]
|
||||||
extern {
|
extern {
|
||||||
|
static FOO: JsValue;
|
||||||
fn hit();
|
fn hit();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
hit();
|
hit();
|
||||||
|
assert_eq!(FOO.as_f64(), Some(1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
@ -118,6 +120,7 @@ fn works() {
|
|||||||
module.exports.hit = function() {
|
module.exports.hit = function() {
|
||||||
called = true;
|
called = true;
|
||||||
};
|
};
|
||||||
|
module.exports.FOO = 1.0;
|
||||||
|
|
||||||
const { math, run, Foo, Color, cycle } = require('./out');
|
const { math, run, Foo, Color, cycle } = require('./out');
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user