Skip to content

Add support for modules importing memory #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct Context<'a> {
pub exported_classes: HashMap<String, ExportedClass>,
pub function_table_needed: bool,
pub interpreter: &'a mut Interpreter,
pub memory_init: Option<ResizableLimits>,
}

#[derive(Default)]
Expand Down Expand Up @@ -377,6 +378,7 @@ impl<'a> Context<'a> {
))
})?;

self.create_memory_export();
self.unexport_unused_internal_exports();
self.gc()?;

Expand Down Expand Up @@ -685,6 +687,20 @@ impl<'a> Context<'a> {
}
}

fn create_memory_export(&mut self) {
let limits = match self.memory_init.clone() {
Some(limits) => limits,
None => return,
};
let mut initializer = String::from("new WebAssembly.Memory({");
initializer.push_str(&format!("initial:{}", limits.initial()));
if let Some(max) = limits.maximum() {
initializer.push_str(&format!(",maximum:{}", max));
}
initializer.push_str("})");
self.export("memory", &initializer, None);
}

fn rewrite_imports(&mut self, module_name: &str) {
for (name, contents) in self._rewrite_imports(module_name) {
self.export(&name, &contents, None);
Expand Down Expand Up @@ -715,6 +731,15 @@ impl<'a> Context<'a> {
continue;
}

// If memory is imported we'll have exported it from the shim module
// so let's import it from there.
if import.field() == "memory" {
import.module_mut().truncate(0);
import.module_mut().push_str("./");
import.module_mut().push_str(module_name);
continue
}

let renamed_import = format!("__wbindgen_{}", import.field());
let mut bind_math = |expr: &str| {
math_imports.push((renamed_import.clone(), format!("function{}", expr)));
Expand Down Expand Up @@ -1333,18 +1358,20 @@ impl<'a> Context<'a> {
if !self.exposed_globals.insert(name) {
return;
}
let mem = self.memory();
self.global(&format!(
"
let cache{name} = null;
function {name}() {{
if (cache{name} === null || cache{name}.buffer !== wasm.memory.buffer) {{
cache{name} = new {js}(wasm.memory.buffer);
if (cache{name} === null || cache{name}.buffer !== {mem}.buffer) {{
cache{name} = new {js}({mem}.buffer);
}}
return cache{name};
}}
",
name = name,
js = js,
mem = mem,
));
}

Expand Down Expand Up @@ -1690,6 +1717,29 @@ impl<'a> Context<'a> {
fn use_node_require(&self) -> bool {
self.config.nodejs && !self.config.nodejs_experimental_modules
}

fn memory(&mut self) -> &'static str {
if self.module.memory_section().is_some() {
return "wasm.memory";
}

let (entry, mem) = self.module.import_section()
.expect("must import memory")
.entries()
.iter()
.filter_map(|i| {
match i.external() {
External::Memory(m) => Some((i, m)),
_ => None,
}
})
.next()
.expect("must import memory");
assert_eq!(entry.module(), "env");
assert_eq!(entry.field(), "memory");
self.memory_init = Some(mem.limits().clone());
"memory"
}
}

impl<'a, 'b> SubContext<'a, 'b> {
Expand Down
1 change: 1 addition & 0 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl Bindgen {
module: &mut module,
function_table_needed: false,
interpreter: &mut instance,
memory_init: None,
};
for program in programs.iter() {
js::SubContext {
Expand Down
13 changes: 0 additions & 13 deletions crates/cli-support/src/wasm2es6js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,6 @@ impl Output {
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(_) => {
bail!("wasm imports a table which isn't supported yet");
}
External::Memory(_) => {
bail!("wasm imports memory which isn't supported yet");
}
External::Global(_) => {
bail!("wasm imports globals which aren't supported yet");
}
}

if !set.insert(entry.module()) {
continue;
}
Expand Down