1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 22:53:16 +00:00

Fix funcdef flags when marshaling.

This commit is contained in:
Calvin Rose 2018-08-21 15:07:37 -04:00
parent 600292fad4
commit 634ec85b07
8 changed files with 19 additions and 25 deletions

View File

@ -100,12 +100,12 @@ static const DstInstructionDef dst_ops[] = {
{"ldu", DOP_LOAD_UPVALUE},
{"len", DOP_LENGTH},
{"lt", DOP_LESS_THAN},
{"lten", DOP_NUMERIC_LESS_THAN_EQUAL},
{"lter", DOP_LESS_THAN_EQUAL_REAL},
{"lti", DOP_LESS_THAN_INTEGER},
{"ltim", DOP_LESS_THAN_IMMEDIATE},
{"ltn", DOP_NUMERIC_LESS_THAN},
{"ltr", DOP_LESS_THAN_REAL},
{"lten", DOP_NUMERIC_LESS_THAN_EQUAL},
{"lter", DOP_LESS_THAN_EQUAL_REAL},
{"mkarr", DOP_MAKE_ARRAY},
{"mkbuf", DOP_MAKE_BUFFER},
{"mkstr", DOP_MAKE_STRING},
@ -716,9 +716,6 @@ static DstAssembleResult dst_asm1(DstAssembler *parent, Dst source, int flags) {
def->environments =
realloc(def->environments, def->environments_length * sizeof(int32_t));
/* Add extra flags */
dst_func_addflags(def);
/* Verify the func def */
if (dst_verify(def)) {
dst_asm_error(&a, "invalid assembly");

View File

@ -112,7 +112,7 @@ enum DstInstructionType dst_instructions[DOP_INSTRUCTION_COUNT] = {
/* Verify some bytecode */
int32_t dst_verify(DstFuncDef *def) {
int vargs = def->flags & DST_FUNCDEF_FLAG_VARARG;
int vargs = !!(def->flags & DST_FUNCDEF_FLAG_VARARG);
int32_t i;
int32_t maxslot = def->arity + vargs;
int32_t sc = def->slotcount;

View File

@ -24,7 +24,6 @@
#include "compile.h"
#include "emit.h"
#include "vector.h"
#include "util.h"
DstFopts dstc_fopts_default(DstCompiler *c) {
DstFopts ret;
@ -614,9 +613,6 @@ DstFuncDef *dstc_pop_funcdef(DstCompiler *c) {
def->flags |= DST_FUNCDEF_FLAG_NEEDSENV;
}
/* Add extra flags */
dst_func_addflags(def);
/* Pop the scope */
dstc_popscope(c);

View File

@ -341,7 +341,6 @@ static void dst_quick_asm(
DST_OUT_OF_MEMORY;
}
memcpy(def->bytecode, bytecode, bytecode_size);
dst_func_addflags(def);
dst_env_def(env, name, dst_wrap_function(dst_thunk(def)));
}

View File

@ -124,6 +124,15 @@ static void marshal_one_env(MarshalState *st, DstFuncEnv *env, int flags) {
}
}
/* Add function flags to dst functions */
static void dst_func_addflags(DstFuncDef *def) {
if (def->name) def->flags |= DST_FUNCDEF_FLAG_HASNAME;
if (def->source) def->flags |= DST_FUNCDEF_FLAG_HASSOURCE;
if (def->defs) def->flags |= DST_FUNCDEF_FLAG_HASDEFS;
if (def->environments) def->flags |= DST_FUNCDEF_FLAG_HASENVS;
if (def->sourcemap) def->flags |= DST_FUNCDEF_FLAG_HASSOURCEMAP;
}
/* Marshal a function def */
static void marshal_one_def(MarshalState *st, DstFuncDef *def, int flags) {
for (int32_t i = 0; i < dst_v_count(st->seen_defs); i++) {
@ -133,6 +142,7 @@ static void marshal_one_def(MarshalState *st, DstFuncDef *def, int flags) {
return;
}
}
dst_func_addflags(def);
/* Add to lookup */
dst_v_push(st->seen_defs, def);
pushint(st, def->flags);
@ -821,8 +831,8 @@ static int cfun_unmarshal(DstArgs args) {
}
static const DstReg cfuns[] = {
{"marsh.marshal", cfun_marshal},
{"marsh.unmarshal", cfun_unmarshal},
{"marshal", cfun_marshal},
{"unmarshal", cfun_unmarshal},
{NULL, NULL}
};

View File

@ -95,15 +95,6 @@ int32_t dst_tablen(int32_t n) {
return n + 1;
}
/* Add function flags to dst functions */
void dst_func_addflags(DstFuncDef *def) {
if (def->name) def->flags |= DST_FUNCDEF_FLAG_HASNAME;
if (def->source) def->flags |= DST_FUNCDEF_FLAG_HASSOURCE;
if (def->defs) def->flags |= DST_FUNCDEF_FLAG_HASDEFS;
if (def->environments) def->flags |= DST_FUNCDEF_FLAG_HASENVS;
if (def->sourcemap) def->flags |= DST_FUNCDEF_FLAG_HASSOURCEMAP;
}
/* Compare a dst string with a cstring. more efficient than loading
* c string as a dst string. */
int dst_cstrcmp(const uint8_t *str, const char *other) {

View File

@ -31,7 +31,6 @@ int32_t dst_array_calchash(const Dst *array, int32_t len);
int32_t dst_kv_calchash(const DstKV *kvs, int32_t len);
int32_t dst_string_calchash(const uint8_t *str, int32_t len);
int32_t dst_tablen(int32_t n);
void dst_func_addflags(DstFuncDef *def);
void dst_buffer_push_types(DstBuffer *buffer, int types);
const void *dst_strbinsearch(
const void *tab,

View File

@ -142,8 +142,8 @@
# Marshal
(defn testmarsh [x msg]
(def marshx (marsh.marshal x))
(def out (-> marshx marsh.unmarshal marsh.marshal))
(def marshx (marshal x))
(def out (-> marshx unmarshal marshal))
(assert (= (string marshx) (string out)) msg))
(testmarsh nil "marshal nil")
@ -164,6 +164,8 @@
(testmarsh (fn name [x] x) "marshal function 1")
(testmarsh (fn [x] (+ 10 x 2)) "marshal function 2")
(testmarsh (fn thing [x] (+ 11 x x 30)) "marshal function 3")
(testmarsh map "marshal function 4")
(testmarsh reduce "marshal function 4")
# Large functions
(def manydefs (for [i :range [0 300]] (tuple 'def (gensym) (string "value_" i))))