mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibJS: Add basic Array constructor
We only support Array() with 0 or 1 parameters so far.
This commit is contained in:
parent
eabdbe0ee9
commit
d155491122
Notes:
sideshowbarker
2024-07-19 07:55:44 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d155491122b
6 changed files with 132 additions and 0 deletions
|
@ -7,6 +7,7 @@ OBJS = \
|
|||
Lexer.o \
|
||||
Parser.o \
|
||||
Runtime/Array.o \
|
||||
Runtime/ArrayConstructor.o \
|
||||
Runtime/ArrayPrototype.o \
|
||||
Runtime/Cell.o \
|
||||
Runtime/ConsoleObject.o \
|
||||
|
|
66
Libraries/LibJS/Runtime/ArrayConstructor.cpp
Normal file
66
Libraries/LibJS/Runtime/ArrayConstructor.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/ArrayConstructor.h>
|
||||
#include <LibJS/Runtime/Shape.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
ArrayConstructor::ArrayConstructor()
|
||||
{
|
||||
put("prototype", interpreter().array_prototype());
|
||||
put("length", Value(1));
|
||||
}
|
||||
|
||||
ArrayConstructor::~ArrayConstructor()
|
||||
{
|
||||
}
|
||||
|
||||
Value ArrayConstructor::call(Interpreter& interpreter)
|
||||
{
|
||||
if (interpreter.argument_count() == 0)
|
||||
return interpreter.heap().allocate<Array>();
|
||||
|
||||
if (interpreter.argument_count() == 1) {
|
||||
auto* array = interpreter.heap().allocate<Array>();
|
||||
array->elements().resize(interpreter.argument(0).to_i32());
|
||||
return array;
|
||||
}
|
||||
|
||||
// FIXME: Handle "new Array(element0, element1, ...)"
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
Value ArrayConstructor::construct(Interpreter& interpreter)
|
||||
{
|
||||
return call(interpreter);
|
||||
}
|
||||
|
||||
}
|
46
Libraries/LibJS/Runtime/ArrayConstructor.h
Normal file
46
Libraries/LibJS/Runtime/ArrayConstructor.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/NativeFunction.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class ArrayConstructor final : public NativeFunction {
|
||||
public:
|
||||
ArrayConstructor();
|
||||
virtual ~ArrayConstructor() override;
|
||||
|
||||
virtual Value call(Interpreter&) override;
|
||||
virtual Value construct(Interpreter&) override;
|
||||
|
||||
private:
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
virtual const char* class_name() const override { return "ArrayConstructor"; }
|
||||
};
|
||||
|
||||
}
|
|
@ -38,6 +38,7 @@ ArrayPrototype::ArrayPrototype()
|
|||
put_native_function("shift", shift);
|
||||
put_native_function("pop", pop);
|
||||
put_native_function("push", push, 1);
|
||||
put("length", Value(0));
|
||||
}
|
||||
|
||||
ArrayPrototype::~ArrayPrototype()
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <AK/String.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/ArrayConstructor.h>
|
||||
#include <LibJS/Runtime/ConsoleObject.h>
|
||||
#include <LibJS/Runtime/DateConstructor.h>
|
||||
#include <LibJS/Runtime/ErrorConstructor.h>
|
||||
|
@ -30,6 +31,7 @@ GlobalObject::GlobalObject()
|
|||
put("Function", heap().allocate<FunctionConstructor>());
|
||||
put("Math", heap().allocate<MathObject>());
|
||||
put("Object", heap().allocate<ObjectConstructor>());
|
||||
put("Array", heap().allocate<ArrayConstructor>());
|
||||
}
|
||||
|
||||
GlobalObject::~GlobalObject()
|
||||
|
|
16
Libraries/LibJS/Tests/Array.js
Normal file
16
Libraries/LibJS/Tests/Array.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
function assert(x) { if (!x) throw 1; }
|
||||
|
||||
try {
|
||||
assert(Array.length === 1);
|
||||
assert(Array.prototype.length === 0);
|
||||
assert(typeof Array() === "object");
|
||||
assert(typeof new Array() === "object");
|
||||
|
||||
x = new Array(5);
|
||||
|
||||
assert(x.length === 5);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
Loading…
Reference in a new issue