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!
This commit is contained in:
Alex Crichton 2019-06-04 09:06:24 -07:00
parent 6f727d7c13
commit cfd3e0406f
3 changed files with 23 additions and 17 deletions

View File

@ -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,

View File

@ -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 => {

View File

@ -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;