Compare commits

...

2 Commits

Author SHA1 Message Date
39060a67e9 fix cosmoB_loadObjLib()
- wrong # of pairs passed to cosmoV_makeTable
- minor refactoring of cmem.c:sweep()
2023-12-26 12:27:45 -06:00
5296495e47 formatting fixes 2023-12-26 10:59:42 -06:00
7 changed files with 27 additions and 23 deletions

View File

@ -243,7 +243,7 @@ COSMO_API void cosmoB_loadObjLib(CState *state)
cosmoV_pushString(state, "__proto"); // key cosmoV_pushString(state, "__proto"); // key
cosmoV_pushCFunction(state, cosmoB_ogetProto); // value cosmoV_pushCFunction(state, cosmoB_ogetProto); // value
cosmoV_makeTable(state, 2); cosmoV_makeTable(state, 1);
// make __setter table // make __setter table
cosmoV_pushString(state, "__setter"); cosmoV_pushString(state, "__setter");
@ -337,7 +337,8 @@ int fileB_read(CState *state, int nargs, CValue *args)
return 1; return 1;
} }
int fileB_write(CState *state, int nargs, CValue *args) { int fileB_write(CState *state, int nargs, CValue *args)
{
CObjObject *fileObj; CObjObject *fileObj;
CObjString *str; CObjString *str;
FILE *file; FILE *file;
@ -365,14 +366,14 @@ int fileB_write(CState *state, int nargs, CValue *args) {
return 0; return 0;
} }
int fileB_gc(CState *state, int nargs, CValue *args) { int fileB_gc(CState *state, int nargs, CValue *args)
{
if (nargs != 1) { if (nargs != 1) {
cosmoV_error(state, "file:read() expected 1 argument, got %d!", nargs); cosmoV_error(state, "file:read() expected 1 argument, got %d!", nargs);
} }
if (!cosmoV_isValueUserType(state, args[0], COSMO_USER_FILE)) { if (!cosmoV_isValueUserType(state, args[0], COSMO_USER_FILE)) {
cosmoV_typeError(state, "file:__gc()", "<file>", "%s", cosmoV_typeError(state, "file:__gc()", "<file>", "%s", cosmoV_typeStr(args[0]));
cosmoV_typeStr(args[0]));
} }
CObjObject *fileObj = cosmoV_readObject(args[0]); CObjObject *fileObj = cosmoV_readObject(args[0]);
@ -411,7 +412,8 @@ int cosmoB_osOpen(CState *state, int nargs, CValue *args)
if (nargs == 2) { if (nargs == 2) {
if (!IS_STRING(args[1])) { if (!IS_STRING(args[1])) {
cosmoV_typeError(state, "os.open()", "<string>, <string>", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1])); cosmoV_typeError(state, "os.open()", "<string>, <string>", "%s, %s",
cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
} }
mode = cosmoV_readCString(args[1]); mode = cosmoV_readCString(args[1]);

View File

@ -231,11 +231,11 @@ static void traceGrays(CState *state)
static void sweep(CState *state) static void sweep(CState *state)
{ {
CObj *prev = NULL; CObj *prev = NULL, *object = state->objects;
CObj *object = state->objects;
while (object != NULL) { while (object != NULL) {
if (object->isMarked) { // skip over it if (object->isMarked) { // skip over it
object->isMarked = false; // rest to white object->isMarked = false; // reset to white
prev = object; prev = object;
object = object->next; object = object->next;
} else { // free it! } else { // free it!
@ -259,7 +259,6 @@ static void sweep(CState *state)
cosmoV_call(state, 1, 0); cosmoV_call(state, 1, 0);
} }
cosmoO_free(state, oldObj); cosmoO_free(state, oldObj);
} }
} }

View File

@ -493,7 +493,6 @@ void cosmoO_unlock(CObjObject *object)
object->isLocked = false; object->isLocked = false;
} }
bool rawgetIString(CState *state, CObjObject *object, int flag, CValue *val) bool rawgetIString(CState *state, CObjObject *object, int flag, CValue *val)
{ {
if (readFlag(object->istringFlags, flag)) if (readFlag(object->istringFlags, flag))

View File

@ -168,7 +168,8 @@ void cosmoV_addRegistry(CState *state, int pairs)
} }
// expects 1 key on the stack, pushes result // expects 1 key on the stack, pushes result
void cosmoV_getRegistry(CState *state) { void cosmoV_getRegistry(CState *state)
{
CValue key = *cosmoV_pop(state); CValue key = *cosmoV_pop(state);
CValue val; CValue val;
@ -179,12 +180,14 @@ void cosmoV_getRegistry(CState *state) {
cosmoV_pushValue(state, val); cosmoV_pushValue(state, val);
} }
void cosmoV_setProto(CState *state) { void cosmoV_setProto(CState *state)
{
StkPtr objVal = cosmoV_getTop(state, 1); StkPtr objVal = cosmoV_getTop(state, 1);
StkPtr protoVal = cosmoV_getTop(state, 0); StkPtr protoVal = cosmoV_getTop(state, 0);
if (!IS_REF(*objVal) || !IS_OBJECT(*protoVal)) { if (!IS_REF(*objVal) || !IS_OBJECT(*protoVal)) {
cosmoV_error(state, "cannot set %s to proto of type %s", cosmoV_typeStr(*objVal), cosmoV_typeStr(*protoVal)); cosmoV_error(state, "cannot set %s to proto of type %s", cosmoV_typeStr(*objVal),
cosmoV_typeStr(*protoVal));
} }
// actually set the protos // actually set the protos

View File

@ -382,7 +382,7 @@ void callCValue(CState *state, CValue func, int args, int nresults, int offset)
// push the nils to fill up the expected return values. // push the nils to fill up the expected return values.
// -1 since the we already pushed the important value // -1 since the we already pushed the important value
for (int i = 0; i < nresults - 1;i++) { for (int i = 0; i < nresults - 1; i++) {
cosmoV_pushValue(state, cosmoV_newNil()); cosmoV_pushValue(state, cosmoV_newNil());
} }
} }
@ -582,7 +582,8 @@ void cosmoV_getMethod(CState *state, CObj *obj, CValue key, CValue *val)
} }
} }
bool cosmoV_isValueUserType(CState *state, CValue val, int userType) { bool cosmoV_isValueUserType(CState *state, CValue val, int userType)
{
if (!IS_OBJECT(val)) { if (!IS_OBJECT(val)) {
return false; return false;
} }
@ -855,7 +856,6 @@ int cosmoV_execute(CState *state)
CObjObject *proto = cosmoO_grabProto(obj); CObjObject *proto = cosmoO_grabProto(obj);
CValue val = cosmoV_newNil(); // to hold our value CValue val = cosmoV_newNil(); // to hold our value
if (proto != NULL) { if (proto != NULL) {
// check for __index metamethod // check for __index metamethod
cosmoO_indexObject(state, proto, *key, &val); cosmoO_indexObject(state, proto, *key, &val);

View File

@ -72,7 +72,8 @@ COSMO_API bool cosmoV_undump(CState *state, cosmo_Reader reader, const void *ud)
COSMO_API void cosmoV_get(CState *state); COSMO_API void cosmoV_get(CState *state);
/* /*
expects object to be pushed, then the key, and finally the new value. pops the object, key & value expects object to be pushed, then the key, and finally the new value. pops the object, key &
value
*/ */
COSMO_API void cosmoV_set(CState *state); COSMO_API void cosmoV_set(CState *state);