Renamed cosmoV_readObj to cosmoV_readRef to reduce ambiguity

also fixed several typos
This commit is contained in:
CPunch 2021-02-07 14:00:00 -06:00
parent 5faa40bdef
commit 8d37f1f243
10 changed files with 48 additions and 48 deletions

View File

@ -145,7 +145,7 @@ void cosmoB_loadLibrary(CState *state) {
int cosmoB_osetProto(CState *state, int nargs, CValue *args) { int cosmoB_osetProto(CState *state, int nargs, CValue *args) {
if (nargs == 2) { if (nargs == 2) {
CObj *obj = cosmoV_readObj(args[0]); // object to set proto too CObj *obj = cosmoV_readRef(args[0]); // object to set proto too
CObjObject *proto = cosmoV_readObject(args[1]); CObjObject *proto = cosmoV_readObject(args[1]);
obj->proto = proto; // boom done obj->proto = proto; // boom done
@ -174,11 +174,11 @@ int cosmoB_oisChild(CState *state, int nargs, CValue *args) {
} }
if (!IS_OBJ(args[0]) || !IS_OBJECT(args[1])) { if (!IS_OBJ(args[0]) || !IS_OBJECT(args[1])) {
cosmoV_typeError(state, "oject.ischild()", "<reference obj>, <object>", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1])); cosmoV_typeError(state, "object.ischild()", "<reference obj>, <object>", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
return 0; return 0;
} }
CObj *obj = cosmoV_readObj(args[0]); CObj *obj = cosmoV_readRef(args[0]);
CObjObject *proto = cosmoV_readObject(args[1]); CObjObject *proto = cosmoV_readObject(args[1]);
// push result // push result
@ -545,7 +545,7 @@ int cosmoB_vsetGlobal(CState *state, int nargs, CValue *args) {
} }
// this makes me very nervous ngl // this makes me very nervous ngl
CObjTable *tbl = (CObjTable*)cosmoV_readObj(args[1]); CObjTable *tbl = (CObjTable*)cosmoV_readRef(args[1]);
state->globals = tbl; state->globals = tbl;
return 0; return 0;
} }

View File

@ -109,7 +109,7 @@ int disasmInstr(CChunk *chunk, int offset, int indent) {
int index = readu16Chunk(chunk, offset + 1); int index = readu16Chunk(chunk, offset + 1);
printf("%-16s [%05d] - ", "OP_CLOSURE", index); printf("%-16s [%05d] - ", "OP_CLOSURE", index);
CValue val = chunk->constants.values[index]; CValue val = chunk->constants.values[index];
CObjFunction *cobjFunc = (CObjFunction*)cosmoV_readObj(val); CObjFunction *cobjFunc = (CObjFunction*)cosmoV_readRef(val);
offset += 3; // we consumed the opcode + u16 offset += 3; // we consumed the opcode + u16
printValue(val); printValue(val);

View File

@ -71,7 +71,7 @@ void tableRemoveWhite(CState *state, CTable *tbl) {
int cap = tbl->capacityMask + 1; int cap = tbl->capacityMask + 1;
for (int i = 0; i < cap; i++) { for (int i = 0; i < cap; i++) {
CTableEntry *entry = &tbl->table[i]; CTableEntry *entry = &tbl->table[i];
if (IS_OBJ(entry->key) && !(cosmoV_readObj(entry->key))->isMarked) { // if the key is a object and it's white (unmarked), remove it from the table if (IS_OBJ(entry->key) && !(cosmoV_readRef(entry->key))->isMarked) { // if the key is a object and it's white (unmarked), remove it from the table
cosmoT_remove(state, tbl, entry->key); cosmoT_remove(state, tbl, entry->key);
} }
} }
@ -175,7 +175,7 @@ void markObject(CState *state, CObj *obj) {
void markValue(CState *state, CValue val) { void markValue(CState *state, CValue val) {
if (IS_OBJ(val)) if (IS_OBJ(val))
markObject(state, cosmoV_readObj(val)); markObject(state, cosmoV_readRef(val));
} }
// trace our gray references // trace our gray references

View File

@ -469,7 +469,7 @@ CObjString *cosmoO_toString(CState *state, CObj *obj) {
// return string // return string
cosmoV_pop(state); cosmoV_pop(state);
return (CObjString*)cosmoV_readObj(*ret); return (CObjString*)cosmoV_readRef(*ret);
} }
switch (obj->type) { switch (obj->type) {

View File

@ -119,18 +119,18 @@ typedef struct CObjUpval {
#define IS_METHOD(x) isObjType(x, COBJ_METHOD) #define IS_METHOD(x) isObjType(x, COBJ_METHOD)
#define IS_CLOSURE(x) isObjType(x, COBJ_CLOSURE) #define IS_CLOSURE(x) isObjType(x, COBJ_CLOSURE)
#define cosmoV_readString(x) ((CObjString*)cosmoV_readObj(x)) #define cosmoV_readString(x) ((CObjString*)cosmoV_readRef(x))
#define cosmoV_readObject(x) ((CObjObject*)cosmoV_readObj(x)) #define cosmoV_readObject(x) ((CObjObject*)cosmoV_readRef(x))
#define cosmoV_readTable(x) ((CObjTable*)cosmoV_readObj(x)) #define cosmoV_readTable(x) ((CObjTable*)cosmoV_readRef(x))
#define cosmoV_readFunction(x) ((CObjFunction*)cosmoV_readObj(x)) #define cosmoV_readFunction(x) ((CObjFunction*)cosmoV_readRef(x))
#define cosmoV_readCFunction(x) (((CObjCFunction*)cosmoV_readObj(x))->cfunc) #define cosmoV_readCFunction(x) (((CObjCFunction*)cosmoV_readRef(x))->cfunc)
#define cosmoV_readMethod(x) ((CObjMethod*)cosmoV_readObj(x)) #define cosmoV_readMethod(x) ((CObjMethod*)cosmoV_readRef(x))
#define cosmoV_readClosure(x) ((CObjClosure*)cosmoV_readObj(x)) #define cosmoV_readClosure(x) ((CObjClosure*)cosmoV_readRef(x))
#define cosmoO_readCString(x) ((CObjString*)x)->str #define cosmoO_readCString(x) ((CObjString*)x)->str
static inline bool isObjType(CValue val, CObjType type) { static inline bool isObjType(CValue val, CObjType type) {
return IS_OBJ(val) && cosmoV_readObj(val)->type == type; return IS_OBJ(val) && cosmoV_readRef(val)->type == type;
} }
// just protects against macro expansion // just protects against macro expansion

View File

@ -15,11 +15,8 @@
this will produce undefined behavior as you reach the stack limit (and may cause a seg fault!). It is recommended to keep this enabled. this will produce undefined behavior as you reach the stack limit (and may cause a seg fault!). It is recommended to keep this enabled.
*/ */
#define SAFE_STACK #define SAFE_STACK
//#define NAN_BOXXED //#define NAN_BOXXED
#define COSMOASSERT(x) assert(x)
// forward declare *most* stuff so our headers are cleaner // forward declare *most* stuff so our headers are cleaner
typedef struct CState CState; typedef struct CState CState;
typedef struct CChunk CChunk; typedef struct CChunk CChunk;
@ -49,6 +46,7 @@ typedef uint8_t INSTRUCTION;
#define COSMO_API extern #define COSMO_API extern
#define UNNAMEDCHUNK "_main" #define UNNAMEDCHUNK "_main"
#define COSMOASSERT(x) assert(x)
#define CERROR(err) \ #define CERROR(err) \
printf("%s : %s\n", "[ERROR]", err) printf("%s : %s\n", "[ERROR]", err)

View File

@ -67,7 +67,7 @@ uint32_t getObjectHash(CObj *obj) {
uint32_t getValueHash(CValue *val) { uint32_t getValueHash(CValue *val) {
switch (GET_TYPE(*val)) { switch (GET_TYPE(*val)) {
case COSMO_TOBJ: case COSMO_TOBJ:
return getObjectHash(cosmoV_readObj(*val)); return getObjectHash(cosmoV_readRef(*val));
case COSMO_TNUMBER: { case COSMO_TNUMBER: {
uint32_t buf[sizeof(cosmo_Number)/sizeof(uint32_t)]; uint32_t buf[sizeof(cosmo_Number)/sizeof(uint32_t)];
cosmo_Number num = cosmoV_readNumber(*val); cosmo_Number num = cosmoV_readNumber(*val);
@ -238,7 +238,7 @@ CObjString *cosmoT_lookupString(CTable *tbl, const char *str, int length, uint32
return NULL; return NULL;
} else if (IS_STRING(entry->key) && cosmoV_readString(entry->key)->length == length && memcmp(cosmoV_readString(entry->key)->str, str, length) == 0) { } else if (IS_STRING(entry->key) && cosmoV_readString(entry->key)->length == length && memcmp(cosmoV_readString(entry->key)->str, str, length) == 0) {
// it's a match! // it's a match!
return (CObjString*)cosmoV_readObj(entry->key); return (CObjString*)cosmoV_readRef(entry->key);
} }
indx = (indx + 1) & tbl->capacityMask; // fast mod here too indx = (indx + 1) & tbl->capacityMask; // fast mod here too

View File

@ -27,7 +27,7 @@ bool cosmoV_equal(CValue valA, CValue valB) {
switch (GET_TYPE(valA)) { switch (GET_TYPE(valA)) {
case COSMO_TBOOLEAN: return cosmoV_readBoolean(valA) == cosmoV_readBoolean(valB); case COSMO_TBOOLEAN: return cosmoV_readBoolean(valA) == cosmoV_readBoolean(valB);
case COSMO_TNUMBER: return cosmoV_readNumber(valA) == cosmoV_readNumber(valB); case COSMO_TNUMBER: return cosmoV_readNumber(valA) == cosmoV_readNumber(valB);
case COSMO_TOBJ: return cosmoO_equal(cosmoV_readObj(valA), cosmoV_readObj(valB)); case COSMO_TOBJ: return cosmoO_equal(cosmoV_readRef(valA), cosmoV_readRef(valB));
case COSMO_TNIL: return true; case COSMO_TNIL: return true;
default: default:
return false; return false;
@ -45,7 +45,7 @@ CObjString *cosmoV_toString(CState *state, CValue val) {
return cosmoV_readBoolean(val) ? cosmoO_copyString(state, "true", 4) : cosmoO_copyString(state, "false", 5); return cosmoV_readBoolean(val) ? cosmoO_copyString(state, "true", 4) : cosmoO_copyString(state, "false", 5);
} }
case COSMO_TOBJ: { case COSMO_TOBJ: {
return cosmoO_toString(state, cosmoV_readObj(val)); return cosmoO_toString(state, cosmoV_readRef(val));
} }
case COSMO_TNIL: { case COSMO_TNIL: {
return cosmoO_copyString(state, "nil", 3); return cosmoO_copyString(state, "nil", 3);
@ -64,7 +64,7 @@ cosmo_Number cosmoV_toNumber(CState *state, CValue val) {
return cosmoV_readBoolean(val) ? 1 : 0; return cosmoV_readBoolean(val) ? 1 : 0;
} }
case COSMO_TOBJ: { case COSMO_TOBJ: {
return cosmoO_toNumber(state, cosmoV_readObj(val)); return cosmoO_toNumber(state, cosmoV_readRef(val));
} }
case COSMO_TNIL: // fall through case COSMO_TNIL: // fall through
default: default:
@ -77,7 +77,7 @@ const char *cosmoV_typeStr(CValue val) {
case COSMO_TNIL: return "<nil>"; case COSMO_TNIL: return "<nil>";
case COSMO_TBOOLEAN: return "<bool>"; case COSMO_TBOOLEAN: return "<bool>";
case COSMO_TNUMBER: return "<number>"; case COSMO_TNUMBER: return "<number>";
case COSMO_TOBJ: return cosmoO_typeStr(cosmoV_readObj(val)); case COSMO_TOBJ: return cosmoO_typeStr(cosmoV_readRef(val));
default: default:
return "<unkn val>"; return "<unkn val>";
@ -93,7 +93,7 @@ void printValue(CValue val) {
printf(cosmoV_readBoolean(val) ? "true" : "false"); printf(cosmoV_readBoolean(val) ? "true" : "false");
break; break;
case COSMO_TOBJ: { case COSMO_TOBJ: {
printObject(cosmoV_readObj(val)); printObject(cosmoV_readRef(val));
break; break;
} }
case COSMO_TNIL: case COSMO_TNIL:

View File

@ -57,7 +57,7 @@ typedef union CValue {
#define cosmoV_readNumber(x) ((x).num) #define cosmoV_readNumber(x) ((x).num)
#define cosmoV_readBoolean(x) ((bool)READ_PAYLOAD(x)) #define cosmoV_readBoolean(x) ((bool)READ_PAYLOAD(x))
#define cosmoV_readObj(x) ((CObj*)READ_PAYLOAD(x)) #define cosmoV_readRef(x) ((CObj*)READ_PAYLOAD(x))
#define IS_NUMBER(x) (((x).data & MASK_QUIETNAN) != MASK_QUIETNAN) #define IS_NUMBER(x) (((x).data & MASK_QUIETNAN) != MASK_QUIETNAN)
#define IS_BOOLEAN(x) (((x).data & SIG_MASK) == BOOL_SIG) #define IS_BOOLEAN(x) (((x).data & SIG_MASK) == BOOL_SIG)
@ -90,7 +90,9 @@ typedef struct CValue {
#define cosmoV_readNumber(x) ((cosmo_Number)(x).val.num) #define cosmoV_readNumber(x) ((cosmo_Number)(x).val.num)
#define cosmoV_readBoolean(x) ((bool)(x).val.b) #define cosmoV_readBoolean(x) ((bool)(x).val.b)
#define cosmoV_readObj(x) ((CObj*)(x).val.obj)
// grabs the CObj* pointer from the CValue
#define cosmoV_readRef(x) ((CObj*)(x).val.obj)
#define IS_NUMBER(x) (GET_TYPE(x) == COSMO_TNUMBER) #define IS_NUMBER(x) (GET_TYPE(x) == COSMO_TNUMBER)
#define IS_BOOLEAN(x) (GET_TYPE(x) == COSMO_TBOOLEAN) #define IS_BOOLEAN(x) (GET_TYPE(x) == COSMO_TBOOLEAN)

View File

@ -311,17 +311,17 @@ bool callCValue(CState *state, CValue func, int args, int nresults, int offset)
return false; return false;
} }
switch (cosmoV_readObj(func)->type) { switch (cosmoV_readRef(func)->type) {
case COBJ_CLOSURE: case COBJ_CLOSURE:
return rawCall(state, cosmoV_readClosure(func), args, nresults, offset); return rawCall(state, cosmoV_readClosure(func), args, nresults, offset);
case COBJ_CFUNCTION: case COBJ_CFUNCTION:
return callCFunction(state, cosmoV_readCFunction(func), args, nresults, offset); return callCFunction(state, cosmoV_readCFunction(func), args, nresults, offset);
case COBJ_METHOD: { case COBJ_METHOD: {
CObjMethod *method = (CObjMethod*)cosmoV_readObj(func); CObjMethod *method = (CObjMethod*)cosmoV_readRef(func);
return invokeMethod(state, method->obj, method->func, args, nresults, offset + 1); return invokeMethod(state, method->obj, method->func, args, nresults, offset + 1);
} }
case COBJ_OBJECT: { // object is being instantiated, making another object case COBJ_OBJECT: { // object is being instantiated, making another object
CObjObject *protoObj = (CObjObject*)cosmoV_readObj(func); CObjObject *protoObj = (CObjObject*)cosmoV_readRef(func);
CValue ret; CValue ret;
cosmoV_pushObj(state, (CObj*)protoObj); // push proto to stack for GC to find cosmoV_pushObj(state, (CObj*)protoObj); // push proto to stack for GC to find
@ -523,7 +523,7 @@ int _tbl__next(CState *state, int nargs, CValue *args) {
return 0; // someone set the __reserved member to something else. this will exit the iterator loop return 0; // someone set the __reserved member to something else. this will exit the iterator loop
} }
CObjTable *table = (CObjTable*)cosmoV_readObj(val); CObjTable *table = (CObjTable*)cosmoV_readRef(val);
// while the entry is invalid, go to the next entry // while the entry is invalid, go to the next entry
int cap = table->tbl.capacityMask + 1; int cap = table->tbl.capacityMask + 1;
@ -706,7 +706,7 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
CObj *obj = cosmoV_readObj(*temp); CObj *obj = cosmoV_readRef(*temp);
CObjObject *proto = cosmoO_grabProto(obj); CObjObject *proto = cosmoO_grabProto(obj);
CValue val; // to hold our value CValue val; // to hold our value
@ -738,7 +738,7 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
CObj *obj = cosmoV_readObj(*temp); CObj *obj = cosmoV_readRef(*temp);
CObjObject *proto = cosmoO_grabProto(obj); CObjObject *proto = cosmoO_grabProto(obj);
if (proto != NULL) { if (proto != NULL) {
@ -770,7 +770,7 @@ int cosmoV_execute(CState *state) {
// sanity check // sanity check
if (IS_OBJ(*temp)) { if (IS_OBJ(*temp)) {
if (!cosmoV_set(state, cosmoV_readObj(*temp), constants[ident], *value)) if (!cosmoV_set(state, cosmoV_readRef(*temp), constants[ident], *value))
return -1; return -1;
} else { } else {
CObjString *field = cosmoV_toString(state, constants[ident]); CObjString *field = cosmoV_toString(state, constants[ident]);
@ -789,7 +789,7 @@ int cosmoV_execute(CState *state) {
// sanity check // sanity check
if (IS_OBJ(*temp)) { if (IS_OBJ(*temp)) {
if (!cosmoV_get(state, cosmoV_readObj(*temp), constants[ident], &val)) if (!cosmoV_get(state, cosmoV_readRef(*temp), constants[ident], &val))
return -1; return -1;
} else { } else {
CObjString *field = cosmoV_toString(state, constants[ident]); CObjString *field = cosmoV_toString(state, constants[ident]);
@ -808,7 +808,7 @@ int cosmoV_execute(CState *state) {
// this is almost identical to GETOBJECT, however cosmoV_getMethod is used instead of just cosmoV_get // this is almost identical to GETOBJECT, however cosmoV_getMethod is used instead of just cosmoV_get
if (IS_OBJ(*temp)) { if (IS_OBJ(*temp)) {
if (!cosmoV_getMethod(state, cosmoV_readObj(*temp), constants[ident], &val)) if (!cosmoV_getMethod(state, cosmoV_readRef(*temp), constants[ident], &val))
return -1; return -1;
} else { } else {
CObjString *field = cosmoV_toString(state, constants[ident]); CObjString *field = cosmoV_toString(state, constants[ident]);
@ -830,11 +830,11 @@ int cosmoV_execute(CState *state) {
// sanity check // sanity check
if (IS_OBJ(*temp)) { if (IS_OBJ(*temp)) {
// get the field from the object // get the field from the object
if (!cosmoV_get(state, cosmoV_readObj(*temp), constants[ident], &val)) if (!cosmoV_get(state, cosmoV_readRef(*temp), constants[ident], &val))
return -1; return -1;
// now invoke the method! // now invoke the method!
invokeMethod(state, cosmoV_readObj(*temp), val, args, nres, 1); invokeMethod(state, cosmoV_readRef(*temp), val, args, nres, 1);
} else { } else {
cosmoV_error(state, "Couldn't get from type %s!", cosmoV_typeStr(*temp)); cosmoV_error(state, "Couldn't get from type %s!", cosmoV_typeStr(*temp));
return -1; return -1;
@ -850,7 +850,7 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
CObj *obj = cosmoV_readObj(*temp); CObj *obj = cosmoV_readRef(*temp);
CObjObject *proto = cosmoO_grabProto(obj); CObjObject *proto = cosmoO_grabProto(obj);
CValue val; CValue val;
@ -871,7 +871,7 @@ int cosmoV_execute(CState *state) {
} }
// get __next method and place it at the top of the stack // get __next method and place it at the top of the stack
cosmoV_getMethod(state, cosmoV_readObj(*iObj), cosmoV_newObj(state->iStrings[ISTRING_NEXT]), iObj); cosmoV_getMethod(state, cosmoV_readRef(*iObj), cosmoV_newObj(state->iStrings[ISTRING_NEXT]), iObj);
} else { } else {
cosmoV_error(state, "Expected iterable object! '__iter' not defined!"); cosmoV_error(state, "Expected iterable object! '__iter' not defined!");
return -1; return -1;
@ -973,7 +973,7 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
int count = cosmoO_count(state, cosmoV_readObj(*temp)); int count = cosmoO_count(state, cosmoV_readRef(*temp));
cosmoV_pop(state); cosmoV_pop(state);
cosmoV_pushNumber(state, count); // pushes the count onto the stack cosmoV_pushNumber(state, count); // pushes the count onto the stack
@ -1043,7 +1043,7 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
CObj *obj = cosmoV_readObj(*temp); CObj *obj = cosmoV_readRef(*temp);
CObjObject *proto = cosmoO_grabProto(obj); CObjObject *proto = cosmoO_grabProto(obj);
CValue val; CValue val;
@ -1090,7 +1090,7 @@ int cosmoV_execute(CState *state) {
// sanity check // sanity check
if (IS_OBJ(*temp)) { if (IS_OBJ(*temp)) {
CObj *obj = cosmoV_readObj(*temp); CObj *obj = cosmoV_readRef(*temp);
CValue val; CValue val;
if (!cosmoV_get(state, obj, ident, &val)) if (!cosmoV_get(state, obj, ident, &val))