mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-28 23:22:16 +00:00
Rename JsObject
to JsValue
Let's reserve `JsObject` for something we actually know is an object
This commit is contained in:
parent
9de4f1106b
commit
e60aa6a990
36
DESIGN.md
36
DESIGN.md
@ -53,13 +53,13 @@ Let's take a look at an example.
|
|||||||
```rust
|
```rust
|
||||||
// foo.rs
|
// foo.rs
|
||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
pub fn foo(a: &JsObject) {
|
pub fn foo(a: &JsValue) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Here we're using the special `JsObject` type from the `wasm-bindgen` library
|
Here we're using the special `JsValue` type from the `wasm-bindgen` library
|
||||||
itself. Our exported function, `foo`, takes a *reference* to an object. This
|
itself. Our exported function, `foo`, takes a *reference* to an object. This
|
||||||
notably means that it can't persist the object past the lifetime of this
|
notably means that it can't persist the object past the lifetime of this
|
||||||
function call.
|
function call.
|
||||||
@ -112,14 +112,14 @@ there! Let's take a look at the code that `wasm_bindgen!` generates in Rust:
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
// what the user wrote
|
// what the user wrote
|
||||||
pub fn foo(a: &JsObject) {
|
pub fn foo(a: &JsValue) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
#[export_name = "foo"]
|
#[export_name = "foo"]
|
||||||
pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
||||||
let arg0 = unsafe {
|
let arg0 = unsafe {
|
||||||
ManuallyDrop::new(JsObject::__from_idx(arg0))
|
ManuallyDrop::new(JsValue::__from_idx(arg0))
|
||||||
};
|
};
|
||||||
let arg0 = &*arg0;
|
let arg0 = &*arg0;
|
||||||
foo(arg0);
|
foo(arg0);
|
||||||
@ -132,7 +132,7 @@ And as with the JS, the notable points here are:
|
|||||||
* A generated function here (with a unique name) is the one that's actually
|
* A generated function here (with a unique name) is the one that's actually
|
||||||
exported from the wasm module
|
exported from the wasm module
|
||||||
* Our generated function takes an integer argument (our index) and then wraps it
|
* Our generated function takes an integer argument (our index) and then wraps it
|
||||||
in a `JsObject`. There's some trickery here that's not worth going into just
|
in a `JsValue`. There's some trickery here that's not worth going into just
|
||||||
yet, but we'll see in a bit what's happening under the hood.
|
yet, but we'll see in a bit what's happening under the hood.
|
||||||
|
|
||||||
### Long-lived JS objects in a slab
|
### Long-lived JS objects in a slab
|
||||||
@ -153,13 +153,13 @@ example.
|
|||||||
```rust
|
```rust
|
||||||
// foo.rs
|
// foo.rs
|
||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
pub fn foo(a: JsObject) {
|
pub fn foo(a: JsValue) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that the `&` is missing in front of the `JsObject` we had before, and in
|
Note that the `&` is missing in front of the `JsValue` we had before, and in
|
||||||
Rust parlance this means it's taking ownership of the JS value. The exported ES
|
Rust parlance this means it's taking ownership of the JS value. The exported ES
|
||||||
module interface is the same as before, but the ownership mechanics are slightly
|
module interface is the same as before, but the ownership mechanics are slightly
|
||||||
different. Let's see the generated JS's slab in action:
|
different. Let's see the generated JS's slab in action:
|
||||||
@ -208,7 +208,7 @@ using `Rc`, but it's overall not too important to worry about here.
|
|||||||
Another curious aspect of this generated module is the
|
Another curious aspect of this generated module is the
|
||||||
`__wbindgen_object_drop_ref` function. This is one that's actually imported from
|
`__wbindgen_object_drop_ref` function. This is one that's actually imported from
|
||||||
wasm rather than used in this module! This function is used to signal the end of
|
wasm rather than used in this module! This function is used to signal the end of
|
||||||
the lifetime of a `JsObject` in Rust, or in other words when it goes out of
|
the lifetime of a `JsValue` in Rust, or in other words when it goes out of
|
||||||
scope. Otherwise though this function is largely just a general "slab free"
|
scope. Otherwise though this function is largely just a general "slab free"
|
||||||
implementation.
|
implementation.
|
||||||
|
|
||||||
@ -216,14 +216,14 @@ And finally, let's take a look at the Rust generated again too:
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
// what the user wrote
|
// what the user wrote
|
||||||
pub fn foo(a: JsObject) {
|
pub fn foo(a: JsValue) {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
#[export_name = "foo"]
|
#[export_name = "foo"]
|
||||||
pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
||||||
let arg0 = unsafe {
|
let arg0 = unsafe {
|
||||||
JsObject::__from_idx(arg0)
|
JsValue::__from_idx(arg0)
|
||||||
};
|
};
|
||||||
foo(arg0);
|
foo(arg0);
|
||||||
}
|
}
|
||||||
@ -232,18 +232,18 @@ pub extern fn __wasm_bindgen_generated_foo(arg0: u32) {
|
|||||||
Ah that looks much more familiar! Not much interesting is happening here, so
|
Ah that looks much more familiar! Not much interesting is happening here, so
|
||||||
let's move on to...
|
let's move on to...
|
||||||
|
|
||||||
### Anatomy of `JsObject`
|
### Anatomy of `JsValue`
|
||||||
|
|
||||||
Currently the `JsObject` struct is actually quite simple in Rust, it's:
|
Currently the `JsValue` struct is actually quite simple in Rust, it's:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
pub struct JsObject {
|
pub struct JsValue {
|
||||||
idx: u32,
|
idx: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
// "private" constructors
|
// "private" constructors
|
||||||
|
|
||||||
impl Drop for JsObject {
|
impl Drop for JsValue {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
__wbindgen_object_drop_ref(self.idx);
|
__wbindgen_object_drop_ref(self.idx);
|
||||||
@ -257,7 +257,7 @@ passed from wasm. The destructor here is where the `__wbindgen_object_drop_ref`
|
|||||||
function is called to relinquish our reference count of the JS object, freeing
|
function is called to relinquish our reference count of the JS object, freeing
|
||||||
up our slot in the `slab` that we saw above.
|
up our slot in the `slab` that we saw above.
|
||||||
|
|
||||||
If you'll recall as well, when we took `&JsObject` above we generated a wrapper
|
If you'll recall as well, when we took `&JsValue` above we generated a wrapper
|
||||||
of `ManuallyDrop` around the local binding, and that's because we wanted to
|
of `ManuallyDrop` around the local binding, and that's because we wanted to
|
||||||
avoid invoking this destructor when the object comes from the stack.
|
avoid invoking this destructor when the object comes from the stack.
|
||||||
|
|
||||||
@ -673,7 +673,7 @@ take a look:
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
pub struct Bar {
|
pub struct Bar {
|
||||||
obj: JsObject,
|
obj: JsValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bar {
|
impl Bar {
|
||||||
@ -683,7 +683,7 @@ impl Bar {
|
|||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
let ret = __wbg_s_Bar_new();
|
let ret = __wbg_s_Bar_new();
|
||||||
Bar { obj: JsObject::__from_idx(ret) }
|
Bar { obj: JsValue::__from_idx(ret) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -711,7 +711,7 @@ impl Bar {
|
|||||||
```
|
```
|
||||||
|
|
||||||
In Rust we're seeing that a new type, `Bar`, is generated for this import of a
|
In Rust we're seeing that a new type, `Bar`, is generated for this import of a
|
||||||
class. The type `Bar` internally contains a `JsObject` as an instance of `Bar`
|
class. The type `Bar` internally contains a `JsValue` as an instance of `Bar`
|
||||||
is meant to represent a JS object stored in our module's stack/slab. This then
|
is meant to represent a JS object stored in our module's stack/slab. This then
|
||||||
works mostly the same way that we saw JS objects work in the beginning.
|
works mostly the same way that we saw JS objects work in the beginning.
|
||||||
|
|
||||||
|
@ -264,16 +264,16 @@ wasm_bindgen! {
|
|||||||
|
|
||||||
pub struct Bar {
|
pub struct Bar {
|
||||||
contents: u32,
|
contents: u32,
|
||||||
opaque: JsObject, // defined in `wasm_bindgen`, imported via prelude
|
opaque: JsValue, // defined in `wasm_bindgen`, imported via prelude
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_module = "./index"] // what ES6 module to import this functionality from
|
#[wasm_module = "./index"] // what ES6 module to import this functionality from
|
||||||
extern "JS" {
|
extern "JS" {
|
||||||
fn bar_on_reset(to: &str, opaque: &JsObject);
|
fn bar_on_reset(to: &str, opaque: &JsValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bar {
|
impl Bar {
|
||||||
pub fn from_str(s: &str, opaque: JsObject) -> Bar {
|
pub fn from_str(s: &str, opaque: JsValue) -> Bar {
|
||||||
Bar { contents: s.parse().unwrap_or(0), opaque }
|
Bar { contents: s.parse().unwrap_or(0), opaque }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,7 +362,7 @@ macro itself. Arguments allowed are:
|
|||||||
* Owned strings (`String`)
|
* Owned strings (`String`)
|
||||||
* Owned structs (`Foo`) defined in the same bindgen macro
|
* Owned structs (`Foo`) defined in the same bindgen macro
|
||||||
* Borrowed structs (`&Foo` or `&mut Bar`) defined in the same bindgen macro
|
* Borrowed structs (`&Foo` or `&mut Bar`) defined in the same bindgen macro
|
||||||
* The `JsObject` type and `&JsObject` (not mutable references)
|
* The `JsValue` type and `&JsValue` (not mutable references)
|
||||||
|
|
||||||
All of the above can also be returned except borrowed references. Strings are
|
All of the above can also be returned except borrowed references. Strings are
|
||||||
implemented with shim functions to copy data in/out of the Rust heap. That is, a
|
implemented with shim functions to copy data in/out of the Rust heap. That is, a
|
||||||
|
@ -377,7 +377,7 @@ fn bindgen_imported_struct(import: &ast::ImportStruct, tokens: &mut Tokens) {
|
|||||||
|
|
||||||
(my_quote! {
|
(my_quote! {
|
||||||
pub struct #name {
|
pub struct #name {
|
||||||
obj: ::wasm_bindgen::JsObject,
|
obj: ::wasm_bindgen::JsValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl #name {
|
impl #name {
|
||||||
@ -385,9 +385,9 @@ fn bindgen_imported_struct(import: &ast::ImportStruct, tokens: &mut Tokens) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
impl ::wasm_bindgen::convert::WasmBoundary for #name {
|
||||||
type Js = <::wasm_bindgen::JsObject as
|
type Js = <::wasm_bindgen::JsValue as
|
||||||
::wasm_bindgen::convert::WasmBoundary>::Js;
|
::wasm_bindgen::convert::WasmBoundary>::Js;
|
||||||
const DESCRIPTOR: char = <::wasm_bindgen::JsObject as
|
const DESCRIPTOR: char = <::wasm_bindgen::JsValue as
|
||||||
::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR;
|
::wasm_bindgen::convert::WasmBoundary>::DESCRIPTOR;
|
||||||
|
|
||||||
fn into_js(self) -> Self::Js {
|
fn into_js(self) -> Self::Js {
|
||||||
@ -395,7 +395,7 @@ fn bindgen_imported_struct(import: &ast::ImportStruct, tokens: &mut Tokens) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_js(js: Self::Js) -> Self {
|
unsafe fn from_js(js: Self::Js) -> Self {
|
||||||
#name { obj: ::wasm_bindgen::JsObject::from_js(js) }
|
#name { obj: ::wasm_bindgen::JsValue::from_js(js) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).to_tokens(tokens);
|
}).to_tokens(tokens);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::mem::{self, ManuallyDrop};
|
use std::mem::{self, ManuallyDrop};
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use super::JsObject;
|
use super::JsValue;
|
||||||
|
|
||||||
// keep in sync with shared/src/lib.rs TYPE constants
|
// keep in sync with shared/src/lib.rs TYPE constants
|
||||||
pub const DESCRIPTOR_CUSTOM_REF_FLAG: u32 = 0x1;
|
pub const DESCRIPTOR_CUSTOM_REF_FLAG: u32 = 0x1;
|
||||||
@ -92,7 +92,7 @@ impl<T> WasmBoundary for *mut T {
|
|||||||
unsafe fn from_js(js: u32) -> *mut T { js as *mut T }
|
unsafe fn from_js(js: u32) -> *mut T { js as *mut T }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WasmBoundary for JsObject {
|
impl WasmBoundary for JsValue {
|
||||||
type Js = u32;
|
type Js = u32;
|
||||||
const DESCRIPTOR: char = DESCRIPTOR_JS_OWNED;
|
const DESCRIPTOR: char = DESCRIPTOR_JS_OWNED;
|
||||||
|
|
||||||
@ -102,21 +102,21 @@ impl WasmBoundary for JsObject {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_js(js: u32) -> JsObject {
|
unsafe fn from_js(js: u32) -> JsValue {
|
||||||
JsObject { idx: js }
|
JsValue { idx: js }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToRefWasmBoundary for JsObject {
|
impl ToRefWasmBoundary for JsValue {
|
||||||
fn to_js_ref(&self) -> u32 {
|
fn to_js_ref(&self) -> u32 {
|
||||||
self.idx
|
self.idx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRefWasmBoundary for JsObject {
|
impl FromRefWasmBoundary for JsValue {
|
||||||
type RefAnchor = ManuallyDrop<JsObject>;
|
type RefAnchor = ManuallyDrop<JsValue>;
|
||||||
|
|
||||||
unsafe fn from_js_ref(js: u32) -> ManuallyDrop<JsObject> {
|
unsafe fn from_js_ref(js: u32) -> ManuallyDrop<JsValue> {
|
||||||
ManuallyDrop::new(JsObject { idx: js })
|
ManuallyDrop::new(JsValue { idx: js })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
src/lib.rs
70
src/lib.rs
@ -2,7 +2,7 @@
|
|||||||
//!
|
//!
|
||||||
//! This crate contains the runtime support necessary for `wasm-bindgen` the
|
//! This crate contains the runtime support necessary for `wasm-bindgen` the
|
||||||
//! macro and tool. Crates pull in the `wasm_bindgen!` macro through this crate
|
//! macro and tool. Crates pull in the `wasm_bindgen!` macro through this crate
|
||||||
//! and this crate also provides JS bindings through the `JsObject` interface.
|
//! and this crate also provides JS bindings through the `JsValue` interface.
|
||||||
|
|
||||||
#![feature(use_extern_macros)]
|
#![feature(use_extern_macros)]
|
||||||
|
|
||||||
@ -17,29 +17,29 @@ use std::ptr;
|
|||||||
/// ```
|
/// ```
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use wasm_bindgen_macro::wasm_bindgen;
|
pub use wasm_bindgen_macro::wasm_bindgen;
|
||||||
pub use JsObject;
|
pub use JsValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod convert;
|
pub mod convert;
|
||||||
|
|
||||||
/// Representation of an object owned by JS.
|
/// Representation of an object owned by JS.
|
||||||
///
|
///
|
||||||
/// A `JsObject` doesn't actually live in Rust right now but actually in a table
|
/// A `JsValue` doesn't actually live in Rust right now but actually in a table
|
||||||
/// owned by the `wasm-bindgen` generated JS glue code. Eventually the ownership
|
/// owned by the `wasm-bindgen` generated JS glue code. Eventually the ownership
|
||||||
/// will transfer into wasm directly and this will likely become more efficient,
|
/// will transfer into wasm directly and this will likely become more efficient,
|
||||||
/// but for now it may be slightly slow.
|
/// but for now it may be slightly slow.
|
||||||
pub struct JsObject {
|
pub struct JsValue {
|
||||||
idx: u32,
|
idx: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsObject {
|
impl JsValue {
|
||||||
/// Creates a new JS value which is a string.
|
/// Creates a new JS value which is a string.
|
||||||
///
|
///
|
||||||
/// The utf-8 string provided is copied to the JS heap and the string will
|
/// The utf-8 string provided is copied to the JS heap and the string will
|
||||||
/// be owned by the JS garbage collector.
|
/// be owned by the JS garbage collector.
|
||||||
pub fn from_str(s: &str) -> JsObject {
|
pub fn from_str(s: &str) -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
JsObject { idx: __wbindgen_string_new(s.as_ptr(), s.len()) }
|
JsValue { idx: __wbindgen_string_new(s.as_ptr(), s.len()) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,9 +47,9 @@ impl JsObject {
|
|||||||
///
|
///
|
||||||
/// This function creates a JS value representing a number (a heap
|
/// This function creates a JS value representing a number (a heap
|
||||||
/// allocated number) and returns a handle to the JS version of it.
|
/// allocated number) and returns a handle to the JS version of it.
|
||||||
pub fn from_f64(n: f64) -> JsObject {
|
pub fn from_f64(n: f64) -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
JsObject { idx: __wbindgen_number_new(n) }
|
JsValue { idx: __wbindgen_number_new(n) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,23 +57,23 @@ impl JsObject {
|
|||||||
///
|
///
|
||||||
/// This function creates a JS object representing a boolean (a heap
|
/// This function creates a JS object representing a boolean (a heap
|
||||||
/// allocated boolean) and returns a handle to the JS version of it.
|
/// allocated boolean) and returns a handle to the JS version of it.
|
||||||
pub fn from_bool(b: bool) -> JsObject {
|
pub fn from_bool(b: bool) -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
JsObject { idx: __wbindgen_boolean_new(b as u32) }
|
JsValue { idx: __wbindgen_boolean_new(b as u32) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new JS value representing `undefined`.
|
/// Creates a new JS value representing `undefined`.
|
||||||
pub fn undefined() -> JsObject {
|
pub fn undefined() -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
JsObject { idx: __wbindgen_undefined_new() }
|
JsValue { idx: __wbindgen_undefined_new() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new JS value representing `null`.
|
/// Creates a new JS value representing `null`.
|
||||||
pub fn null() -> JsObject {
|
pub fn null() -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
JsObject { idx: __wbindgen_null_new() }
|
JsValue { idx: __wbindgen_null_new() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,17 +81,17 @@ impl JsObject {
|
|||||||
///
|
///
|
||||||
/// This function will invoke the `Symbol` constructor in JS and return the
|
/// This function will invoke the `Symbol` constructor in JS and return the
|
||||||
/// JS object corresponding to the symbol created.
|
/// JS object corresponding to the symbol created.
|
||||||
pub fn symbol(description: Option<&str>) -> JsObject {
|
pub fn symbol(description: Option<&str>) -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = description.map(|s| s.as_ptr()).unwrap_or(ptr::null());
|
let ptr = description.map(|s| s.as_ptr()).unwrap_or(ptr::null());
|
||||||
let len = description.map(|s| s.len()).unwrap_or(0);
|
let len = description.map(|s| s.len()).unwrap_or(0);
|
||||||
JsObject { idx: __wbindgen_symbol_new(ptr, len) }
|
JsValue { idx: __wbindgen_symbol_new(ptr, len) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[doc(hidden)]
|
// #[doc(hidden)]
|
||||||
// pub fn __from_idx(idx: u32) -> JsObject {
|
// pub fn __from_idx(idx: u32) -> JsValue {
|
||||||
// JsObject { idx }
|
// JsValue { idx }
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// #[doc(hidden)]
|
// #[doc(hidden)]
|
||||||
@ -178,29 +178,29 @@ impl JsObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a str> for JsObject {
|
impl<'a> From<&'a str> for JsValue {
|
||||||
fn from(s: &'a str) -> JsObject {
|
fn from(s: &'a str) -> JsValue {
|
||||||
JsObject::from_str(s)
|
JsValue::from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a String> for JsObject {
|
impl<'a> From<&'a String> for JsValue {
|
||||||
fn from(s: &'a String) -> JsObject {
|
fn from(s: &'a String) -> JsValue {
|
||||||
JsObject::from_str(s)
|
JsValue::from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<bool> for JsObject {
|
impl From<bool> for JsValue {
|
||||||
fn from(s: bool) -> JsObject {
|
fn from(s: bool) -> JsValue {
|
||||||
JsObject::from_bool(s)
|
JsValue::from_bool(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! numbers {
|
macro_rules! numbers {
|
||||||
($($n:ident)*) => ($(
|
($($n:ident)*) => ($(
|
||||||
impl From<$n> for JsObject {
|
impl From<$n> for JsValue {
|
||||||
fn from(n: $n) -> JsObject {
|
fn from(n: $n) -> JsValue {
|
||||||
JsObject::from_f64(n.into())
|
JsValue::from_f64(n.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)*)
|
)*)
|
||||||
@ -225,16 +225,16 @@ extern {
|
|||||||
fn __wbindgen_string_get(idx: u32, len: *mut usize) -> *mut u8;
|
fn __wbindgen_string_get(idx: u32, len: *mut usize) -> *mut u8;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for JsObject {
|
impl Clone for JsValue {
|
||||||
fn clone(&self) -> JsObject {
|
fn clone(&self) -> JsValue {
|
||||||
unsafe {
|
unsafe {
|
||||||
let idx = __wbindgen_object_clone_ref(self.idx);
|
let idx = __wbindgen_object_clone_ref(self.idx);
|
||||||
JsObject { idx }
|
JsValue { idx }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for JsObject {
|
impl Drop for JsValue {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
__wbindgen_object_drop_ref(self.idx);
|
__wbindgen_object_drop_ref(self.idx);
|
||||||
|
56
tests/api.rs
56
tests/api.rs
@ -11,35 +11,35 @@ fn works() {
|
|||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
pub fn foo() -> JsObject {
|
pub fn foo() -> JsValue {
|
||||||
JsObject::from("foo")
|
JsValue::from("foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bar(s: &str) -> JsObject {
|
pub fn bar(s: &str) -> JsValue {
|
||||||
JsObject::from(s)
|
JsValue::from(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn baz() -> JsObject {
|
pub fn baz() -> JsValue {
|
||||||
JsObject::from(1.0)
|
JsValue::from(1.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn baz2(a: &JsObject, b: &JsObject) {
|
pub fn baz2(a: &JsValue, b: &JsValue) {
|
||||||
assert_eq!(a.as_f64(), Some(2.0));
|
assert_eq!(a.as_f64(), Some(2.0));
|
||||||
assert_eq!(b.as_f64(), None);
|
assert_eq!(b.as_f64(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn js_null() -> JsObject {
|
pub fn js_null() -> JsValue {
|
||||||
JsObject::null()
|
JsValue::null()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn js_undefined() -> JsObject {
|
pub fn js_undefined() -> JsValue {
|
||||||
JsObject::undefined()
|
JsValue::undefined()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_is_null_undefined(
|
pub fn test_is_null_undefined(
|
||||||
a: &JsObject,
|
a: &JsValue,
|
||||||
b: &JsObject,
|
b: &JsValue,
|
||||||
c: &JsObject,
|
c: &JsValue,
|
||||||
) {
|
) {
|
||||||
assert!(a.is_null());
|
assert!(a.is_null());
|
||||||
assert!(!a.is_undefined());
|
assert!(!a.is_undefined());
|
||||||
@ -51,47 +51,47 @@ fn works() {
|
|||||||
assert!(!c.is_undefined());
|
assert!(!c.is_undefined());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_true() -> JsObject {
|
pub fn get_true() -> JsValue {
|
||||||
JsObject::from(true)
|
JsValue::from(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_false() -> JsObject {
|
pub fn get_false() -> JsValue {
|
||||||
JsObject::from(false)
|
JsValue::from(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_bool(
|
pub fn test_bool(
|
||||||
a: &JsObject,
|
a: &JsValue,
|
||||||
b: &JsObject,
|
b: &JsValue,
|
||||||
c: &JsObject,
|
c: &JsValue,
|
||||||
) {
|
) {
|
||||||
assert_eq!(a.as_bool(), Some(true));
|
assert_eq!(a.as_bool(), Some(true));
|
||||||
assert_eq!(b.as_bool(), Some(false));
|
assert_eq!(b.as_bool(), Some(false));
|
||||||
assert_eq!(c.as_bool(), None);
|
assert_eq!(c.as_bool(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mk_symbol() -> JsObject {
|
pub fn mk_symbol() -> JsValue {
|
||||||
let a = JsObject::symbol(None);
|
let a = JsValue::symbol(None);
|
||||||
assert!(a.is_symbol());
|
assert!(a.is_symbol());
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mk_symbol2(s: &str) -> JsObject {
|
pub fn mk_symbol2(s: &str) -> JsValue {
|
||||||
let a = JsObject::symbol(Some(s));
|
let a = JsValue::symbol(Some(s));
|
||||||
assert!(a.is_symbol());
|
assert!(a.is_symbol());
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn assert_symbols(a: &JsObject, b: &JsObject) {
|
pub fn assert_symbols(a: &JsValue, b: &JsValue) {
|
||||||
assert!(a.is_symbol());
|
assert!(a.is_symbol());
|
||||||
assert!(!b.is_symbol());
|
assert!(!b.is_symbol());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn acquire_string(a: &JsObject, b: &JsObject) {
|
pub fn acquire_string(a: &JsValue, b: &JsValue) {
|
||||||
assert_eq!(a.as_string().unwrap(), "foo");
|
assert_eq!(a.as_string().unwrap(), "foo");
|
||||||
assert_eq!(b.as_string(), None);
|
assert_eq!(b.as_string(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn acquire_string2(a: &JsObject) -> String {
|
pub fn acquire_string2(a: &JsValue) -> String {
|
||||||
a.as_string().unwrap_or("wrong".to_string())
|
a.as_string().unwrap_or("wrong".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ fn simple() {
|
|||||||
fn foo(s: &str);
|
fn foo(s: &str);
|
||||||
fn another(a: u32) -> i32;
|
fn another(a: u32) -> i32;
|
||||||
fn take_and_return_bool(a: bool) -> bool;
|
fn take_and_return_bool(a: bool) -> bool;
|
||||||
fn return_object() -> JsObject;
|
fn return_object() -> JsValue;
|
||||||
}
|
}
|
||||||
pub fn bar(s: &str) {
|
pub fn bar(s: &str) {
|
||||||
foo(s);
|
foo(s);
|
||||||
@ -28,7 +28,7 @@ fn simple() {
|
|||||||
take_and_return_bool(a)
|
take_and_return_bool(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_the_object() -> JsObject {
|
pub fn get_the_object() -> JsValue {
|
||||||
return_object()
|
return_object()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,9 @@ fn simple() {
|
|||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
#[wasm_module = "./test"]
|
#[wasm_module = "./test"]
|
||||||
extern "JS" {
|
extern "JS" {
|
||||||
fn foo(s: &JsObject);
|
fn foo(s: &JsValue);
|
||||||
}
|
}
|
||||||
pub fn bar(s: &JsObject) {
|
pub fn bar(s: &JsValue) {
|
||||||
foo(s);
|
foo(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,9 +54,9 @@ fn owned() {
|
|||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
#[wasm_module = "./test"]
|
#[wasm_module = "./test"]
|
||||||
extern "JS" {
|
extern "JS" {
|
||||||
fn foo(s: JsObject);
|
fn foo(s: JsValue);
|
||||||
}
|
}
|
||||||
pub fn bar(s: JsObject) {
|
pub fn bar(s: JsValue) {
|
||||||
foo(s);
|
foo(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,14 +95,14 @@ fn clone() {
|
|||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
#[wasm_module = "./test"]
|
#[wasm_module = "./test"]
|
||||||
extern "JS" {
|
extern "JS" {
|
||||||
fn foo1(s: JsObject);
|
fn foo1(s: JsValue);
|
||||||
fn foo2(s: &JsObject);
|
fn foo2(s: &JsValue);
|
||||||
fn foo3(s: JsObject);
|
fn foo3(s: JsValue);
|
||||||
fn foo4(s: &JsObject);
|
fn foo4(s: &JsValue);
|
||||||
fn foo5(s: JsObject);
|
fn foo5(s: JsValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bar(s: JsObject) {
|
pub fn bar(s: JsValue) {
|
||||||
foo1(s.clone());
|
foo1(s.clone());
|
||||||
foo2(&s);
|
foo2(&s);
|
||||||
foo3(s.clone());
|
foo3(s.clone());
|
||||||
@ -143,13 +143,13 @@ fn promote() {
|
|||||||
wasm_bindgen! {
|
wasm_bindgen! {
|
||||||
#[wasm_module = "./test"]
|
#[wasm_module = "./test"]
|
||||||
extern "JS" {
|
extern "JS" {
|
||||||
fn foo1(s: &JsObject);
|
fn foo1(s: &JsValue);
|
||||||
fn foo2(s: JsObject);
|
fn foo2(s: JsValue);
|
||||||
fn foo3(s: &JsObject);
|
fn foo3(s: &JsValue);
|
||||||
fn foo4(s: JsObject);
|
fn foo4(s: JsValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bar(s: &JsObject) {
|
pub fn bar(s: &JsValue) {
|
||||||
foo1(s);
|
foo1(s);
|
||||||
foo2(s.clone());
|
foo2(s.clone());
|
||||||
foo3(s);
|
foo3(s);
|
||||||
|
@ -19,7 +19,7 @@ fn works() {
|
|||||||
A {}
|
A {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn clone(a: &JsObject) -> JsObject {
|
pub fn clone(a: &JsValue) -> JsValue {
|
||||||
drop(a.clone());
|
drop(a.clone());
|
||||||
a.clone()
|
a.clone()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user