1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
# Copyright (c) Corporation for National Research Initiatives
import jast
import org, java
def makeAnys(args):
ret = []
for arg in args:
ret.append(arg.asAny())
return ret
def PyObjectArray(args):
aargs = makeAnys(args)
return Object(
jast.FilledArray("PyObject", aargs),
findType(java.lang.Class.forName("[Lorg.python.core.PyObject;")))
def makeStringArray(args):
return Object(
jast.StringArray(args),
findType(java.lang.Class.forName("[Ljava.lang.String;")))
class DelegateMethod:
def __init__(self, method, code):
self.method = method
self.code = code
def __call__(self, *args):
return apply(self.method, (self.code, )+args)
class Object:
def __init__(self, code, value):
self.code = code
self.value = value
def asAny(self):
return self.asa(org.python.core.PyObject)
def makeReference(self, code):
return Object(code, self.value)
def makeStatement(self):
return self.value.getStatement(self.code)
def mergeWith(self, other):
return Object(jast.Comment("oops"), Generic)
def __repr__(self):
return "Object(%s, %s)" % (self.code, self.value.__class__.__name__)
# delegate most methods to self.value using DelegateMethod
def __getattr__(self, name):
return DelegateMethod(getattr(self.value, name), self.code)
primitives = {java.lang.Integer.TYPE : 'py2int',
java.lang.Character.TYPE: 'py2char',
}
import JavaCall
class JavaObject:
def __init__(self, javaclass):
self.javaclass = javaclass
def isa(self, code, type):
if type.isAssignableFrom(self.javaclass):
return code
def asa(self, code, type, message=None):
ret = self.isa(code, type)
if ret is not None:
return ret
if type == org.python.core.PyObject:
return self.asAny(code)
def asAny(self, code):
return jast.InvokeStatic('Py', 'java2py', [code])
def getStatement(self, code):
return code
class JavaInteger(JavaObject):
def __init__(self):
JavaObject.__init__(self, java.lang.Integer.TYPE)
def asAny(self, code):
return jast.InvokeStatic('Py', 'newInteger', [code])
class JavaString(JavaObject):
def __init__(self):
JavaObject.__init__(self, java.lang.String)
def asAny(self, code):
return jast.InvokeStatic('Py', 'newString', [code])
PyObject_as_java_class = org.python.core.PyJavaClass.lookup(org.python.core.PyObject) # xxx temporary fix
class PyObject:
#I hate static fields for dynamic info!
attributes = {}
def getStatement(self, code):
return code
def asAny(self, code):
return code
def isa(self, code, type):
if type == org.python.core.PyObject:
return code
def asa(self, code, type, message=None):
ret = self.isa(code, type)
if ret is not None:
return ret
if primitives.has_key(type):
return jast.InvokeStatic('Py', primitives[type], [code])
if type == java.lang.Boolean.TYPE:
return jast.Invoke(code, '__nonzero__', [])
tname = type.__name__
tojava = jast.InvokeStatic('Py', 'tojava',
[code, jast.GetStaticAttribute(tname,'class')])
return jast.Cast(tname, tojava)
def print_line(self, code):
return jast.InvokeStatic("Py", "println", [self.asAny(code)])
def print_continued(self, code):
return jast.InvokeStatic("Py", "printComma", [self.asAny(code)])
def print_line_to(self, file, code=None):
if code is None:
return jast.InvokeStatic("Py", "printlnv", [self.asAny(file)])
else:
return jast.InvokeStatic("Py", "println", [self.asAny(file),
self.asAny(code)])
def print_continued_to(self, file, code):
return jast.InvokeStatic("Py", "printComma", [self.asAny(file),
self.asAny(code)])
def domethod(self, code, name, *args):
meth = getattr(PyObject_as_java_class, name) # xxx temporary fix
code, type = JavaCall.call(meth, Object(code, self), args)
return Object(code, findType(type))
def nonzero(self, code):
return self.domethod(code, "__nonzero__").asa(java.lang.Boolean.TYPE)
def unop(self, code, op):
return self.domethod(code, op)
def compop(self, code, op, y):
return self.domethod(code, op, y)
binop = compop
def augbinop(self, code, op, y):
return self.domethod(code, op, y)
def igetitem(self, code, index):
return self.domethod(code, "__getitem__",
Object(jast.IntegerConstant(index), IntType))
def getslice(self, code, start, stop, step):
#print start, stop, step
return self.domethod(code, "__getslice__", start, stop, step)
def delslice(self, code, start, stop, step):
return self.domethod(code, "__delslice__",
start, stop, step).getStatement()
def setslice(self, code, start, stop, step, value):
return self.domethod(code, "__setslice__",
start, stop, step, value).getStatement()
def getitem(self, code, index):
return self.domethod(code, "__getitem__", index)
def delitem(self, code, index):
return self.domethod(code, "__delitem__", index).getStatement()
def setitem(self, code, index, value):
return self.domethod(code, "__setitem__", index, value).getStatement()
def getattr(self, code, name):
if not PyObject.attributes.has_key(name):
PyObject.attributes[name] = None
name = Object(jast.StringConstant(name), StringType)
return self.domethod(code, "__getattr__", name)
def delattr(self, code, name):
name = Object(jast.StringConstant(name), StringType)
return self.domethod(code, "__delattr__", name).getStatement()
def setattr(self, code, name, value):
PyObject.attributes[name] = value
name = Object(jast.StringConstant(name), StringType)
ret = self.domethod(code, "__setattr__", name, value)
#print ret
return ret.getStatement()
def call(self, code, args, keyargs=None):
nargs = len(args)
if keyargs is None or len(keyargs) == 0:
if nargs == 0:
return self.domethod(code, "__call__")
elif nargs == 1:
return self.domethod(code, "__call__", args[0])
elif nargs == 2:
return self.domethod(code, "__call__", args[0], args[1])
elif nargs == 3:
return self.domethod(code, "__call__",
args[0], args[1], args[2])
else:
return self.domethod(code, "__call__", PyObjectArray(args))
else:
keynames = []
for name, value in keyargs:
PyObject.attributes[name] = value
keynames.append(name)
args.append(value)
return self.domethod(code, "__call__", PyObjectArray(args),
makeStringArray(keynames))
def call_extra(self, code, args, keyargs, starargs, kwargs):
keynames = []
for name, value in keyargs:
PyObject.attributes[name] = value
keynames.append(name)
args.append(value)
if not starargs:
starargs = Object(jast.Null, self)
if not kwargs:
kwargs = Object(jast.Null, self)
return self.domethod(code, "_callextra",
PyObjectArray(args),
makeStringArray(keynames),
starargs,
kwargs)
def invoke(self, code, name, args, keyargs):
if keyargs:
return self.getattr(code, name).call(args, keyargs)
name = Object(jast.StringConstant(name), StringType)
nargs = len(args)
if nargs == 0:
return self.domethod(code, "invoke", name)
elif nargs == 1:
return self.domethod(code, "invoke", name, args[0])
elif nargs == 2:
return self.domethod(code, "invoke", name, args[0], args[1])
else:
return self.domethod(code, "invoke", name, PyObjectArray(args))
def doraise(self, code, exc_value=None, exc_traceback=None):
args = [code]
if exc_value is not None:
args.append(exc_value.asAny())
if exc_traceback is not None:
args.append(exc_traceback.asAny())
return jast.Throw(jast.InvokeStatic("Py", "makeException", args))
def mergeWith(self, other):
# In simplest world, all types can be merged with each other
return self
def makeTemp(self, frame):
return PyObject(jast.Set(frame.gettemp(self.type),
self.asAny()), self.parent)
def freeTemp(self, frame):
frame.freetemp(self.value)
types = {}
def findType(type):
if types.has_key(type):
return types[type]
if type == java.lang.Integer.TYPE:
ret = JavaInteger()
elif type == java.lang.String:
ret = JavaString()
elif type == org.python.core.PyObject:
ret = PyObject()
elif type == org.python.core.PyString:
ret = PyObject()
else:
ret = JavaObject(type)
types[type] = ret
return ret
Generic = findType(org.python.core.PyObject)
IntType = findType(java.lang.Integer.TYPE)
StringType = findType(java.lang.String)
if __name__ == '__main__':
foo = Object(jast.Identifier("foo"), Generic)
one = Object(jast.IntegerConstant(1), IntType)
hello = Object(jast.StringConstant("hello"), StringType)
print foo, one, hello
print foo.binop("add", foo)
print foo.binop("add", one)
print foo.binop("add", hello)
print foo.nonzero()
print foo.getitem(foo)
print foo.getitem(one)
print foo.call([one, hello])
print foo.call([one, hello, foo])
|