1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-18 06:34:48 +00:00

Update marsh.c

This commit is contained in:
Calvin Rose 2018-08-21 13:59:01 -04:00
parent 5464fd5173
commit 4e65eede1c

View File

@ -133,7 +133,6 @@ static void marshal_one_def(MarshalState *st, DstFuncDef *def, int flags) {
return; return;
} }
} }
dst_v_push(st->seen_defs, def);
pushint(st, def->flags); pushint(st, def->flags);
pushint(st, def->slotcount); pushint(st, def->slotcount);
pushint(st, def->arity); pushint(st, def->arity);
@ -176,6 +175,9 @@ static void marshal_one_def(MarshalState *st, DstFuncDef *def, int flags) {
pushint(st, map.column); pushint(st, map.column);
} }
} }
/* Add to lookup */
dst_v_push(st->seen_defs, def);
} }
/* The main body of the marshaling function. Is the main /* The main body of the marshaling function. Is the main
@ -501,57 +503,56 @@ static const uint8_t *unmarshal_one_def(
longjmp(st->err, UMR_INVALID_REFERENCE); longjmp(st->err, UMR_INVALID_REFERENCE);
*out = st->lookup_defs[index]; *out = st->lookup_defs[index];
} else { } else {
DstFuncDef *def = dst_gcalloc(DST_MEMORY_FUNCDEF, sizeof(DstFuncDef)); DstFuncDef def;
dst_v_push(st->lookup_defs, def);
/* Read flags and other fixed values */ /* Read flags and other fixed values */
def->flags = readint(st, &data); def.flags = readint(st, &data);
def->slotcount = readint(st, &data); def.slotcount = readint(st, &data);
def->arity = readint(st, &data); def.arity = readint(st, &data);
def->constants_length = readint(st, &data); def.constants_length = readint(st, &data);
def->bytecode_length = readint(st, &data); def.bytecode_length = readint(st, &data);
def->environments_length = 0; def.environments_length = 0;
def->defs_length = 0; def.defs_length = 0;
def->name = NULL; def.name = NULL;
def->source = NULL; def.source = NULL;
if (def->flags & DST_FUNCDEF_FLAG_HASENVS) if (def.flags & DST_FUNCDEF_FLAG_HASENVS)
def->environments_length = readint(st, &data); def.environments_length = readint(st, &data);
if (def->flags & DST_FUNCDEF_FLAG_HASDEFS) if (def.flags & DST_FUNCDEF_FLAG_HASDEFS)
def->defs_length = readint(st, &data); def.defs_length = readint(st, &data);
if (def->flags & DST_FUNCDEF_FLAG_HASNAME) { if (def.flags & DST_FUNCDEF_FLAG_HASNAME) {
Dst x; Dst x;
data = unmarshal_one(st, data, &x, flags + 1); data = unmarshal_one(st, data, &x, flags + 1);
if (!dst_checktype(x, DST_STRING)) longjmp(st->err, UMR_EXPECTED_STRING); if (!dst_checktype(x, DST_STRING)) longjmp(st->err, UMR_EXPECTED_STRING);
def->name = dst_unwrap_string(x); def.name = dst_unwrap_string(x);
} }
if (def->flags & DST_FUNCDEF_FLAG_HASSOURCE) { if (def.flags & DST_FUNCDEF_FLAG_HASSOURCE) {
Dst x; Dst x;
data = unmarshal_one(st, data, &x, flags + 1); data = unmarshal_one(st, data, &x, flags + 1);
if (!dst_checktype(x, DST_STRING)) longjmp(st->err, UMR_EXPECTED_STRING); if (!dst_checktype(x, DST_STRING)) longjmp(st->err, UMR_EXPECTED_STRING);
def->source = dst_unwrap_string(x); def.source = dst_unwrap_string(x);
} }
/* Unmarshal constants */ /* Unmarshal constants */
if (def->constants_length) { if (def.constants_length) {
def->constants = malloc(sizeof(Dst) * def->constants_length); def.constants = malloc(sizeof(Dst) * def.constants_length);
if (!def->constants) { if (!def.constants) {
DST_OUT_OF_MEMORY; DST_OUT_OF_MEMORY;
} }
for (int32_t i = 0; i < def->constants_length; i++) for (int32_t i = 0; i < def.constants_length; i++)
data = unmarshal_one(st, data, def->constants + i, flags + 1); data = unmarshal_one(st, data, def.constants + i, flags + 1);
} else { } else {
def->constants = NULL; def.constants = NULL;
} }
/* Unmarshal bytecode */ /* Unmarshal bytecode */
def->bytecode = malloc(sizeof(uint32_t) * def->bytecode_length); def.bytecode = malloc(sizeof(uint32_t) * def.bytecode_length);
if (!def->bytecode) { if (!def.bytecode) {
DST_OUT_OF_MEMORY; DST_OUT_OF_MEMORY;
} }
for (int32_t i = 0; i < def->bytecode_length; i++) { for (int32_t i = 0; i < def.bytecode_length; i++) {
if (data + 4 > st->end) longjmp(st->err, UMR_EOS); if (data + 4 > st->end) longjmp(st->err, UMR_EOS);
def->bytecode[i] = def.bytecode[i] =
(uint32_t)(data[0]) | (uint32_t)(data[0]) |
((uint32_t)(data[1]) << 8) | ((uint32_t)(data[1]) << 8) |
((uint32_t)(data[2]) << 16) | ((uint32_t)(data[2]) << 16) |
@ -560,50 +561,52 @@ static const uint8_t *unmarshal_one_def(
} }
/* Unmarshal environments */ /* Unmarshal environments */
if (def->flags & DST_FUNCDEF_FLAG_HASENVS) { if (def.flags & DST_FUNCDEF_FLAG_HASENVS) {
def->environments = malloc(sizeof(int32_t) * def->environments_length); def.environments = malloc(sizeof(int32_t) * def.environments_length);
if (!def->environments) { if (!def.environments) {
DST_OUT_OF_MEMORY; DST_OUT_OF_MEMORY;
} }
for (int32_t i = 0; i < def->environments_length; i++) { for (int32_t i = 0; i < def.environments_length; i++) {
def->environments[i] = readint(st, &data); def.environments[i] = readint(st, &data);
} }
} else { } else {
def->environments = NULL; def.environments = NULL;
} }
/* Unmarshal sub funcdefs */ /* Unmarshal sub funcdefs */
if (def->flags & DST_FUNCDEF_FLAG_HASDEFS) { if (def.flags & DST_FUNCDEF_FLAG_HASDEFS) {
def->defs = malloc(sizeof(DstFuncDef *) * def->defs_length); def.defs = malloc(sizeof(DstFuncDef *) * def.defs_length);
if (!def->defs) { if (!def.defs) {
DST_OUT_OF_MEMORY; DST_OUT_OF_MEMORY;
} }
for (int32_t i = 0; i < def->defs_length; i++) { for (int32_t i = 0; i < def.defs_length; i++) {
data = unmarshal_one_def(st, data, def->defs + i, flags + 1); data = unmarshal_one_def(st, data, def.defs + i, flags + 1);
} }
} else { } else {
def->defs = NULL; def.defs = NULL;
} }
/* Unmarshal source maps if needed */ /* Unmarshal source maps if needed */
if (def->flags & DST_FUNCDEF_FLAG_HASSOURCEMAP) { if (def.flags & DST_FUNCDEF_FLAG_HASSOURCEMAP) {
def->sourcemap = malloc(sizeof(DstSourceMapping) * def->bytecode_length); def.sourcemap = malloc(sizeof(DstSourceMapping) * def.bytecode_length);
if (!def->sourcemap) { if (!def.sourcemap) {
DST_OUT_OF_MEMORY; DST_OUT_OF_MEMORY;
} }
for (int32_t i = 0; i < def->bytecode_length; i++) { for (int32_t i = 0; i < def.bytecode_length; i++) {
def->sourcemap[i].line = readint(st, &data); def.sourcemap[i].line = readint(st, &data);
def->sourcemap[i].column = readint(st, &data); def.sourcemap[i].column = readint(st, &data);
} }
} else { } else {
def->sourcemap = NULL; def.sourcemap = NULL;
} }
/* Validate */ /* Validate */
if (dst_verify(def)) longjmp(st->err, UMR_INVALID_BYTECODE); if (dst_verify(&def)) longjmp(st->err, UMR_INVALID_BYTECODE);
/* Set def */ /* Set def */
*out = def; *out = dst_gcalloc(DST_MEMORY_FUNCDEF, sizeof(DstFuncDef));
dst_v_push(st->lookup_defs, *out);
memcpy(*out, &def, sizeof(def));
} }
return data; return data;
} }
@ -701,6 +704,7 @@ static const uint8_t *unmarshal_one(
data = unmarshal_one_def(st, data + 1, &def, flags + 1); data = unmarshal_one_def(st, data + 1, &def, flags + 1);
func = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction) + func = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction) +
def->environments_length * sizeof(DstFuncEnv)); def->environments_length * sizeof(DstFuncEnv));
func->def = def;
*out = dst_wrap_function(func); *out = dst_wrap_function(func);
dst_array_push(&st->lookup, *out); dst_array_push(&st->lookup, *out);
for (int32_t i = 0; i < def->environments_length; i++) { for (int32_t i = 0; i < def->environments_length; i++) {