mirror of
https://github.com/janet-lang/janet
synced 2024-11-28 02:59:54 +00:00
Fix serilaization bug. Still need to serialize c functions.
This commit is contained in:
parent
145688b49f
commit
f066047112
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// Place your settings in this file to overwrite default and user settings.
|
||||||
|
{
|
||||||
|
}
|
@ -26,17 +26,17 @@
|
|||||||
#define OP_WIDTH 20
|
#define OP_WIDTH 20
|
||||||
|
|
||||||
/* Print various register and arguments to instructions */
|
/* Print various register and arguments to instructions */
|
||||||
static void dasm_print_slot(FILE * out, uint16_t index) { fprintf(out, "%d ", index); }
|
static void dasm_print_slot(FILE *out, uint16_t index) { fprintf(out, "%d ", index); }
|
||||||
static void dasm_print_i16(FILE * out, int16_t number) { fprintf(out, "#%d ", number); }
|
static void dasm_print_i16(FILE *out, int16_t number) { fprintf(out, "#%d ", number); }
|
||||||
static void dasm_print_i32(FILE * out, int32_t number) { fprintf(out, "#%d ", number); }
|
static void dasm_print_i32(FILE *out, int32_t number) { fprintf(out, "#%d ", number); }
|
||||||
static void dasm_print_f64(FILE * out, double number) { fprintf(out, "#%f ", number); }
|
static void dasm_print_f64(FILE *out, double number) { fprintf(out, "#%f ", number); }
|
||||||
static void dasm_print_literal(FILE * out, uint16_t index) { fprintf(out, "(%d) ", index); }
|
static void dasm_print_literal(FILE *out, uint16_t index) { fprintf(out, "(%d) ", index); }
|
||||||
static void dasm_print_upvalue(FILE * out, uint16_t level, uint16_t index) {
|
static void dasm_print_upvalue(FILE *out, uint16_t level, uint16_t index) {
|
||||||
fprintf(out, "<%d, %d> ", level, index);
|
fprintf(out, "<%d, %d> ", level, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print the name of the argument but pad it */
|
/* Print the name of the argument but pad it */
|
||||||
static void dasm_print_arg(FILE * out, const char * name) {
|
static void dasm_print_arg(FILE *out, const char *name) {
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
char c;
|
char c;
|
||||||
while ((c = *name++)) {
|
while ((c = *name++)) {
|
||||||
@ -48,7 +48,7 @@ static void dasm_print_arg(FILE * out, const char * name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Print instructions that take a fixed number of arguments */
|
/* Print instructions that take a fixed number of arguments */
|
||||||
static uint32_t dasm_fixed_op(FILE * out, const uint16_t * current,
|
static uint32_t dasm_fixed_op(FILE *out, const uint16_t *current,
|
||||||
const char * name, uint32_t size) {
|
const char * name, uint32_t size) {
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
dasm_print_arg(out, name);
|
dasm_print_arg(out, name);
|
||||||
@ -58,7 +58,7 @@ static uint32_t dasm_fixed_op(FILE * out, const uint16_t * current,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Print instructions that take a variable number of arguments */
|
/* Print instructions that take a variable number of arguments */
|
||||||
static uint32_t dasm_varg_op(FILE * out, const uint16_t * current,
|
static uint32_t dasm_varg_op(FILE *out, const uint16_t *current,
|
||||||
const char * name, uint32_t extra) {
|
const char * name, uint32_t extra) {
|
||||||
uint32_t i, argCount;
|
uint32_t i, argCount;
|
||||||
dasm_print_arg(out, name);
|
dasm_print_arg(out, name);
|
||||||
@ -75,17 +75,17 @@ static uint32_t dasm_varg_op(FILE * out, const uint16_t * current,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Print the disassembly for a function definition */
|
/* Print the disassembly for a function definition */
|
||||||
void gst_dasm_funcdef(FILE * out, GstFuncDef * def) {
|
void gst_dasm_funcdef(FILE *out, GstFuncDef *def) {
|
||||||
gst_dasm(out, def->byteCode, def->byteCodeLen);
|
gst_dasm(out, def->byteCode, def->byteCodeLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print the disassembly for a function */
|
/* Print the disassembly for a function */
|
||||||
void gst_dasm_function(FILE * out, GstFunction * f) {
|
void gst_dasm_function(FILE *out, GstFunction *f) {
|
||||||
gst_dasm(out, f->def->byteCode, f->def->byteCodeLen);
|
gst_dasm(out, f->def->byteCode, f->def->byteCodeLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Disassemble some bytecode and display it as opcode + arguments assembly */
|
/* Disassemble some bytecode and display it as opcode + arguments assembly */
|
||||||
void gst_dasm(FILE * out, uint16_t *byteCode, uint32_t len) {
|
void gst_dasm(FILE *out, uint16_t *byteCode, uint32_t len) {
|
||||||
uint16_t *current = byteCode;
|
uint16_t *current = byteCode;
|
||||||
uint16_t *end = byteCode + len;
|
uint16_t *end = byteCode + len;
|
||||||
|
|
||||||
|
@ -339,7 +339,7 @@ static const char *gst_deserialize_impl(
|
|||||||
if (err != NULL) return err;
|
if (err != NULL) return err;
|
||||||
}
|
}
|
||||||
read_u32(byteCodeLen);
|
read_u32(byteCodeLen);
|
||||||
deser_datacheck(byteCodeLen);
|
deser_datacheck(byteCodeLen * sizeof(uint16_t));
|
||||||
def->byteCode = gst_alloc(vm, byteCodeLen * sizeof(uint16_t));
|
def->byteCode = gst_alloc(vm, byteCodeLen * sizeof(uint16_t));
|
||||||
def->byteCodeLen = byteCodeLen;
|
def->byteCodeLen = byteCodeLen;
|
||||||
for (i = 0; i < byteCodeLen; ++i) {
|
for (i = 0; i < byteCodeLen; ++i) {
|
||||||
@ -412,8 +412,12 @@ static const char *gst_deserialize_impl(
|
|||||||
|
|
||||||
case 216: /* C function */
|
case 216: /* C function */
|
||||||
/* TODO - add registry for c functions */
|
/* TODO - add registry for c functions */
|
||||||
read_u32(length);
|
/* For now just add 64 bit integer */
|
||||||
|
{
|
||||||
|
int64_t rawp;
|
||||||
|
read_i64(rawp);
|
||||||
ret.type = GST_NIL;
|
ret.type = GST_NIL;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 217: /* Reference */
|
case 217: /* Reference */
|
||||||
@ -443,7 +447,6 @@ const char *gst_deserialize(
|
|||||||
const char *err;
|
const char *err;
|
||||||
GstArray *visited = gst_array(vm, 10);
|
GstArray *visited = gst_array(vm, 10);
|
||||||
err = gst_deserialize_impl(vm, data, data + len, nextData, visited, &ret);
|
err = gst_deserialize_impl(vm, data, data + len, nextData, visited, &ret);
|
||||||
printf("Read %ld\n", *nextData - data);
|
|
||||||
if (err != NULL) return err;
|
if (err != NULL) return err;
|
||||||
*out = ret;
|
*out = ret;
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -453,7 +456,7 @@ const char *gst_deserialize(
|
|||||||
BUFFER_DEFINE(real, GstReal)
|
BUFFER_DEFINE(real, GstReal)
|
||||||
BUFFER_DEFINE(integer, GstInteger)
|
BUFFER_DEFINE(integer, GstInteger)
|
||||||
BUFFER_DEFINE(u32, uint32_t)
|
BUFFER_DEFINE(u32, uint32_t)
|
||||||
BUFFER_DEFINE(u16, uint32_t)
|
BUFFER_DEFINE(u16, uint16_t)
|
||||||
|
|
||||||
/* Serialize a value and write to a buffer. Returns possible
|
/* Serialize a value and write to a buffer. Returns possible
|
||||||
* error messages. */
|
* error messages. */
|
||||||
@ -541,13 +544,13 @@ const char *gst_serialize_impl(
|
|||||||
if (err != NULL) return err;
|
if (err != NULL) return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Record reference */
|
/* Record reference after serialization */
|
||||||
gst_table_put(vm, visited, x, gst_wrap_integer(*nextId));
|
gst_table_put(vm, visited, x, gst_wrap_integer(*nextId));
|
||||||
*nextId = *nextId + 1;
|
*nextId = *nextId + 1;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Record reference */
|
/* Record reference before serialization */
|
||||||
gst_table_put(vm, visited, x, gst_wrap_integer(*nextId));
|
gst_table_put(vm, visited, x, gst_wrap_integer(*nextId));
|
||||||
*nextId = *nextId + 1;
|
*nextId = *nextId + 1;
|
||||||
|
|
||||||
|
@ -162,6 +162,9 @@ int gst_stl_length(Gst *vm) {
|
|||||||
case GST_STRUCT:
|
case GST_STRUCT:
|
||||||
ret.data.integer = gst_struct_length(x.data.st);
|
ret.data.integer = gst_struct_length(x.data.st);
|
||||||
break;
|
break;
|
||||||
|
case GST_FUNCDEF:
|
||||||
|
ret.data.integer = x.data.def->byteCodeLen;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
gst_c_return(vm, ret);
|
gst_c_return(vm, ret);
|
||||||
}
|
}
|
||||||
@ -404,8 +407,9 @@ int gst_stl_thread(Gst *vm) {
|
|||||||
GstThread *t;
|
GstThread *t;
|
||||||
GstValue callee = gst_arg(vm, 0);
|
GstValue callee = gst_arg(vm, 0);
|
||||||
if (callee.type != GST_FUNCTION && callee.type != GST_CFUNCTION)
|
if (callee.type != GST_FUNCTION && callee.type != GST_CFUNCTION)
|
||||||
gst_c_throwc(vm, "expected function");
|
gst_c_throwc(vm, "expected function in thread constructor");
|
||||||
t = gst_thread(vm, callee, 10);
|
t = gst_thread(vm, callee, 10);
|
||||||
|
t->parent = vm->thread;
|
||||||
gst_c_return(vm, gst_wrap_thread(t));
|
gst_c_return(vm, gst_wrap_thread(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ GstThread *gst_thread(Gst *vm, GstValue callee, uint32_t capacity) {
|
|||||||
gst_frame_env(stack) = NULL;
|
gst_frame_env(stack) = NULL;
|
||||||
gst_frame_callee(stack) = callee;
|
gst_frame_callee(stack) = callee;
|
||||||
gst_thread_endframe(vm, thread);
|
gst_thread_endframe(vm, thread);
|
||||||
thread->parent = vm->thread;
|
thread->parent = NULL; /* Need to set parent manually */
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,8 +143,6 @@ void gst_thread_endframe(Gst *vm, GstThread *thread) {
|
|||||||
gst_thread_pushnil(vm, thread, locals - gst_frame_size(stack));
|
gst_thread_pushnil(vm, thread, locals - gst_frame_size(stack));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stack = thread->data + thread->count;
|
|
||||||
gst_frame_args(stack) = gst_frame_size(stack) + GST_FRAME_SIZE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
core/vm.c
21
core/vm.c
@ -397,14 +397,6 @@ int gst_continue(Gst *vm) {
|
|||||||
if (vm->thread->parent == NULL)
|
if (vm->thread->parent == NULL)
|
||||||
return GST_RETURN_ERROR;
|
return GST_RETURN_ERROR;
|
||||||
vm->thread = vm->thread->parent;
|
vm->thread = vm->thread->parent;
|
||||||
while (vm->thread->count < GST_FRAME_SIZE) {
|
|
||||||
if (vm->thread->parent) {
|
|
||||||
vm->thread->status = GST_THREAD_DEAD;
|
|
||||||
vm->thread = vm->thread->parent;
|
|
||||||
} else {
|
|
||||||
return GST_RETURN_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stack = vm->thread->data + vm->thread->count;
|
stack = vm->thread->data + vm->thread->count;
|
||||||
stack[gst_frame_ret(stack)] = vm->ret;
|
stack[gst_frame_ret(stack)] = vm->ret;
|
||||||
pc = gst_frame_pc(stack);
|
pc = gst_frame_pc(stack);
|
||||||
@ -424,21 +416,16 @@ int gst_continue(Gst *vm) {
|
|||||||
/* Run the vm with a given function. This function is
|
/* Run the vm with a given function. This function is
|
||||||
* called to start the vm. */
|
* called to start the vm. */
|
||||||
int gst_run(Gst *vm, GstValue callee) {
|
int gst_run(Gst *vm, GstValue callee) {
|
||||||
int status;
|
|
||||||
GstValue *stack;
|
|
||||||
vm->thread = gst_thread(vm, callee, 64);
|
vm->thread = gst_thread(vm, callee, 64);
|
||||||
if (vm->thread == NULL)
|
if (vm->thread == NULL)
|
||||||
return GST_RETURN_CRASH;
|
return GST_RETURN_CRASH;
|
||||||
stack = gst_thread_stack(vm->thread);
|
|
||||||
/* If callee was not actually a function, get the delegate function */
|
|
||||||
callee = gst_frame_callee(stack);
|
|
||||||
if (callee.type == GST_CFUNCTION) {
|
if (callee.type == GST_CFUNCTION) {
|
||||||
vm->ret.type = GST_NIL;
|
vm->ret.type = GST_NIL;
|
||||||
status = callee.data.cfunction(vm);
|
return callee.data.cfunction(vm);
|
||||||
gst_thread_popframe(vm, vm->thread);
|
} else if (callee.type == GST_FUNCTION) {
|
||||||
return status;
|
|
||||||
} else {
|
|
||||||
return gst_continue(vm);
|
return gst_continue(vm);
|
||||||
|
} else {
|
||||||
|
return GST_RETURN_CRASH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,7 +352,7 @@ const uint8_t *gst_buffer_to_string(Gst *vm, GstBuffer *buffer);
|
|||||||
|
|
||||||
/* Define a push function for pushing a certain type to the buffer */
|
/* Define a push function for pushing a certain type to the buffer */
|
||||||
#define BUFFER_DEFINE(name, type) \
|
#define BUFFER_DEFINE(name, type) \
|
||||||
static void gst_buffer_push_##name(Gst * vm, GstBuffer * buffer, type x) { \
|
static void gst_buffer_push_##name(Gst *vm, GstBuffer *buffer, type x) { \
|
||||||
union { type t; uint8_t bytes[sizeof(type)]; } u; \
|
union { type t; uint8_t bytes[sizeof(type)]; } u; \
|
||||||
u.t = x; gst_buffer_append(vm, buffer, u.bytes, sizeof(type)); \
|
u.t = x; gst_buffer_append(vm, buffer, u.bytes, sizeof(type)); \
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user