Minor table refactor, added cosmoV_compileString and loadstring() to baselib

cosmoV_compileString is recommended since it'll push the result (<error> or <closure>) onto the stack.

also, fixed some GC-related bugs, so yay!
This commit is contained in:
2021-01-10 14:38:53 -06:00
parent 8dc8cef7dc
commit c510c9aebf
9 changed files with 122 additions and 59 deletions

View File

@@ -1587,20 +1587,19 @@ CObjFunction* cosmoP_compileString(CState *state, const char *source, const char
endCompiler(&parser);
freeParseState(&parser);
// the VM still expects a result on the stack
cosmoV_pushValue(state, cosmoV_newNil());
cosmoM_unfreezeGC(state);
return NULL;
}
CObjFunction* resFunc = compiler.function;
// VM expects the closure on the stack :P (we do this before ending the compiler so our GC doesn't free it)
cosmoV_pushValue(state, cosmoV_newObj((CObj*)cosmoO_newClosure(state, resFunc)));
// finally free out parser states
endCompiler(&parser);
freeParseState(&parser);
// push the funciton onto the stack so if we cause an GC event, it won't be free'd
cosmoV_pushValue(state, cosmoV_newObj(resFunc));
cosmoM_unfreezeGC(state);
cosmoV_pop(state);
return resFunc;
}