Skip to content

Import the wasm-gc-api crate into this repository #936

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
Oct 5, 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
2 changes: 1 addition & 1 deletion crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ serde_json = "1.0"
tempfile = "3.0"
wasm-bindgen-shared = { path = "../shared", version = '=0.2.24' }
wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.24' }
wasm-gc-api = "0.1.9"
wasm-bindgen-gc = { path = '../gc', version = '=0.2.24' }
19 changes: 6 additions & 13 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::fmt::Write;
use std::mem;

use failure::{Error, ResultExt};
use parity_wasm;
use parity_wasm::elements::*;
use shared;
use wasm_gc;
use wasm_bindgen_gc;

use super::Bindgen;
use descriptor::{Descriptor, VectorKind};
Expand Down Expand Up @@ -403,7 +402,7 @@ impl<'a> Context<'a> {
self.create_memory_export();
self.unexport_unused_internal_exports();
closures::rewrite(self)?;
self.gc()?;
self.gc();

// Note that it's important `throw` comes last *after* we gc. The
// `__wbindgen_malloc` function may call this but we only want to
Expand Down Expand Up @@ -482,7 +481,7 @@ impl<'a> Context<'a> {
};

self.export_table();
self.gc()?;
self.gc();

while js.contains("\n\n\n") {
js = js.replace("\n\n\n", "\n\n");
Expand Down Expand Up @@ -1661,18 +1660,12 @@ impl<'a> Context<'a> {
);
}

fn gc(&mut self) -> Result<(), Error> {
fn gc(&mut self) {
self.parse_wasm_names();
let module = mem::replace(self.module, Module::default());
let result = wasm_gc::Config::new()
wasm_bindgen_gc::Config::new()
.demangle(self.config.demangle)
.keep_debug(self.config.keep_debug || self.config.debug)
.run(module, |m| parity_wasm::serialize(m).unwrap())?;
*self.module = match result.into_module() {
Ok(m) => m,
Err(result) => deserialize_buffer(&result.into_bytes()?)?,
};
Ok(())
.run(&mut self.module);
}

fn parse_wasm_names(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate parity_wasm;
extern crate serde_json;
extern crate wasm_bindgen_shared as shared;
extern crate wasm_gc;
extern crate wasm_bindgen_gc;
#[macro_use]
extern crate failure;
extern crate wasm_bindgen_wasm_interpreter as wasm_interpreter;
Expand Down
16 changes: 16 additions & 0 deletions crates/gc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "wasm-bindgen-gc"
version = "0.2.24"
authors = ["The wasm-bindgen Developers"]
license = "MIT/Apache-2.0"
repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/gc"
homepage = "https://rustwasm.github.io/wasm-bindgen/"
documentation = "https://docs.rs/wasm-bindgen-gc"
description = """
Support for removing unused items from a wasm executable
"""

[dependencies]
parity-wasm = "0.32"
log = "0.4"
rustc-demangle = "0.1.9"
66 changes: 66 additions & 0 deletions crates/gc/src/bitvec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::mem;

type T = usize;

const BITS: usize = mem::size_of::<T>() * 8;

pub struct BitSet {
bits: Vec<T>,
}

impl BitSet {
pub fn new() -> BitSet {
BitSet { bits: Vec::new() }
}

pub fn insert(&mut self, i: u32) -> bool {
let i = i as usize;
let idx = i / BITS;
let bit = 1 << (i % BITS);
if self.bits.len() <= idx {
self.bits.resize(idx + 1, 0);
}
let slot = &mut self.bits[idx];
if *slot & bit != 0 {
false
} else {
*slot |= bit;
true
}
}

pub fn contains(&self, i: &u32) -> bool {
let i = *i as usize;
let idx = i / BITS;
let bit = 1 << (i % BITS);
self.bits.get(idx)
.map(|x| *x & bit != 0)
.unwrap_or(false)
}
}

impl Default for BitSet {
fn default() -> BitSet {
BitSet::new()
}
}

#[cfg(test)]
mod tests {
use super::BitSet;

#[test]
fn simple() {
let mut x = BitSet::new();
assert!(!x.contains(&1));
assert!(!x.contains(&0));
assert!(!x.contains(&3));
assert!(x.insert(3));
assert!(x.contains(&3));
assert!(!x.insert(3));
assert!(x.contains(&3));
assert!(!x.contains(&1));
assert!(x.insert(2));
assert!(x.contains(&2));
}
}
Loading