Skip to content

Commit

Permalink
#74 continue implementing C__new
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Oct 14, 2021
1 parent 8a8c200 commit 712674c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2922,7 +2922,7 @@ static int emit_op_embed_C__new(Function *fn, Instruction *insn) {
Pseudo *prefix = get_operand(insn, 0);
Pseudo *tagname = get_operand(insn, 1);
Pseudo *size = get_operand(insn, 2);

Pseudo *target = get_target(insn, 0);
C_Type *ty = hashmap_get(&global_scope->tags, tagname->constant->s->str);
size_t tagsz = 0;
if (ty != NULL) {
Expand All @@ -2947,12 +2947,21 @@ static int emit_op_embed_C__new(Function *fn, Instruction *insn) {
raviX_buffer_add_string(&fn->body, " TValue *raviX__elements = ");
emit_reg_accessor(fn, size, 0);
raviX_buffer_add_string(&fn->body, ";\n");
raviX_buffer_add_string(&fn->body, " TValue *raviX__target = ");
emit_reg_accessor(fn, target, 0);
raviX_buffer_add_string(&fn->body, ";\n");

raviX_buffer_add_string(&fn->body, " if (ttisinteger(raviX__elements)) {\n");
raviX_buffer_add_string(&fn->body, " lua_Integer n = ivalue(raviX__elements);\n");
raviX_buffer_add_fstring(&fn->body, " void *p = raviL_newuserdata(L, %d * n, \"%s\");\n", (int)tagsz, tagname->constant->s->str);

raviX_buffer_add_fstring(&fn->body, " Udata *u = luaS_newudata(L, %d * n);\n", (int)tagsz);
raviX_buffer_add_string(&fn->body, " setuvalue(L, raviX__target, u);\n");
raviX_buffer_add_string(&fn->body, " }\n");
raviX_buffer_add_string(&fn->body, " else {\n");
raviX_buffer_add_fstring(&fn->body, " error_code = %d;\n", Error_type_mismatch);
raviX_buffer_add_string(&fn->body, " goto Lraise_error;\n");
raviX_buffer_add_string(&fn->body, " }\n");
raviX_buffer_add_string(&fn->body, "}\n");

status = 0;

Lexit:
C_parser_destroy(&parser);
Expand Down

0 comments on commit 712674c

Please sign in to comment.