Skip to content

Commit 7aaf780

Browse files
authored
Merge pull request #164 from rustwasm/failure
Migrate to the `failure` crate
2 parents 2b9c48d + 5f59d95 commit 7aaf780

File tree

10 files changed

+359
-256
lines changed

10 files changed

+359
-256
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ matrix:
1919
rust: nightly
2020
before_script: rustup target add $TARGET
2121
script: cargo build --manifest-path crates/cli/Cargo.toml --release --target $TARGET
22+
addons:
23+
apt:
24+
packages:
25+
- musl-tools
2226

2327
# Dist OSX binary
2428
- os: osx

crates/cli-support/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Shared support for the wasm-bindgen-cli package, an internal dependency
1212

1313
[dependencies]
1414
base64 = "0.9"
15+
failure = "0.1"
1516
parity-wasm = "0.27"
1617
serde_json = "1.0"
1718
wasm-bindgen-shared = { path = "../shared", version = '=0.2.5' }

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use failure::Error;
2+
13
use super::{indent, Context};
24
use descriptor::{Descriptor, Function};
35

@@ -59,12 +61,12 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
5961

6062
/// Generates all bindings necessary for the signature in `Function`,
6163
/// creating necessary argument conversions and return value processing.
62-
pub fn process(&mut self, function: &Function) -> &mut Self {
64+
pub fn process(&mut self, function: &Function) -> Result<&mut Self, Error> {
6365
for arg in function.arguments.iter() {
64-
self.argument(arg);
66+
self.argument(arg)?;
6567
}
66-
self.ret(&function.ret);
67-
self
68+
self.ret(&function.ret)?;
69+
Ok(self)
6870
}
6971

7072
/// Flag this shim as a method call into Rust, so the first Rust argument
@@ -100,16 +102,16 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
100102
self
101103
}
102104

103-
pub fn argument(&mut self, arg: &Descriptor) -> &mut Self {
105+
pub fn argument(&mut self, arg: &Descriptor) -> Result<&mut Self, Error> {
104106
let i = self.arg_idx;
105107
self.arg_idx += 1;
106108
let name = format!("arg{}", i);
107109

108110
if let Some(kind) = arg.vector_kind() {
109111
self.js_arguments.push((name.clone(), kind.js_ty().to_string()));
110112

111-
let func = self.cx.pass_to_wasm_function(kind);
112-
self.cx.expose_set_global_argument();
113+
let func = self.cx.pass_to_wasm_function(kind)?;
114+
self.cx.expose_set_global_argument()?;
113115
let global_idx = self.global_idx();
114116
self.prelude(&format!("\
115117
const [ptr{i}, len{i}] = {func}({arg});\n\
@@ -119,10 +121,10 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
119121
self.finally(&format!("\
120122
wasm.__wbindgen_free(ptr{i}, len{i} * {size});\n\
121123
", i = i, size = kind.size()));
122-
self.cx.require_internal_export("__wbindgen_free");
124+
self.cx.require_internal_export("__wbindgen_free")?;
123125
}
124126
self.rust_arguments.push(format!("ptr{}", i));
125-
return self
127+
return Ok(self)
126128
}
127129

128130
if let Some(s) = arg.rust_struct() {
@@ -144,7 +146,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
144146
", i = i, arg = name));
145147
self.rust_arguments.push(format!("ptr{}", i));
146148
}
147-
return self
149+
return Ok(self)
148150
}
149151

150152
if arg.is_number() {
@@ -156,15 +158,15 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
156158
}
157159

158160
self.rust_arguments.push(name);
159-
return self
161+
return Ok(self)
160162
}
161163

162164
if arg.is_ref_anyref() {
163165
self.js_arguments.push((name.clone(), "any".to_string()));
164166
self.cx.expose_borrowed_objects();
165167
self.finally("stack.pop();");
166168
self.rust_arguments.push(format!("addBorrowedObject({})", name));
167-
return self
169+
return Ok(self)
168170
}
169171

170172
match *arg {
@@ -184,58 +186,58 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
184186
self.rust_arguments.push(format!("addHeapObject({})", name));
185187
}
186188
_ => {
187-
panic!("unsupported argument to rust function {:?}", arg)
189+
bail!("unsupported argument to rust function {:?}", arg)
188190
}
189191
}
190-
self
192+
Ok(self)
191193
}
192194

193-
pub fn ret(&mut self, ret: &Option<Descriptor>) -> &mut Self {
195+
pub fn ret(&mut self, ret: &Option<Descriptor>) -> Result<&mut Self, Error> {
194196
let ty = match *ret {
195197
Some(ref t) => t,
196198
None => {
197199
self.ret_ty = "void".to_string();
198200
self.ret_expr = format!("return RET;");
199-
return self
201+
return Ok(self)
200202
}
201203
};
202204

203205
if ty.is_ref_anyref() {
204206
self.ret_ty = "any".to_string();
205207
self.cx.expose_get_object();
206208
self.ret_expr = format!("return getObject(RET);");
207-
return self
209+
return Ok(self)
208210
}
209211

210212
if ty.is_by_ref() {
211-
panic!("cannot return references from Rust to JS yet")
213+
bail!("cannot return references from Rust to JS yet")
212214
}
213215

214216
if let Some(ty) = ty.vector_kind() {
215217
self.ret_ty = ty.js_ty().to_string();
216218
let f = self.cx.expose_get_vector_from_wasm(ty);
217-
self.cx.expose_get_global_argument();
218-
self.cx.require_internal_export("__wbindgen_free");
219+
self.cx.expose_get_global_argument()?;
220+
self.cx.require_internal_export("__wbindgen_free")?;
219221
self.ret_expr = format!("\
220222
const ret = RET;\n\
221223
const len = getGlobalArgument(0);\n\
222224
const realRet = {}(ret, len);\n\
223225
wasm.__wbindgen_free(ret, len * {});\n\
224226
return realRet;\n\
225227
", f, ty.size());
226-
return self
228+
return Ok(self)
227229
}
228230

229231
if let Some(name) = ty.rust_struct() {
230232
self.ret_ty = name.to_string();
231233
self.ret_expr = format!("return {name}.__construct(RET);", name = name);
232-
return self
234+
return Ok(self)
233235
}
234236

235237
if ty.is_number() {
236238
self.ret_ty = "number".to_string();
237239
self.ret_expr = format!("return RET;");
238-
return self
240+
return Ok(self)
239241
}
240242

241243
match *ty {
@@ -248,9 +250,9 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
248250
self.cx.expose_take_object();
249251
self.ret_expr = format!("return takeObject(RET);");
250252
}
251-
_ => panic!("unsupported return from Rust to JS {:?}", ty),
253+
_ => bail!("unsupported return from Rust to JS {:?}", ty),
252254
}
253-
self
255+
Ok(self)
254256
}
255257

256258
/// Generate the actual function.

0 commit comments

Comments
 (0)