fixed __index

This commit is contained in:
CPunch 2020-11-20 15:10:49 -06:00
parent 85e7deae7b
commit 6d45c0a676
2 changed files with 33 additions and 3 deletions

28
examples/increment.cosmo Normal file
View File

@ -0,0 +1,28 @@
class Stack
function __init(self)
self.stack = {}
self.x = 0
end
function push(self, val)
self.stack[self.x++] = val
end
function pop(self)
return self.stack[--self.x]
end
function __index(self, key)
return self.stack[key]
end
end
var stack = Stack()
for (var i = 0; i < 10000; i++) do
stack.push(i)
end
for (var i = 0; i < 10000; i++) do
print(stack.pop() .. " : " .. stack[i])
end

View File

@ -206,7 +206,11 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin
bool cosmoO_getObject(CState *state, CObjObject *object, CValue key, CValue *val) {
if (!cosmoT_get(&object->tbl, key, val)) { // if the field doesn't exist in the object, check the proto
if (object->proto != NULL) { // sanity check
// first though, check for __index, if that exists, call it
// first though, check for a member of the proto object, if it fails then lookup __index
if (cosmoO_getObject(state, object->proto, key, val))
return true;
// then check for __index, if that exists, call it
if (cosmoO_getIString(state, object->proto, ISTRING_INDEX, val)) {
cosmoV_pushValue(state, *val); // push function
cosmoV_pushValue(state, cosmoV_newObj(object)); // push object
@ -215,8 +219,6 @@ bool cosmoO_getObject(CState *state, CObjObject *object, CValue key, CValue *val
*val = *cosmoV_pop(state); // set value to the return value of __index
return true;
}
return cosmoO_getObject(state, object->proto, key, val);
}
return false; // no protoobject to check against