diff --git a/src/cvm.c b/src/cvm.c index b3735ff..67a01bd 100644 --- a/src/cvm.c +++ b/src/cvm.c @@ -4,6 +4,7 @@ #include "cmem.h" #include "cparse.h" #include "cstate.h" +#include "cundump.h" #include #include @@ -30,6 +31,27 @@ COSMO_API void cosmo_insert(CState *state, int indx, CValue val) 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) { CObjFunction *func; @@ -46,7 +68,7 @@ COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char * return true; } - // fail + // fail recovery state->panic = false; cosmoV_pushRef(state, (CObj *)state->error); return false; diff --git a/src/cvm.h b/src/cvm.h index 6e1fe52..6e3953c 100644 --- a/src/cvm.h +++ b/src/cvm.h @@ -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); /* - compiles string into a , if successful, will be pushed onto the stack + compiles string into a . if successful, will be pushed onto the stack otherwise the will be pushed. 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); +/* + loads a from a dump. if successful, will be pushed onto the stack + otherwise the will be pushed. + + returns: + false : is at the top of the stack + true : 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.