Add JsValue::Undefined

This commit is contained in:
Ivan Pavluk 2020-11-02 21:32:54 +07:00 committed by theduke
parent 93d0a2214a
commit f9eb75785c
5 changed files with 15 additions and 8 deletions

View file

@ -87,6 +87,10 @@ fn js_create_bigint_function(context: *mut q::JSContext) -> q::JSValue {
/// Serialize a Rust value into a quickjs runtime value.
fn serialize_value(context: *mut q::JSContext, value: JsValue) -> Result<q::JSValue, ValueError> {
let v = match value {
JsValue::Undefined => q::JSValue {
u: q::JSValueUnion { int32: 0 },
tag: TAG_UNDEFINED,
},
JsValue::Null => q::JSValue {
u: q::JSValueUnion { int32: 0 },
tag: TAG_NULL,
@ -408,7 +412,7 @@ fn deserialize_value(
// Null.
TAG_NULL => Ok(JsValue::Null),
// Undefined.
TAG_UNDEFINED => Ok(JsValue::Null),
TAG_UNDEFINED => Ok(JsValue::Undefined),
// Float.
TAG_FLOAT64 => {
let val = unsafe { r.u.float64 };

View file

@ -122,7 +122,7 @@ where
fn call(&self, args: Vec<JsValue>) -> Result<Result<JsValue, String>, ValueError> {
(self)(Arguments(args));
Ok(Ok(JsValue::Null))
Ok(Ok(JsValue::Undefined))
}
}

View file

@ -79,6 +79,7 @@ mod log {
fn print_value(value: JsValue) -> String {
match value {
JsValue::Undefined => "undefined".to_string(),
JsValue::Null => "null".to_string(),
JsValue::Bool(v) => v.to_string(),
JsValue::Int(v) => v.to_string(),

View file

@ -385,6 +385,7 @@ mod tests {
let c = Context::new().unwrap();
let cases = vec![
("undefined", Ok(JsValue::Undefined)),
("null", Ok(JsValue::Null)),
("true", Ok(JsValue::Bool(true))),
("2 > 10", Ok(JsValue::Bool(false))),
@ -405,11 +406,11 @@ mod tests {
let obj_cases = vec![
(
r#" {"a": null} "#,
Ok(JsValue::Object(HashMap::from_iter(vec![(
"a".to_string(),
JsValue::Null,
)]))),
r#" {"a": null, "b": undefined} "#,
Ok(JsValue::Object(HashMap::from_iter(vec![
("a".to_string(), JsValue::Null),
("b".to_string(), JsValue::Undefined),
]))),
),
(
r#" {a: 1, b: true, c: {c1: false}} "#,
@ -706,7 +707,7 @@ mod tests {
);
})
.unwrap();
c.eval(" cb('hello', true, 100) ").unwrap();
assert_eq!(c.eval_as::<bool>("cb('hello', true, 100) === undefined").unwrap(), true);
// With return.
c.add_callback("cb2", |args: Arguments| -> u32 {

View file

@ -11,6 +11,7 @@ pub use bigint::BigInt;
#[derive(PartialEq, Clone, Debug)]
#[allow(missing_docs)]
pub enum JsValue {
Undefined,
Null,
Bool(bool),
Int(i32),