[pypy-svn] r60464 - in pypy/branch/oo-jit/pypy/jit/tl: . test
antocuni at codespeak.net
antocuni at codespeak.net
Fri Dec 12 18:13:14 CET 2008
Author: antocuni
Date: Fri Dec 12 18:13:13 2008
New Revision: 60464
Modified:
pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py
pypy/branch/oo-jit/pypy/jit/tl/tlc.py
pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py
Log:
add support for calling methods on objects (without arguments)
Modified: pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py (original)
+++ pypy/branch/oo-jit/pypy/jit/tl/test/test_tlc.py Fri Dec 12 18:13:13 2008
@@ -191,3 +191,21 @@
""", pool)
res = interp_eval(bytecode, 0, nil, pool)
assert res.int_o() == 0
+
+ def test_method(self):
+ from pypy.jit.tl.tlc import interp_eval, nil
+ pool = ConstantPool()
+ bytecode = compile("""
+ NEW foo,meth=meth
+ PICK 0
+ PUSH 42
+ SETATTR foo
+ SEND meth
+ RETURN
+ meth:
+ PUSHARG
+ GETATTR foo
+ RETURN
+ """, pool)
+ res = interp_eval(bytecode, 0, nil, pool)
+ assert res.int_o() == 42
Modified: pypy/branch/oo-jit/pypy/jit/tl/tlc.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/tlc.py (original)
+++ pypy/branch/oo-jit/pypy/jit/tl/tlc.py Fri Dec 12 18:13:13 2008
@@ -29,6 +29,7 @@
# object oriented features
def getattr(self, name): raise TypeError
def setattr(self, name, value): raise TypeError
+ def send(self, name): raise TypeError # return the bytecode position where the method starts
class ClassDescr(object):
@@ -59,11 +60,12 @@
class Class(object):
- classes = [] # [(attributes, cls), ...]
+ classes = [] # [(descr, cls), ...]
def get(key):
- for attributes, cls in Class.classes:
- if attributes == key:
+ for descr, cls in Class.classes:
+ if key.attributes == descr.attributes and\
+ key.methods == descr.methods:
return cls
result = Class(key)
Class.classes.append((key, result))
@@ -71,11 +73,14 @@
get._pure_function_ = True
get = staticmethod(get)
- def __init__(self, attrlist):
+ def __init__(self, descr):
attributes = {} # attrname -> index
- for name in attrlist:
+ for name in descr.attributes:
attributes[name] = len(attributes)
self.attributes = attributes
+ self.methods = {}
+ for methname, pc in descr.methods:
+ self.methods[methname] = pc
class InstanceObj(Obj):
@@ -104,6 +109,10 @@
self.values[i] = value
return value
+ def send(self, name):
+ return self.getclass().methods[name]
+
+
class IntObj(Obj):
def __init__(self, value):
@@ -360,7 +369,7 @@
idx = char2int(code[pc])
pc += 1
descr = pool.classdescrs[idx]
- cls = Class.get(descr.attributes)
+ cls = Class.get(descr)
stack.append(InstanceObj(cls))
elif opcode == GETATTR:
@@ -380,6 +389,16 @@
hint(b, promote_class=True)
b.setattr(name, a)
+ elif supports_call and opcode == SEND:
+ idx = char2int(code[pc])
+ pc += 1
+ name = pool.strings[idx]
+ a = stack.pop()
+ hint(a, promote_class=True)
+ method_pc = a.send(name)
+ res = interp_eval(code, method_pc, a, pool2)
+ stack.append( res )
+
else:
raise RuntimeError("unknown opcode: " + str(opcode))
Modified: pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py
==============================================================================
--- pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py (original)
+++ pypy/branch/oo-jit/pypy/jit/tl/tlopcode.py Fri Dec 12 18:13:13 2008
@@ -48,6 +48,7 @@
opcode(28, "NEW")
opcode(29, "GETATTR")
opcode(30, "SETATTR")
+opcode(31, "SEND")
del opcode
@@ -90,9 +91,8 @@
idx = pool.add_classdescr(attributes, methods)
method_usage.append(methods)
bytecode.append(idx)
- elif t[0] in ('GETATTR', 'SETATTR'):
+ elif t[0] in ('GETATTR', 'SETATTR', 'SEND'):
# it's a string
- assert pool is not None
idx = pool.add_string(arg)
bytecode.append(idx)
else:
More information about the Pypy-commit
mailing list