Added OP_POW to cdebug.c; refactored cosmoV_registerProtoObject

- cosmoV_registerProtoObject now walks the object list and updates the proto's for objects of the objType which have a NULL proto.
- misc. comment changes in cvm.h as well.
This commit is contained in:
cpunch 2021-02-13 20:08:35 -06:00
parent 1fff6c7fe9
commit 5c71efbe40
3 changed files with 23 additions and 5 deletions

View File

@ -161,6 +161,8 @@ int disasmInstr(CChunk *chunk, int offset, int indent) {
return simpleInstruction("OP_DIV", offset); return simpleInstruction("OP_DIV", offset);
case OP_MOD: case OP_MOD:
return simpleInstruction("OP_MOD", offset); return simpleInstruction("OP_MOD", offset);
case OP_POW:
return simpleInstruction("OP_POW", offset);
case OP_TRUE: case OP_TRUE:
return simpleInstruction("OP_TRUE", offset); return simpleInstruction("OP_TRUE", offset);
case OP_FALSE: case OP_FALSE:

View File

@ -427,6 +427,17 @@ COSMO_API CObjObject* cosmoV_makeObject(CState *state, int pairs) {
COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj) { COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj) {
bool replaced = state->protoObjects[objType] != NULL; bool replaced = state->protoObjects[objType] != NULL;
state->protoObjects[objType] = obj; state->protoObjects[objType] = obj;
// walk through the object list
CObj *curr = state->objects;
while (curr != NULL) {
// update the proto
if (curr->type == objType && curr->proto != NULL) {
curr->proto = obj;
}
curr = curr->next;
}
return replaced; return replaced;
} }

View File

@ -28,7 +28,12 @@ COSMO_API CObjError* cosmoV_throw(CState *state);
COSMO_API void cosmoV_error(CState *state, const char *format, ...); COSMO_API void cosmoV_error(CState *state, const char *format, ...);
COSMO_API void cosmo_insert(CState *state, int indx, CValue val); COSMO_API void cosmo_insert(CState *state, int indx, CValue val);
// returns true if replacing a previously registered proto object for this type /*
Sets the default proto objects for the passed objType. Also walks through the object heap and updates protos
for the passed objType if that CObj* has no proto.
returns true if replacing a previously registered proto object for this type
*/
COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj); COSMO_API bool cosmoV_registerProtoObject(CState *state, CObjType objType, CObjObject *obj);
/* /*
@ -43,14 +48,14 @@ COSMO_API bool cosmoV_compileString(CState *state, const char *src, const char *
/* /*
expects object to be pushed, then the key. expects object to be pushed, then the key.
if returns false an error was thrown, if returns true the value was pushed onto the stack and the object and key were popped returns false if an error was thrown, returns true if the value was pushed onto the stack and the object and key were popped
*/ */
COSMO_API bool cosmoV_get(CState *state); COSMO_API bool cosmoV_get(CState *state);
/* /*
expects object to be pushed, then the key, and finally the new value. expects object to be pushed, then the key, and finally the new value.
if returns false an error was thrown, if returns true the value was set and the object key, and value were popped returns false if an error was thrown, returns true if the value was set and the object key, and value were popped
*/ */
COSMO_API bool cosmoV_set(CState *state); COSMO_API bool cosmoV_set(CState *state);
@ -59,7 +64,7 @@ COSMO_API bool cosmoV_getMethod(CState *state, CObj *obj, CValue key, CValue *va
// nice to have wrappers // nice to have wrappers
// pushes raw CValue to the stack // pushes a raw CValue to the stack, might throw an error if the stack is overflowed (with the SAFE_STACK macro on)
static inline void cosmoV_pushValue(CState *state, CValue val) { static inline void cosmoV_pushValue(CState *state, CValue val) {
#ifdef SAFE_STACK #ifdef SAFE_STACK
ptrdiff_t stackSize = state->top - state->stack; ptrdiff_t stackSize = state->top - state->stack;
@ -92,7 +97,7 @@ static inline StkPtr cosmoV_getTop(CState *state, int indx) {
return &state->top[-(indx + 1)]; return &state->top[-(indx + 1)];
} }
// pops 1 value off the stack // pops 1 value off the stack, returns the popped value
static inline StkPtr cosmoV_pop(CState *state) { static inline StkPtr cosmoV_pop(CState *state) {
return cosmoV_setTop(state, 1); return cosmoV_setTop(state, 1);
} }