mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:07:34 +00:00
LibJS: Add generic InvalidLength error type
We have multiple array types now, so ArrayInvalidLength has been replaced with a generic InvalidLength. Also fixes a small issue in the Array constructor, it should throw RangeError for invalid lengths, not TypeError.
This commit is contained in:
parent
5e08ae4e14
commit
6de4f1fcb3
4 changed files with 4 additions and 4 deletions
|
@ -77,7 +77,7 @@ JS_DEFINE_NATIVE_SETTER(Array::length_setter)
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return;
|
return;
|
||||||
if (length.is_nan() || length.is_infinity() || length.as_double() < 0) {
|
if (length.is_nan() || length.is_infinity() || length.as_double() < 0) {
|
||||||
vm.throw_exception<RangeError>(global_object, ErrorType::ArrayInvalidLength);
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
array->indexed_properties().set_array_like_size(length.as_double());
|
array->indexed_properties().set_array_like_size(length.as_double());
|
||||||
|
|
|
@ -67,7 +67,7 @@ Value ArrayConstructor::call()
|
||||||
if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
|
if (vm().argument_count() == 1 && vm().argument(0).is_number()) {
|
||||||
auto array_length_value = vm().argument(0);
|
auto array_length_value = vm().argument(0);
|
||||||
if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
|
if (!array_length_value.is_integer() || array_length_value.as_i32() < 0) {
|
||||||
vm().throw_exception<TypeError>(global_object(), ErrorType::ArrayInvalidLength);
|
vm().throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto* array = Array::create(global_object());
|
auto* array = Array::create(global_object());
|
||||||
|
|
|
@ -27,7 +27,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define JS_ENUMERATE_ERROR_TYPES(M) \
|
#define JS_ENUMERATE_ERROR_TYPES(M) \
|
||||||
M(ArrayInvalidLength, "Invalid array length") \
|
|
||||||
M(ArrayMaxSize, "Maximum array size exceeded") \
|
M(ArrayMaxSize, "Maximum array size exceeded") \
|
||||||
M(ArrayPrototypeOneArg, "Array.prototype.{}() requires at least one argument") \
|
M(ArrayPrototypeOneArg, "Array.prototype.{}() requires at least one argument") \
|
||||||
M(AccessorBadField, "Accessor descriptor's '{}' field must be a function or undefined") \
|
M(AccessorBadField, "Accessor descriptor's '{}' field must be a function or undefined") \
|
||||||
|
@ -48,6 +47,7 @@
|
||||||
M(InstanceOfOperatorBadPrototype, "'prototype' property of {} is not an object") \
|
M(InstanceOfOperatorBadPrototype, "'prototype' property of {} is not an object") \
|
||||||
M(InvalidAssignToConst, "Invalid assignment to const variable") \
|
M(InvalidAssignToConst, "Invalid assignment to const variable") \
|
||||||
M(InvalidLeftHandAssignment, "Invalid left-hand side in assignment") \
|
M(InvalidLeftHandAssignment, "Invalid left-hand side in assignment") \
|
||||||
|
M(InvalidLength, "Invalid {} length") \
|
||||||
M(InvalidRadix, "Radix must be an integer no less than 2, and no greater than 36") \
|
M(InvalidRadix, "Radix must be an integer no less than 2, and no greater than 36") \
|
||||||
M(IsNotA, "{} is not a {}") \
|
M(IsNotA, "{} is not a {}") \
|
||||||
M(IsNotAEvaluatedFrom, "{} is not a {} (evaluated from '{}')") \
|
M(IsNotAEvaluatedFrom, "{} is not a {} (evaluated from '{}')") \
|
||||||
|
|
|
@ -9,7 +9,7 @@ describe("errors", () => {
|
||||||
[-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
|
[-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new Array(value);
|
new Array(value);
|
||||||
}).toThrowWithMessage(TypeError, "Invalid array length");
|
}).toThrowWithMessage(RangeError, "Invalid array length");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue