Skip to content

Commit

Permalink
Fix bug in callConstructor with class
Browse files Browse the repository at this point in the history
Signed-off-by: Seonghyun Kim <sh8281.kim@samsung.com>
  • Loading branch information
ksh8281 authored and clover2123 committed Sep 19, 2024
1 parent 91eef62 commit 32f1ebb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/interpreter/ByteCodeInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4165,10 +4165,14 @@ NEVER_INLINE void InterpreterSlowPath::callFunctionComplexCase(ExecutionState& s
// using this value on stack is trick for callConstructor util
if (stackStorage[0].isPointerValue() && stackStorage[0].asPointerValue()) {
result = stackStorage[0].asObject();
Object* proto = Object::getPrototypeFromConstructor(state, newTarget, [](ExecutionState& state, Context* constructorRealm) -> Object* {
return constructorRealm->globalObject()->objectPrototype();
});
result.asObject()->setPrototype(state, proto);
if (registerFile[code->m_calleeIndex].isObject() && registerFile[code->m_calleeIndex].asObject()->isScriptClassConstructorFunctionObject()) {
Object::callConstructor(state, registerFile[code->m_calleeIndex], result.asObject(), argc, argv, newTarget);
} else {
Object* proto = Object::getPrototypeFromConstructor(state, newTarget, [](ExecutionState& state, Context* constructorRealm) -> Object* {
return constructorRealm->globalObject()->objectPrototype();
});
result.asObject()->setPrototype(state, proto);
}
} else {
result = Object::construct(state, registerFile[code->m_calleeIndex], argc, argv, newTarget);
}
Expand Down
14 changes: 14 additions & 0 deletions test/cctest/testapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,20 @@ TEST(FunctionObject, CallConsturctor)

return ValueRef::createUndefined();
});

eval(g_context.get(), StringRef::createFromASCII("class callconstructortest3 extends Number { constructor() { super(); this.baseProperty = 123 } };"));
eval(g_context.get(), StringRef::createFromASCII("class callconstructortest4 extends callconstructortest3{ #pri; constructor() { super(); this.#pri = 123; } read() { return this.#pri + this.baseProperty; } }; this.testClass = callconstructortest4;"));

Evaluator::execute(g_context.get(), [](ExecutionStateRef* state) -> ValueRef* {
auto testClass = state->context()->globalObject()->get(state, StringRef::createFromASCII("testClass"));
ObjectRef* obj = ObjectRef::create(state);
testClass->callConstructor(state, obj, 0, nullptr);
EXPECT_TRUE(obj->get(state, StringRef::createFromASCII("read"))->call(state, obj, 0, nullptr)->asNumber() == 123 * 2);
EXPECT_TRUE(obj->instanceOf(state, testClass));
EXPECT_TRUE(obj->instanceOf(state, state->context()->globalObject()->number()));

return ValueRef::createUndefined();
});
}

TEST(ObjectTemplate, Basic1)
Expand Down

0 comments on commit 32f1ebb

Please sign in to comment.