Skip to content

First refactor for WebIDL bindings #1566

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 26 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
68c5233
First refactor for WebIDL bindings
alexcrichton May 23, 2019
3e28e6e
Fix web, no-modules, and bundler output types
alexcrichton May 30, 2019
e8e84a3
Remove __exports map on the `web` target
alexcrichton May 30, 2019
22b26db
Use `delete_typed` to improve some ergonomics
alexcrichton May 30, 2019
346868f
Fix a failing CLI test
alexcrichton May 30, 2019
ff0a50e
Fix failing interpreter tests
alexcrichton May 30, 2019
cba1e70
Fix TypeScript output for fields
alexcrichton May 30, 2019
164712c
Temporarily ignore intentionally failing test
alexcrichton May 30, 2019
edd1469
Include docs in generated JS getters/setters
alexcrichton May 30, 2019
b4c395b
Fix an inverted condition for catch_and_throw
alexcrichton May 31, 2019
81fbc64
Don't pass strings in raytrace-parallel example
alexcrichton May 31, 2019
cbd4b87
Fix handling imported memories
alexcrichton May 31, 2019
55fc536
Fix a typo in the `JsvalEq` intrinsic
alexcrichton May 31, 2019
b51df39
Reimplement `anyref` processing and passes
alexcrichton May 31, 2019
3b5e3ed
Fix anyref closure transformations
alexcrichton Jun 3, 2019
c7021ba
Update crates/cli-support/src/js/mod.rs
alexcrichton Jun 4, 2019
7120968
Use `unwrap_call` instead of an explicit `match`
alexcrichton Jun 4, 2019
4eafaea
Handle the function table export on-demand
alexcrichton Jun 4, 2019
6f727d7
Refactor the module name slightly in `gen_init`
alexcrichton Jun 4, 2019
cfd3e04
Split symbol intrinsics into two
alexcrichton Jun 4, 2019
ee426c0
Ensure that generated JS is deterministic
alexcrichton Jun 4, 2019
c22b907
Touch up some comments
alexcrichton Jun 4, 2019
6e8c3e8
Directly import `__wrap` functions if possible
alexcrichton Jun 4, 2019
bf1a31e
Don't generate a free function shim for classes
alexcrichton Jun 4, 2019
59e773f
Update walrus
alexcrichton Jun 5, 2019
e24c031
Attempt to fix CI
alexcrichton Jun 5, 2019
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
6 changes: 3 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
steps:
- template: ci/azure-install-rust.yml
- template: ci/azure-install-node.yml
- template: ci/azure-install-sccache.yml
#- template: ci/azure-install-sccache.yml
- script: cargo test -p wasm-bindgen-webidl
- script: cargo test -p webidl-tests --target wasm32-unknown-unknown
env:
Expand All @@ -128,7 +128,7 @@ jobs:
cd wabt/build
cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=off -DCMAKE_CXX_COMPILER_LAUNCHER=$RUSTC_WRAPPER
cmake --build . -- -j$(nproc)
echo "##vso[task.setvariable variable=PATH;]$PATH:$PWD"
echo "##vso[task.prependpath]$PWD"
- script: cargo test -p wasm-bindgen-wasm-interpreter

- job: test_typescript_output
Expand Down Expand Up @@ -171,7 +171,7 @@ jobs:
- script: |
set -e
curl -L https://github.com/japaric/xargo/releases/download/v0.3.13/xargo-v0.3.13-x86_64-unknown-linux-musl.tar.gz | tar xzf -
echo "##vso[task.setvariable variable=PATH;]$PATH:$PWD"
echo "##vso[task.prependpath]$PWD"
displayName: "install xargo"
- script: |
set -e
Expand Down
15 changes: 9 additions & 6 deletions ci/azure-install-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ parameters:

steps:
- bash: |
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN
echo "##vso[task.setvariable variable=PATH;]$PATH:$HOME/.cargo/bin"
set -e
if command -v rustup; then
rustup update $TOOLCHAIN
rustup default $TOOLCHAIN
else
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN
echo "##vso[task.prependpath]$HOME/.cargo/bin"
fi
displayName: Install rust - Unix
condition: ne( variables['Agent.OS'], 'Windows_NT' )
env:
TOOLCHAIN: ${{ parameters.toolchain }}

- script: |
curl -sSf -o rustup-init.exe https://win.rustup.rs
rustup-init.exe -y --default-toolchain %TOOLCHAIN%
echo "##vso[task.setvariable variable=PATH;]%PATH%;%USERPROFILE%\.cargo\bin"
- bash: rustup update --no-self-update $TOOLCHAIN && rustup default $TOOLCHAIN
displayName: Install rust - Windows
condition: eq( variables['Agent.OS'], 'Windows_NT' )
env:
Expand Down
2 changes: 1 addition & 1 deletion ci/azure-install-wasm-pack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ steps:
- script: |
set -ex
cargo build -p wasm-bindgen-cli
ln -snf `pwd`/target/debug/wasm-bindgen $HOME/.cargo/bin/wasm-bindgen
ln -snf `pwd`/target/debug/wasm-bindgen $(dirname `which cargo`)/wasm-bindgen
displayName: "install wasm-bindgen for `wasm-pack` to use"
2 changes: 1 addition & 1 deletion crates/anyref-xform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ edition = '2018'

[dependencies]
failure = "0.1"
walrus = "0.7.0"
walrus = "0.8.0"
104 changes: 36 additions & 68 deletions crates/anyref-xform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::cmp;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::mem;
use walrus::ir::*;
use walrus::{ExportId, ImportId};
use walrus::{FunctionId, GlobalId, InitExpr, Module, TableId, ValType};

// must be kept in sync with src/lib.rs and ANYREF_HEAP_START
Expand All @@ -32,8 +33,8 @@ pub struct Context {
// Functions within the module that we're gonna be wrapping, organized by
// type. The `Function` contains information about what arguments/return
// values in the function signature should turn into anyref.
imports: HashMap<String, HashMap<String, Function>>,
exports: HashMap<String, Function>,
imports: HashMap<ImportId, Function>,
exports: HashMap<ExportId, Function>,
elements: BTreeMap<u32, (u32, Function)>,

// When wrapping closures with new shims, this is the index of the next
Expand All @@ -42,9 +43,6 @@ pub struct Context {

// The anyref table we'll be using, injected after construction
table: Option<TableId>,

// Whether or not the transformation will actually be run at the end
pub enabled: bool,
}

struct Transform<'a> {
Expand All @@ -68,7 +66,6 @@ struct Transform<'a> {
}

struct Function {
name: String,
// A map of argument index to whether it's an owned or borrowed anyref
// (owned = true)
args: HashMap<usize, bool>,
Expand All @@ -87,10 +84,6 @@ impl Context {
/// large the function table is so we know what indexes to hand out when
/// we're appending entries.
pub fn prepare(&mut self, module: &mut Module) -> Result<(), Error> {
if !self.enabled {
return Ok(());
}

// Figure out what the maximum index of functions pointers are. We'll
// be adding new entries to the function table later (maybe) so
// precalculate this ahead of time.
Expand Down Expand Up @@ -118,35 +111,27 @@ impl Context {
/// transformed. The actual transformation happens later during `run`.
pub fn import_xform(
&mut self,
module: &str,
name: &str,
id: ImportId,
anyref: &[(usize, bool)],
ret_anyref: bool,
) -> &mut Self {
if !self.enabled {
return self;
if let Some(f) = self.function(anyref, ret_anyref) {
self.imports.insert(id, f);
}
let f = self.function(name, anyref, ret_anyref);
self.imports
.entry(module.to_string())
.or_insert_with(Default::default)
.insert(name.to_string(), f);
self
}

/// Store information about an exported function that needs to be
/// transformed. The actual transformation happens later during `run`.
pub fn export_xform(
&mut self,
name: &str,
id: ExportId,
anyref: &[(usize, bool)],
ret_anyref: bool,
) -> &mut Self {
if !self.enabled {
return self;
if let Some(f) = self.function(anyref, ret_anyref) {
self.exports.insert(id, f);
}
let f = self.function(name, anyref, ret_anyref);
self.exports.insert(name.to_string(), f);
self
}

Expand All @@ -158,34 +143,26 @@ impl Context {
idx: u32,
anyref: &[(usize, bool)],
ret_anyref: bool,
) -> u32 {
if !self.enabled {
return idx;
}
let name = format!("closure{}", idx);
let f = self.function(&name, anyref, ret_anyref);
let ret = self.next_element;
self.next_element += 1;
self.elements.insert(ret, (idx, f));
ret
) -> Option<u32> {
self.function(anyref, ret_anyref).map(|f| {
let ret = self.next_element;
self.next_element += 1;
self.elements.insert(ret, (idx, f));
ret
})
}

fn function(&self, name: &str, anyref: &[(usize, bool)], ret_anyref: bool) -> Function {
Function {
name: name.to_string(),
fn function(&self, anyref: &[(usize, bool)], ret_anyref: bool) -> Option<Function> {
if !ret_anyref && anyref.len() == 0 {
return None;
}
Some(Function {
args: anyref.iter().cloned().collect(),
ret_anyref,
}
}

pub fn anyref_table_id(&self) -> TableId {
self.table.unwrap()
})
}

pub fn run(&mut self, module: &mut Module) -> Result<(), Error> {
if !self.enabled {
return Ok(());
}
let table = self.table.unwrap();

// Inject a stack pointer global which will be used for managing the
Expand Down Expand Up @@ -261,9 +238,7 @@ impl Transform<'_> {

// Perform transformations of imports, exports, and function pointers.
self.process_imports(module);
for m in self.cx.imports.values() {
assert!(m.is_empty());
}
assert!(self.cx.imports.is_empty());
self.process_exports(module);
assert!(self.cx.exports.is_empty());
self.process_elements(module)?;
Expand Down Expand Up @@ -333,20 +308,15 @@ impl Transform<'_> {
walrus::ImportKind::Function(f) => f,
_ => continue,
};
let import = {
let entries = match self.cx.imports.get_mut(&import.module) {
Some(s) => s,
None => continue,
};
match entries.remove(&import.name) {
Some(s) => s,
None => continue,
}
let func = match self.cx.imports.remove(&import.id()) {
Some(s) => s,
None => continue,
};

let shim = self.append_shim(
f,
import,
&import.name,
func,
&mut module.types,
&mut module.funcs,
&mut module.locals,
Expand All @@ -356,29 +326,25 @@ impl Transform<'_> {
}

fn process_exports(&mut self, module: &mut Module) {
let mut new_exports = Vec::new();
for export in module.exports.iter() {
// let mut new_exports = Vec::new();
for export in module.exports.iter_mut() {
let f = match export.item {
walrus::ExportItem::Function(f) => f,
_ => continue,
};
let function = match self.cx.exports.remove(&export.name) {
let function = match self.cx.exports.remove(&export.id()) {
Some(s) => s,
None => continue,
};
let shim = self.append_shim(
f,
&export.name,
function,
&mut module.types,
&mut module.funcs,
&mut module.locals,
);
new_exports.push((export.name.to_string(), shim, export.id()));
}

for (name, shim, old_id) in new_exports {
module.exports.add(&name, shim);
module.exports.delete(old_id);
export.item = shim.into();
}
}

Expand All @@ -402,6 +368,7 @@ impl Transform<'_> {
let target = kind.elements[idx as usize].unwrap();
let shim = self.append_shim(
target,
&format!("closure{}", idx),
function,
&mut module.types,
&mut module.funcs,
Expand All @@ -422,6 +389,7 @@ impl Transform<'_> {
fn append_shim(
&mut self,
shim_target: FunctionId,
name: &str,
mut func: Function,
types: &mut walrus::ModuleTypes,
funcs: &mut walrus::ModuleFunctions,
Expand Down Expand Up @@ -625,7 +593,7 @@ impl Transform<'_> {
// nice name for debugging and then we're good to go!
let expr = builder.with_side_effects(before, result, after);
let id = builder.finish_parts(shim_ty, params, vec![expr], types, funcs);
let name = format!("{}_anyref_shim", func.name);
let name = format!("{}_anyref_shim", name);
funcs.get_mut(id).name = Some(name);
self.shims.insert(id);
return id;
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ log = "0.4"
rustc-demangle = "0.1.13"
serde_json = "1.0"
tempfile = "3.0"
walrus = "0.7.0"
walrus = "0.8.0"
wasm-bindgen-anyref-xform = { path = '../anyref-xform', version = '=0.2.45' }
wasm-bindgen-shared = { path = "../shared", version = '=0.2.45' }
wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.45' }
Expand Down
Loading