All pastes #804553 Raw Edit

Untitled

public text v1 · immutable
·published 2007-12-03 15:57 UTC
rendered paste body
int typel_mtindex(lua_State *state) {
	typel_inst *inst = lua_touserdata(state, 1);
	typel_class *type = inst->type;
	char *attrname = lua_tostring(state, 2);
	int result = 0;
	if(!inst->self) { lua_pushnil(state); return 1; }
	if(type->getattr)
		result = type->getattr(state, inst->self, attrname);
	if(!result) {
		// the attribute was not handled by the getattr method
		// next, try the prototype table
		sl_pushref(state, type->protoref);
		lua_getfield(state, -1, attrname);
		if(lua_isnil(state, -1)) {
			lua_pop(state, 2); // clean up the stack
			// then try the data table associated with the instance
			//lua_pushvalue(state,-1
			sl_pushref(state, inst->dataref);
			lua_getfield(state, -1, attrname);
			if(lua_isnil(state, -1)) {
				// there is no such attribute; push nil
				lua_pop(state, 1);
				lua_pushnil(state);
			}
		}
		lua_replace(state, -2);
	}
	return 1;
}
int typel_mtnewindex(lua_State *state) {
	typel_inst *inst = lua_touserdata(state, 1);
	typel_class *type = inst->type;
	char *attrname = lua_tostring(state, 2);
	if(!inst->self) { return 0; }
	int result = 0;
	if(type->setattr)
		result = type->setattr(state, inst->self, attrname);
	if(!result) { /* TODO: error */ }
	return 0;
}