mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 17:31:06 +00:00
Don't generate code for dead imports
This commit is contained in:
parent
5e80b082a9
commit
b548239e02
@ -100,7 +100,7 @@ impl Object {
|
|||||||
ts.nodejs = self.nodejs;
|
ts.nodejs = self.nodejs;
|
||||||
ts.debug = self.debug;
|
ts.debug = self.debug;
|
||||||
ts.generate_program(&self.program, &self.module);
|
ts.generate_program(&self.program, &self.module);
|
||||||
ts.to_string(&self.module)
|
ts.to_string(&self.module, &self.program)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ pub struct Js {
|
|||||||
exports: Vec<(String, String, String)>,
|
exports: Vec<(String, String, String)>,
|
||||||
wasm_exports_bound: HashSet<String>,
|
wasm_exports_bound: HashSet<String>,
|
||||||
classes: Vec<String>,
|
classes: Vec<String>,
|
||||||
imports: Vec<(String, String, String)>,
|
|
||||||
pub nodejs: bool,
|
pub nodejs: bool,
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
}
|
}
|
||||||
@ -26,9 +25,6 @@ impl Js {
|
|||||||
for s in program.structs.iter() {
|
for s in program.structs.iter() {
|
||||||
self.generate_struct(s);
|
self.generate_struct(s);
|
||||||
}
|
}
|
||||||
for s in program.imports.iter() {
|
|
||||||
self.generate_import(s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_free_function(&mut self, func: &shared::Function) {
|
pub fn generate_free_function(&mut self, func: &shared::Function) {
|
||||||
@ -257,7 +253,9 @@ impl Js {
|
|||||||
(format!("{} {}", prefix, dst), dst_ts)
|
(format!("{} {}", prefix, dst), dst_ts)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_import(&mut self, import: &shared::Function) {
|
pub fn generate_import(&mut self, import: &shared::Function)
|
||||||
|
-> (String, String)
|
||||||
|
{
|
||||||
let mut dst = String::new();
|
let mut dst = String::new();
|
||||||
let mut ts_dst = String::new();
|
let mut ts_dst = String::new();
|
||||||
|
|
||||||
@ -348,10 +346,10 @@ impl Js {
|
|||||||
};
|
};
|
||||||
dst.push_str(&format!("return {};\n}}", invoc));
|
dst.push_str(&format!("return {};\n}}", invoc));
|
||||||
|
|
||||||
self.imports.push((import.name.clone(), dst, ts_dst));
|
(dst, ts_dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_string(&mut self, m: &Module) -> String {
|
pub fn to_string(&mut self, m: &Module, program: &shared::Program) -> String {
|
||||||
if self.debug {
|
if self.debug {
|
||||||
self.expose_global_slab();
|
self.expose_global_slab();
|
||||||
self.expose_global_stack();
|
self.expose_global_stack();
|
||||||
@ -428,13 +426,21 @@ impl Js {
|
|||||||
let mut extra_imports_interface = String::new();
|
let mut extra_imports_interface = String::new();
|
||||||
let mut imports_bound = HashSet::new();
|
let mut imports_bound = HashSet::new();
|
||||||
let mut imports_interface = String::new();
|
let mut imports_interface = String::new();
|
||||||
for &(ref import, ref val, ref ts_import) in self.imports.iter() {
|
for import in program.imports.iter() {
|
||||||
imports_bound.insert(import.clone());
|
// Only actually generate this import if it ended up being used in
|
||||||
imports_object.push_str(import);
|
// the wasm module, an optimization pass at some point may have
|
||||||
|
// ended up removing the code that needed the import, removing the
|
||||||
|
// import.
|
||||||
|
if !wasm_imports.contains_key(&import.name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
imports_bound.insert(import.name.clone());
|
||||||
|
let (val, ts) = self.generate_import(import);
|
||||||
|
imports_object.push_str(&import.name);
|
||||||
imports_object.push_str(":");
|
imports_object.push_str(":");
|
||||||
imports_object.push_str(val);
|
imports_object.push_str(&val);
|
||||||
imports_object.push_str(",\n");
|
imports_object.push_str(",\n");
|
||||||
imports_interface.push_str(ts_import);
|
imports_interface.push_str(&ts);
|
||||||
imports_interface.push_str("\n");
|
imports_interface.push_str("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,3 +77,33 @@ fn simple() {
|
|||||||
"#)
|
"#)
|
||||||
.test();
|
.test();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unused() {
|
||||||
|
test_support::project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
wasm_bindgen! {
|
||||||
|
extern "JS" {
|
||||||
|
fn debug_print(s: &str);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bar() {}
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import { Exports, Imports } from "./out";
|
||||||
|
|
||||||
|
export const imports: Imports = {};
|
||||||
|
|
||||||
|
export function test(wasm: Exports) {
|
||||||
|
wasm.bar();
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user