mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
LibJS: Add all of the WeakMap.prototype methods (delete, get, has, set)
This commit is contained in:
parent
39554f3787
commit
77c2db4183
9 changed files with 182 additions and 0 deletions
|
@ -45,6 +45,19 @@ TESTJS_GLOBAL_FUNCTION(get_weak_set_size, getWeakSetSize)
|
||||||
return JS::Value(weak_set->values().size());
|
return JS::Value(weak_set->values().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TESTJS_GLOBAL_FUNCTION(get_weak_map_size, getWeakMapSize)
|
||||||
|
{
|
||||||
|
auto* object = vm.argument(0).to_object(global_object);
|
||||||
|
if (!object)
|
||||||
|
return {};
|
||||||
|
if (!is<JS::WeakMap>(object)) {
|
||||||
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WeakMap");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
auto* weak_map = static_cast<JS::WeakMap*>(object);
|
||||||
|
return JS::Value(weak_map->values().size());
|
||||||
|
}
|
||||||
|
|
||||||
TESTJS_RUN_FILE_FUNCTION(const String& test_file, JS::Interpreter&)
|
TESTJS_RUN_FILE_FUNCTION(const String& test_file, JS::Interpreter&)
|
||||||
{
|
{
|
||||||
if (!test262_parser_tests)
|
if (!test262_parser_tests)
|
||||||
|
|
|
@ -18,6 +18,12 @@ void WeakMapPrototype::initialize(GlobalObject& global_object)
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
auto& vm = this->vm();
|
||||||
Object::initialize(global_object);
|
Object::initialize(global_object);
|
||||||
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
|
|
||||||
|
define_native_function(vm.names.delete_, delete_, 1, attr);
|
||||||
|
define_native_function(vm.names.get, get, 1, attr);
|
||||||
|
define_native_function(vm.names.has, has, 1, attr);
|
||||||
|
define_native_function(vm.names.set, set, 2, attr);
|
||||||
|
|
||||||
define_property(vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), vm.names.WeakMap), Attribute::Configurable);
|
define_property(vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), vm.names.WeakMap), Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
@ -38,4 +44,56 @@ WeakMap* WeakMapPrototype::typed_this(VM& vm, GlobalObject& global_object)
|
||||||
return static_cast<WeakMap*>(this_object);
|
return static_cast<WeakMap*>(this_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::delete_)
|
||||||
|
{
|
||||||
|
auto* weak_map = typed_this(vm, global_object);
|
||||||
|
if (!weak_map)
|
||||||
|
return {};
|
||||||
|
auto value = vm.argument(0);
|
||||||
|
if (!value.is_object())
|
||||||
|
return Value(false);
|
||||||
|
return Value(weak_map->values().remove(&value.as_object()));
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::get)
|
||||||
|
{
|
||||||
|
auto* weak_map = typed_this(vm, global_object);
|
||||||
|
if (!weak_map)
|
||||||
|
return {};
|
||||||
|
auto value = vm.argument(0);
|
||||||
|
if (!value.is_object())
|
||||||
|
return js_undefined();
|
||||||
|
auto& values = weak_map->values();
|
||||||
|
auto result = values.find(&value.as_object());
|
||||||
|
if (result == values.end())
|
||||||
|
return js_undefined();
|
||||||
|
return result->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::has)
|
||||||
|
{
|
||||||
|
auto* weak_map = typed_this(vm, global_object);
|
||||||
|
if (!weak_map)
|
||||||
|
return {};
|
||||||
|
auto value = vm.argument(0);
|
||||||
|
if (!value.is_object())
|
||||||
|
return Value(false);
|
||||||
|
auto& values = weak_map->values();
|
||||||
|
return Value(values.find(&value.as_object()) != values.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(WeakMapPrototype::set)
|
||||||
|
{
|
||||||
|
auto* weak_map = typed_this(vm, global_object);
|
||||||
|
if (!weak_map)
|
||||||
|
return {};
|
||||||
|
auto value = vm.argument(0);
|
||||||
|
if (!value.is_object()) {
|
||||||
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, value.to_string_without_side_effects());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
weak_map->values().set(&value.as_object(), vm.argument(1));
|
||||||
|
return weak_map;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,11 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static WeakMap* typed_this(VM&, GlobalObject&);
|
static WeakMap* typed_this(VM&, GlobalObject&);
|
||||||
|
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(delete_);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(get);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(has);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(set);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
34
Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.js
Normal file
34
Userland/Libraries/LibJS/Tests/builtins/WeakMap/WeakMap.js
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
test("constructor properties", () => {
|
||||||
|
expect(WeakMap).toHaveLength(0);
|
||||||
|
expect(WeakMap.name).toBe("WeakMap");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("errors", () => {
|
||||||
|
test("invalid array iterators", () => {
|
||||||
|
[-100, Infinity, NaN, {}, 152n].forEach(value => {
|
||||||
|
expect(() => {
|
||||||
|
new WeakMap(value);
|
||||||
|
}).toThrowWithMessage(TypeError, "is not iterable");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
test("called without new", () => {
|
||||||
|
expect(() => {
|
||||||
|
WeakMap();
|
||||||
|
}).toThrowWithMessage(TypeError, "WeakMap constructor must be called with 'new'");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("normal behavior", () => {
|
||||||
|
test("typeof", () => {
|
||||||
|
expect(typeof new WeakMap()).toBe("object");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("constructor with single array of entries argument", () => {
|
||||||
|
var a = new WeakMap([
|
||||||
|
[{ a: 1 }, 1],
|
||||||
|
[{ a: 2 }, 2],
|
||||||
|
[{ a: 3 }, 3],
|
||||||
|
]);
|
||||||
|
expect(a instanceof WeakMap).toBeTrue();
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,13 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(WeakMap.prototype.delete).toHaveLength(1);
|
||||||
|
|
||||||
|
var original = [
|
||||||
|
[{ a: 1 }, 1],
|
||||||
|
[{ a: 2 }, 2],
|
||||||
|
[{ a: 3 }, 3],
|
||||||
|
];
|
||||||
|
const weakMap = new WeakMap(original);
|
||||||
|
expect(weakMap.delete(original[0][0])).toBeTrue();
|
||||||
|
expect(weakMap.delete(original[0][0])).toBeFalse();
|
||||||
|
expect(weakMap.delete(null)).toBeFalse();
|
||||||
|
});
|
|
@ -0,0 +1,12 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(WeakMap.prototype.get).toHaveLength(1);
|
||||||
|
|
||||||
|
var original = [
|
||||||
|
[{ a: 1 }, 1],
|
||||||
|
[{ a: 2 }, 2],
|
||||||
|
[{ a: 3 }, 3],
|
||||||
|
];
|
||||||
|
const weakMap = new WeakMap(original);
|
||||||
|
expect(weakMap.get(original[0][0])).toBe(original[0][1]);
|
||||||
|
expect(weakMap.get(null)).toBe(undefined);
|
||||||
|
});
|
|
@ -0,0 +1,16 @@
|
||||||
|
test("length is 1", () => {
|
||||||
|
expect(WeakMap.prototype.has).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
var original = [
|
||||||
|
[{ a: 1 }, 1],
|
||||||
|
[{ a: 2 }, 2],
|
||||||
|
[{ a: 3 }, 3],
|
||||||
|
];
|
||||||
|
var weakMap = new WeakMap(original);
|
||||||
|
|
||||||
|
expect(new WeakMap().has()).toBeFalse();
|
||||||
|
expect(weakMap.has(original[0][0])).toBeTrue();
|
||||||
|
expect(weakMap.has({ a: 1 })).toBeFalse();
|
||||||
|
});
|
|
@ -0,0 +1,30 @@
|
||||||
|
test("basic functionality", () => {
|
||||||
|
expect(WeakMap.prototype.set).toHaveLength(2);
|
||||||
|
|
||||||
|
const weakMap = new WeakMap([
|
||||||
|
[{ a: 1 }, 1],
|
||||||
|
[{ a: 2 }, 2],
|
||||||
|
[{ a: 3 }, 3],
|
||||||
|
]);
|
||||||
|
expect(weakMap.set({ a: 4 }, 4)).toBe(weakMap);
|
||||||
|
expect(weakMap.set({ a: 1 }, 2)).toBe(weakMap);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("invalid values", () => {
|
||||||
|
const weakMap = new WeakMap();
|
||||||
|
[-100, Infinity, NaN, "hello", 152n].forEach(value => {
|
||||||
|
expect(() => {
|
||||||
|
weakMap.set(value, value);
|
||||||
|
}).toThrowWithMessage(TypeError, "is not an object");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("automatic removal of garbage-collected values", () => {
|
||||||
|
const weakMap = new WeakMap();
|
||||||
|
{
|
||||||
|
expect(weakMap.set({ a: 1 }, 1)).toBe(weakMap);
|
||||||
|
expect(getWeakMapSize(weakMap)).toBe(1);
|
||||||
|
}
|
||||||
|
gc();
|
||||||
|
expect(getWeakMapSize(weakMap)).toBe(0);
|
||||||
|
});
|
|
@ -27,6 +27,7 @@
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/JSONObject.h>
|
#include <LibJS/Runtime/JSONObject.h>
|
||||||
#include <LibJS/Runtime/TypedArray.h>
|
#include <LibJS/Runtime/TypedArray.h>
|
||||||
|
#include <LibJS/Runtime/WeakMap.h>
|
||||||
#include <LibJS/Runtime/WeakSet.h>
|
#include <LibJS/Runtime/WeakSet.h>
|
||||||
#include <LibTest/Results.h>
|
#include <LibTest/Results.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
Loading…
Add table
Reference in a new issue