mirror of https://github.com/boa-dev/boa.git
Browse Source
* First draft of Array prototype object * First draft of Array prototype object * Update to working Array object * Remove unneeded HashMap import * Fix indexing by removing private internal HashMap * Fix formatting issues with cargo fmtpull/53/head
Callum Ward
5 years ago
committed by
Jason Williams
5 changed files with 99 additions and 40 deletions
@ -1,19 +1,56 @@ |
|||||||
use crate::js::function::NativeFunctionData; |
use crate::js::function::NativeFunctionData; |
||||||
use crate::js::value::{to_value, ResultValue, Value, ValueData}; |
use crate::js::object::{Property, PROTOTYPE}; |
||||||
|
use crate::js::value::{from_value, to_value, ResultValue, Value, ValueData}; |
||||||
use gc::Gc; |
use gc::Gc; |
||||||
|
|
||||||
/// Create a new array
|
/// Create a new array
|
||||||
pub fn make_array(this: Value, _: Value, _: Vec<Value>) -> ResultValue { |
pub fn make_array(this: Value, _: Value, args: Vec<Value>) -> ResultValue { |
||||||
let this_ptr = this.clone(); |
let this_ptr = this.clone(); |
||||||
|
// Make a new Object which will internally represent the Array (mapping
|
||||||
|
// between indices and values): this creates an Object with no prototype
|
||||||
|
match args.len() { |
||||||
|
0 => { |
||||||
this_ptr.set_field_slice("length", to_value(0i32)); |
this_ptr.set_field_slice("length", to_value(0i32)); |
||||||
Ok(Gc::new(ValueData::Undefined)) |
} |
||||||
|
1 => { |
||||||
|
let length_chosen: i32 = from_value(args[0].clone()).unwrap(); |
||||||
|
this_ptr.set_field_slice("length", to_value(length_chosen)); |
||||||
|
} |
||||||
|
n => { |
||||||
|
this_ptr.set_field_slice("length", to_value(n)); |
||||||
|
for k in 0..n { |
||||||
|
let index_str = k.to_string(); |
||||||
|
this_ptr.set_field(index_str, args[k].clone()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Ok(this_ptr) |
||||||
} |
} |
||||||
|
|
||||||
|
/// Get an array's length
|
||||||
|
pub fn get_array_length(this: Value, _: Value, _: Vec<Value>) -> ResultValue { |
||||||
|
// Access the inner hash map which represents the actual Array contents
|
||||||
|
// (mapping between indices and values)
|
||||||
|
Ok(this.get_field_slice("length")) |
||||||
|
} |
||||||
|
|
||||||
/// Create a new `Array` object
|
/// Create a new `Array` object
|
||||||
pub fn _create() -> Value { |
pub fn _create(global: &Value) -> Value { |
||||||
let array = to_value(make_array as NativeFunctionData); |
let array = to_value(make_array as NativeFunctionData); |
||||||
|
let proto = ValueData::new_obj(Some(global)); |
||||||
|
let length = Property { |
||||||
|
configurable: false, |
||||||
|
enumerable: false, |
||||||
|
writable: false, |
||||||
|
value: Gc::new(ValueData::Undefined), |
||||||
|
get: to_value(get_array_length as NativeFunctionData), |
||||||
|
set: Gc::new(ValueData::Undefined), |
||||||
|
}; |
||||||
|
proto.set_prop_slice("length", length); |
||||||
|
array.set_field_slice(PROTOTYPE, proto); |
||||||
array |
array |
||||||
} |
} |
||||||
/// Initialise the global object with the `Array` object
|
/// Initialise the global object with the `Array` object
|
||||||
pub fn init(global: &Value) { |
pub fn init(global: &Value) { |
||||||
global.set_field_slice("Array", _create()); |
global.set_field_slice("Array", _create(global)); |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue