
This patch adds two macros to declare per-type allocators: - JS_DECLARE_ALLOCATOR(TypeName) - JS_DEFINE_ALLOCATOR(TypeName) When used, they add a type-specific CellAllocator that the Heap will delegate allocation requests to. The result of this is that GC objects of the same type always end up within the same HeapBlock, drastically reducing the ability to perform type confusion attacks. It also improves HeapBlock utilization, since each block now has cells sized exactly to the type used within that block. (Previously we only had a handful of block sizes available, and most GC allocations ended up with a large amount of slack in their tails.) There is a small performance hit from this, but I'm sure we can make up for it elsewhere. Note that the old size-based allocators still exist, and we fall back to them for any type that doesn't have its own CellAllocator.
89 lines
3.3 KiB
C++
89 lines
3.3 KiB
C++
/*
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
#include <LibJS/Runtime/Intrinsics.h>
|
|
#include <LibJS/Runtime/Iterator.h>
|
|
#include <LibJS/Runtime/IteratorConstructor.h>
|
|
#include <LibJS/Runtime/IteratorPrototype.h>
|
|
#include <LibJS/Runtime/Realm.h>
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
|
namespace JS {
|
|
|
|
JS_DEFINE_ALLOCATOR(IteratorConstructor);
|
|
|
|
// 3.1.1.1 The Iterator Constructor, https://tc39.es/proposal-iterator-helpers/#sec-iterator-constructor
|
|
IteratorConstructor::IteratorConstructor(Realm& realm)
|
|
: Base(realm.vm().names.Iterator.as_string(), realm.intrinsics().function_prototype())
|
|
{
|
|
}
|
|
|
|
void IteratorConstructor::initialize(Realm& realm)
|
|
{
|
|
Base::initialize(realm);
|
|
|
|
auto& vm = this->vm();
|
|
|
|
// 3.1.1.2.1 Iterator.prototype, https://tc39.es/proposal-iterator-helpers/#sec-iterator.prototype
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().iterator_prototype(), 0);
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
|
define_native_function(realm, vm.names.from, from, 1, attr);
|
|
|
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
|
}
|
|
|
|
// 3.1.1.1.1 Iterator ( ), https://tc39.es/proposal-iterator-helpers/#sec-iterator
|
|
ThrowCompletionOr<Value> IteratorConstructor::call()
|
|
{
|
|
auto& vm = this->vm();
|
|
|
|
// 1. If NewTarget is undefined or the active function object, throw a TypeError exception.
|
|
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Iterator");
|
|
}
|
|
|
|
// 3.1.1.1.1 Iterator ( ), https://tc39.es/proposal-iterator-helpers/#sec-iterator
|
|
ThrowCompletionOr<NonnullGCPtr<Object>> IteratorConstructor::construct(FunctionObject& new_target)
|
|
{
|
|
auto& vm = this->vm();
|
|
|
|
// 1. If NewTarget is undefined or the active function object, throw a TypeError exception.
|
|
if (&new_target == this)
|
|
return vm.throw_completion<TypeError>(ErrorType::ClassIsAbstract, "Iterator");
|
|
|
|
// 2. Return ? OrdinaryCreateFromConstructor(NewTarget, "%Iterator.prototype%").
|
|
return TRY(ordinary_create_from_constructor<Iterator>(vm, new_target, &Intrinsics::iterator_prototype));
|
|
}
|
|
|
|
// 3.1.1.2.2 Iterator.from ( O ), https://tc39.es/proposal-iterator-helpers/#sec-iterator.from
|
|
JS_DEFINE_NATIVE_FUNCTION(IteratorConstructor::from)
|
|
{
|
|
auto& realm = *vm.current_realm();
|
|
|
|
auto object = vm.argument(0);
|
|
|
|
// 1. Let iteratorRecord be ? GetIteratorFlattenable(O, iterate-strings).
|
|
auto iterator_record = TRY(get_iterator_flattenable(vm, object, StringHandling::IterateStrings));
|
|
|
|
// 2. Let hasInstance be ? OrdinaryHasInstance(%Iterator%, iteratorRecord.[[Iterator]]).
|
|
auto has_instance = TRY(ordinary_has_instance(vm, iterator_record.iterator, realm.intrinsics().iterator_constructor()));
|
|
|
|
// 3. If hasInstance is true, then
|
|
if (has_instance.is_boolean() && has_instance.as_bool()) {
|
|
// a. Return iteratorRecord.[[Iterator]].
|
|
return iterator_record.iterator;
|
|
}
|
|
|
|
// 4. Let wrapper be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, « [[Iterated]] »).
|
|
// 5. Set wrapper.[[Iterated]] to iteratorRecord.
|
|
auto wrapper = Iterator::create(realm, realm.intrinsics().wrap_for_valid_iterator_prototype(), move(iterator_record));
|
|
|
|
// 6. Return wrapper.
|
|
return wrapper;
|
|
}
|
|
|
|
}
|