Skip to content

Commit 5ee3552

Browse files
authored
Merge pull request rustwasm#936 from alexcrichton/import-gc
Import the `wasm-gc-api` crate into this repository
2 parents 4357d7d + 4750927 commit 5ee3552

File tree

6 files changed

+877
-15
lines changed

6 files changed

+877
-15
lines changed

crates/cli-support/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ serde_json = "1.0"
1919
tempfile = "3.0"
2020
wasm-bindgen-shared = { path = "../shared", version = '=0.2.24' }
2121
wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.24' }
22-
wasm-gc-api = "0.1.9"
22+
wasm-bindgen-gc = { path = '../gc', version = '=0.2.24' }

crates/cli-support/src/js/mod.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use std::fmt::Write;
33
use std::mem;
44

55
use failure::{Error, ResultExt};
6-
use parity_wasm;
76
use parity_wasm::elements::*;
87
use shared;
9-
use wasm_gc;
8+
use wasm_bindgen_gc;
109

1110
use super::Bindgen;
1211
use descriptor::{Descriptor, VectorKind};
@@ -403,7 +402,7 @@ impl<'a> Context<'a> {
403402
self.create_memory_export();
404403
self.unexport_unused_internal_exports();
405404
closures::rewrite(self)?;
406-
self.gc()?;
405+
self.gc();
407406

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

484483
self.export_table();
485-
self.gc()?;
484+
self.gc();
486485

487486
while js.contains("\n\n\n") {
488487
js = js.replace("\n\n\n", "\n\n");
@@ -1661,18 +1660,12 @@ impl<'a> Context<'a> {
16611660
);
16621661
}
16631662

1664-
fn gc(&mut self) -> Result<(), Error> {
1663+
fn gc(&mut self) {
16651664
self.parse_wasm_names();
1666-
let module = mem::replace(self.module, Module::default());
1667-
let result = wasm_gc::Config::new()
1665+
wasm_bindgen_gc::Config::new()
16681666
.demangle(self.config.demangle)
16691667
.keep_debug(self.config.keep_debug || self.config.debug)
1670-
.run(module, |m| parity_wasm::serialize(m).unwrap())?;
1671-
*self.module = match result.into_module() {
1672-
Ok(m) => m,
1673-
Err(result) => deserialize_buffer(&result.into_bytes()?)?,
1674-
};
1675-
Ok(())
1668+
.run(&mut self.module);
16761669
}
16771670

16781671
fn parse_wasm_names(&mut self) {

crates/cli-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate parity_wasm;
44
extern crate serde_json;
55
extern crate wasm_bindgen_shared as shared;
6-
extern crate wasm_gc;
6+
extern crate wasm_bindgen_gc;
77
#[macro_use]
88
extern crate failure;
99
extern crate wasm_bindgen_wasm_interpreter as wasm_interpreter;

crates/gc/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "wasm-bindgen-gc"
3+
version = "0.2.24"
4+
authors = ["The wasm-bindgen Developers"]
5+
license = "MIT/Apache-2.0"
6+
repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/gc"
7+
homepage = "https://rustwasm.github.io/wasm-bindgen/"
8+
documentation = "https://docs.rs/wasm-bindgen-gc"
9+
description = """
10+
Support for removing unused items from a wasm executable
11+
"""
12+
13+
[dependencies]
14+
parity-wasm = "0.32"
15+
log = "0.4"
16+
rustc-demangle = "0.1.9"

crates/gc/src/bitvec.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::mem;
2+
3+
type T = usize;
4+
5+
const BITS: usize = mem::size_of::<T>() * 8;
6+
7+
pub struct BitSet {
8+
bits: Vec<T>,
9+
}
10+
11+
impl BitSet {
12+
pub fn new() -> BitSet {
13+
BitSet { bits: Vec::new() }
14+
}
15+
16+
pub fn insert(&mut self, i: u32) -> bool {
17+
let i = i as usize;
18+
let idx = i / BITS;
19+
let bit = 1 << (i % BITS);
20+
if self.bits.len() <= idx {
21+
self.bits.resize(idx + 1, 0);
22+
}
23+
let slot = &mut self.bits[idx];
24+
if *slot & bit != 0 {
25+
false
26+
} else {
27+
*slot |= bit;
28+
true
29+
}
30+
}
31+
32+
pub fn contains(&self, i: &u32) -> bool {
33+
let i = *i as usize;
34+
let idx = i / BITS;
35+
let bit = 1 << (i % BITS);
36+
self.bits.get(idx)
37+
.map(|x| *x & bit != 0)
38+
.unwrap_or(false)
39+
}
40+
}
41+
42+
impl Default for BitSet {
43+
fn default() -> BitSet {
44+
BitSet::new()
45+
}
46+
}
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::BitSet;
51+
52+
#[test]
53+
fn simple() {
54+
let mut x = BitSet::new();
55+
assert!(!x.contains(&1));
56+
assert!(!x.contains(&0));
57+
assert!(!x.contains(&3));
58+
assert!(x.insert(3));
59+
assert!(x.contains(&3));
60+
assert!(!x.insert(3));
61+
assert!(x.contains(&3));
62+
assert!(!x.contains(&1));
63+
assert!(x.insert(2));
64+
assert!(x.contains(&2));
65+
}
66+
}

0 commit comments

Comments
 (0)