Skip to main content

JSArray

Struct JSArray 

Source
pub struct JSArray { /* private fields */ }
Expand description

A JavaScript array.

Implementations§

Source§

impl JSArray

Source

pub fn new(object: JSObject) -> Self

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>§

Source

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.

Source

pub fn as_string(&self) -> JSResult<JSString>

Converts a JavaScript value to a js string and returns the resulting js string.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::string(&ctx, "Hello, World!");
assert!(value.as_string().is_ok());
§Returns

A JavaScript string.

Source

pub fn as_object(&self) -> JSResult<JSObject>

Converts a JavaScript value to an object and returns the resulting object.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
assert!(value.as_object().is_ok());
§Returns

A JavaScript object.

Source

pub fn as_boolean(&self) -> bool

Converts a JavaScript value to boolean and returns the resulting boolean.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::boolean(&ctx, true);
assert_eq!(value.as_boolean(), true);
§Returns

A boolean value.

Source

pub fn as_number(&self) -> JSResult<f64>

Converts a JavaScript value to number and returns the resulting number.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::number(&ctx, 42.0);
assert_eq!(value.as_number().unwrap(), 42.0);
§Returns

A number value.

Source

pub fn is_undefined(&self) -> bool

Checks if the value is undefined.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::undefined(&ctx);
assert!(value.is_undefined());
§Returns

A boolean value.

Source

pub fn is_null(&self) -> bool

Checks if the value is null.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::null(&ctx);
assert!(value.is_null());
§Returns

A boolean value.

Source

pub fn is_boolean(&self) -> bool

Checks if the value is a boolean.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::boolean(&ctx, true);
assert!(value.is_boolean());
§Returns

A boolean value.

Source

pub fn is_number(&self) -> bool

Checks if the value is a number.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::number(&ctx, 42.0);
assert!(value.is_number());
§Returns

A boolean value.

Source

pub fn is_string(&self) -> bool

Checks if the value is a string.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::string(&ctx, "Hello, World!");
assert!(value.is_string());
§Returns

A boolean value.

Source

pub fn is_object(&self) -> bool

Checks if the value is an object.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = ctx.evaluate_script("({ key: 'value' })", None).unwrap();
assert!(value.is_object());
§Returns

A boolean value.

Source

pub fn is_symbol(&self) -> bool

Checks if the value is a symbol.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::symbol(&ctx, "symbol");
assert!(value.is_symbol());
§Returns

A boolean value.

Source

pub fn is_array(&self) -> bool

Checks if the value is an array.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = ctx.evaluate_script("[1, 2, 3]", None).unwrap();
assert!(value.is_array());
§Returns

A boolean value.

Source

pub fn is_date(&self) -> bool

Checks if the value is a date.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = ctx.evaluate_script("new Date()", None).unwrap();
assert!(value.is_date());
§Returns

A boolean value.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_type(&self) -> JSValueType

Returns the type of a JavaScript value.

§Examples
use rust_jsc::*;

let ctx = JSContext::new();
let value = JSValue::number(&ctx, 42.0);
assert_eq!(value.get_type(), JSValueType::Number);
§Returns

The type of the JavaScript value.

Trait Implementations§

Source§

impl Deref for JSArray

Source§

type Target = JSValue

The resulting type after dereferencing.
Source§

fn deref(&self) -> &JSValue

Dereferences the value.
Source§

impl From<JSArray> for JSObject

Source§

fn from(array: JSArray) -> Self

Converts to this type from the input type.
Source§

impl From<JSArray> for JSValue

Source§

fn from(array: JSArray) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.