pub struct JSArray { /* private fields */ }Expand description
A JavaScript array.
Implementations§
Source§impl JSArray
impl JSArray
pub fn new(object: JSObject) -> Self
Sourcepub fn new_array(ctx: &JSContext, args: &[JSValue]) -> JSResult<Self>
pub fn new_array(ctx: &JSContext, args: &[JSValue]) -> JSResult<Self>
Creates a new JSArray object.
§Arguments
ctx: The JavaScript context to create the array in.args: The values to initialize the array with.
§Example
use rust_jsc::{JSArray, JSContext, JSValue};
let ctx = JSContext::new();
let array = JSArray::new_array(
&ctx,
&[
JSValue::number(&ctx, 1.0),
JSValue::number(&ctx, 2.0),
JSValue::number(&ctx, 3.0),
]
).unwrap();
assert_eq!(array.as_string().unwrap(), "1,2,3");§Errors
If an exception is thrown while creating the array.
A JSError will be returned.
§Returns
The new JSArray object.
Sourcepub fn get(&self, index: u32) -> JSResult<JSValue>
pub fn get(&self, index: u32) -> JSResult<JSValue>
Gets the value at the specified index.
This is equivalent to array[index] in JavaScript.
§Arguments
index: The index of the value to get.
§Example
use rust_jsc::{JSArray, JSContext, JSValue};
let ctx = JSContext::new();
let array = JSArray::new_array(
&ctx,
&[
JSValue::number(&ctx, 1.0),
JSValue::number(&ctx, 2.0),
JSValue::number(&ctx, 3.0),
]
).unwrap();
assert_eq!(array.get(0).unwrap().as_number().unwrap(), 1.0);§Errors
If an exception is thrown while getting the value.
A JSError will be returned.
§Returns
The value at the specified index.
Sourcepub fn set(&self, index: u32, value: &JSValue) -> JSResult<()>
pub fn set(&self, index: u32, value: &JSValue) -> JSResult<()>
Sets the value at the specified index.
This is equivalent to array[index] = value in JavaScript.
§Arguments
index: The index of the value to set.value: The value to set.
§Example
use rust_jsc::{JSArray, JSContext, JSValue};
let ctx = JSContext::new();
let array = JSArray::new_array(
&ctx,
&[
JSValue::number(&ctx, 1.0),
JSValue::number(&ctx, 2.0),
JSValue::number(&ctx, 3.0),
]
).unwrap();
array.set(0, &JSValue::number(&ctx, 4.0)).unwrap();
array.set(1, &JSValue::number(&ctx, 5.0)).unwrap();
array.set(2, &JSValue::number(&ctx, 6.0)).unwrap();
assert_eq!(array.as_string().unwrap(), "4,5,6");§Errors
If an exception is thrown while setting the value.
A JSError will be returned.
§Returns
An empty JSResult.
Sourcepub fn length(&self) -> JSResult<f64>
pub fn length(&self) -> JSResult<f64>
Gets the length of the array.
This is equivalent to array.length in JavaScript.
§Example
use rust_jsc::{JSArray, JSContext, JSValue};
let ctx = JSContext::new();
let array = JSArray::new_array(
&ctx,
&[
JSValue::number(&ctx, 1.0),
JSValue::number(&ctx, 2.0),
JSValue::number(&ctx, 3.0),
]
).unwrap();
assert_eq!(array.length().unwrap(), 3.0);§Errors
If an exception is thrown while getting the length.
A JSError will be returned.
§Returns
The length of the array.
Sourcepub fn push(&self, value: &JSValue) -> JSResult<f64>
pub fn push(&self, value: &JSValue) -> JSResult<f64>
Pushes a value to the end of the array.
This is equivalent to array.push(value) in JavaScript.
Returns the new length of the array.
§Arguments
value: The value to push.
§Example
use rust_jsc::{JSArray, JSContext, JSValue};
let ctx = JSContext::new();
let array = JSArray::new_array(
&ctx,
&[
JSValue::number(&ctx, 1.0),
JSValue::number(&ctx, 2.0),
JSValue::number(&ctx, 3.0),
]
).unwrap();
array.push(&JSValue::number(&ctx, 4 as f64)).unwrap();
array.push(&JSValue::number(&ctx, 5 as f64)).unwrap();
array.push(&JSValue::number(&ctx, 6 as f64)).unwrap();
assert_eq!(array.as_string().unwrap(), "1,2,3,4,5,6");§Errors
If an exception is thrown while pushing the value.
A JSError will be returned.
§Returns
The new length of the array.
Methods from Deref<Target = JSValue>§
Sourcepub fn as_json_string(&self, indent: u32) -> JSResult<JSString>
pub fn as_json_string(&self, indent: u32) -> JSResult<JSString>
Creates a JavaScript string containing the JSON serialized representation of a JS value.
§Arguments
indent- The number of spaces to indent when nesting. If 0, the resulting JSON will not contains newlines. The size of the indent is clamped to 10 spaces.
§Examples
use rust_jsc::*;
let ctx = JSContext::new();
let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
assert!(value.as_json_string(2).is_ok());§Returns
A JSString with the result of serialization, or JSError if an exception occurs.
Sourcepub fn as_boolean(&self) -> bool
pub fn as_boolean(&self) -> bool
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Sourcepub fn is_instance_of(&self, constructor: &JSObject) -> JSResult<bool>
pub fn is_instance_of(&self, constructor: &JSObject) -> JSResult<bool>
Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator.
§Arguments
constructor- The constructor to test against.
§Returns
true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false.
Sourcepub fn is_object_of_class(&self, class: &JSClass) -> JSResult<bool>
pub fn is_object_of_class(&self, class: &JSClass) -> JSResult<bool>
Tests whether an object is an instance of a particular class.
§Arguments
class- The class to test against.
§Examples
use rust_jsc::*;
let ctx = JSContext::new();
let class = JSClass::builder("Test").build::<i32>().unwrap();
let value = class.object::<i32>(&ctx, Some(42));
assert!(value.is_object_of_class(&class).unwrap());§Returns
true if the object is an instance of class, otherwise false.
Sourcepub fn is_equal(&self, other: &JSValue) -> JSResult<bool>
pub fn is_equal(&self, other: &JSValue) -> JSResult<bool>
Tests whether two JavaScript values are equal, as compared by the JS == operator.
§Arguments
other- The value to compare.
§Examples
use rust_jsc::*;
let ctx = JSContext::new();
let value1 = JSValue::number(&ctx, 42.0);
let value2 = JSValue::number(&ctx, 42.0);
assert!(value1.is_equal(&value2).unwrap());§Returns
true if the values are equal, otherwise false.
Sourcepub fn protect(&self)
pub fn protect(&self)
Protects a JavaScript value from garbage collection.
§Examples
use rust_jsc::*;
let ctx = JSContext::new();
let value = JSValue::number(&ctx, 42.0);
value.protect();§Note
You must call unprotect when you no longer need the value.
Use this method when you want to store a JSValue in a global or on the heap,
where the garbage collector will not be able to discover your reference to it.\n
A value may be protected multiple times and must be unprotected an equal number of times
before becoming eligible for garbage collection.
Sourcepub fn unprotect(&self)
pub fn unprotect(&self)
Unprotects a JavaScript value from garbage collection.
§Examples
use rust_jsc::*;
let ctx = JSContext::new();
let value = JSValue::number(&ctx, 42.0);
value.protect();
value.unprotect();§Note
A value may be protected multiple times and must be unprotected an\n equal number of times before becoming eligible for garbage collection.