Skip to content

Commit

Permalink
fix object literal instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Aug 5, 2022
1 parent 82001c5 commit 1af7ceb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/checker/types2.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,14 @@ namespace ts::vm2 {
}

/**
* Returns true of there is only one child
* Returns true if there is only one child
*/
bool singleChild() {
return type && ((TypeRef *) type)->next == nullptr;
}

/**
* Returns true of there is only one child
* Returns the type if there is only one child
*/
Type *child() {
return type ? ((TypeRef *) type)->type : nullptr;
Expand Down
42 changes: 33 additions & 9 deletions src/checker/vm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ namespace ts::vm2 {
case OP::CheckBody: {
const auto address = subroutine->parseUint32();
auto expectedType = stack[sp - 1];
report("Nope");
break;
}
case OP::InferBody: {
Expand Down Expand Up @@ -1441,25 +1442,48 @@ namespace ts::vm2 {
}
case OP::ObjectLiteral: {
const auto size = subroutine->parseUint16();
auto type = allocate(TypeKind::ObjectLiteral);
auto type = allocate(TypeKind::ObjectLiteral, hash::const_hash("object"));
if (!size) {
push(type);
break;
}

type->size = size;
auto types = subroutine->pop(size);
if (size<5) {
type->type = useAsRef(types[0]);
auto current = (TypeRef *) type->type;
for (unsigned int i = 1; i<size; i++) {
current->next = useAsRef(types[i]);
current = current->next;
}
auto first = types[0];

if (first->kind == TypeKind::Rest) {
TypeRef *current = nullptr;
//todo: check if ObjectLiteral, otherwise report error
forEachChild(first, [&type, &current](Type *child, auto) {
if (current) {
current = current->next = useAsRef(child);
} else {
type->type = current = useAsRef(child);
}
});
gc(first);
} else {
type->type = useAsRef(first);
}

auto current = (TypeRef *) type->type;
for (unsigned int i = 1; i<size; i++) {
if (types[i]->kind == TypeKind::Rest) {
//todo: check if ObjectLiteral, otherwise report error
forEachChild(types[i], [&current](Type *child, auto) {
current = current->next = useAsRef(child);
});
gc(types[i]);
} else {
current = (current->next = useAsRef(types[i]));
}
}

if (size>5) {
type->children = allocateRefs(size);
for (unsigned int i = 0; i<size; i++) {
addHashChild(type, types[i], size);
addHashChildWithoutRefCounter(type, types[i], size);
}
}
push(type);
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_vm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ TEST_CASE("gcTuple") {
TEST_CASE("gcObject") {
ts::checker::Program program;
for (auto i = 0; i<10; i++) {
program.pushOp(OP::StringLiteral);
program.pushStorage("a");
program.pushOp(OP::StringLiteral);
program.pushStorage("foo1");
program.pushOp(OP::StringLiteral);
program.pushStorage("a");
program.pushOp(OP::PropertySignature);
}
program.pushOp(OP::ObjectLiteral);
Expand Down
9 changes: 3 additions & 6 deletions tests/bench.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {CompilerOptions, createCompilerHost, createProgram, getPreEmitDiagnostics} from "typescript";

const code = `
class MyDate {
static now(): number {
return 0;
}
function doIt(): string {
return 1;
}
const now: number = MyDate.now();
const now2: string = MyDate.now();
doIt();
`;

const options: CompilerOptions = {
Expand Down

0 comments on commit 1af7ceb

Please sign in to comment.