Enum quickjs_wasm_rs::JSValue
source · pub enum JSValue {
Undefined,
Null,
Bool(bool),
Int(i32),
Float(f64),
String(String),
Array(Vec<JSValue>),
ArrayBuffer(Vec<u8>),
Object(HashMap<String, JSValue>),
}
Expand description
A safe and high level representation of a JavaScript value.
This enum implements From
and TryFrom
for many types, so it can be used to convert between Rust and JavaScript types.
§Example
// Convert a &str to a JSValue::String
let js_value: JSValue = "hello".into();
assert_eq!("hello", js_value.to_string());
// Convert a JSValue::String to a String
let result: String = js_value.try_into().unwrap();
assert_eq!("hello", result);
Variants§
Undefined
Represents the JavaScript undefined
value
Null
Represents the JavaScript null
value
Bool(bool)
Represents a JavaScript boolean value
Int(i32)
Represents a JavaScript integer
Float(f64)
Represents a JavaScript floating-point number
String(String)
Represents a JavaScript string value
Array(Vec<JSValue>)
Represents a JavaScript array of JSValue
s
ArrayBuffer(Vec<u8>)
Represents a JavaScript ArrayBuffer of bytes
Object(HashMap<String, JSValue>)
Represents a JavaScript object, with string keys and JSValue
values
Implementations§
source§impl JSValue
impl JSValue
sourcepub fn from_hashmap<T: Into<JSValue>>(hm: HashMap<&str, T>) -> JSValue
pub fn from_hashmap<T: Into<JSValue>>(hm: HashMap<&str, T>) -> JSValue
Constructs a JSValue::Object
variant from a HashMap of key-value pairs that can be converted to JSValue
.
§Arguments
hm
- A HashMap of key-value pairs to be converted toJSValue::Object
§Example
let mut hashmap = std::collections::HashMap::from([
("first_name", "John"),
("last_name", "Smith"),
]);
let js_obj = JSValue::from_hashmap(hashmap);
Trait Implementations§
source§impl Display for JSValue
impl Display for JSValue
The implementation matches the default JavaScript display format for each value.
Used http://numcalc.com/ to determine the default display format for each type.