mirror of
https://github.com/CPunch/Cosmo.git
synced 2025-11-17 04:30:05 +00:00
Minor refactor, added vm.* library
- cosmoV_pushObj() has been added - cbaselib.h has been cleaned up a bit
This commit is contained in:
57
src/cvm.c
57
src/cvm.c
@@ -37,14 +37,14 @@ COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *
|
||||
disasmChunk(&func->chunk, func->module->str, 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_pushValue(state, cosmoV_newObj(func));
|
||||
cosmoV_pushObj(state, (CObj*)func);
|
||||
*(cosmoV_getTop(state, 0)) = cosmoV_newObj(cosmoO_newClosure(state, func));
|
||||
return true;
|
||||
}
|
||||
|
||||
// fail
|
||||
state->panic = false;
|
||||
cosmoV_pushValue(state, cosmoV_newObj(state->error));
|
||||
cosmoV_pushObj(state, (CObj*)state->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -173,9 +173,9 @@ void cosmoV_concat(CState *state, int vals) {
|
||||
|
||||
CObjString *result = cosmoV_toString(state, *start);
|
||||
for (StkPtr current = start + 1; current <= end; current++) {
|
||||
cosmoV_pushValue(state, cosmoV_newObj(result)); // so our GC can find our current result string
|
||||
cosmoV_pushObj(state, (CObj*)result); // so our GC can find our current result string
|
||||
CObjString *otherStr = cosmoV_toString(state, *current);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(otherStr)); // also so our GC won't free otherStr
|
||||
cosmoV_pushObj(state, (CObj*)otherStr); // also so our GC won't free otherStr
|
||||
|
||||
// concat the two strings together
|
||||
size_t sz = result->length + otherStr->length;
|
||||
@@ -190,7 +190,7 @@ void cosmoV_concat(CState *state, int vals) {
|
||||
}
|
||||
|
||||
state->top = start;
|
||||
cosmoV_pushValue(state, cosmoV_newObj(result));
|
||||
cosmoV_pushObj(state, (CObj*)result);
|
||||
}
|
||||
|
||||
int cosmoV_execute(CState *state);
|
||||
@@ -324,7 +324,7 @@ bool callCValue(CState *state, CValue func, int args, int nresults, int offset)
|
||||
CObjObject *protoObj = (CObjObject*)cosmoV_readObj(func);
|
||||
CValue ret;
|
||||
|
||||
cosmoV_pushValue(state, cosmoV_newObj(protoObj)); // push proto to stack for GC to find
|
||||
cosmoV_pushObj(state, (CObj*)protoObj); // push proto to stack for GC to find
|
||||
CObjObject *newObj = cosmoO_newObject(state);
|
||||
newObj->_obj.proto = protoObj;
|
||||
cosmoV_pop(state); // pop proto
|
||||
@@ -340,7 +340,7 @@ bool callCValue(CState *state, CValue func, int args, int nresults, int offset)
|
||||
}
|
||||
|
||||
if (nresults > 0) {
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj));
|
||||
cosmoV_pushObj(state, (CObj*)newObj);
|
||||
|
||||
// push the nils to fill up the expected return values
|
||||
for (int i = 0; i < nresults - 1; i++) { // -1 since the we already pushed the important value
|
||||
@@ -374,7 +374,7 @@ COSMOVMRESULT cosmoV_pcall(CState *state, int args, int nresults) {
|
||||
state->panic = false;
|
||||
|
||||
if (nresults > 0) {
|
||||
cosmoV_pushValue(state, cosmoV_newObj(state->error));
|
||||
cosmoV_pushObj(state, (CObj*)state->error);
|
||||
|
||||
// push other expected results onto the stack
|
||||
for (int i = 0; i < nresults-1; i++)
|
||||
@@ -404,10 +404,10 @@ static inline bool isFalsey(StkPtr val) {
|
||||
return IS_NIL(*val) || (IS_BOOLEAN(*val) && !cosmoV_readBoolean(*val));
|
||||
}
|
||||
|
||||
COSMO_API void cosmoV_makeObject(CState *state, int pairs) {
|
||||
COSMO_API CObjObject* cosmoV_makeObject(CState *state, int pairs) {
|
||||
StkPtr key, val;
|
||||
CObjObject *newObj = cosmoO_newObject(state);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj)); // so our GC doesn't free our new object
|
||||
cosmoV_pushObj(state, (CObj*)newObj); // so our GC doesn't free our new object
|
||||
|
||||
for (int i = 0; i < pairs; i++) {
|
||||
val = cosmoV_getTop(state, (i*2) + 1);
|
||||
@@ -420,13 +420,20 @@ COSMO_API void cosmoV_makeObject(CState *state, int pairs) {
|
||||
|
||||
// once done, pop everything off the stack + push new object
|
||||
cosmoV_setTop(state, (pairs * 2) + 1); // + 1 for our object
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj));
|
||||
cosmoV_pushObj(state, (CObj*)newObj);
|
||||
return newObj;
|
||||
}
|
||||
|
||||
COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj) {
|
||||
bool replaced = state->protoObjects[objType] != NULL;
|
||||
state->protoObjects[objType] = obj;
|
||||
return replaced;
|
||||
}
|
||||
|
||||
COSMO_API void cosmoV_makeTable(CState *state, int pairs) {
|
||||
StkPtr key, val;
|
||||
CObjTable *newObj = cosmoO_newTable(state);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj)); // so our GC doesn't free our new table
|
||||
cosmoV_pushObj(state, (CObj*)newObj); // so our GC doesn't free our new table
|
||||
|
||||
for (int i = 0; i < pairs; i++) {
|
||||
val = cosmoV_getTop(state, (i*2) + 1);
|
||||
@@ -439,7 +446,7 @@ COSMO_API void cosmoV_makeTable(CState *state, int pairs) {
|
||||
|
||||
// once done, pop everything off the stack + push new table
|
||||
cosmoV_setTop(state, (pairs * 2) + 1); // + 1 for our table
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj));
|
||||
cosmoV_pushObj(state, (CObj*)newObj);
|
||||
}
|
||||
|
||||
COSMO_API bool cosmoV_get(CState *state, CObj *_obj, CValue key, CValue *val) {
|
||||
@@ -454,7 +461,7 @@ COSMO_API bool cosmoV_get(CState *state, CObj *_obj, CValue key, CValue *val) {
|
||||
}
|
||||
|
||||
// push the object onto the stack so the GC can find it
|
||||
cosmoV_pushValue(state, cosmoV_newObj(object));
|
||||
cosmoV_pushObj(state, (CObj*)object);
|
||||
if (cosmoO_getRawObject(state, object, key, val, _obj)) {
|
||||
// *val now equals the response, pop the object
|
||||
cosmoV_pop(state);
|
||||
@@ -486,7 +493,7 @@ COSMO_API bool cosmoV_getMethod(CState *state, CObj *obj, CValue key, CValue *va
|
||||
// if the result is callable, wrap it in an method
|
||||
if (IS_CALLABLE(*val)) {
|
||||
// push object to stack so the GC can find it
|
||||
cosmoV_pushValue(state, cosmoV_newObj(obj));
|
||||
cosmoV_pushObj(state, (CObj*)obj);
|
||||
CObjMethod *method = cosmoO_newMethod(state, *val, obj);
|
||||
cosmoV_pop(state); // pop the object
|
||||
*val = cosmoV_newObj(method);
|
||||
@@ -643,7 +650,7 @@ int cosmoV_execute(CState *state) {
|
||||
uint16_t index = READUINT();
|
||||
CObjFunction *func = cosmoV_readFunction(constants[index]);
|
||||
CObjClosure *closure = cosmoO_newClosure(state, func);
|
||||
cosmoV_pushValue(state, cosmoV_newObj((CObj*)closure));
|
||||
cosmoV_pushObj(state, (CObj*)closure);
|
||||
|
||||
for (int i = 0; i < closure->upvalueCount; i++) {
|
||||
uint8_t encoding = READBYTE();
|
||||
@@ -673,7 +680,7 @@ int cosmoV_execute(CState *state) {
|
||||
uint16_t pairs = READUINT();
|
||||
StkPtr val;
|
||||
CObjTable *newObj = cosmoO_newTable(state);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj)); // so our GC doesn't free our new table
|
||||
cosmoV_pushObj(state, (CObj*)newObj); // so our GC doesn't free our new table
|
||||
|
||||
for (int i = 0; i < pairs; i++) {
|
||||
val = cosmoV_getTop(state, i + 1);
|
||||
@@ -685,7 +692,7 @@ int cosmoV_execute(CState *state) {
|
||||
|
||||
// once done, pop everything off the stack + push new table
|
||||
cosmoV_setTop(state, pairs + 1); // + 1 for our table
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj));
|
||||
cosmoV_pushObj(state, (CObj*)newObj);
|
||||
continue;
|
||||
}
|
||||
case OP_INDEX: {
|
||||
@@ -851,7 +858,7 @@ int cosmoV_execute(CState *state) {
|
||||
if (cosmoO_getIString(state, proto, ISTRING_ITER, &val)) {
|
||||
cosmoV_pop(state); // pop the object from the stack
|
||||
cosmoV_pushValue(state, val);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(obj));
|
||||
cosmoV_pushObj(state, (CObj*)obj);
|
||||
if (cosmoV_call(state, 1, 1) != COSMOVM_OK) // we expect 1 return value on the stack, the iterable object
|
||||
return -1;
|
||||
|
||||
@@ -871,23 +878,21 @@ int cosmoV_execute(CState *state) {
|
||||
} else if (obj->type == COBJ_TABLE) {
|
||||
CObjTable *tbl = (CObjTable*)obj;
|
||||
|
||||
cosmoV_pushValue(state, cosmoV_newObj(state->iStrings[ISTRING_RESERVED])); // key
|
||||
cosmoV_pushValue(state, cosmoV_newObj(tbl)); // value
|
||||
cosmoV_pushObj(state, (CObj*)state->iStrings[ISTRING_RESERVED]); // key
|
||||
cosmoV_pushObj(state, (CObj*)tbl); // value
|
||||
|
||||
cosmoV_pushString(state, "__next"); // key
|
||||
CObjCFunction *tbl_next = cosmoO_newCFunction(state, _tbl__next);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(tbl_next)); // value
|
||||
cosmoV_pushObj(state, (CObj*)tbl_next); // value
|
||||
|
||||
cosmoV_makeObject(state, 2); // pushes the new object to the stack
|
||||
|
||||
CObjObject *obj = cosmoV_readObject(*(cosmoV_getTop(state, 0)));
|
||||
CObjObject *obj = cosmoV_makeObject(state, 2); // pushes the new object to the stack
|
||||
cosmoO_setUserI(state, obj, 0); // increment for iterator
|
||||
|
||||
// make our CObjMethod for OP_NEXT to call
|
||||
CObjMethod *method = cosmoO_newMethod(state, cosmoV_newObj(tbl_next), (CObj*)obj);
|
||||
|
||||
cosmoV_setTop(state, 2); // pops the object & the tbl
|
||||
cosmoV_pushValue(state, cosmoV_newObj(method)); // pushes the method for OP_NEXT
|
||||
cosmoV_pushObj(state, (CObj*)method); // pushes the method for OP_NEXT
|
||||
} else {
|
||||
cosmoV_error(state, "No proto defined! Couldn't get from type %s", cosmoO_typeStr(obj));
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user