mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-26 00:50:14 +00:00
renamed metaobjects to protoobjects
This commit is contained in:
parent
ae22a6cef5
commit
c7be39a5d4
@ -20,12 +20,12 @@ CValue cosmoB_print(CState *state, int nargs, CValue *args) {
|
|||||||
return cosmoV_newNil(); // print doesn't return any args
|
return cosmoV_newNil(); // print doesn't return any args
|
||||||
}
|
}
|
||||||
|
|
||||||
CValue cosmoB_dsetMeta(CState *state, int nargs, CValue *args) {
|
CValue cosmoB_dsetProto(CState *state, int nargs, CValue *args) {
|
||||||
if (nargs == 2) {
|
if (nargs == 2) {
|
||||||
CObjObject *obj = cosmoV_readObject(args[0]); // object to set meta too
|
CObjObject *obj = cosmoV_readObject(args[0]); // object to set proto too
|
||||||
CObjObject *meta = cosmoV_readObject(args[1]);
|
CObjObject *proto = cosmoV_readObject(args[1]);
|
||||||
|
|
||||||
obj->meta = meta; // boom done
|
obj->proto = proto; // boom done
|
||||||
} else {
|
} else {
|
||||||
cosmoV_error(state, "Expected 2 parameters, got %d!", nargs);
|
cosmoV_error(state, "Expected 2 parameters, got %d!", nargs);
|
||||||
}
|
}
|
||||||
@ -33,20 +33,20 @@ CValue cosmoB_dsetMeta(CState *state, int nargs, CValue *args) {
|
|||||||
return cosmoV_newNil(); // nothing
|
return cosmoV_newNil(); // nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
CValue cosmoB_dgetMeta(CState *state, int nargs, CValue *args) {
|
CValue cosmoB_dgetProto(CState *state, int nargs, CValue *args) {
|
||||||
if (nargs != 1) {
|
if (nargs != 1) {
|
||||||
cosmoV_error(state, "Expected 1 parameter, got %d!", nargs);
|
cosmoV_error(state, "Expected 1 parameter, got %d!", nargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cosmoV_newObj(cosmoV_readObject(args[0])->meta); // just return the meta
|
return cosmoV_newObj(cosmoV_readObject(args[0])->proto); // just return the proto
|
||||||
}
|
}
|
||||||
|
|
||||||
void cosmoB_loadDebug(CState *state) {
|
void cosmoB_loadDebug(CState *state) {
|
||||||
cosmoV_pushString(state, "getMeta");
|
cosmoV_pushString(state, "getProto");
|
||||||
cosmoV_pushCFunction(state, cosmoB_dgetMeta);
|
cosmoV_pushCFunction(state, cosmoB_dgetProto);
|
||||||
cosmoV_pushString(state, "setMeta");
|
cosmoV_pushString(state, "setProto");
|
||||||
cosmoV_pushCFunction(state, cosmoB_dsetMeta);
|
cosmoV_pushCFunction(state, cosmoB_dsetProto);
|
||||||
cosmoV_pushObject(state, 2);
|
cosmoV_pushObject(state, 2);
|
||||||
|
|
||||||
state->metaObj = cosmoV_readObject(*cosmoV_pop(state));
|
state->protoObj = cosmoV_readObject(*cosmoV_pop(state));
|
||||||
}
|
}
|
@ -88,7 +88,7 @@ void blackenObject(CState *state, CObj *obj) {
|
|||||||
// mark everything this object is keeping track of
|
// mark everything this object is keeping track of
|
||||||
CObjObject *cobj = (CObjObject*)obj;
|
CObjObject *cobj = (CObjObject*)obj;
|
||||||
markTable(state, &cobj->tbl);
|
markTable(state, &cobj->tbl);
|
||||||
markObject(state, (CObj*)cobj->meta);
|
markObject(state, (CObj*)cobj->proto);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case COBJ_UPVALUE: {
|
case COBJ_UPVALUE: {
|
||||||
@ -214,8 +214,8 @@ void markRoots(CState *state) {
|
|||||||
for (int i = 0; i < INTERNALSTRING_MAX; i++)
|
for (int i = 0; i < INTERNALSTRING_MAX; i++)
|
||||||
markObject(state, (CObj*)state->internalStrings[i]);
|
markObject(state, (CObj*)state->internalStrings[i]);
|
||||||
|
|
||||||
// mark our meta object
|
// mark our proto object
|
||||||
markObject(state, (CObj*)state->metaObj);
|
markObject(state, (CObj*)state->protoObj);
|
||||||
traceGrays(state);
|
traceGrays(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ bool cosmoO_equal(CObj* obj1, CObj* obj2) {
|
|||||||
|
|
||||||
CObjObject *cosmoO_newObject(CState *state) {
|
CObjObject *cosmoO_newObject(CState *state) {
|
||||||
CObjObject *obj = (CObjObject*)cosmoO_allocateBase(state, sizeof(CObjObject), COBJ_OBJECT);
|
CObjObject *obj = (CObjObject*)cosmoO_allocateBase(state, sizeof(CObjObject), COBJ_OBJECT);
|
||||||
obj->meta = state->metaObj;
|
obj->proto = state->protoObj;
|
||||||
obj->user = NULL; // reserved for C API
|
obj->user = NULL; // reserved for C API
|
||||||
cosmoV_pushValue(state, cosmoV_newObj(obj)); // so our GC can keep track of it
|
cosmoV_pushValue(state, cosmoV_newObj(obj)); // so our GC can keep track of it
|
||||||
cosmoT_initTable(state, &obj->tbl, ARRAY_START);
|
cosmoT_initTable(state, &obj->tbl, ARRAY_START);
|
||||||
@ -204,8 +204,8 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool cosmoO_getObject(CState *state, CObjObject *object, CValue key, CValue *val) {
|
bool cosmoO_getObject(CState *state, CObjObject *object, CValue key, CValue *val) {
|
||||||
if (!cosmoT_get(&object->tbl, key, val) && object->meta != NULL) { // if the field doesn't exist in the object, check the meta
|
if (!cosmoT_get(&object->tbl, key, val) && object->proto != NULL) { // if the field doesn't exist in the object, check the proto
|
||||||
return cosmoO_getObject(state, object->meta, key, val);
|
return cosmoO_getObject(state, object->proto, key, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -40,7 +40,7 @@ typedef struct CObjObject {
|
|||||||
CommonHeader; // "is a" CObj
|
CommonHeader; // "is a" CObj
|
||||||
CTable tbl;
|
CTable tbl;
|
||||||
void *user; // userdata (NULL by default)
|
void *user; // userdata (NULL by default)
|
||||||
struct CObjObject *meta; // metaobject, describes the behavior of the object
|
struct CObjObject *proto; // protoobject, describes the behavior of the object
|
||||||
} CObjObject;
|
} CObjObject;
|
||||||
|
|
||||||
typedef struct CObjFunction {
|
typedef struct CObjFunction {
|
||||||
|
@ -30,7 +30,7 @@ CState *cosmoV_newState() {
|
|||||||
state->top = state->stack;
|
state->top = state->stack;
|
||||||
state->frameCount = 0;
|
state->frameCount = 0;
|
||||||
state->openUpvalues = NULL;
|
state->openUpvalues = NULL;
|
||||||
state->metaObj = NULL;
|
state->protoObj = NULL;
|
||||||
|
|
||||||
cosmoT_initTable(state, &state->strings, 8); // init string table
|
cosmoT_initTable(state, &state->strings, 8); // init string table
|
||||||
cosmoT_initTable(state, &state->globals, 8); // init global table
|
cosmoT_initTable(state, &state->globals, 8); // init global table
|
||||||
|
@ -38,7 +38,7 @@ typedef struct CState {
|
|||||||
int frameCount;
|
int frameCount;
|
||||||
|
|
||||||
CObjString *internalStrings[INTERNALSTRING_MAX]; // strings used internally by the VM, eg. __init
|
CObjString *internalStrings[INTERNALSTRING_MAX]; // strings used internally by the VM, eg. __init
|
||||||
CObjObject *metaObj; // start met obj for all objects (NULL by default)
|
CObjObject *protoObj; // start met obj for all objects (NULL by default)
|
||||||
} CState;
|
} CState;
|
||||||
|
|
||||||
COSMO_API CState *cosmoV_newState();
|
COSMO_API CState *cosmoV_newState();
|
||||||
|
@ -184,14 +184,14 @@ COSMOVMRESULT cosmoV_call(CState *state, int args) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case COBJ_OBJECT: {
|
case COBJ_OBJECT: {
|
||||||
CObjObject *metaObj = (CObjObject*)val->val.obj;
|
CObjObject *protoObj = (CObjObject*)val->val.obj;
|
||||||
CObjObject *newObj = cosmoO_newObject(state);
|
CObjObject *newObj = cosmoO_newObject(state);
|
||||||
newObj->meta = metaObj;
|
newObj->proto = protoObj;
|
||||||
*val = cosmoV_newObj(newObj);
|
*val = cosmoV_newObj(newObj);
|
||||||
CValue ret;
|
CValue ret;
|
||||||
|
|
||||||
// check if they defined an initalizer
|
// check if they defined an initalizer
|
||||||
if (cosmoV_getObject(state, metaObj, cosmoV_newObj(state->internalStrings[INTERNALSTRING_INIT]), &ret) && IS_METHOD(ret)) {
|
if (cosmoV_getObject(state, protoObj, cosmoV_newObj(state->internalStrings[INTERNALSTRING_INIT]), &ret) && IS_METHOD(ret)) {
|
||||||
callMethod(state, cosmoV_readMethod(ret), args);
|
callMethod(state, cosmoV_readMethod(ret), args);
|
||||||
cosmoV_pop(state);
|
cosmoV_pop(state);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user