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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
import sys
from pypy.interpreter.error import oefmt
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import jit_libffi
from pypy.module._rawffi.interp_rawffi import letter2tp
from pypy.module._cppyy import helper, capi, ffitypes, lowlevelviews
# Executor objects are used to dispatch C++ methods. They are defined by their
# return type only: arguments are converted by Converter objects, and Executors
# only deal with arrays of memory that are either passed to a stub or libffi.
# No argument checking or conversions are done.
#
# If a libffi function is not implemented, FastCallNotPossible is raised. If a
# stub function is missing (e.g. if no reflection info is available for the
# return type), an app-level TypeError is raised.
#
# Executor instances are created by get_executor(<return type name>), see
# below. The name given should be qualified in case there is a specialised,
# exact match for the qualified type.
NULL = lltype.nullptr(jit_libffi.FFI_TYPE_P.TO)
class Executor(object):
def __init__(self, space, extra):
pass
def cffi_type(self, space):
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible
raise FastCallNotPossible
def execute(self, space, cppmethod, cppthis, num_args, args):
raise oefmt(space.w_TypeError,
"return type not available or supported")
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible
raise FastCallNotPossible
class PtrTypeExecutor(Executor):
_immutable_fields_ = ['typecode']
typecode = 'P'
def cffi_type(self, space):
state = space.fromcache(ffitypes.State)
return state.c_voidp
def execute(self, space, cppmethod, cppthis, num_args, args):
if hasattr(space, "fake"):
raise NotImplementedError
lresult = capi.c_call_l(space, cppmethod, cppthis, num_args, args)
ptrval = rffi.cast(rffi.ULONG, lresult)
if ptrval == rffi.cast(rffi.ULONG, 0):
from pypy.module._cppyy import interp_cppyy
return interp_cppyy.get_nullptr(space)
shape = letter2tp(space, self.typecode)
return lowlevelviews.W_LowLevelView(space, shape, sys.maxint/shape.size, ptrval)
class VoidExecutor(Executor):
def cffi_type(self, space):
state = space.fromcache(ffitypes.State)
return state.c_void
def execute(self, space, cppmethod, cppthis, num_args, args):
capi.c_call_v(space, cppmethod, cppthis, num_args, args)
return space.w_None
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
jit_libffi.jit_ffi_call(cif_descr, funcaddr, buffer)
return space.w_None
class NumericExecutorMixin(object):
_mixin_ = True
def execute(self, space, cppmethod, cppthis, num_args, args):
result = self.c_stubcall(space, cppmethod, cppthis, num_args, args)
return self._wrap_object(space, rffi.cast(self.c_type, result))
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
jit_libffi.jit_ffi_call(cif_descr, funcaddr, buffer)
result = rffi.ptradd(buffer, cif_descr.exchange_result)
return self._wrap_object(space, rffi.cast(self.c_ptrtype, result)[0])
class NumericRefExecutorMixin(object):
_mixin_ = True
def __init__(self, space, extra):
Executor.__init__(self, space, extra)
self.do_assign = False
self.w_item = space.w_None
def set_item(self, space, w_item):
self.w_item = w_item
self.do_assign = True
def _wrap_reference(self, space, rffiptr):
if self.do_assign:
rffiptr[0] = rffi.cast(self.c_type, self._unwrap_object(space, self.w_item))
self.do_assign = False
return self._wrap_object(space, rffiptr[0]) # all paths, for rtyper
def execute(self, space, cppmethod, cppthis, num_args, args):
result = capi.c_call_r(space, cppmethod, cppthis, num_args, args)
return self._wrap_reference(space, rffi.cast(self.c_ptrtype, result))
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
jit_libffi.jit_ffi_call(cif_descr, funcaddr, buffer)
result = rffi.ptradd(buffer, cif_descr.exchange_result)
return self._wrap_reference(space,
rffi.cast(self.c_ptrtype, rffi.cast(rffi.VOIDPP, result)[0]))
class LongDoubleExecutorMixin(object):
# Note: not really supported, but returns normal double
_mixin_ = True
def execute(self, space, cppmethod, cppthis, num_args, args):
result = self.c_stubcall(space, cppmethod, cppthis, num_args, args)
return space.newfloat(result)
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible
raise FastCallNotPossible
class LongDoubleExecutor(ffitypes.typeid(rffi.LONGDOUBLE), LongDoubleExecutorMixin, Executor):
_immutable_ = True
c_stubcall = staticmethod(capi.c_call_ld)
class LongDoubleRefExecutorMixin(NumericRefExecutorMixin):
# Note: not really supported, but returns normal double
_mixin_ = True
def _wrap_reference(self, space, rffiptr):
if self.do_assign:
capi.c_double2longdouble(space, space.float_w(self.w_item), rffiptr)
self.do_assign = False
return self.w_item
return space.newfloat(capi.c_longdouble2double(space, rffiptr))
def execute(self, space, cppmethod, cppthis, num_args, args):
result = capi.c_call_r(space, cppmethod, cppthis, num_args, args)
return self._wrap_reference(space, rffi.cast(self.c_ptrtype, result))
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
jit_libffi.jit_ffi_call(cif_descr, funcaddr, buffer)
result = rffi.ptradd(buffer, cif_descr.exchange_result)
return self._wrap_reference(space,
rffi.cast(self.c_ptrtype, rffi.cast(rffi.VOIDPP, result)[0]))
class LongDoubleRefExecutor(ffitypes.typeid(rffi.LONGDOUBLE), LongDoubleRefExecutorMixin, Executor):
def cffi_type(self, space):
state = space.fromcache(ffitypes.State)
return state.c_voidp
class CStringExecutor(Executor):
def execute(self, space, cppmethod, cppthis, num_args, args):
lresult = capi.c_call_l(space, cppmethod, cppthis, num_args, args)
ccpresult = rffi.cast(rffi.CCHARP, lresult)
if ccpresult == rffi.cast(rffi.CCHARP, 0):
return space.newbytes("")
result = rffi.charp2str(ccpresult) # TODO: make it a choice to free
# debatable whether this should be newtext or newbytes; they are bytes
# but will be more likely used as text after binding ... probably need
# to make this configurable on a per-function bases (same as the old
# char* v.s. byte* problem)
return space.newtext(result)
class ConstructorExecutor(Executor):
def execute(self, space, cppmethod, cpptype, num_args, args):
from pypy.module._cppyy import interp_cppyy
newthis = capi.c_constructor(space, cppmethod, cpptype, num_args, args)
assert lltype.typeOf(newthis) == capi.C_OBJECT
return space.newlong(rffi.cast(rffi.LONG, newthis)) # really want ptrdiff_t here
class InstanceExecutor(Executor):
# For return of a C++ instance by pointer: MyClass* func()
_immutable_fields_ = ['clsdecl']
def __init__(self, space, clsdecl):
Executor.__init__(self, space, clsdecl)
self.clsdecl = clsdecl
def _wrap_result(self, space, obj):
from pypy.module._cppyy import interp_cppyy
return interp_cppyy.wrap_cppinstance(space,
obj, self.clsdecl, do_cast=False, python_owns=True, fresh=True)
def execute(self, space, cppmethod, cppthis, num_args, args):
oresult = capi.c_call_o(space, cppmethod, cppthis, num_args, args, self.clsdecl)
return self._wrap_result(space, rffi.cast(capi.C_OBJECT, oresult))
class InstancePtrExecutor(InstanceExecutor):
# For return of a C++ instance by pointer: MyClass* func()
def cffi_type(self, space):
state = space.fromcache(ffitypes.State)
return state.c_voidp
def _wrap_result(self, space, obj):
from pypy.module._cppyy import interp_cppyy
return interp_cppyy.wrap_cppinstance(space, obj, self.clsdecl)
def execute(self, space, cppmethod, cppthis, num_args, args):
lresult = capi.c_call_l(space, cppmethod, cppthis, num_args, args)
return self._wrap_result(space, rffi.cast(capi.C_OBJECT, lresult))
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
jit_libffi.jit_ffi_call(cif_descr, funcaddr, buffer)
presult = rffi.ptradd(buffer, cif_descr.exchange_result)
obj = rffi.cast(capi.C_OBJECT, rffi.cast(rffi.VOIDPP, presult)[0])
return self._wrap_result(space, obj)
class InstancePtrPtrExecutor(InstancePtrExecutor):
# For return of a C++ instance by ptr-to-ptr or ptr-to-ref: MyClass*& func()
def execute(self, space, cppmethod, cppthis, num_args, args):
presult = capi.c_call_r(space, cppmethod, cppthis, num_args, args)
ref = rffi.cast(rffi.VOIDPP, presult)
return self._wrap_result(space, rffi.cast(capi.C_OBJECT, ref[0]))
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible
raise FastCallNotPossible
class StdStringExecutor(InstancePtrExecutor):
def execute(self, space, cppmethod, cppthis, num_args, args):
cstr, cstr_len = capi.c_call_s(space, cppmethod, cppthis, num_args, args)
pystr = rffi.charpsize2str(cstr, cstr_len)
capi.c_free(space, rffi.cast(rffi.VOIDP, cstr))
return space.newbytes(pystr)
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
from pypy.module._cppyy.interp_cppyy import FastCallNotPossible
raise FastCallNotPossible
class StdStringRefExecutor(InstancePtrExecutor):
def __init__(self, space, clsdecl):
from pypy.module._cppyy import interp_cppyy
clsdecl = interp_cppyy.scope_byname(space, capi.std_string_name)
InstancePtrExecutor.__init__(self, space, clsdecl)
class PyObjectExecutor(PtrTypeExecutor):
def wrap_result(self, space, lresult):
space.getbuiltinmodule("cpyext")
from pypy.module.cpyext.pyobject import PyObject, from_ref, make_ref, decref
result = rffi.cast(PyObject, lresult)
w_obj = from_ref(space, result)
if result:
decref(space, result)
return w_obj
def execute(self, space, cppmethod, cppthis, num_args, args):
if hasattr(space, "fake"):
raise NotImplementedError
lresult = capi.c_call_l(space, cppmethod, cppthis, num_args, args)
return self.wrap_result(space, lresult)
def execute_libffi(self, space, cif_descr, funcaddr, buffer):
if hasattr(space, "fake"):
raise NotImplementedError
jit_libffi.jit_ffi_call(cif_descr, funcaddr, buffer)
result = rffi.ptradd(buffer, cif_descr.exchange_result)
return self.wrap_result(space, rffi.cast(rffi.LONGP, result)[0])
class SmartPointerExecutor(InstanceExecutor):
_immutable_fields_ = ['smartdecl', 'deref']
def __init__(self, space, smartdecl, raw, deref):
from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl, get_pythonized_cppclass
w_raw = get_pythonized_cppclass(space, raw)
rawdecl = space.interp_w(W_CPPClassDecl, space.findattr(w_raw, space.newtext("__cppdecl__")))
InstanceExecutor.__init__(self, space, rawdecl)
self.smartdecl = smartdecl
self.deref = deref
def _wrap_result(self, space, obj):
from pypy.module._cppyy import interp_cppyy
return interp_cppyy.wrap_cppinstance(space, obj, self.clsdecl,
self.smartdecl, self.deref, do_cast=False, python_owns=True, fresh=True)
class SmartPointerPtrExecutor(InstancePtrExecutor):
_immutable_fields_ = ['smartdecl', 'deref']
def __init__(self, space, smartdecl, raw, deref):
# TODO: share this with SmartPointerExecutor through in mixin
from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl, get_pythonized_cppclass
w_raw = get_pythonized_cppclass(space, raw)
rawdecl = space.interp_w(W_CPPClassDecl, space.findattr(w_raw, space.newtext("__cppdecl__")))
InstancePtrExecutor.__init__(self, space, rawdecl)
self.smartdecl = smartdecl
self.deref = deref
def _wrap_result(self, space, obj):
from pypy.module._cppyy import interp_cppyy
# TODO: this is a pointer to a smart pointer, take ownership on the smart one?
return interp_cppyy.wrap_cppinstance(space, obj, self.clsdecl,
self.smartdecl, self.deref, do_cast=False)
_executors = {}
def get_executor(space, name):
# Matching of 'name' to an executor factory goes through up to four levels:
# 1) full, qualified match
# 2) drop '&': by-ref is pretty much the same as by-value, python-wise
# 3) types/classes, either by ref/ptr or by value
# 4) additional special cases
#
# If all fails, a default is used, which can be ignored at least until use.
name = capi.c_resolve_name(space, name)
# full, qualified match
try:
return _executors[name](space, None)
except KeyError:
pass
compound = helper.compound(name)
clean_name = capi.c_resolve_name(space, helper.clean_type(name))
# clean lookup
try:
return _executors[clean_name+compound](space, None)
except KeyError:
pass
# drop '&': by-ref is pretty much the same as by-value, python-wise
if compound and compound[len(compound)-1] == '&':
# TODO: this does not actually work with Reflex (?)
try:
return _executors[clean_name](space, None)
except KeyError:
pass
# types/classes, either by ref/ptr or by value
from pypy.module._cppyy import interp_cppyy
cppclass = interp_cppyy.scope_byname(space, clean_name)
if cppclass:
# type check for the benefit of the annotator
from pypy.module._cppyy.interp_cppyy import W_CPPClassDecl
clsdecl = space.interp_w(W_CPPClassDecl, cppclass, can_be_None=False)
# check smart pointer type
check_smart = capi.c_smartptr_info(space, clean_name)
if check_smart[0]:
if compound == '':
return SmartPointerExecutor(space, clsdecl, check_smart[1], check_smart[2])
elif compound == '*' or compound == '&':
return SmartPointerPtrExecutor(space, clsdecl, check_smart[1], check_smart[2])
# fall through: can still return smart pointer in non-smart way
if compound == '':
return InstanceExecutor(space, clsdecl)
elif compound == '*' or compound == '&':
return InstancePtrExecutor(space, clsdecl)
elif compound == '**' or compound == '*&':
return InstancePtrPtrExecutor(space, clsdecl)
elif "(anonymous)" in name:
# special case: enum w/o a type name
return _executors["internal_enum_type_t"](space, None)
# 4) additional special cases
if compound == '*':
return _executors['void*'](space, None) # allow at least passing of the pointer
# currently used until proper lazy instantiation available in interp_cppyy
return Executor(space, None)
_executors["void"] = VoidExecutor
_executors["void*"] = PtrTypeExecutor
_executors["const char*"] = CStringExecutor
# long double not really supported: narrows to double
_executors["long double"] = LongDoubleExecutor
_executors["long double&"] = LongDoubleRefExecutor
# special cases (note: 'string' aliases added below)
_executors["constructor"] = ConstructorExecutor
_executors["std::basic_string<char>"] = StdStringExecutor
_executors["const std::basic_string<char>&"] = StdStringRefExecutor
_executors["std::basic_string<char>&"] = StdStringRefExecutor
_executors["PyObject*"] = PyObjectExecutor
# add basic (builtin) executors
def _build_basic_executors():
"NOT_RPYTHON"
type_info = (
(bool, capi.c_call_b, ("bool",)),
# TODO: either signed or unsigned is correct for a given platform ...
(rffi.CHAR, capi.c_call_c, ("char", "unsigned char", "signed char")),
(rffi.SHORT, capi.c_call_h, ("short", "short int", "unsigned short", "unsigned short int")),
(rffi.INT, capi.c_call_i, ("int", "internal_enum_type_t")),
(rffi.UINT, capi.c_call_l, ("unsigned", "unsigned int")),
(rffi.LONG, capi.c_call_l, ("long", "long int")),
(rffi.ULONG, capi.c_call_l, ("unsigned long", "unsigned long int")),
(rffi.LONGLONG, capi.c_call_ll, ("long long", "long long int", "Long64_t")),
(rffi.ULONGLONG, capi.c_call_ll, ("unsigned long long", "unsigned long long int", "ULong64_t")),
(rffi.FLOAT, capi.c_call_f, ("float",)),
(rffi.DOUBLE, capi.c_call_d, ("double",)),
# (rffi.LONGDOUBLE, capi.c_call_ld, ("long double",)),
)
for c_type, stub, names in type_info:
class BasicExecutor(ffitypes.typeid(c_type), NumericExecutorMixin, Executor):
_immutable_ = True
c_stubcall = staticmethod(stub)
class BasicRefExecutor(ffitypes.typeid(c_type), NumericRefExecutorMixin, Executor):
def cffi_type(self, space):
state = space.fromcache(ffitypes.State)
return state.c_voidp
for name in names:
_executors[name] = BasicExecutor
_executors[name+'&'] = BasicRefExecutor
_executors['const '+name+'&'] = BasicRefExecutor # no copy needed for builtins
_build_basic_executors()
# create the pointer executors; all real work is in the PtrTypeExecutor, since
# all pointer types are of the same size
def _build_ptr_executors():
"NOT_RPYTHON"
ptr_info = (
('b', ("bool",)), # really unsigned char, but this works ...
('h', ("short int", "short")),
('H', ("unsigned short int", "unsigned short")),
('i', ("int",)),
('I', ("unsigned int", "unsigned")),
('l', ("long int", "long")),
('L', ("unsigned long int", "unsigned long")),
('f', ("float",)),
('d', ("double",)),
)
for tcode, names in ptr_info:
class PtrExecutor(PtrTypeExecutor):
_immutable_fields_ = ['typecode']
typecode = tcode
for name in names:
_executors[name+'*'] = PtrExecutor
_build_ptr_executors()
# add another set of aliased names
def _add_aliased_executors():
"NOT_RPYTHON"
aliases = (
("const char*", "char*"),
("std::basic_string<char>", "string"),
("const std::basic_string<char>&", "const string&"),
("std::basic_string<char>&", "string&"),
("PyObject*", "_object*"),
)
for c_type, alias in aliases:
_executors[alias] = _executors[c_type]
_add_aliased_executors()
|