Skip to content

Commit

Permalink
[jv] Major refactor around jv_* method naming to honour memory manage…
Browse files Browse the repository at this point in the history
…ment

+ jv_* functions which return a jv object - produce it, i.e. the returned object is always (incref)ed
+ jv_* functions which perform non-memory operations consume (decref) their jv arguments
+ jv_* functions which have `_copy` suffix will perform jv_copy of all jv arguments on callers behalf.

`jv_copy` itself follows this rule by (incref)ing due to the `_copy` suffix in its name
and NOT (decref)ing since it's not performing any non-memory operation.
  • Loading branch information
Leonid S. Usov committed Oct 22, 2018
1 parent 401817b commit 1475482
Show file tree
Hide file tree
Showing 19 changed files with 736 additions and 726 deletions.
298 changes: 149 additions & 149 deletions src/builtin.c

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void dump_disassembly(int indent, struct bytecode* bc) {
for (int i=0; i<bc->nclosures; i++) {
if (i) printf(", ");
jv name = jv_array_get(jv_copy(params), i);
printf("%s", jv_string_value(name));
printf("%s", jv_string_copy_get_value(name));
jv_free(name);
}
jv_free(params);
Expand All @@ -78,7 +78,7 @@ void dump_disassembly(int indent, struct bytecode* bc) {
for (int i=0; i<bc->nsubfunctions; i++) {
struct bytecode* subfn = bc->subfunctions[i];
jv name = jv_object_get(jv_copy(subfn->debuginfo), jv_string("name"));
printf("%*s%s:%d:\n", indent, "", jv_string_value(name), i);
printf("%*s%s:%d:\n", indent, "", jv_string_copy_get_value(name), i);
jv_free(name);
dump_disassembly(indent+2, subfn);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
jv_string("params")), idx);
}
printf(" %s:%d",
jv_string_value(name),
jv_string_copy_get_value(name),
idx);
jv_free(name);
if (level) {
Expand All @@ -123,7 +123,7 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
} else if (op->op == CALL_BUILTIN) {
int func = bc->code[pc++];
jv name = jv_array_get(jv_copy(bc->globals->cfunc_names), func);
printf(" %s", jv_string_value(name));
printf(" %s", jv_string_copy_get_value(name));
jv_free(name);
} else if (op->flags & OP_HAS_BRANCH) {
printf(" %04d", pc + imm);
Expand All @@ -134,7 +134,7 @@ void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
uint16_t v = bc->code[pc++];
jv name = jv_array_get(jv_object_get(jv_copy(getlevel(bc,imm)->debuginfo), jv_string("locals")), v);
printf(" $%s:%d",
jv_string_value(name),
jv_string_copy_get_value(name),
v);
jv_free(name);
if (imm) {
Expand Down
20 changes: 10 additions & 10 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ int block_is_const(block b) {

int block_is_const_inf(block b) {
return (block_is_single(b) && b.first->op == LOADK &&
jv_get_kind(b.first->imm.constant) == JV_KIND_NUMBER &&
isinf(jv_number_value(b.first->imm.constant)));
jv_copy_get_kind(b.first->imm.constant) == JV_KIND_NUMBER &&
isinf(jv_number_copy_get_value(b.first->imm.constant)));
}

jv_kind block_const_kind(block b) {
assert(block_is_const(b));
return jv_get_kind(b.first->imm.constant);
return jv_copy_get_kind(b.first->imm.constant);
}

jv block_const(block b) {
Expand Down Expand Up @@ -507,7 +507,7 @@ jv block_list_funcs(block body, int omit_underscores) {
block gen_module(block metadata) {
inst* i = inst_new(MODULEMETA);
i->imm.constant = block_const(metadata);
if (jv_get_kind(i->imm.constant) != JV_KIND_OBJECT)
if (jv_copy_get_kind(i->imm.constant) != JV_KIND_OBJECT)
i->imm.constant = jv_object_set(jv_object(), jv_string("metadata"), i->imm.constant);
block_free(metadata);
return inst_block(i);
Expand Down Expand Up @@ -633,7 +633,7 @@ block gen_const_object(block expr) {
is_const = 0;
break;
}
if (jv_get_kind(k) != JV_KIND_STRING) {
if (jv_copy_get_kind(k) != JV_KIND_STRING) {
is_const = 0;
break;
}
Expand Down Expand Up @@ -745,7 +745,7 @@ static block bind_matcher(block matcher, block body) {
// *vars should be a jv_object (for set semantics)
static void block_get_unbound_vars(block b, jv *vars) {
assert(vars != NULL);
assert(jv_get_kind(*vars) == JV_KIND_OBJECT);
assert(jv_copy_get_kind(*vars) == JV_KIND_OBJECT);
for (inst* i = b.first; i; i = i->next) {
if (i->subfn.first) {
block_get_unbound_vars(i->subfn, vars);
Expand Down Expand Up @@ -785,11 +785,11 @@ static block bind_alternation_matchers(block matchers, block body) {
block_get_unbound_vars(final_matcher, &all_vars);

// We need a preamble of STOREVs to which to bind the matchers and the body.
jv_object_keys_foreach(all_vars, key) {
jv_object_copy_keys_foreach(all_vars, key) {
preamble = BLOCK(preamble,
gen_op_simple(DUP),
gen_const(jv_null()),
gen_op_unbound(STOREV, jv_string_value(key)));
gen_op_unbound(STOREV, jv_string_copy_get_value(key)));
jv_free(key);
}
jv_free(all_vars);
Expand Down Expand Up @@ -977,7 +977,7 @@ block gen_array_matcher(block left, block curr) {
assert(left.first->next->next->op == LOADK);
i = left.first->next->next;
}
index = 1 + (int) jv_number_value(i->imm.constant);
index = 1 + (int) jv_number_copy_get_value(i->imm.constant);
}

// `left` goes at the end so that the const index is in a predictable place
Expand Down Expand Up @@ -1113,7 +1113,7 @@ extern char **environ;
static jv
make_env(jv env)
{
if (jv_is_valid(env))
if (jv_copy_is_valid(env))
return jv_copy(env);
jv r = jv_object();
if (environ == NULL)
Expand Down
Loading

0 comments on commit 1475482

Please sign in to comment.