cvm.c: added cosmoV_undump

This commit is contained in:
CPunch 2023-05-27 23:16:47 -05:00
parent 45f36e6e87
commit 355842989b
2 changed files with 34 additions and 2 deletions

View File

@ -4,6 +4,7 @@
#include "cmem.h" #include "cmem.h"
#include "cparse.h" #include "cparse.h"
#include "cstate.h" #include "cstate.h"
#include "cundump.h"
#include <math.h> #include <math.h>
#include <stdarg.h> #include <stdarg.h>
@ -30,6 +31,27 @@ COSMO_API void cosmo_insert(CState *state, int indx, CValue val)
state->top++; state->top++;
} }
COSMO_API bool cosmoV_undump(CState *state, cosmo_Reader reader, const void *ud) {
CObjFunction *func;
if (cosmoD_undump(state, reader, ud, &func)) {
// fail recovery
state->panic = false;
cosmoV_pushRef(state, (CObj *)state->error);
return false;
};
#ifdef VM_DEBUG
disasmChunk(&func->chunk, func->name ? func->name->str : UNNAMEDCHUNK, 0);
#endif
// push function onto the stack so it doesn't it cleaned up by the GC, at the same stack
// location put our closure
cosmoV_pushRef(state, (CObj *)func);
*(cosmoV_getTop(state, 0)) = cosmoV_newRef(cosmoO_newClosure(state, func));
return true;
}
COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *name) COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *name)
{ {
CObjFunction *func; CObjFunction *func;
@ -46,7 +68,7 @@ COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *
return true; return true;
} }
// fail // fail recovery
state->panic = false; state->panic = false;
cosmoV_pushRef(state, (CObj *)state->error); cosmoV_pushRef(state, (CObj *)state->error);
return false; return false;

View File

@ -38,7 +38,7 @@ COSMO_API void cosmo_insert(CState *state, int indx, CValue val);
COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj); COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj);
/* /*
compiles string into a <closure>, if successful, <closure> will be pushed onto the stack compiles string into a <closure>. if successful, <closure> will be pushed onto the stack
otherwise the <error> will be pushed. otherwise the <error> will be pushed.
returns: returns:
@ -47,6 +47,16 @@ COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjO
*/ */
COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *name); COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *name);
/*
loads a <closure> from a dump. if successful, <closure> will be pushed onto the stack
otherwise the <error> will be pushed.
returns:
false : <error> is at the top of the stack
true : <closure> is at the top of the stack
*/
COSMO_API bool cosmoV_undump(CState *state, cosmo_Reader reader, const void *ud);
/* /*
expects object to be pushed, then the key. expects object to be pushed, then the key.