Skip to content

Commit d8f5971

Browse files
committed
LibJS: Allow constructing generator from function with null "prototype"
Fixes 4 test262 tests and simplifies some upcoming stuff.
1 parent acce880 commit d8f5971

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

Libraries/LibJS/Runtime/AsyncGenerator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ ThrowCompletionOr<GC::Ref<AsyncGenerator>> AsyncGenerator::create(Realm& realm,
2424
// This is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
2525
static Bytecode::PropertyLookupCache cache;
2626
auto generating_function_prototype = TRY(generating_function->get(vm.names.prototype, cache));
27-
auto generating_function_prototype_object = TRY(generating_function_prototype.to_object(vm));
27+
GC::Ptr<Object> generating_function_prototype_object = nullptr;
28+
if (!generating_function_prototype.is_nullish())
29+
generating_function_prototype_object = MUST(generating_function_prototype.to_object(vm));
2830
auto object = realm.create<AsyncGenerator>(realm, generating_function_prototype_object, move(execution_context));
2931
object->m_generating_function = generating_function;
3032
object->m_previous_value = initial_value;
3133
return object;
3234
}
3335

34-
AsyncGenerator::AsyncGenerator(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext> context)
35-
: Object(ConstructWithPrototypeTag::Tag, prototype)
36+
AsyncGenerator::AsyncGenerator(Realm& realm, Object* prototype, NonnullOwnPtr<ExecutionContext> context)
37+
: Object(realm, prototype)
3638
, m_async_generator_context(move(context))
3739
{
3840
}

Libraries/LibJS/Runtime/AsyncGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class AsyncGenerator final : public Object {
4444
Optional<String> const& generator_brand() const { return m_generator_brand; }
4545

4646
private:
47-
AsyncGenerator(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext>);
47+
AsyncGenerator(Realm&, Object* prototype, NonnullOwnPtr<ExecutionContext>);
4848

4949
virtual void visit_edges(Cell::Visitor&) override;
5050

Libraries/LibJS/Runtime/GeneratorObject.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@ ThrowCompletionOr<GC::Ref<GeneratorObject>> GeneratorObject::create(Realm& realm
3232
static Bytecode::PropertyLookupCache cache;
3333
generating_function_prototype = TRY(generating_function->get(vm.names.prototype, cache));
3434
}
35-
auto generating_function_prototype_object = TRY(generating_function_prototype.to_object(vm));
35+
GC::Ptr<Object> generating_function_prototype_object = nullptr;
36+
if (!generating_function_prototype.is_nullish())
37+
generating_function_prototype_object = MUST(generating_function_prototype.to_object(vm));
3638
auto object = realm.create<GeneratorObject>(realm, generating_function_prototype_object, move(execution_context));
3739
object->m_generating_function = generating_function;
3840
object->m_previous_value = initial_value;
3941
return object;
4042
}
4143

42-
GeneratorObject::GeneratorObject(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext> context, Optional<StringView> generator_brand)
43-
: Object(ConstructWithPrototypeTag::Tag, prototype)
44+
GeneratorObject::GeneratorObject(Realm& realm, Object* prototype, NonnullOwnPtr<ExecutionContext> context, Optional<StringView> generator_brand)
45+
: Object(realm, prototype)
4446
, m_execution_context(move(context))
4547
, m_generator_brand(move(generator_brand))
4648
{

Libraries/LibJS/Runtime/GeneratorObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class GeneratorObject : public Object {
4646
void set_generator_state(GeneratorState generator_state) { m_generator_state = generator_state; }
4747

4848
protected:
49-
GeneratorObject(Realm&, Object& prototype, NonnullOwnPtr<ExecutionContext>, Optional<StringView> generator_brand = {});
49+
GeneratorObject(Realm&, Object* prototype, NonnullOwnPtr<ExecutionContext>, Optional<StringView> generator_brand = {});
5050

5151
ThrowCompletionOr<GeneratorState> validate(VM&, Optional<StringView> const& generator_brand);
5252
virtual ThrowCompletionOr<IterationResult> execute(VM&, JS::Completion const& completion);

Libraries/LibJS/Runtime/IteratorHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ThrowCompletionOr<GC::Ref<IteratorHelper>> IteratorHelper::create(Realm& realm,
1919
}
2020

2121
IteratorHelper::IteratorHelper(Realm& realm, Object& prototype, GC::Ref<IteratorRecord> underlying_iterator, GC::Ref<Closure> closure, GC::Ptr<AbruptClosure> abrupt_closure)
22-
: GeneratorObject(realm, prototype, realm.vm().running_execution_context().copy(), "Iterator Helper"sv)
22+
: GeneratorObject(realm, &prototype, realm.vm().running_execution_context().copy(), "Iterator Helper"sv)
2323
, m_underlying_iterator(move(underlying_iterator))
2424
, m_closure(closure)
2525
, m_abrupt_closure(abrupt_closure)

0 commit comments

Comments
 (0)