mirror of
https://github.com/RGBCube/serenity
synced 2025-07-16 12:37:34 +00:00
LibJS: Support multiple arguments in Array constructor
This commit is contained in:
parent
8137f40b73
commit
01fd6ce045
2 changed files with 41 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -29,6 +30,7 @@
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
#include <LibJS/Runtime/Array.h>
|
#include <LibJS/Runtime/Array.h>
|
||||||
#include <LibJS/Runtime/ArrayConstructor.h>
|
#include <LibJS/Runtime/ArrayConstructor.h>
|
||||||
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Shape.h>
|
#include <LibJS/Runtime/Shape.h>
|
||||||
|
|
||||||
|
@ -50,14 +52,21 @@ Value ArrayConstructor::call(Interpreter& interpreter)
|
||||||
if (interpreter.argument_count() <= 0)
|
if (interpreter.argument_count() <= 0)
|
||||||
return Array::create(interpreter.global_object());
|
return Array::create(interpreter.global_object());
|
||||||
|
|
||||||
if (interpreter.argument_count() == 1) {
|
if (interpreter.argument_count() == 1 && interpreter.argument(0).is_number()) {
|
||||||
|
auto array_length_value = interpreter.argument(0);
|
||||||
|
if (!array_length_value.is_integer() || array_length_value.to_i32() < 0) {
|
||||||
|
interpreter.throw_exception<TypeError>("Invalid array length");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
auto* array = Array::create(interpreter.global_object());
|
auto* array = Array::create(interpreter.global_object());
|
||||||
array->elements().resize(interpreter.argument(0).to_i32());
|
array->elements().resize(array_length_value.to_i32());
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Handle "new Array(element0, element1, ...)"
|
auto* array = Array::create(interpreter.global_object());
|
||||||
ASSERT_NOT_REACHED();
|
for (size_t i = 0; i < interpreter.argument_count(); ++i)
|
||||||
|
array->elements().append(interpreter.argument(i));
|
||||||
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value ArrayConstructor::construct(Interpreter& interpreter)
|
Value ArrayConstructor::construct(Interpreter& interpreter)
|
||||||
|
|
|
@ -8,9 +8,36 @@ try {
|
||||||
assert(typeof Array() === "object");
|
assert(typeof Array() === "object");
|
||||||
assert(typeof new Array() === "object");
|
assert(typeof new Array() === "object");
|
||||||
|
|
||||||
var a = new Array(5);
|
var a;
|
||||||
|
|
||||||
|
a = new Array(5);
|
||||||
assert(a.length === 5);
|
assert(a.length === 5);
|
||||||
|
|
||||||
|
a = new Array("5");
|
||||||
|
assert(a.length === 1);
|
||||||
|
assert(a[0] === "5");
|
||||||
|
|
||||||
|
a = new Array(1, 2, 3);
|
||||||
|
assert(a.length === 3);
|
||||||
|
assert(a[0] === 1);
|
||||||
|
assert(a[1] === 2);
|
||||||
|
assert(a[2] === 3);
|
||||||
|
|
||||||
|
a = new Array([1, 2, 3]);
|
||||||
|
assert(a.length === 1);
|
||||||
|
assert(a[0][0] === 1);
|
||||||
|
assert(a[0][1] === 2);
|
||||||
|
assert(a[0][2] === 3);
|
||||||
|
|
||||||
|
[-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
|
||||||
|
assertThrowsError(() => {
|
||||||
|
new Array(value);
|
||||||
|
}, {
|
||||||
|
error: TypeError,
|
||||||
|
message: "Invalid array length"
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("FAIL: " + e);
|
console.log("FAIL: " + e);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue