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
sourceimpl JSValue
impl JSValue
pub const NULL: JSValue = _
sourcepub fn to_ref(&self) -> JSResult<JSRef>
pub fn to_ref(&self) -> JSResult<JSRef>
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()
.
pub fn to_json(&self, heap: &Heap) -> JSResult<JSON>
sourcepub fn to_string(&self, heap: &mut Heap) -> JSResult<JSString>
pub fn to_string(&self, heap: &mut Heap) -> JSResult<JSString>
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]"
);
sourcepub fn stringify(&self, heap: &mut Heap) -> JSResult<JSString>
pub fn stringify(&self, heap: &mut Heap) -> JSResult<JSString>
stringify() makes everything into a string used for evaluation in a string context. It corresponds to .toString() in JavaScript
sourcepub fn numberify(&self, heap: &Heap) -> Option<JSNumber>
pub fn numberify(&self, heap: &Heap) -> Option<JSNumber>
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.
sourcepub fn boolify(&self, heap: &Heap) -> bool
pub fn boolify(&self, heap: &Heap) -> bool
boolify() treats everythings as a truthy value. ES5: ToBoolean
sourcepub fn objectify(&self, heap: &mut Heap) -> JSRef
pub fn objectify(&self, heap: &mut Heap) -> JSRef
objectify() wraps a primitive into its object:
undefined
becomesnull
bool
/number
/string
becomesBoolean
/Number
/String
- objects just return their reference.
sourcepub fn loose_eq(&self, other: &JSValue, heap: &Heap) -> bool
pub fn loose_eq(&self, other: &JSValue, heap: &Heap) -> bool
Abstract Equality Comparison, ==
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using_
pub fn numerically<F>(&self, other: &JSValue, heap: &Heap, op: F) -> JSValue where
F: Fn(f64, f64) -> f64,
sourcepub fn minus(&self, other: &JSValue, heap: &Heap) -> JSResult<JSValue>
pub fn minus(&self, other: &JSValue, heap: &Heap) -> JSResult<JSValue>
Subtraction operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction
pub fn compare<StrCmpFn: Fn(&str, &str) -> bool, NumCmpFn: Fn(f64, f64) -> bool>(
&self,
other: &JSValue,
heap: &Heap,
stringly: StrCmpFn,
numberly: NumCmpFn
) -> JSValue
Trait Implementations
impl StructuralPartialEq for JSValue
Auto Trait Implementations
impl RefUnwindSafe for JSValue
impl !Send for JSValue
impl !Sync for JSValue
impl Unpin for JSValue
impl UnwindSafe for JSValue
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more