summaryrefslogtreecommitdiff
path: root/src/parser/parser_core.c
diff options
context:
space:
mode:
authorsuresh <sureshkrishnan.ai@gmail.com>2026-01-25 11:24:22 -0500
committersuresh <sureshkrishnan.ai@gmail.com>2026-01-25 11:24:22 -0500
commit0bb69cb67078dfa921b5b8a42275ef31dfbc9a56 (patch)
treeb579695576ae27f7316866b18bd54073f8e2ca1f /src/parser/parser_core.c
parent0bd7b99fbf813415b9a0217eaa2a4e8f6f74e1ea (diff)
fixed beffer overflow in vector. Added serialize and deserialize in json with vector reading from the struct with json
Diffstat (limited to 'src/parser/parser_core.c')
-rw-r--r--src/parser/parser_core.c215
1 files changed, 215 insertions, 0 deletions
diff --git a/src/parser/parser_core.c b/src/parser/parser_core.c
index 7468bfb..3bc6d66 100644
--- a/src/parser/parser_core.c
+++ b/src/parser/parser_core.c
@@ -637,6 +637,221 @@ static ASTNode *generate_derive_impls(ParserContext *ctx, ASTNode *strct, char *
code = xmalloc(1024);
sprintf(code, "impl Copy for %s {}", name);
}
+ else if (0 == strcmp(trait, "FromJson"))
+ {
+ // Generate from_json(j: JsonValue*) -> Result<StructName>
+ // Only works for structs (not enums)
+ if (strct->type != NODE_STRUCT)
+ {
+ zwarn_at(strct->token, "@derive(FromJson) only works on structs");
+ continue;
+ }
+
+ char body[8192];
+ body[0] = 0;
+
+ // Track Vec<String> fields for forget calls
+ char *vec_fields[32];
+ int vec_field_count = 0;
+
+ // Build field assignments
+ ASTNode *f = strct->strct.fields;
+ while (f)
+ {
+ if (f->type == NODE_FIELD)
+ {
+ char *fn = f->field.name;
+ char *ft = f->field.type;
+ char assign[2048];
+
+ if (!fn || !ft)
+ {
+ f = f->next;
+ continue;
+ }
+
+ // Map types to appropriate get_* calls
+ if (strcmp(ft, "int") == 0)
+ {
+ sprintf(assign, "var _f_%s = (*j).get_int(\"%s\").unwrap_or(0);\n", fn, fn);
+ }
+ else if (strcmp(ft, "double") == 0)
+ {
+ sprintf(assign, "var _f_%s = (*j).get_float(\"%s\").unwrap_or(0.0);\n", fn, fn);
+ }
+ else if (strcmp(ft, "bool") == 0)
+ {
+ sprintf(assign, "var _f_%s = (*j).get_bool(\"%s\").unwrap_or(false);\n", fn, fn);
+ }
+ else if (strcmp(ft, "char*") == 0)
+ {
+ sprintf(assign, "var _f_%s = (*j).get_string(\"%s\").unwrap_or(\"\");\n", fn, fn);
+ }
+ else if (strcmp(ft, "String") == 0)
+ {
+ sprintf(assign, "var _f_%s = String::new((*j).get_string(\"%s\").unwrap_or(\"\"));\n", fn, fn);
+ }
+ else if (ft && strstr(ft, "Vec") && strstr(ft, "String"))
+ {
+ // Track this field for forget() call later
+ if (vec_field_count < 32)
+ {
+ vec_fields[vec_field_count++] = fn;
+ }
+ sprintf(assign,
+ "var _f_%s = Vec<String>::new();\n"
+ "var _arr_%s = (*j).get_array(\"%s\");\n"
+ "if _arr_%s.is_some() {\n"
+ " var _a_%s = _arr_%s.unwrap();\n"
+ " for var _i_%s: usize = 0; _i_%s < _a_%s.len(); _i_%s = _i_%s + 1 {\n"
+ " var _item_%s = _a_%s.at(_i_%s);\n"
+ " if _item_%s.is_some() {\n"
+ " var _str_%s = (*_item_%s.unwrap()).as_string();\n"
+ " if _str_%s.is_some() {\n"
+ " var _s_%s = String::new(_str_%s.unwrap());\n"
+ " _f_%s.push(_s_%s); _s_%s.forget();\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}\n",
+ fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn);
+ }
+ else
+ {
+ // Nested struct: call NestedType::from_json recursively
+ sprintf(assign,
+ "var _opt_%s = (*j).get(\"%s\");\n"
+ "var _f_%s: %s;\n"
+ "if _opt_%s.is_some() { _f_%s = %s::from_json(_opt_%s.unwrap()).unwrap(); }\n",
+ fn, fn, fn, ft, fn, fn, ft, fn);
+ }
+ strcat(body, assign);
+ }
+ f = f->next;
+ }
+
+ // Build struct initialization
+ strcat(body, "return Result<");
+ strcat(body, name);
+ strcat(body, ">::Ok(");
+ strcat(body, name);
+ strcat(body, " { ");
+
+ f = strct->strct.fields;
+ int first = 1;
+ while (f)
+ {
+ if (f->type == NODE_FIELD)
+ {
+ if (!first) strcat(body, ", ");
+ char init[128];
+ // Check if this is a Vec<String> field - clone it to avoid double-free
+ int is_vec_field = 0;
+ for (int vi = 0; vi < vec_field_count; vi++)
+ {
+ if (strcmp(vec_fields[vi], f->field.name) == 0)
+ {
+ is_vec_field = 1;
+ break;
+ }
+ }
+ if (is_vec_field)
+ {
+ sprintf(init, "%s: _f_%s.clone()", f->field.name, f->field.name);
+ }
+ else
+ {
+ sprintf(init, "%s: _f_%s", f->field.name, f->field.name);
+ }
+ strcat(body, init);
+ first = 0;
+ }
+ f = f->next;
+ }
+ strcat(body, " }); ");
+
+ code = xmalloc(8192 + 1024);
+ sprintf(code,
+ "impl %s { fn from_json(j: JsonValue*) -> Result<%s> { %s } }",
+ name, name, body);
+ }
+ else if (0 == strcmp(trait, "ToJson"))
+ {
+ // Generate to_json(self) -> JsonValue
+ // Only works for structs (not enums)
+ if (strct->type != NODE_STRUCT)
+ {
+ zwarn_at(strct->token, "@derive(ToJson) only works on structs");
+ continue;
+ }
+
+ char body[8192];
+ strcpy(body, "var _obj = JsonValue::object();\n");
+
+ ASTNode *f = strct->strct.fields;
+ while (f)
+ {
+ if (f->type == NODE_FIELD)
+ {
+ char *fn = f->field.name;
+ char *ft = f->field.type;
+ char set_call[2048];
+
+ if (!fn || !ft)
+ {
+ f = f->next;
+ continue;
+ }
+
+ if (strcmp(ft, "int") == 0)
+ {
+ sprintf(set_call, "_obj.set(\"%s\", JsonValue::number((double)self.%s));\n", fn, fn);
+ }
+ else if (strcmp(ft, "double") == 0)
+ {
+ sprintf(set_call, "_obj.set(\"%s\", JsonValue::number(self.%s));\n", fn, fn);
+ }
+ else if (strcmp(ft, "bool") == 0)
+ {
+ sprintf(set_call, "_obj.set(\"%s\", JsonValue::bool(self.%s));\n", fn, fn);
+ }
+ else if (strcmp(ft, "char*") == 0)
+ {
+ sprintf(set_call, "_obj.set(\"%s\", JsonValue::string(self.%s));\n", fn, fn);
+ }
+ else if (strcmp(ft, "String") == 0)
+ {
+ sprintf(set_call, "_obj.set(\"%s\", JsonValue::string(self.%s.c_str()));\n", fn, fn);
+ }
+ else if (ft && strstr(ft, "Vec") && strstr(ft, "String"))
+ {
+ sprintf(set_call,
+ "var _arr_%s = JsonValue::array();\n"
+ "for var _i_%s: usize = 0; _i_%s < self.%s.length(); _i_%s = _i_%s + 1 {\n"
+ " _arr_%s.push(JsonValue::string(self.%s.get(_i_%s).c_str()));\n"
+ "}\n"
+ "_obj.set(\"%s\", _arr_%s);\n",
+ fn, fn, fn, fn, fn, fn, fn, fn, fn, fn, fn);
+ }
+ else
+ {
+ // Nested struct: call to_json recursively
+ sprintf(set_call,
+ "_obj.set(\"%s\", self.%s.to_json());\n",
+ fn, fn);
+ }
+ strcat(body, set_call);
+ }
+ f = f->next;
+ }
+
+ strcat(body, "return _obj;");
+
+ code = xmalloc(8192 + 1024);
+ sprintf(code,
+ "impl %s { fn to_json(self) -> JsonValue { %s } }",
+ name, body);
+ }
if (code)
{