Exemplo n.º 1
0
def cli2py(space, b_obj):
    # TODO: support other types and find the most efficient way to
    # select the correct case
    if b_obj is None:
        return space.w_None

    w_obj = unbox(b_obj, W_Root)
    if w_obj is not None:
        return w_obj # it's already a wrapped object!
    
    b_type = b_obj.GetType()
    if b_type == typeof(System.Int32):
        intval = unbox(b_obj, ootype.Signed)
        return space.wrap(intval)
    elif b_type == typeof(System.Double):
        floatval = unbox(b_obj, ootype.Float)
        return space.wrap(floatval)
    elif b_type == typeof(System.Boolean):
        boolval = unbox(b_obj, ootype.Bool)
        return space.wrap(boolval)
    elif b_type == typeof(System.String):
        strval = unbox(b_obj, ootype.String)
        return space.wrap(strval)
    else:
        namespace, classname = split_fullname(b_type.ToString())
        assemblyname = b_type.get_Assembly().get_FullName()
        w_cls = load_cli_class(space, assemblyname, namespace, classname)
        cliobj = W_CliObject(space, b_obj)
        return wrapper_from_cliobj(space, w_cls, cliobj)
Exemplo n.º 2
0
 def fn(flag):
     if flag:
         clitype = typeof(System.Int32)
     else:
         clitype = typeof(System.String)
     cls = type2class(clitype)
     return cls is cInt32
Exemplo n.º 3
0
def cli2py(space, b_obj):
    # TODO: support other types and find the most efficient way to
    # select the correct case
    if b_obj is None:
        return space.w_None

    w_obj = unbox(b_obj, W_Root)
    if w_obj is not None:
        return w_obj # it's already a wrapped object!
    
    b_type = b_obj.GetType()
    if b_type == typeof(System.Int32):
        intval = unbox(b_obj, ootype.Signed)
        return space.wrap(intval)
    elif b_type == typeof(System.Double):
        floatval = unbox(b_obj, ootype.Float)
        return space.wrap(floatval)
    elif b_type == typeof(System.Boolean):
        boolval = unbox(b_obj, ootype.Bool)
        return space.wrap(boolval)
    elif b_type == typeof(System.String):
        strval = unbox(b_obj, ootype.String)
        return space.wrap(strval)
    else:
        namespace, classname = split_fullname(b_type.ToString())
        assemblyname = b_type.get_Assembly().get_FullName()
        w_cls = load_cli_class(space, assemblyname, namespace, classname)
        cliobj = W_CliObject(space, b_obj)
        return wrapper_from_cliobj(space, w_cls, cliobj)
Exemplo n.º 4
0
 def fn(flag):
     if flag:
         clitype = typeof(System.Int32)
     else:
         clitype = typeof(System.String)
     cls = type2class(clitype)
     return cls is cInt32
Exemplo n.º 5
0
def cli2py(space, b_obj):
    # TODO: support other types and find the most efficient way to
    # select the correct case
    if b_obj is None:
        return space.w_None

    w_obj = unbox(b_obj, W_Root)
    if w_obj is not None:
        return w_obj # it's already a wrapped object!
    
    b_type = b_obj.GetType()
    if b_type == typeof(System.Int32):
        intval = unbox(b_obj, ootype.Signed)
        return space.wrap(intval)
    elif b_type == typeof(System.Double):
        floatval = unbox(b_obj, ootype.Float)
        return space.wrap(floatval)
    elif b_type == typeof(System.Boolean):
        boolval = unbox(b_obj, ootype.Bool)
        return space.wrap(boolval)
    elif b_type == typeof(System.String):
        strval = unbox(b_obj, ootype.String)
        return space.wrap(strval)
    else:
        msg = "Can't convert object %s to Python" % str(b_obj.ToString())
        raise OperationError(space.w_TypeError, space.wrap(msg))
Exemplo n.º 6
0
 def build_fn():
     tInt = typeof(System.Int32)
     args = init_array(System.Type, tInt, tInt)
     meth = Utils.CreateDynamicMethod("add", tInt, args)
     il = meth.GetILGenerator()
     il.Emit(OpCodes.Ldarg_0)
     il.Emit(OpCodes.Ldarg_1)
     il.Emit(OpCodes.Add)
     il.Emit(OpCodes.Ret)
     myfunc = meth.CreateDelegate(typeof(FUNCTYPE))
     return myfunc
Exemplo n.º 7
0
 def build_fn():
     tInt = typeof(System.Int32)
     args = init_array(System.Type, tInt, tInt)
     meth = Utils.CreateDynamicMethod("add", tInt, args)
     il = meth.GetILGenerator()
     il.Emit(OpCodes.Ldarg_0)
     il.Emit(OpCodes.Ldarg_1)
     il.Emit(OpCodes.Add)
     il.Emit(OpCodes.Ret)
     myfunc = meth.CreateDelegate(typeof(FUNCTYPE))
     return myfunc
Exemplo n.º 8
0
 def getCliType(self, meth):
     if self in meth.box2type:
         return meth.box2type[self]
     
     if self.type == history.INT:
         return dotnet.typeof(System.Int32)
     elif self.type == history.FLOAT:
         return dotnet.typeof(System.Double)
     elif self.type == history.REF:
         return dotnet.typeof(System.Object)
     else:
         assert False, 'Unknown type: %s' % self.type
Exemplo n.º 9
0
 def build_fn():
     tObjArray = System.Type.GetType("System.Object[]")
     tInt = typeof(System.Int32)
     args = init_array(System.Type, tObjArray, tInt, tInt)
     meth = Utils.CreateDynamicMethod("add", tInt, args)
     il = meth.GetILGenerator()
     il.Emit(OpCodes.Ldarg_1)
     il.Emit(OpCodes.Ldarg_2)
     il.Emit(OpCodes.Add)
     il.Emit(OpCodes.Ret)
     array = new_array(System.Object, 0)
     myfunc = meth.CreateDelegate(typeof(FUNCTYPE), array)
     return myfunc
Exemplo n.º 10
0
 def build_fn():
     tObjArray = System.Type.GetType("System.Object[]")
     tInt = typeof(System.Int32)
     args = init_array(System.Type, tObjArray, tInt, tInt)
     meth = Utils.CreateDynamicMethod("add", tInt, args)
     il = meth.GetILGenerator()
     il.Emit(OpCodes.Ldarg_1)
     il.Emit(OpCodes.Ldarg_2)
     il.Emit(OpCodes.Add)
     il.Emit(OpCodes.Ret)
     array = new_array(System.Object, 0)
     myfunc = meth.CreateDelegate(typeof(FUNCTYPE), array)
     return myfunc
Exemplo n.º 11
0
 def __init__(self, cpu, cliloop):
     self.setoptions()
     self.cpu = cpu
     self.name = cliloop.get_fresh_cli_name()
     self.cliloop = cliloop
     self.boxes = {}  # box --> local var
     self.branches = []
     self.branchlabels = []
     self.consts = {}  # object --> index
     self.meth_wrapper = self._get_meth_wrapper()
     self.il = self.meth_wrapper.get_il_generator()
     self.av_consts = MethodArgument(0,
                                     System.Type.GetType("System.Object[]"))
     t_InputArgs = dotnet.typeof(InputArgs)
     self.av_inputargs = MethodArgument(1, t_InputArgs)
     self.av_ovf_flag = BoxInt()
     self.exc_value_field = t_InputArgs.GetField('exc_value')
     if cpu.rtyper:
         self.av_OverflowError = ConstObj(
             ootype.cast_to_object(cpu.ll_ovf_exc))
         self.av_ZeroDivisionError = ConstObj(
             ootype.cast_to_object(cpu.ll_zero_exc))
     else:
         self.av_OverflowError = None
         self.av_ZeroDivisionError = None
     self.box2type = {}
Exemplo n.º 12
0
 def emit_op_subclassof(self, op):
     clitype_utils = dotnet.typeof(Utils)
     methinfo = clitype_utils.GetMethod('SubclassOf')
     op.args[0].load(self)
     op.args[1].load(self)
     self.il.Emit(OpCodes.Call, methinfo)
     self.store_result(op)
Exemplo n.º 13
0
 def load(self, meth):
     holdertype = self.holder.GetType()
     funcfield = holdertype.GetField('func')
     Const.load(self, meth)
     meth.il.Emit(OpCodes.Castclass, holdertype)
     meth.il.Emit(OpCodes.Ldfld, funcfield)
     meth.il.Emit(OpCodes.Castclass, dotnet.typeof(LoopDelegate))
Exemplo n.º 14
0
 def emit_op_guard_class(self, op):
     assert len(op.args) == 2
     il_label = self.newbranch(op)
     self.push_arg(op, 0)
     meth = dotnet.typeof(System.Object).GetMethod("GetType")
     self.il.Emit(OpCodes.Callvirt, meth)
     self.push_arg(op, 1)
     self.il.Emit(OpCodes.Bne_Un, il_label)
Exemplo n.º 15
0
 def finish_code(self):
     delegatetype = dotnet.typeof(LoopDelegate)
     # initialize the array of genconsts
     consts = dotnet.new_array(System.Object, len(self.consts))
     for av_const, i in self.consts.iteritems():
         consts[i] = av_const.get_cliobj()
     # build the delegate
     func = self.meth_wrapper.create_delegate(delegatetype, consts)
     return dotnet.clidowncast(func, LoopDelegate)
Exemplo n.º 16
0
 def emit_return_failed_op(self, op, args):
     # store the index of the failed op
     index_op = self.get_index_for_failing_op(op)
     self.av_inputargs.load(self)
     self.il.Emit(OpCodes.Ldc_I4, index_op)
     field = dotnet.typeof(InputArgs).GetField('failed_op')
     self.il.Emit(OpCodes.Stfld, field)
     self.emit_store_opargs(args)
     self.il.Emit(OpCodes.Ret)
Exemplo n.º 17
0
 def emit_new_arrayofvoids(self, op):
     clitype = dotnet.typeof(ListOfVoid)
     ctor = clitype.GetConstructor(dotnet.new_array(System.Type, 0))
     _ll_resize = clitype.GetMethod('_ll_resize')
     self.il.Emit(OpCodes.Newobj, ctor)
     self.il.Emit(OpCodes.Dup)
     op.args[0].load(self)
     self.il.Emit(OpCodes.Callvirt, _ll_resize)
     self.store_result(op)
Exemplo n.º 18
0
 def get_inputarg_field(self, type):
     t = dotnet.typeof(InputArgs)
     if type == history.INT:
         fieldname = 'ints'
     elif type == history.FLOAT:
         fieldname = 'floats'
     elif type == history.REF:
         fieldname = 'objs'
     else:
         assert False, 'Unknown type %s' % type
     return t.GetField(fieldname)        
Exemplo n.º 19
0
 def emit_ovf_op_and_guard(self, op, opguard, emit_op):
     # emit the checked operation
     lbl = self.il.BeginExceptionBlock()
     emit_op(self, op)
     self.il.Emit(OpCodes.Leave, lbl)
     self.il.BeginCatchBlock(dotnet.typeof(System.OverflowException))
     # emit the guard
     assert len(opguard.args) == 0
     il_label = self.newbranch(opguard)
     self.il.Emit(OpCodes.Leave, il_label)
     self.il.EndExceptionBlock()
Exemplo n.º 20
0
def rewrap_args(space, w_args, startfrom):
    args = space.unpackiterable(w_args)
    paramlen = len(args)-startfrom
    b_args = new_array(System.Object, paramlen)
    b_paramtypes = new_array(System.Type, paramlen)
    for i in range(startfrom, len(args)):
        j = i-startfrom
        b_obj = py2cli(space, args[i])
        b_args[j] = b_obj
        if b_obj is None:
            b_paramtypes[j] = typeof(System.Object) # we really can't be more precise
        else:
            b_paramtypes[j] = b_obj.GetType() # XXX: potentially inefficient
    return b_args, b_paramtypes
Exemplo n.º 21
0
def rewrap_args(space, w_args, startfrom):
    args = space.unpackiterable(w_args)
    paramlen = len(args)-startfrom
    b_args = new_array(System.Object, paramlen)
    b_paramtypes = new_array(System.Type, paramlen)
    for i in range(startfrom, len(args)):
        j = i-startfrom
        b_obj = py2cli(space, args[i])
        b_args[j] = b_obj
        if b_obj is None:
            b_paramtypes[j] = typeof(System.Object) # we really can't be more precise
        else:
            b_paramtypes[j] = b_obj.GetType() # XXX: potentially inefficient
    return b_args, b_paramtypes
Exemplo n.º 22
0
 def emit_ovf_op(self, op, emit_op):
     next_op = self.oplist[self.i+1]
     if next_op.opnum == rop.GUARD_NO_OVERFLOW:
             self.i += 1
             self.emit_ovf_op_and_guard(op, next_op, emit_op)
             return
     # clear the overflow flag
     self.il.Emit(OpCodes.Ldc_I4_0)
     self.av_ovf_flag.store(self)
     lbl = self.il.BeginExceptionBlock()
     emit_op(self, op)
     self.il.Emit(OpCodes.Leave, lbl)
     self.il.BeginCatchBlock(dotnet.typeof(System.OverflowException))
     self.il.Emit(OpCodes.Ldc_I4_1)
     self.av_ovf_flag.store(self)
     self.il.EndExceptionBlock()
Exemplo n.º 23
0
 def emit_raising_op(self, op, emit_op, exctypes):
     self.emit_clear_exception()
     lbl = self.il.BeginExceptionBlock()
     emit_op(self, op)
     self.il.Emit(OpCodes.Leave, lbl)
     for exctype in exctypes:
         v = self.il.DeclareLocal(exctype)
         self.il.BeginCatchBlock(exctype)
         if exctype == dotnet.typeof(System.OverflowException) and self.av_OverflowError:
             self.il.Emit(OpCodes.Ldc_I4_1)
             self.av_ovf_flag.store(self)
         else:
             self.il.Emit(OpCodes.Stloc, v)
             self.av_inputargs.load(self)
             self.il.Emit(OpCodes.Ldloc, v)
             self.il.Emit(OpCodes.Stfld, self.exc_value_field)
     self.il.EndExceptionBlock()
Exemplo n.º 24
0
 def emit_op_jump(self, op):
     target_token = op.descr
     assert isinstance(target_token, LoopToken)
     if target_token.cliloop is self.cliloop:
         # jump to the beginning of the loop
         i = 0
         for i in range(len(op.args)):
             op.args[i].load(self)
             self.cliloop.inputargs[i].store(self)
         self.il.Emit(OpCodes.Br, self.il_loop_start)
     else:
         # it's a real bridge
         cliloop = target_token.cliloop
         assert len(op.args) == len(cliloop.inputargs)
         self.emit_debug('jumping to ' + cliloop.name)
         self.emit_store_opargs(op.args)
         cliloop.funcbox.load(self)
         self.av_inputargs.load(self)
         methinfo = dotnet.typeof(LoopDelegate).GetMethod('Invoke')
         if self.tailcall:
             self.il.Emit(OpCodes.Tailcall)
         self.il.Emit(OpCodes.Callvirt, methinfo)
         self.il.Emit(OpCodes.Ret)
Exemplo n.º 25
0
 def __init__(self, cpu, cliloop):
     self.setoptions()
     self.cpu = cpu
     self.name = cliloop.get_fresh_cli_name()
     self.cliloop = cliloop
     self.boxes = {}       # box --> local var
     self.branches = []
     self.branchlabels = []
     self.consts = {}      # object --> index
     self.meth_wrapper = self._get_meth_wrapper()
     self.il = self.meth_wrapper.get_il_generator()
     self.av_consts = MethodArgument(0, System.Type.GetType("System.Object[]"))
     t_InputArgs = dotnet.typeof(InputArgs)
     self.av_inputargs = MethodArgument(1,t_InputArgs )
     self.av_ovf_flag = BoxInt()
     self.exc_value_field = t_InputArgs.GetField('exc_value')
     if cpu.rtyper:
         self.av_OverflowError = ConstObj(ootype.cast_to_object(cpu.ll_ovf_exc))
         self.av_ZeroDivisionError = ConstObj(ootype.cast_to_object(cpu.ll_zero_exc))
     else:
         self.av_OverflowError = None
         self.av_ZeroDivisionError = None
     self.box2type = {}
Exemplo n.º 26
0
def restype_bool(self):     return typeof(System.Boolean)
def restype_int(self):      return typeof(System.Int32)
Exemplo n.º 27
0
def restype_bool(self):
    return typeof(System.Boolean)
Exemplo n.º 28
0
def restype_uint(self):
    return typeof(System.Int32)
Exemplo n.º 29
0
def restype_float(self):
    return typeof(System.Double)
Exemplo n.º 30
0
def restype_unichar(self):  return typeof(System.Char)
def restype_longlong(self): return typeof(System.Int64)
Exemplo n.º 31
0
def restype_float(self):    return typeof(System.Double)
def restype_char(self):     return typeof(System.Char)
Exemplo n.º 32
0
 def emit_op_call(self, op):
     emit_op = Method.emit_op_call_impl.im_func
     exctypes = [dotnet.typeof(System.Exception)]
     self.emit_raising_op(op, emit_op, exctypes)
Exemplo n.º 33
0
 def fn():
     return typeof(DelegateType) is not None
Exemplo n.º 34
0
 def emit_op_runtimenew(self, op):
     clitype_utils = dotnet.typeof(Utils)
     methinfo = clitype_utils.GetMethod('RuntimeNew')
     op.args[0].load(self)
     self.il.Emit(OpCodes.Call, methinfo)
     self.store_result(op)
Exemplo n.º 35
0
 def fn():
     t = typeof(FUNCTYPE)
     return t.get_Name()
Exemplo n.º 36
0
 def fn():
     t = typeof(FUNCTYPE)
     return t.get_Name()
Exemplo n.º 37
0
def restype_int(self):      return typeof(System.Int32)
def restype_uint(self):     return typeof(System.Int32)
Exemplo n.º 38
0
 def fn():
     x = box(42)
     return x.GetType() == typeof(System.Int32)
Exemplo n.º 39
0
def restype_uint(self):     return typeof(System.Int32)
def restype_float(self):    return typeof(System.Double)
Exemplo n.º 40
0
def restype_longlong(self):
    return typeof(System.Int64)
Exemplo n.º 41
0
def restype_char(self):     return typeof(System.Char)
def restype_unichar(self):  return typeof(System.Char)
Exemplo n.º 42
0
def restype_unichar(self):
    return typeof(System.Char)
Exemplo n.º 43
0
def restype_longlong(self): return typeof(System.Int64)

def fillops(ops, baseclass):
Exemplo n.º 44
0
 def _get_meth_wrapper(self):
     restype = dotnet.class2type(cVoid)
     args = self._get_args_array([dotnet.typeof(InputArgs)])
     return get_method_wrapper(self.name, restype, args)