Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/codegen/types/objects/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ export class ClassGenerator {
}
}
for (let i = 0; i < classNode.fields.length; i++) {
// Static fields are globals, not part of the instance struct
if (classNode.fields[i].isStatic) continue;
if ((classNode.fields[i] as ClassField).isStatic) continue;
allFields.push(classNode.fields[i]);
}
return allFields;
Expand Down Expand Up @@ -524,7 +523,7 @@ export class ClassGenerator {

// Emit static fields as LLVM globals
for (let fi = 0; fi < classNode.fields.length; fi++) {
const field = classNode.fields[fi];
const field = classNode.fields[fi] as ClassField;
if (!field || !field.isStatic) continue;
const globalName = `@${this.ctx.mangleUserName(className)}_${field.name}`;
const llvmType = this.fieldToLlvmType(field);
Expand Down Expand Up @@ -686,7 +685,7 @@ export class ClassGenerator {
}

for (let fi = 0; fi < fields.length; fi++) {
const classField = fields[fi];
const classField = fields[fi] as ClassField;
if (!classField) continue;
if (!classField.initializer) continue;
const initType = classField.initializer.type;
Expand Down Expand Up @@ -809,7 +808,7 @@ export class ClassGenerator {
this.ctx.setThisPointer(objPtr);
this.ctx.setCurrentClassName(className);
for (let fi = 0; fi < classFields.length; fi++) {
const cf = classFields[fi];
const cf = classFields[fi] as ClassField;
if (!cf) continue;
if (!cf.initializer) continue;
const initType = cf.initializer.type;
Expand Down
15 changes: 9 additions & 6 deletions src/parser-native/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2796,7 +2796,13 @@ function transformClassDeclaration(node: TreeSitterNode): ClassNode | null {
}
}
if (alreadyExists === false) {
const newField: ClassField = { name: propName, fieldType, tsType };
const newField: ClassField = {
name: propName,
fieldType,
tsType,
initializer: undefined,
isStatic: false,
};
fields.push(newField);
}
}
Expand Down Expand Up @@ -2872,11 +2878,8 @@ function transformClassField(node: TreeSitterNode): ClassField | null {
}
}

const result: ClassField = { name, fieldType, tsType };
if (isStatic) result.isStatic = true;
if (valueNode) {
result.initializer = transformExpression(valueNode);
}
const initializer = valueNode ? transformExpression(valueNode) : undefined;
const result: ClassField = { name, fieldType, tsType, initializer, isStatic };
return result;
}

Expand Down
18 changes: 9 additions & 9 deletions src/parser-ts/handlers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,8 @@ function transformPropertyDeclaration(
}

const isStatic = node.modifiers?.some((m) => m.kind === ts.SyntaxKind.StaticKeyword) ?? false;
const result: ClassField = { name, fieldType, tsType };
if (node.initializer) {
result.initializer = transformExpression(node.initializer, checker);
}
if (isStatic) {
result.isStatic = true;
}
return result;
const initializer = node.initializer ? transformExpression(node.initializer, checker) : undefined;
return { name, fieldType, tsType, initializer, isStatic };
}

function transformMethodDeclaration(
Expand Down Expand Up @@ -298,7 +292,13 @@ function transformConstructorDeclaration(
tsType = typeStr;
}
}
parameterProperties.push({ name, fieldType, tsType });
parameterProperties.push({
name,
fieldType,
tsType,
initializer: undefined,
isStatic: false,
});
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/arrays/boolean-array-basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const flags: boolean[] = [true, false, true];
if (flags[0] === true && flags[1] === false && flags[2] === true) {
console.log("TEST_PASSED");
}
6 changes: 6 additions & 0 deletions tests/fixtures/arrays/boolean-array-index-assign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const flags: boolean[] = [false, false, false, false];
flags[0] = true;
flags[2] = true;
if (flags[0] === true && flags[1] === false && flags[2] === true && flags[3] === false) {
console.log("TEST_PASSED");
}
7 changes: 7 additions & 0 deletions tests/fixtures/arrays/boolean-array-push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const flags: boolean[] = [];
flags.push(true);
flags.push(false);
flags.push(true);
if (flags.length === 3 && flags[0] === true && flags[1] === false) {
console.log("TEST_PASSED");
}
39 changes: 39 additions & 0 deletions tests/fixtures/arrays/boolean-array-sieve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const limit = 50;
const sieve: boolean[] = [];
let i = 0;
while (i < limit) {
sieve.push(true);
i = i + 1;
}
sieve[0] = false;
sieve[1] = false;

i = 2;
while (i * i < limit) {
if (sieve[i]) {
let j = i * i;
while (j < limit) {
sieve[j] = false;
j = j + i;
}
}
i = i + 1;
}

let primes = "";
i = 2;
while (i < limit) {
if (sieve[i]) {
if (primes.length > 0) {
primes = primes + ",";
}
primes = primes + i.toString();
}
i = i + 1;
}

if (primes === "2,3,5,7,11,13,17,19,23,29,31,37,41,43,47") {
console.log("TEST_PASSED");
} else {
console.log("FAIL: " + primes);
}
2 changes: 0 additions & 2 deletions tests/fixtures/classes/class-field-init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @test-skip
// native compiler's tree-sitter parser doesn't populate field initializers yet
class Config {
maxRetries: number = 5;
name: string = "default";
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/control-flow/switch-string-toplevel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @test-skip
// native switch string matching fails in self-hosted stages on linux
const s = "b";
switch (s) {
case "a":
Expand Down
Loading