Skip to content

Commit

Permalink
fix _newInstance to create instance from builtin classes
Browse files Browse the repository at this point in the history
  • Loading branch information
khchen committed Aug 11, 2022
1 parent 01d4530 commit 257d16b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/core/public.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,18 @@ static Var _newInstance(PKVM* vm, Class* cls, int argc, Var* argv) {
Var instance = preConstructSelf(vm, cls);
if (VM_HAS_ERROR(vm)) return VAR_NULL;

if (IS_OBJ(instance)) vmPushTempRef(vm, AS_OBJ(instance)); // instance.
bool pushed = false;
if (IS_OBJ(instance)) {
vmPushTempRef(vm, AS_OBJ(instance)); // instance.
pushed = true;
}

Closure* ctor = getMagicMethod(cls, METHOD_INIT);
if (ctor != NULL) vmCallMethod(vm, instance, ctor, argc, argv, NULL);
if (IS_OBJ(instance)) vmPopTempRef(vm); // instance.
Closure* init = getMagicMethod(cls, METHOD_INIT);
if (init != NULL) {
// for builtin classes, preConstructSelf returns null,
// and instance is returned by _init.
vmCallMethod(vm, instance, init, argc, argv,
IS_NULL(instance) ? &instance : NULL);

return instance;
}
Expand Down

0 comments on commit 257d16b

Please sign in to comment.