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
10 changes: 10 additions & 0 deletions c_bridges/yyjson-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ void *csyyjson_mut_arr_add_obj(void *doc, void *arr) {
return (void *)yyjson_mut_arr_add_obj((yyjson_mut_doc *)doc, (yyjson_mut_val *)arr);
}

void csyyjson_arr_add_str(void *doc, void *arr, const char *val) {
if (!doc || !arr) return;
yyjson_mut_arr_add_str((yyjson_mut_doc *)doc, (yyjson_mut_val *)arr, val ? val : "");
}

void csyyjson_arr_add_num(void *doc, void *arr, double val) {
if (!doc || !arr) return;
yyjson_mut_arr_add_real((yyjson_mut_doc *)doc, (yyjson_mut_val *)arr, val);
}

void *csyyjson_mut_get_root(void *doc) {
if (!doc) return NULL;
return (void *)yyjson_mut_doc_get_root((yyjson_mut_doc *)doc);
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ export class RuntimeGenerator {
ir += "declare i8* @csyyjson_create_arr()\n";
ir += "declare i8* @csyyjson_mut_arr_add_obj(i8*, i8*)\n";
ir += "declare i8* @csyyjson_obj_add_obj(i8*, i8*, i8*)\n";
ir += "declare void @csyyjson_arr_add_str(i8*, i8*, i8*)\n";
ir += "declare void @csyyjson_arr_add_num(i8*, i8*, double)\n";
ir += "\n";

ir += "define i32 @csyyjson_get_num_as_int(i8* %item) {\n";
Expand Down
115 changes: 114 additions & 1 deletion src/codegen/stdlib/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,42 @@ export class JsonGenerator {
if (elementType) {
return this.stringifyObjectArray(arg, params, elementType, spaces);
}
if (this.ctx.symbolTable.isStringArray(varNode.name)) {
return this.stringifyStringArray(arg, params, spaces);
}
if (this.ctx.symbolTable.isNumberArray(varNode.name)) {
return this.stringifyNumberArray(arg, params, spaces);
}
}

if (this.ctx.isStringArrayExpression(arg)) {
return this.stringifyStringArray(arg, params, spaces);
}

const interfaceType = this.resolveInterfaceType(arg);
if (interfaceType) {
return this.stringifyInterface(arg, params, interfaceType, spaces);
}

return this.stringifyNumber(arg, params);
if (arg.type === "number" || arg.type === "boolean") {
return this.stringifyNumber(arg, params);
}
if (arg.type === "variable") {
const varNode = arg as { type: string; name: string };
if (
this.ctx.symbolTable.isNumber(varNode.name) ||
this.ctx.symbolTable.isBoolean(varNode.name)
) {
return this.stringifyNumber(arg, params);
}
return this.ctx.emitError(
`JSON.stringify: unsupported type for variable '${varNode.name}' — only string, number, boolean, interface, string[], number[], and object[] are supported`,
);
}

return this.ctx.emitError(
"JSON.stringify: unsupported argument type — only string, number, boolean, interface, string[], number[], and object[] are supported",
);
}

private resolveInterfaceType(arg: Expression): string | null {
Expand Down Expand Up @@ -730,6 +758,91 @@ export class JsonGenerator {
return result;
}

private stringifyStringArray(arg: Expression, params: string[], spaces: number): string {
this.ctx.setUsesJson(true);
const arrPtr = this.ctx.generateExpression(arg, params);
const jsonDoc = this.ctx.emitCall("i8*", "@csyyjson_create_arr", "");
const jsonArr = this.ctx.emitCall("i8*", "@csyyjson_mut_get_root", `i8* ${jsonDoc}`);

// Load length (field 1) and data pointer (field 0) from %StringArray
const lenPtr = this.ctx.emitGep("%StringArray", arrPtr, "i32 0, i32 1");
const len = this.ctx.emitLoad("i32", lenPtr);
const dataPtr = this.ctx.emitGep("%StringArray", arrPtr, "i32 0, i32 0");
const dataRaw = this.ctx.emitLoad("i8**", dataPtr);

const counterAlloca = this.ctx.nextTemp();
this.ctx.emit(`${counterAlloca} = alloca i32`);
this.ctx.emitStore("i32", "0", counterAlloca);

const loopCond = this.ctx.nextLabel("json_str_arr_cond");
const loopBody = this.ctx.nextLabel("json_str_arr_body");
const loopEnd = this.ctx.nextLabel("json_str_arr_end");

this.ctx.emitBr(loopCond);
this.ctx.emitLabel(loopCond);
const i = this.ctx.emitLoad("i32", counterAlloca);
const cond = this.ctx.emitIcmp("slt", "i32", i, len);
this.ctx.emitBrCond(cond, loopBody, loopEnd);

this.ctx.emitLabel(loopBody);
const elemSlot = this.ctx.emitGep("i8*", dataRaw, `i32 ${i}`);
const elem = this.ctx.emitLoad("i8*", elemSlot);
this.ctx.emitCallVoid("@csyyjson_arr_add_str", `i8* ${jsonDoc}, i8* ${jsonArr}, i8* ${elem}`);
const iNext = this.ctx.nextTemp();
this.ctx.emit(`${iNext} = add i32 ${i}, 1`);
this.ctx.emitStore("i32", iNext, counterAlloca);
this.ctx.emitBr(loopCond);

this.ctx.emitLabel(loopEnd);
const result = this.emitStringify(jsonDoc, spaces);
this.ctx.setVariableType(result, "i8*");
return result;
}

private stringifyNumberArray(arg: Expression, params: string[], spaces: number): string {
this.ctx.setUsesJson(true);
const arrPtr = this.ctx.generateExpression(arg, params);
const jsonDoc = this.ctx.emitCall("i8*", "@csyyjson_create_arr", "");
const jsonArr = this.ctx.emitCall("i8*", "@csyyjson_mut_get_root", `i8* ${jsonDoc}`);

// Load length (field 1) and data pointer (field 0) from %Array
const lenPtr = this.ctx.emitGep("%Array", arrPtr, "i32 0, i32 1");
const len = this.ctx.emitLoad("i32", lenPtr);
const dataPtr = this.ctx.emitGep("%Array", arrPtr, "i32 0, i32 0");
const dataRaw = this.ctx.emitLoad("double*", dataPtr);

const counterAlloca = this.ctx.nextTemp();
this.ctx.emit(`${counterAlloca} = alloca i32`);
this.ctx.emitStore("i32", "0", counterAlloca);

const loopCond = this.ctx.nextLabel("json_num_arr_cond");
const loopBody = this.ctx.nextLabel("json_num_arr_body");
const loopEnd = this.ctx.nextLabel("json_num_arr_end");

this.ctx.emitBr(loopCond);
this.ctx.emitLabel(loopCond);
const i = this.ctx.emitLoad("i32", counterAlloca);
const cond = this.ctx.emitIcmp("slt", "i32", i, len);
this.ctx.emitBrCond(cond, loopBody, loopEnd);

this.ctx.emitLabel(loopBody);
const elemSlot = this.ctx.emitGep("double", dataRaw, `i32 ${i}`);
const elem = this.ctx.emitLoad("double", elemSlot);
this.ctx.emitCallVoid(
"@csyyjson_arr_add_num",
`i8* ${jsonDoc}, i8* ${jsonArr}, double ${elem}`,
);
const iNext = this.ctx.nextTemp();
this.ctx.emit(`${iNext} = add i32 ${i}, 1`);
this.ctx.emitStore("i32", iNext, counterAlloca);
this.ctx.emitBr(loopCond);

this.ctx.emitLabel(loopEnd);
const result = this.emitStringify(jsonDoc, spaces);
this.ctx.setVariableType(result, "i8*");
return result;
}

private stringifyString(arg: Expression, params: string[]): string {
const strPtr = this.ctx.generateExpression(arg, params);

Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/builtins/json-stringify-number-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @test-description: json stringify number array
const nums: number[] = [1, 2, 3];
const result = JSON.stringify(nums);
if (result.includes("1") && result.includes("2") && result.includes("3")) {
console.log("TEST_PASSED");
}
6 changes: 6 additions & 0 deletions tests/fixtures/builtins/json-stringify-string-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @test-description: json stringify string array
const tags: string[] = ["alpha", "beta", "gamma"];
const result = JSON.stringify(tags);
if (result.includes("alpha") && result.includes("beta") && result.includes("gamma")) {
console.log("TEST_PASSED");
}
4 changes: 4 additions & 0 deletions tests/fixtures/builtins/json-stringify-unsupported-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @test-compile-error: unsupported type — only string, number, boolean, interface, string[], number[], and object[] are supported
const m: Map<string, number> = new Map();
m.set("key", 42);
JSON.stringify(m);
4 changes: 4 additions & 0 deletions tests/fixtures/builtins/json-stringify-unsupported-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @test-compile-error: unsupported type — only string, number, boolean, interface, string[], number[], and object[] are supported
const s: Set<number> = new Set();
s.add(1);
JSON.stringify(s);
Loading