pub enum JSValue {
    Undefined,
    Bool(bool),
    Number(JSNumber),
    String(JSString),
    Ref(JSRef),
}
Expand description

A JSValue is either a primitive value or a reference to an object.

Variants

Undefined

Bool(bool)

Number(JSNumber)

String(JSString)

Ref(JSRef)

Implementations

to_ref() tries to return the underlying object reference, if any. Throws Exception::ReferenceNotAnObject if it’s not a reference. Checking if a JSValue is a reference: val.to_ref().is_ok().

to_string() makes a human-readable string representation of the value:

assert_eq!(
    JSValue::from("1").to_string(&mut heap).unwrap().as_str(),
    "\"1\""
);
assert_eq!(
    JSValue::from(1).to_string(&mut heap).unwrap().as_str(),
    "1"
);

let json_object = json!({"one": 1});
let example_object = heap.object_from_json(&json_object);
assert_eq!(
    example_object.to_string(&mut heap).unwrap().as_str(),
    "{ one: 1 }"
);

let json_array = json!([1, 2]);
let example_array = heap.object_from_json(&json_array);
assert_eq!(
    example_array.to_string(&mut heap).unwrap().as_str(),
    "[1, 2]"
);

stringify() makes everything into a string used for evaluation in a string context. It corresponds to .toString() in JavaScript

numberify() tries to make everything into a numeric value for evalation in a numeric context. It is slightly more strict than +value in JavaScript: only value.numberify().unwrap_or(f64::NAN) corresponds to +value in JavaScript.

boolify() treats everythings as a truthy value. ES5: ToBoolean

objectify() wraps a primitive into its object:

  • undefined becomes null
  • bool/number/string becomes Boolean/Number/String
  • objects just return their reference.

Javascript’s typeof

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Constructs a pure value (without references), if possible. Excludes objects and arrays.

The type returned in the event of a conversion error.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.