From cfd3e0406f94efc2d76953192ab8eade47c68216 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 4 Jun 2019 09:06:24 -0700 Subject: [PATCH] Split symbol intrinsics into two This allows using WebIDL bindings types to describe both of them instead of having a custom ABI, allowing for more direct and rich bindings eventually! --- crates/cli-support/src/intrinsic.rs | 9 ++++++--- crates/cli-support/src/js/rust2js.rs | 17 ++++++++--------- src/lib.rs | 14 +++++++++----- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index c502689a..6c20bc31 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -103,9 +103,12 @@ intrinsics! { #[symbol = "__wbindgen_string_new"] #[signature = fn(ref_string()) -> Anyref] StringNew, - #[symbol = "__wbindgen_symbol_new"] - #[signature = fn(I32, I32) -> Anyref] - SymbolNew, + #[symbol = "__wbindgen_symbol_anonymous_new"] + #[signature = fn() -> Anyref] + SymbolAnonymousNew, + #[symbol = "__wbindgen_symbol_named_new"] + #[signature = fn(ref_string()) -> Anyref] + SymbolNamedNew, #[symbol = "__wbindgen_number_get"] #[signature = fn(ref_anyref(), F64) -> F64] NumberGet, diff --git a/crates/cli-support/src/js/rust2js.rs b/crates/cli-support/src/js/rust2js.rs index 8d33c550..ed655078 100644 --- a/crates/cli-support/src/js/rust2js.rs +++ b/crates/cli-support/src/js/rust2js.rs @@ -1080,15 +1080,14 @@ impl<'a, 'b> Rust2Js<'a, 'b> { self.js_arguments[0].clone() } - Intrinsic::SymbolNew => { - // FIXME: should probably have two intrinsics, one for a - // new anonymous symbol and one for a symbol with a string - assert_eq!(self.js_arguments.len(), 2); - self.cx.expose_get_string_from_wasm()?; - format!( - "{} === 0 ? Symbol() : Symbol(getStringFromWasm({0}, {}))", - self.js_arguments[0], self.js_arguments[1] - ) + Intrinsic::SymbolNamedNew => { + assert_eq!(self.js_arguments.len(), 1); + format!("Symbol({})", self.js_arguments[0]) + } + + Intrinsic::SymbolAnonymousNew => { + assert_eq!(self.js_arguments.len(), 0); + "Symbol()".to_string() } Intrinsic::NumberGet => { diff --git a/src/lib.rs b/src/lib.rs index c5ded655..f6097f99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,6 @@ use core::fmt; use core::marker; use core::mem; use core::ops::{Deref, DerefMut}; -use core::ptr; use crate::convert::FromWasmAbi; @@ -171,9 +170,13 @@ impl JsValue { /// JS object corresponding to the symbol created. pub fn symbol(description: Option<&str>) -> JsValue { unsafe { - let ptr = description.map(|s| s.as_ptr()).unwrap_or(ptr::null()); - let len = description.map(|s| s.len()).unwrap_or(0); - JsValue::_new(__wbindgen_symbol_new(ptr, len)) + match description { + Some(description) => JsValue::_new(__wbindgen_symbol_named_new( + description.as_ptr(), + description.len(), + )), + None => JsValue::_new(__wbindgen_symbol_anonymous_new()), + } } } @@ -488,7 +491,8 @@ externs! { fn __wbindgen_string_new(ptr: *const u8, len: usize) -> u32; fn __wbindgen_number_new(f: f64) -> u32; - fn __wbindgen_symbol_new(ptr: *const u8, len: usize) -> u32; + fn __wbindgen_symbol_named_new(ptr: *const u8, len: usize) -> u32; + fn __wbindgen_symbol_anonymous_new() -> u32; fn __wbindgen_anyref_heap_live_count() -> u32;