mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:37:46 +00:00
LibJS: Add the Map built-in object
This commit is contained in:
parent
f9d58ec0b4
commit
a96ac8bd56
11 changed files with 335 additions and 0 deletions
|
@ -55,6 +55,9 @@ set(SOURCES
|
||||||
Runtime/IteratorPrototype.cpp
|
Runtime/IteratorPrototype.cpp
|
||||||
Runtime/JSONObject.cpp
|
Runtime/JSONObject.cpp
|
||||||
Runtime/LexicalEnvironment.cpp
|
Runtime/LexicalEnvironment.cpp
|
||||||
|
Runtime/Map.cpp
|
||||||
|
Runtime/MapConstructor.cpp
|
||||||
|
Runtime/MapPrototype.cpp
|
||||||
Runtime/MarkedValueList.cpp
|
Runtime/MarkedValueList.cpp
|
||||||
Runtime/MathObject.cpp
|
Runtime/MathObject.cpp
|
||||||
Runtime/NativeFunction.cpp
|
Runtime/NativeFunction.cpp
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
__JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \
|
__JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \
|
||||||
__JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \
|
__JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \
|
||||||
__JS_ENUMERATE(Function, function, FunctionPrototype, FunctionConstructor, void) \
|
__JS_ENUMERATE(Function, function, FunctionPrototype, FunctionConstructor, void) \
|
||||||
|
__JS_ENUMERATE(Map, map, MapPrototype, MapConstructor, void) \
|
||||||
__JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor, void) \
|
__JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor, void) \
|
||||||
__JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor, void) \
|
__JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor, void) \
|
||||||
__JS_ENUMERATE(Promise, promise, PromisePrototype, PromiseConstructor, void) \
|
__JS_ENUMERATE(Promise, promise, PromisePrototype, PromiseConstructor, void) \
|
||||||
|
|
|
@ -36,6 +36,8 @@
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/IteratorPrototype.h>
|
#include <LibJS/Runtime/IteratorPrototype.h>
|
||||||
#include <LibJS/Runtime/JSONObject.h>
|
#include <LibJS/Runtime/JSONObject.h>
|
||||||
|
#include <LibJS/Runtime/MapConstructor.h>
|
||||||
|
#include <LibJS/Runtime/MapPrototype.h>
|
||||||
#include <LibJS/Runtime/MathObject.h>
|
#include <LibJS/Runtime/MathObject.h>
|
||||||
#include <LibJS/Runtime/NativeFunction.h>
|
#include <LibJS/Runtime/NativeFunction.h>
|
||||||
#include <LibJS/Runtime/NumberConstructor.h>
|
#include <LibJS/Runtime/NumberConstructor.h>
|
||||||
|
@ -150,6 +152,7 @@ void GlobalObject::initialize_global_object()
|
||||||
add_constructor(vm.names.Date, m_date_constructor, m_date_prototype);
|
add_constructor(vm.names.Date, m_date_constructor, m_date_prototype);
|
||||||
add_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
|
add_constructor(vm.names.Error, m_error_constructor, m_error_prototype);
|
||||||
add_constructor(vm.names.Function, m_function_constructor, m_function_prototype);
|
add_constructor(vm.names.Function, m_function_constructor, m_function_prototype);
|
||||||
|
add_constructor(vm.names.Map, m_map_constructor, m_map_prototype);
|
||||||
add_constructor(vm.names.Number, m_number_constructor, m_number_prototype);
|
add_constructor(vm.names.Number, m_number_constructor, m_number_prototype);
|
||||||
add_constructor(vm.names.Object, m_object_constructor, m_object_prototype);
|
add_constructor(vm.names.Object, m_object_constructor, m_object_prototype);
|
||||||
add_constructor(vm.names.Promise, m_promise_constructor, m_promise_prototype);
|
add_constructor(vm.names.Promise, m_promise_constructor, m_promise_prototype);
|
||||||
|
|
34
Userland/Libraries/LibJS/Runtime/Map.cpp
Normal file
34
Userland/Libraries/LibJS/Runtime/Map.cpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Map.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
Map* Map::create(GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
return global_object.heap().allocate<Map>(global_object, *global_object.map_prototype());
|
||||||
|
}
|
||||||
|
|
||||||
|
Map::Map(Object& prototype)
|
||||||
|
: Object(prototype)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Map::~Map()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Map::visit_edges(Cell::Visitor& visitor)
|
||||||
|
{
|
||||||
|
Object::visit_edges(visitor);
|
||||||
|
for (auto& value : m_entries) {
|
||||||
|
visitor.visit(value.key);
|
||||||
|
visitor.visit(value.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
Userland/Libraries/LibJS/Runtime/Map.h
Normal file
34
Userland/Libraries/LibJS/Runtime/Map.h
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Object.h>
|
||||||
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
class Map : public Object {
|
||||||
|
JS_OBJECT(Map, Object);
|
||||||
|
|
||||||
|
public:
|
||||||
|
static Map* create(GlobalObject&);
|
||||||
|
|
||||||
|
explicit Map(Object& prototype);
|
||||||
|
virtual ~Map() override;
|
||||||
|
|
||||||
|
HashMap<Value, Value, ValueTraits> const& entries() const { return m_entries; };
|
||||||
|
HashMap<Value, Value, ValueTraits>& entries() { return m_entries; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual void visit_edges(Visitor& visitor) override;
|
||||||
|
|
||||||
|
HashMap<Value, Value, ValueTraits> m_entries; // FIXME: Replace with a HashMap that maintains a linked list of insertion order for correct iteration order
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
85
Userland/Libraries/LibJS/Runtime/MapConstructor.cpp
Normal file
85
Userland/Libraries/LibJS/Runtime/MapConstructor.cpp
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Error.h>
|
||||||
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/IteratorOperations.h>
|
||||||
|
#include <LibJS/Runtime/Map.h>
|
||||||
|
#include <LibJS/Runtime/MapConstructor.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
MapConstructor::MapConstructor(GlobalObject& global_object)
|
||||||
|
: NativeFunction(vm().names.Map, *global_object.function_prototype())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void MapConstructor::initialize(GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
NativeFunction::initialize(global_object);
|
||||||
|
define_property(vm.names.prototype, global_object.map_prototype(), 0);
|
||||||
|
define_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||||
|
|
||||||
|
define_native_accessor(vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
|
||||||
|
}
|
||||||
|
|
||||||
|
MapConstructor::~MapConstructor()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Value MapConstructor::call()
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Map);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 24.1.1.1 Map ( [ iterable ] ), https://tc39.es/ecma262/#sec-map-iterable
|
||||||
|
Value MapConstructor::construct(Function&)
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
|
||||||
|
// FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%Map.prototype%")
|
||||||
|
auto* map = Map::create(global_object());
|
||||||
|
if (vm.argument(0).is_nullish())
|
||||||
|
return map;
|
||||||
|
|
||||||
|
auto adder = map->get(vm.names.set);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
if (!adder.is_function()) {
|
||||||
|
vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, "'set' property of Map");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
get_iterator_values(global_object(), vm.argument(0), [&](Value iterator_value) {
|
||||||
|
if (vm.exception())
|
||||||
|
return IterationDecision::Break;
|
||||||
|
if (!iterator_value.is_object()) {
|
||||||
|
vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
|
||||||
|
return IterationDecision::Break;
|
||||||
|
}
|
||||||
|
auto key = iterator_value.as_object().get(0).value_or(js_undefined());
|
||||||
|
if (vm.exception())
|
||||||
|
return IterationDecision::Break;
|
||||||
|
auto value = iterator_value.as_object().get(1).value_or(js_undefined());
|
||||||
|
if (vm.exception())
|
||||||
|
return IterationDecision::Break;
|
||||||
|
(void)vm.call(adder.as_function(), Value(map), key, value);
|
||||||
|
return vm.exception() ? IterationDecision::Break : IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 24.1.2.2 get Map [ @@species ], https://tc39.es/ecma262/#sec-get-map-@@species
|
||||||
|
JS_DEFINE_NATIVE_GETTER(MapConstructor::symbol_species_getter)
|
||||||
|
{
|
||||||
|
return vm.this_value(global_object);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
30
Userland/Libraries/LibJS/Runtime/MapConstructor.h
Normal file
30
Userland/Libraries/LibJS/Runtime/MapConstructor.h
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/NativeFunction.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
class MapConstructor final : public NativeFunction {
|
||||||
|
JS_OBJECT(MapConstructor, NativeFunction);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit MapConstructor(GlobalObject&);
|
||||||
|
virtual void initialize(GlobalObject&) override;
|
||||||
|
virtual ~MapConstructor() override;
|
||||||
|
|
||||||
|
virtual Value call() override;
|
||||||
|
virtual Value construct(Function&) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
|
||||||
|
JS_DECLARE_NATIVE_GETTER(symbol_species_getter);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
52
Userland/Libraries/LibJS/Runtime/MapPrototype.cpp
Normal file
52
Userland/Libraries/LibJS/Runtime/MapPrototype.cpp
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <LibJS/Runtime/MapPrototype.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
MapPrototype::MapPrototype(GlobalObject& global_object)
|
||||||
|
: Object(*global_object.object_prototype())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void MapPrototype::initialize(GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
Object::initialize(global_object);
|
||||||
|
|
||||||
|
define_native_accessor(vm.names.size, size_getter, {}, Attribute::Configurable);
|
||||||
|
|
||||||
|
define_property(vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), vm.names.Map), Attribute::Configurable);
|
||||||
|
}
|
||||||
|
|
||||||
|
MapPrototype::~MapPrototype()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Map* MapPrototype::typed_this(VM& vm, GlobalObject& global_object)
|
||||||
|
{
|
||||||
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return {};
|
||||||
|
if (!is<Map>(this_object)) {
|
||||||
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Map");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return static_cast<Map*>(this_object);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 24.1.3.10 get Map.prototype.size, https://tc39.es/ecma262/#sec-get-map.prototype.size
|
||||||
|
JS_DEFINE_NATIVE_GETTER(MapPrototype::size_getter)
|
||||||
|
{
|
||||||
|
auto* map = typed_this(vm, global_object);
|
||||||
|
if (!map)
|
||||||
|
return {};
|
||||||
|
return Value(map->entries().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
Userland/Libraries/LibJS/Runtime/MapPrototype.h
Normal file
27
Userland/Libraries/LibJS/Runtime/MapPrototype.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/Map.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
class MapPrototype final : public Object {
|
||||||
|
JS_OBJECT(MapPrototype, Object);
|
||||||
|
|
||||||
|
public:
|
||||||
|
MapPrototype(GlobalObject&);
|
||||||
|
virtual void initialize(GlobalObject&) override;
|
||||||
|
virtual ~MapPrototype() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Map* typed_this(VM&, GlobalObject&);
|
||||||
|
|
||||||
|
JS_DECLARE_NATIVE_GETTER(size_getter);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
45
Userland/Libraries/LibJS/Tests/builtins/Map/Map.js
Normal file
45
Userland/Libraries/LibJS/Tests/builtins/Map/Map.js
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
test("constructor properties", () => {
|
||||||
|
expect(Map).toHaveLength(0);
|
||||||
|
expect(Map.name).toBe("Map");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("errors", () => {
|
||||||
|
test("invalid array iterators", () => {
|
||||||
|
[-100, Infinity, NaN, {}, 152n].forEach(value => {
|
||||||
|
expect(() => {
|
||||||
|
new Map(value);
|
||||||
|
}).toThrowWithMessage(TypeError, "is not iterable");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test("invalid iterator entries", () => {
|
||||||
|
expect(() => {
|
||||||
|
new Map([1, 2, 3]);
|
||||||
|
}).toThrowWithMessage(TypeError, "Iterator value 1 is not an object");
|
||||||
|
});
|
||||||
|
test("called without new", () => {
|
||||||
|
expect(() => {
|
||||||
|
Map();
|
||||||
|
}).toThrowWithMessage(TypeError, "Map constructor must be called with 'new'");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("normal behavior", () => {
|
||||||
|
test("typeof", () => {
|
||||||
|
expect(typeof new Map()).toBe("object");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("constructor with single entries array argument", () => {
|
||||||
|
var a = new Map([
|
||||||
|
["a", 0],
|
||||||
|
["b", 1],
|
||||||
|
["c", 2],
|
||||||
|
]);
|
||||||
|
expect(a instanceof Map).toBeTrue();
|
||||||
|
expect(a).toHaveSize(3);
|
||||||
|
var seen = [false, false, false];
|
||||||
|
a.forEach(v => {
|
||||||
|
seen[v] = true;
|
||||||
|
});
|
||||||
|
expect(seen[0] && seen[1] && seen[2]);
|
||||||
|
});
|
||||||
|
});
|
|
@ -27,6 +27,7 @@
|
||||||
#include <LibJS/Runtime/Error.h>
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/Function.h>
|
#include <LibJS/Runtime/Function.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/Map.h>
|
||||||
#include <LibJS/Runtime/NativeFunction.h>
|
#include <LibJS/Runtime/NativeFunction.h>
|
||||||
#include <LibJS/Runtime/NumberObject.h>
|
#include <LibJS/Runtime/NumberObject.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
|
@ -278,6 +279,24 @@ static void print_proxy_object(const JS::Object& object, HashTable<JS::Object*>&
|
||||||
print_value(&proxy_object.handler(), seen_objects);
|
print_value(&proxy_object.handler(), seen_objects);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void print_map(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
||||||
|
{
|
||||||
|
auto& map = static_cast<const JS::Map&>(object);
|
||||||
|
auto& entries = map.entries();
|
||||||
|
print_type("Map");
|
||||||
|
out(" {{");
|
||||||
|
bool first = true;
|
||||||
|
for (auto& entry : entries) {
|
||||||
|
print_separator(first);
|
||||||
|
print_value(entry.key, seen_objects);
|
||||||
|
out(" => ");
|
||||||
|
print_value(entry.value, seen_objects);
|
||||||
|
}
|
||||||
|
if (!first)
|
||||||
|
out(" ");
|
||||||
|
out("}}");
|
||||||
|
}
|
||||||
|
|
||||||
static void print_set(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
static void print_set(const JS::Object& object, HashTable<JS::Object*>& seen_objects)
|
||||||
{
|
{
|
||||||
auto& set = static_cast<const JS::Set&>(object);
|
auto& set = static_cast<const JS::Set&>(object);
|
||||||
|
@ -415,6 +434,8 @@ static void print_value(JS::Value value, HashTable<JS::Object*>& seen_objects)
|
||||||
return print_error(object, seen_objects);
|
return print_error(object, seen_objects);
|
||||||
if (is<JS::RegExpObject>(object))
|
if (is<JS::RegExpObject>(object))
|
||||||
return print_regexp_object(object, seen_objects);
|
return print_regexp_object(object, seen_objects);
|
||||||
|
if (is<JS::Map>(object))
|
||||||
|
return print_map(object, seen_objects);
|
||||||
if (is<JS::Set>(object))
|
if (is<JS::Set>(object))
|
||||||
return print_set(object, seen_objects);
|
return print_set(object, seen_objects);
|
||||||
if (is<JS::ProxyObject>(object))
|
if (is<JS::ProxyObject>(object))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue