Commit c4e675ab authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add oldstyle-instance len, getitem, setitem

parent 1d452dde
...@@ -207,6 +207,9 @@ Box* instanceNonzero(Box* _inst) { ...@@ -207,6 +207,9 @@ Box* instanceNonzero(Box* _inst) {
Box* nonzero_func = _instanceGetattribute(inst, boxStrConstant("__nonzero__"), false); Box* nonzero_func = _instanceGetattribute(inst, boxStrConstant("__nonzero__"), false);
if (nonzero_func == NULL)
nonzero_func = _instanceGetattribute(inst, boxStrConstant("__len__"), false);
if (nonzero_func) { if (nonzero_func) {
return runtimeCall(nonzero_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); return runtimeCall(nonzero_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
} else { } else {
...@@ -214,6 +217,30 @@ Box* instanceNonzero(Box* _inst) { ...@@ -214,6 +217,30 @@ Box* instanceNonzero(Box* _inst) {
} }
} }
Box* instanceLen(Box* _inst) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* len_func = _instanceGetattribute(inst, boxStrConstant("__len__"), true);
return runtimeCall(len_func, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
}
Box* instanceGetitem(Box* _inst, Box* key) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* getitem_func = _instanceGetattribute(inst, boxStrConstant("__getitem__"), true);
return runtimeCall(getitem_func, ArgPassSpec(1), key, NULL, NULL, NULL, NULL);
}
Box* instanceSetitem(Box* _inst, Box* key, Box* value) {
RELEASE_ASSERT(_inst->cls == instance_cls, "");
BoxedInstance* inst = static_cast<BoxedInstance*>(_inst);
Box* setitem_func = _instanceGetattribute(inst, boxStrConstant("__setitem__"), true);
return runtimeCall(setitem_func, ArgPassSpec(2), key, value, NULL, NULL, NULL);
}
void setupClassobj() { void setupClassobj() {
classobj_cls = new BoxedClass(type_cls, object_cls, &BoxedClassobj::gcHandler, offsetof(BoxedClassobj, attrs), classobj_cls = new BoxedClass(type_cls, object_cls, &BoxedClassobj::gcHandler, offsetof(BoxedClassobj, attrs),
sizeof(BoxedClassobj), false); sizeof(BoxedClassobj), false);
...@@ -239,6 +266,9 @@ void setupClassobj() { ...@@ -239,6 +266,9 @@ void setupClassobj() {
new BoxedFunction(boxRTFunction((void*)instanceGetattribute, UNKNOWN, 2))); new BoxedFunction(boxRTFunction((void*)instanceGetattribute, UNKNOWN, 2)));
instance_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)instanceStr, UNKNOWN, 1))); instance_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)instanceStr, UNKNOWN, 1)));
instance_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)instanceNonzero, UNKNOWN, 1))); instance_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)instanceNonzero, UNKNOWN, 1)));
instance_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)instanceLen, UNKNOWN, 1)));
instance_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)instanceGetitem, UNKNOWN, 2)));
instance_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)instanceSetitem, UNKNOWN, 3)));
instance_cls->freeze(); instance_cls->freeze();
} }
......
...@@ -44,10 +44,21 @@ class E(): ...@@ -44,10 +44,21 @@ class E():
def __str__(self): def __str__(self):
return "E(%d)" % self.n return "E(%d)" % self.n
def __getitem__(self, x):
print "getitem"
return x
def __len__(self):
print "len"
return self.n
e = E(1) e = E(1)
print e print e
print e.n print e.n
print e.foo() print e.foo()
print e[1]
print e[1:2]
print len(e)
def str2(): def str2():
return "str2" return "str2"
...@@ -55,6 +66,7 @@ e.__str__ = str2 ...@@ -55,6 +66,7 @@ e.__str__ = str2
print e print e
print bool(e) print bool(e)
print bool(E(0))
print bool(E) print bool(E)
class F: class F:
...@@ -66,3 +78,19 @@ class F: ...@@ -66,3 +78,19 @@ class F:
print bool(F(0)) print bool(F(0))
print bool(F(1)) print bool(F(1))
f = F(0)
try:
len(f)
except AttributeError, e:
print e
try:
f[1]
except AttributeError, e:
print e
try:
f[1] = 2
except AttributeError, e:
print e
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment