From 0d344f65df001943ce48b4c0a5cb5498871f1c0d Mon Sep 17 00:00:00 2001 From: CPunch Date: Sun, 7 Feb 2021 14:05:53 -0600 Subject: [PATCH] renamed cosmoV_pushObj->cosmoV_pushRef & cosmoV_newObj->cosmoV_newRef --- src/cbaselib.c | 8 ++++---- src/cobj.c | 24 +++++++++++----------- src/cparse.c | 8 ++++---- src/cvalue.h | 4 ++-- src/cvm.c | 54 +++++++++++++++++++++++++------------------------- src/cvm.h | 11 +++++----- src/main.c | 2 +- 7 files changed, 56 insertions(+), 55 deletions(-) diff --git a/src/cbaselib.c b/src/cbaselib.c index 536f440..ef41d25 100644 --- a/src/cbaselib.c +++ b/src/cbaselib.c @@ -83,7 +83,7 @@ int cosmoB_tostring(CState *state, int nargs, CValue *args) { return 0; } - cosmoV_pushObj(state, (CObj*)cosmoV_toString(state, args[0])); + cosmoV_pushRef(state, (CObj*)cosmoV_toString(state, args[0])); return 1; } @@ -162,7 +162,7 @@ int cosmoB_ogetProto(CState *state, int nargs, CValue *args) { return 0; } - cosmoV_pushObj(state, (CObj*)cosmoV_readObject(args[0])->_obj.proto); // just return the proto + cosmoV_pushRef(state, (CObj*)cosmoV_readObject(args[0])->_obj.proto); // just return the proto return 1; // 1 result } @@ -528,7 +528,7 @@ void cosmoB_loadMathLib(CState *state) { // vm.__getter["globals"] int cosmoB_vgetGlobal(CState *state, int nargs, CValue *args) { // this function doesn't need to check anything, just return the global table - cosmoV_pushObj(state, (CObj*)state->globals); + cosmoV_pushRef(state, (CObj*)state->globals); return 1; } @@ -569,7 +569,7 @@ int cosmoB_vindexBProto(CState *state, int nargs, CValue *args) { } if (state->protoObjects[indx] != NULL) - cosmoV_pushObj(state, (CObj*)state->protoObjects[indx]); + cosmoV_pushRef(state, (CObj*)state->protoObjects[indx]); else cosmoV_pushNil(state); diff --git a/src/cobj.c b/src/cobj.c index 41ae8b7..bb8ae58 100644 --- a/src/cobj.c +++ b/src/cobj.c @@ -116,7 +116,7 @@ CObjObject *cosmoO_newObject(CState *state) { obj->userP = NULL; // reserved for C API obj->userT = 0; obj->isLocked = false; - cosmoV_pushObj(state, (CObj*)obj); // so our GC can keep track of it + cosmoV_pushRef(state, (CObj*)obj); // so our GC can keep track of it cosmoT_initTable(state, &obj->tbl, ARRAY_START); cosmoV_pop(state); @@ -127,7 +127,7 @@ CObjTable *cosmoO_newTable(CState *state) { CObjTable *obj = (CObjTable*)cosmoO_allocateBase(state, sizeof(CObjTable), COBJ_TABLE); // init the table (might cause a GC event) - cosmoV_pushObj(state, (CObj*)obj); // so our GC can keep track of obj + cosmoV_pushRef(state, (CObj*)obj); // so our GC can keep track of obj cosmoT_initTable(state, &obj->tbl, ARRAY_START); cosmoV_pop(state); @@ -238,8 +238,8 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin strObj->hash = hash; // we push & pop the string so our GC can find it (we don't use freezeGC/unfreezeGC because we *want* a GC event to happen) - cosmoV_pushObj(state, (CObj*)strObj); - cosmoT_insert(state, &state->strings, cosmoV_newObj((CObj*)strObj)); + cosmoV_pushRef(state, (CObj*)strObj); + cosmoT_insert(state, &state->strings, cosmoV_newRef((CObj*)strObj)); cosmoV_pop(state); return strObj; @@ -314,7 +314,7 @@ bool cosmoO_getRawObject(CState *state, CObjObject *proto, CValue key, CValue *v if (!cosmoT_get(&proto->tbl, key, val)) { // if the field doesn't exist in the object, check the proto if (cosmoO_getIString(state, proto, ISTRING_GETTER, val) && IS_TABLE(*val) && cosmoT_get(&cosmoV_readTable(*val)->tbl, key, val)) { cosmoV_pushValue(state, *val); // push function - cosmoV_pushObj(state, (CObj*)obj); // push object + cosmoV_pushRef(state, (CObj*)obj); // push object if (cosmoV_call(state, 1, 1) != COSMOVM_OK) // call the function with the 1 argument return false; *val = *cosmoV_pop(state); // set value to the return value of __index @@ -343,7 +343,7 @@ void cosmoO_setRawObject(CState *state, CObjObject *proto, CValue key, CValue va // check for __setters if (cosmoO_getIString(state, proto, ISTRING_SETTER, &ret) && IS_TABLE(ret) && cosmoT_get(&cosmoV_readTable(ret)->tbl, key, &ret)) { cosmoV_pushValue(state, ret); // push function - cosmoV_pushObj(state, (CObj*)obj); // push object + cosmoV_pushRef(state, (CObj*)obj); // push object cosmoV_pushValue(state, val); // push new value cosmoV_call(state, 2, 0); return; @@ -397,7 +397,7 @@ bool rawgetIString(CState *state, CObjObject *object, int flag, CValue *val) { if (readFlag(object->istringFlags, flag)) return false; // it's been cached as bad - if (!cosmoT_get(&object->tbl, cosmoV_newObj(state->iStrings[flag]), val)) { + if (!cosmoT_get(&object->tbl, cosmoV_newRef(state->iStrings[flag]), val)) { // mark it bad! setFlagOn(object->istringFlags, flag); return false; @@ -420,7 +420,7 @@ bool cosmoO_getIString(CState *state, CObjObject *object, int flag, CValue *val) bool cosmoO_indexObject(CState *state, CObjObject *object, CValue key, CValue *val) { if (cosmoO_getIString(state, object, ISTRING_INDEX, val)) { cosmoV_pushValue(state, *val); // push function - cosmoV_pushObj(state, (CObj*)object); // push object + cosmoV_pushRef(state, (CObj*)object); // push object cosmoV_pushValue(state, key); // push key if (cosmoV_call(state, 2, 1) != COSMOVM_OK) // call the function with the 2 arguments return false; @@ -438,7 +438,7 @@ bool cosmoO_newIndexObject(CState *state, CObjObject *object, CValue key, CValue if (cosmoO_getIString(state, object, ISTRING_NEWINDEX, &ret)) { cosmoV_pushValue(state, ret); // push function - cosmoV_pushObj(state, (CObj*)object); // push object + cosmoV_pushRef(state, (CObj*)object); // push object cosmoV_pushValue(state, key); // push key & value pair cosmoV_pushValue(state, val); return cosmoV_call(state, 3, 0) == COSMOVM_OK; @@ -456,7 +456,7 @@ CObjString *cosmoO_toString(CState *state, CObj *obj) { // use user-defined __tostring if (protoObject != NULL && cosmoO_getIString(state, protoObject, ISTRING_TOSTRING, &res)) { cosmoV_pushValue(state, res); - cosmoV_pushObj(state, (CObj*)obj); + cosmoV_pushRef(state, (CObj*)obj); if (cosmoV_call(state, 1, 1) != COSMOVM_OK) return cosmoO_copyString(state, "", 5); @@ -518,7 +518,7 @@ cosmo_Number cosmoO_toNumber(CState *state, CObj *obj) { if (proto != NULL && cosmoO_getIString(state, proto, ISTRING_TONUMBER, &res)) { cosmoV_pushValue(state, res); - cosmoV_pushObj(state, (CObj*)obj); + cosmoV_pushRef(state, (CObj*)obj); if (cosmoV_call(state, 1, 1) != COSMOVM_OK) // call res, expect 1 return val of return 0; @@ -549,7 +549,7 @@ int cosmoO_count(CState *state, CObj *obj) { if (proto != NULL && cosmoO_getIString(state, proto, ISTRING_COUNT, &res)) { cosmoV_pushValue(state, res); - cosmoV_pushObj(state, (CObj*)obj); + cosmoV_pushRef(state, (CObj*)obj); if (cosmoV_call(state, 1, 1) != COSMOVM_OK) // call res, we expect 1 return value of type return 0; diff --git a/src/cparse.c b/src/cparse.c index 950264d..bc7a8ee 100644 --- a/src/cparse.c +++ b/src/cparse.c @@ -296,7 +296,7 @@ void patchJmp(CParseState *pstate, int index) { } static uint16_t identifierConstant(CParseState *pstate, CToken *name) { - return makeConstant(pstate, cosmoV_newObj((CObj*)cosmoO_copyString(pstate->state, name->start, name->length))); + return makeConstant(pstate, cosmoV_newRef((CObj*)cosmoO_copyString(pstate->state, name->start, name->length))); } static void addLocal(CParseState *pstate, CToken name) { @@ -419,7 +419,7 @@ static void binnumber(CParseState *pstate, bool canAssign, Precedence prec) { static void string(CParseState *pstate, bool canAssign, Precedence prec) { CObjString *strObj = cosmoO_takeString(pstate->state, pstate->previous.start, pstate->previous.length); - writeConstant(pstate, cosmoV_newObj((CObj*)strObj)); + writeConstant(pstate, cosmoV_newRef((CObj*)strObj)); } static void literal(CParseState *pstate, bool canAssign, Precedence prec) { @@ -1296,7 +1296,7 @@ static void function(CParseState *pstate, FunctionType type) { // push closure writeu8(pstate, OP_CLOSURE); - writeu16(pstate, makeConstant(pstate, cosmoV_newObj(objFunc))); + writeu16(pstate, makeConstant(pstate, cosmoV_newRef(objFunc))); valuePushed(pstate, 1); // tell the vm what locals/upvalues to pass to this closure @@ -1651,7 +1651,7 @@ CObjFunction* cosmoP_compileString(CState *state, const char *source, const char freeParseState(&parser); // push the funciton onto the stack so if we cause an GC event, it won't be free'd - cosmoV_pushObj(state, (CObj*)resFunc); + cosmoV_pushRef(state, (CObj*)resFunc); cosmoM_unfreezeGC(state); cosmoV_pop(state); return resFunc; diff --git a/src/cvalue.h b/src/cvalue.h index 6dafbb9..7f9afbd 100644 --- a/src/cvalue.h +++ b/src/cvalue.h @@ -52,7 +52,7 @@ typedef union CValue { #define cosmoV_newNumber(x) ((CValue){.num = x}) #define cosmoV_newBoolean(x) ((CValue){.data = MAKE_PAYLOAD(x) | BOOL_SIG}) -#define cosmoV_newObj(x) ((CValue){.data = MAKE_PAYLOAD((uintptr_t)x) | OBJ_SIG}) +#define cosmoV_newRef(x) ((CValue){.data = MAKE_PAYLOAD((uintptr_t)x) | OBJ_SIG}) #define cosmoV_newNil() ((CValue){.data = NIL_SIG}) #define cosmoV_readNumber(x) ((x).num) @@ -83,7 +83,7 @@ typedef struct CValue { #define cosmoV_newNumber(x) ((CValue){COSMO_TNUMBER, {.num = (x)}}) #define cosmoV_newBoolean(x) ((CValue){COSMO_TBOOLEAN, {.b = (x)}}) -#define cosmoV_newObj(x) ((CValue){COSMO_TOBJ, {.obj = (CObj*)(x)}}) +#define cosmoV_newRef(x) ((CValue){COSMO_TOBJ, {.obj = (CObj*)(x)}}) #define cosmoV_newNil() ((CValue){COSMO_TNIL, {.num = 0}}) // read CValues diff --git a/src/cvm.c b/src/cvm.c index 4814900..91dbd4a 100644 --- a/src/cvm.c +++ b/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_pushObj(state, (CObj*)func); - *(cosmoV_getTop(state, 0)) = cosmoV_newObj(cosmoO_newClosure(state, func)); + cosmoV_pushRef(state, (CObj*)func); + *(cosmoV_getTop(state, 0)) = cosmoV_newRef(cosmoO_newClosure(state, func)); return true; } // fail state->panic = false; - cosmoV_pushObj(state, (CObj*)state->error); + cosmoV_pushRef(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_pushObj(state, (CObj*)result); // so our GC can find our current result string + cosmoV_pushRef(state, (CObj*)result); // so our GC can find our current result string CObjString *otherStr = cosmoV_toString(state, *current); - cosmoV_pushObj(state, (CObj*)otherStr); // also so our GC won't free otherStr + cosmoV_pushRef(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_pushObj(state, (CObj*)result); + cosmoV_pushRef(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_readRef(func); CValue ret; - cosmoV_pushObj(state, (CObj*)protoObj); // push proto to stack for GC to find + cosmoV_pushRef(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_pushObj(state, (CObj*)newObj); + cosmoV_pushRef(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 @@ -360,7 +360,7 @@ bool callCValue(CState *state, CValue func, int args, int nresults, int offset) bool invokeMethod(CState* state, CObj *obj, CValue func, int args, int nresults, int offset) { // first, set the first argument to the object StkPtr temp = cosmoV_getTop(state, args); - *temp = cosmoV_newObj(obj); + *temp = cosmoV_newRef(obj); return callCValue(state, func, args+1, nresults, offset); } @@ -374,7 +374,7 @@ COSMOVMRESULT cosmoV_pcall(CState *state, int args, int nresults) { state->panic = false; if (nresults > 0) { - cosmoV_pushObj(state, (CObj*)state->error); + cosmoV_pushRef(state, (CObj*)state->error); // push other expected results onto the stack for (int i = 0; i < nresults-1; i++) @@ -407,7 +407,7 @@ static inline bool isFalsey(StkPtr val) { COSMO_API CObjObject* cosmoV_makeObject(CState *state, int pairs) { StkPtr key, val; CObjObject *newObj = cosmoO_newObject(state); - cosmoV_pushObj(state, (CObj*)newObj); // so our GC doesn't free our new object + cosmoV_pushRef(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,7 +420,7 @@ COSMO_API CObjObject* 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_pushObj(state, (CObj*)newObj); + cosmoV_pushRef(state, (CObj*)newObj); return newObj; } @@ -433,7 +433,7 @@ COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjO COSMO_API void cosmoV_makeTable(CState *state, int pairs) { StkPtr key, val; CObjTable *newObj = cosmoO_newTable(state); - cosmoV_pushObj(state, (CObj*)newObj); // so our GC doesn't free our new table + cosmoV_pushRef(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); @@ -446,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_pushObj(state, (CObj*)newObj); + cosmoV_pushRef(state, (CObj*)newObj); } COSMO_API bool cosmoV_get(CState *state, CObj *_obj, CValue key, CValue *val) { @@ -461,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_pushObj(state, (CObj*)object); + cosmoV_pushRef(state, (CObj*)object); if (cosmoO_getRawObject(state, object, key, val, _obj)) { // *val now equals the response, pop the object cosmoV_pop(state); @@ -493,10 +493,10 @@ 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_pushObj(state, (CObj*)obj); + cosmoV_pushRef(state, (CObj*)obj); CObjMethod *method = cosmoO_newMethod(state, *val, obj); cosmoV_pop(state); // pop the object - *val = cosmoV_newObj(method); + *val = cosmoV_newRef(method); } return true; @@ -651,7 +651,7 @@ int cosmoV_execute(CState *state) { uint16_t index = READUINT(); CObjFunction *func = cosmoV_readFunction(constants[index]); CObjClosure *closure = cosmoO_newClosure(state, func); - cosmoV_pushObj(state, (CObj*)closure); + cosmoV_pushRef(state, (CObj*)closure); for (int i = 0; i < closure->upvalueCount; i++) { uint8_t encoding = READBYTE(); @@ -681,7 +681,7 @@ int cosmoV_execute(CState *state) { uint16_t pairs = READUINT(); StkPtr val; CObjTable *newObj = cosmoO_newTable(state); - cosmoV_pushObj(state, (CObj*)newObj); // so our GC doesn't free our new table + cosmoV_pushRef(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); @@ -693,7 +693,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_pushObj(state, (CObj*)newObj); + cosmoV_pushRef(state, (CObj*)newObj); continue; } case OP_INDEX: { @@ -859,7 +859,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_pushObj(state, (CObj*)obj); + cosmoV_pushRef(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,7 +871,7 @@ int cosmoV_execute(CState *state) { } // get __next method and place it at the top of the stack - cosmoV_getMethod(state, cosmoV_readRef(*iObj), cosmoV_newObj(state->iStrings[ISTRING_NEXT]), iObj); + cosmoV_getMethod(state, cosmoV_readRef(*iObj), cosmoV_newRef(state->iStrings[ISTRING_NEXT]), iObj); } else { cosmoV_error(state, "Expected iterable object! '__iter' not defined!"); return -1; @@ -879,21 +879,21 @@ int cosmoV_execute(CState *state) { } else if (obj->type == COBJ_TABLE) { CObjTable *tbl = (CObjTable*)obj; - cosmoV_pushObj(state, (CObj*)state->iStrings[ISTRING_RESERVED]); // key - cosmoV_pushObj(state, (CObj*)tbl); // value + cosmoV_pushRef(state, (CObj*)state->iStrings[ISTRING_RESERVED]); // key + cosmoV_pushRef(state, (CObj*)tbl); // value cosmoV_pushString(state, "__next"); // key CObjCFunction *tbl_next = cosmoO_newCFunction(state, _tbl__next); - cosmoV_pushObj(state, (CObj*)tbl_next); // value + cosmoV_pushRef(state, (CObj*)tbl_next); // value CObjObject *obj = cosmoV_makeObject(state, 2); // pushes the new object to the stack cosmoO_setUserI(obj, 0); // increment for iterator // make our CObjMethod for OP_NEXT to call - CObjMethod *method = cosmoO_newMethod(state, cosmoV_newObj(tbl_next), (CObj*)obj); + CObjMethod *method = cosmoO_newMethod(state, cosmoV_newRef(tbl_next), (CObj*)obj); cosmoV_setTop(state, 2); // pops the object & the tbl - cosmoV_pushObj(state, (CObj*)method); // pushes the method for OP_NEXT + cosmoV_pushRef(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; diff --git a/src/cvm.h b/src/cvm.h index 70847c7..35b9a16 100644 --- a/src/cvm.h +++ b/src/cvm.h @@ -47,7 +47,7 @@ COSMO_API bool cosmoV_getMethod(CState *state, CObj *obj, CValue key, CValue *va // nice to have wrappers -// pushes value to the stack +// pushes raw CValue to the stack static inline void cosmoV_pushValue(CState *state, CValue val) { #ifdef SAFE_STACK ptrdiff_t stackSize = state->top - state->stack; @@ -95,17 +95,18 @@ static inline void cosmoV_pushBoolean(CState *state, bool b) { cosmoV_pushValue(state, cosmoV_newBoolean(b)); } -static inline void cosmoV_pushObj(CState *state, CObj *obj) { - cosmoV_pushValue(state, cosmoV_newObj(obj)); +static inline void cosmoV_pushRef(CState *state, CObj *obj) { + cosmoV_pushValue(state, cosmoV_newRef(obj)); } // pushes a C Function to the stack static inline void cosmoV_pushCFunction(CState *state, CosmoCFunction func) { - cosmoV_pushObj(state, (CObj*)cosmoO_newCFunction(state, func)); + cosmoV_pushRef(state, (CObj*)cosmoO_newCFunction(state, func)); } +// len is the length of the string without the NULL terminator static inline void cosmoV_pushLString(CState *state, const char *str, size_t len) { - cosmoV_pushObj(state, (CObj*)cosmoO_copyString(state, str, len)); + cosmoV_pushRef(state, (CObj*)cosmoO_copyString(state, str, len)); } // accepts a null terminated string and copys the buffer to the VM heap diff --git a/src/main.c b/src/main.c index 2e44349..cb0de1c 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,7 @@ int cosmoB_input(CState *state, int nargs, CValue *args) { char line[1024]; fgets(line, sizeof(line), stdin); - cosmoV_pushObj(state, (CObj*)cosmoO_copyString(state, line, strlen(line)-1)); // -1 for the \n + cosmoV_pushRef(state, (CObj*)cosmoO_copyString(state, line, strlen(line)-1)); // -1 for the \n return 1; // 1 return value }