diff --git a/Base/home/anon/js/array.js b/Base/home/anon/js/array.js new file mode 100644 index 0000000000..41e3b559a3 --- /dev/null +++ b/Base/home/anon/js/array.js @@ -0,0 +1,8 @@ +var a = [1, 2, 3]; +console.log(a); + +/* +for (var i = 0; i < 3; ++i) { + console.log(a[i]); +} +*/ diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 7edbc26e44..216b7758b5 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -682,4 +683,21 @@ Value NullLiteral::execute(Interpreter&) const return js_null(); } +void ArrayExpression::dump(int indent) const +{ + ASTNode::dump(indent); + for (auto& element : m_elements) { + element.dump(indent + 1); + } +} + +Value ArrayExpression::execute(Interpreter& interpreter) const +{ + auto* array = interpreter.heap().allocate(); + for (auto& element : m_elements) { + array->append(element.execute(interpreter)); + } + return array; +} + } diff --git a/Libraries/LibJS/AST.h b/Libraries/LibJS/AST.h index f4cc905285..eb5847a59f 100644 --- a/Libraries/LibJS/AST.h +++ b/Libraries/LibJS/AST.h @@ -581,6 +581,24 @@ private: virtual const char* class_name() const override { return "ObjectExpression"; } }; +class ArrayExpression : public Expression { +public: + ArrayExpression(NonnullRefPtrVector elements) + : m_elements(move(elements)) + { + } + + const NonnullRefPtrVector& elements() const { return m_elements; } + + virtual Value execute(Interpreter&) const override; + virtual void dump(int indent) const override; + +private: + virtual const char* class_name() const override { return "ArrayExpression"; } + + NonnullRefPtrVector m_elements; +}; + class MemberExpression final : public Expression { public: MemberExpression(NonnullRefPtr object, NonnullRefPtr property) diff --git a/Libraries/LibJS/Makefile b/Libraries/LibJS/Makefile index 084a2155fe..cf1ef690f8 100644 --- a/Libraries/LibJS/Makefile +++ b/Libraries/LibJS/Makefile @@ -6,6 +6,7 @@ OBJS = \ Interpreter.o \ Lexer.o \ Parser.o \ + Runtime/Array.o \ Runtime/Cell.o \ Runtime/ConsoleObject.o \ Runtime/Function.o \ diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 46f45f14ee..6e3a3935b0 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -231,6 +231,8 @@ NonnullRefPtr Parser::parse_primary_expression() return parse_object_expression(); case TokenType::Function: return parse_function_node(); + case TokenType::BracketOpen: + return parse_array_expression(); default: m_has_errors = true; expected("primary expression (missing switch case)"); @@ -273,6 +275,22 @@ NonnullRefPtr Parser::parse_object_expression() return create_ast_node(); } +NonnullRefPtr Parser::parse_array_expression() +{ + consume(TokenType::BracketOpen); + + NonnullRefPtrVector elements; + while (match_expression()) { + elements.append(parse_expression(0)); + if (!match(TokenType::Comma)) + break; + consume(TokenType::Comma); + } + + consume(TokenType::BracketClose); + return create_ast_node(move(elements)); +} + NonnullRefPtr Parser::parse_expression(int min_precedence, Associativity associativity) { if (match_unary_prefixed_expression()) diff --git a/Libraries/LibJS/Parser.h b/Libraries/LibJS/Parser.h index 6115c9424b..746e599578 100644 --- a/Libraries/LibJS/Parser.h +++ b/Libraries/LibJS/Parser.h @@ -56,6 +56,7 @@ public: NonnullRefPtr parse_primary_expression(); NonnullRefPtr parse_unary_prefixed_expression(); NonnullRefPtr parse_object_expression(); + NonnullRefPtr parse_array_expression(); NonnullRefPtr parse_secondary_expression(NonnullRefPtr, int min_precedence, Associativity associate = Associativity::Right); NonnullRefPtr parse_call_expression(NonnullRefPtr); diff --git a/Libraries/LibJS/Runtime/Array.cpp b/Libraries/LibJS/Runtime/Array.cpp new file mode 100644 index 0000000000..09e7267621 --- /dev/null +++ b/Libraries/LibJS/Runtime/Array.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020, Andreas Kling + * 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 +#include + +namespace JS { + +Array::Array() +{ + put_native_property( + "length", + [this](Object*) { + return Value(length()); + }, + [](Object*, Value) { + ASSERT_NOT_REACHED(); + }); +} + +Array::~Array() +{ +} + +void Array::append(Value value) +{ + m_elements.append(value); +} + +void Array::visit_children(Cell::Visitor& visitor) +{ + Object::visit_children(visitor); + for (auto& element : m_elements) + visitor.visit(element); +} + +} diff --git a/Libraries/LibJS/Runtime/Array.h b/Libraries/LibJS/Runtime/Array.h new file mode 100644 index 0000000000..63baca7dfe --- /dev/null +++ b/Libraries/LibJS/Runtime/Array.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020, Andreas Kling + * 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 + +namespace JS { + +class Array final : public Object { +public: + Array(); + virtual ~Array() override; + + i32 length() const { return static_cast(m_elements.size()); } + const Vector& elements() const { return m_elements; } + + void append(Value); + +private: + virtual const char* class_name() const override { return "Array"; } + virtual void visit_children(Cell::Visitor&) override; + virtual bool is_array() const override { return true; } + + Vector m_elements; +}; + +} diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index cca3c587d6..631f87801b 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -46,6 +46,7 @@ public: void put_native_function(String property_name, AK::Function)>); void put_native_property(String property_name, AK::Function getter, AK::Function setter); + virtual bool is_array() const { return false; } virtual bool is_function() const { return false; } virtual bool is_native_function() const { return false; } virtual bool is_string_object() const { return false; }