Changeset 264 for reflect

Show
Ignore:
Timestamp:
04/18/09 12:43:16 (3 years ago)
Author:
dhasenan
Message:

Converted to reflect.Variant.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • reflect/reflect/invoke.d

    r263 r264  
    22 
    33import reflect.RuntimeTraits; 
    4 import std.boxer; 
    54import std.stdio; 
     5import reflect.variant; 
    66 
    77enum StackAlign = 4; 
     
    4343} 
    4444 
    45 Box invoke (void* func, TypeInfo returnType, ...) 
     45Variant invoke (void* func, TypeInfo returnType, ...) 
    4646{ 
    4747    return invokeDirect (func, returnType, _arguments, _argptr); 
    4848} 
    4949 
    50 Box invokeDirect (void* func, TypeInfo returnType, TypeInfo[] _arguments, void* _argptr) 
     50Variant invokeDirect (void* func, TypeInfo returnType, TypeInfo[] _arguments, void* _argptr) 
    5151{ 
    5252    void*[] args; 
     
    5454    { 
    5555        args ~= _argptr; 
    56         //writefln ("item: %s %s", a, *(cast(int*)_argptr)); 
     56        //writefln ("item: %s %s %s", a, *(cast(int*)_argptr), *(cast(double*)_argptr)); 
    5757        _argptr += a.tsize; 
    5858    } 
    5959    auto ret = invokeDirect (func, returnType, returnType.tsize(), args, _arguments); 
    60     return box (returnType, ret.ptr); 
     60    return getRuntimeVariant (returnType, ret.ptr); 
    6161} 
    6262 
     
    7979    int eax, edx; 
    8080    double st0; // TODO 
    81     if (ets == 3 || ets > 5 || et is typeid(float)) 
     81    if (ets == 3 || ets > 5 || isFloat(et)) 
    8282    { 
    8383        end = args.length; 
     
    8989    for (int i = 0; i < end; i++) 
    9090    { 
    91         auto pointer = cast(int*)args[i]; 
     91        auto pointer = args[i]; 
    9292        auto size = types[i].tsize; 
    9393        int tmp; 
     
    101101                mov tmp, EBX; 
    102102            } 
    103             //writefln("pushed %s as %s", *pointer, tmp); 
     103            //writefln("pushed %s as %s", *cast(int*)pointer, tmp); 
    104104            pointer++; 
    105105            size -= 4; 
    106106        } 
    107         /* 
    108         size = types[i].tsize; 
    109         size %= StackAlign; 
    110         size = StackAlign - size; 
    111         while (size) 
    112         { 
    113             size--; 
    114              
    115             asm 
    116             { 
    117                 mov EAX, pad; 
    118                 push [EAX]; 
    119             } 
    120         } 
    121         */ 
    122107    } 
    123108    if (end < args.length) 
     
    136121        mov eax, EAX; 
    137122        mov edx, EDX; 
    138         fst st0, ST(0)
     123        fst st0
    139124    } 
    140125 
     
    167152    else if (isFloat(returnType)) 
    168153    { 
    169         assert (false, "cannot yet handle floating point types"); 
     154        //assert (false, "cannot yet handle floating point types"); 
    170155        if (returnType == typeid(float)) 
    171156        { 
     
    213198    auto box = invoke (func, typeid(int), 2, 3); 
    214199    assert (invokeCount == 1); 
    215     auto val = unbox!(int)(box); 
     200    auto val = box.to!(int); 
    216201    if (val != 5) 
    217202    { 
     
    233218    assert (invokeCount == 1); 
    234219} 
     220 
     221// test with floating point returns 
     222double bar(int i, double j)  
     223{  
     224    invokeCount++; 
     225    writefln("bar: i = %s j = %s", i, j); 
     226    return (4.5 + i) * j;  
     227} 
     228unittest 
     229{ 
     230    writeln("test: floating point"); 
     231    invokeCount = 0; 
     232    void* func = cast(void*)&bar; 
     233    auto box = invoke (func, typeid(double), 2, 3.5); 
     234    assert (invokeCount == 1); 
     235    auto ret = box.get!(double); 
     236    auto expected = bar(2, 3.5); 
     237    if (ret != expected) 
     238    { 
     239        writefln("expected: %s but was: %s", expected, ret); 
     240        assert (false); 
     241    } 
     242} 
     243 
    235244/* 
    236245unittest 
  • reflect/reflect/typeinfoex.d

    r262 r264  
    44module reflect.typeinfoex; 
    55import reflect.invoke; 
     6import reflect.variant; 
    67 
    78import std.traits; 
     
    5960    /** The argument types for this function. */ 
    6061    TypeInfo[] arguments; 
    61     private this (string name, TypeInfo returnType, TypeInfo[] arguments) 
     62    /** Pointer to the function. */ 
     63    void* funcptr; 
     64 
     65    private this (string name, TypeInfo returnType, TypeInfo[] arguments, void* funcptr) 
    6266    { 
    6367        this.name = name; 
     
    6973    // TODO invoke 
    7074 
    71     /** NOT IMPLEMENTED: invoke a function, returning a Box containing the return value. */ 
    72     Box invoke(Box[] arguments) 
     75    /** NOT IMPLEMENTED: invoke a function, returning a Variant containing the return value. */ 
     76    Variant invoke(...) 
     77    { 
     78        return invoke (_arguments, _argptr); 
     79    } 
     80 
     81    Variant invoke (TypeInfo[] arguments, void* argptr) 
     82    { 
     83        return .invokeDirect (funcptr, returnType, arguments, argptr); 
     84    } 
     85 
     86    Variant invoke(Variant[] arguments) 
    7387    { 
    7488        throw new Exception ("FuncInfo.invoke: not implemented"); 
     
    101115 
    102116    /** Get the value for this field for the given object. */ 
    103     Box get (Object o)  
     117    Variant get (Object o)  
    104118    { 
    105119        return get (*cast(void**)&o); 
     
    107121 
    108122    /// ditto 
    109     Box get (void* ptr)  
     123    Variant get (void* ptr)  
    110124    { 
    111125        ptr += offset; 
    112         return box (type, ptr); 
     126        return getRuntimeVariant (type, (cast(ubyte*)ptr)[0..type.tsize]); 
    113127    } 
    114128 
     
    254268            alias typeof(__traits(getVirtualFunctions, This, name)[overload]) Type; 
    255269            TypeInfo[] types = get_parameters!(Type)(); 
    256             FuncInfo info = new FuncInfo(name, typeid(ReturnType!(Type)), types); 
     270            FuncInfo info = new FuncInfo(name, typeid(ReturnType!(Type)), types, null); 
    257271            funcs ~= info; 
    258272            get_overloads!(This, name, overload + 1)(funcs); 
     
    347361        auto field = tix.field("field1"); 
    348362        auto box = field.get(t); 
    349         assert (unbox!(int)(box) == 8); 
     363        assert (box.to!(int) == 8); 
    350364    } 
    351365 
  • reflect/reflect/variant.d

    r250 r264  
    2727import reflect.variant_array; 
    2828import reflect.RuntimeTraits; 
    29 import Float=tango.text.convert.Float; 
    30 import tango.text.convert.Format; 
    31 import tango.core.Traits; 
    32 import tango.math.IEEE; 
    33 import tango.io.Stdout; 
     29version (Tango) 
     30
     31    import Float=tango.text.convert.Float; 
     32    import tango.text.convert.Format; 
     33    import tango.core.Traits; 
     34    import tango.math.IEEE; 
     35    import tango.io.Stdout; 
     36
     37else 
     38
     39    template isIntegerType(T) 
     40    { 
     41        static if (is (T == uint) || is (T == ulong) || is (T == ushort) || is (T == ubyte)) 
     42        { 
     43            enum isIntegerType = true; 
     44        } 
     45        else static if (is (T == int) || is (T == long) || is (T == short) || is (T == byte)) 
     46        { 
     47            enum isIntegerType = true; 
     48        } 
     49        else 
     50        { 
     51            enum isIntegerType = false; 
     52        } 
     53    } 
     54    template isFloatingPointType(T) 
     55    { 
     56        static if (is (T == float) || is (T == real) || is (T == double)) 
     57        { 
     58            enum isFloatingPointType = true; 
     59        } 
     60        else 
     61        { 
     62            enum isFloatingPointType = false; 
     63        } 
     64    } 
     65
    3466 
    3567const MaxInline = 16; // I'll store 16 bytes in the variant itself 
     
    3769class VariantTypeMismatchException : Exception 
    3870{ 
    39     this (char[] message) 
     71    this (string message) 
    4072    { 
    4173        super (message); 
     
    104136        } 
    105137        type = typeid(T); 
    106         return *this; 
     138        version (D_Version2) 
     139        { 
     140            return this; 
     141        } 
     142        else 
     143        { 
     144            return *this; 
     145        } 
    107146    } 
    108147 
     
    150189    bool comparable (Variant other) 
    151190    { 
    152         return !(isNaN || other.isNaN); 
     191        version (D_Version1) 
     192            return !(isNaN || other.isNaN); 
     193        else 
     194            return true; 
    153195    } 
    154196 
     
    159201        if (isArray (type) && isArray (other.type)) 
    160202        { 
    161             auto arrthis = VariantArray.fromVariant (*this); 
     203            version (D_Version2) 
     204            { 
     205                auto arrthis = VariantArray.fromVariant (this); 
     206            } 
     207            else 
     208            { 
     209                auto arrthis = VariantArray.fromVariant (*this); 
     210            } 
    162211            auto arrother = VariantArray.fromVariant (other); 
    163212            return (arrthis == arrother); 
     
    174223    } 
    175224 
    176     bool isNaN () 
    177     { 
    178         if (isFloat (type)) 
    179         { 
    180             real stored = force!(real); 
    181             return (.isNaN (stored)) != 0; 
    182         } 
    183         return false; 
    184     } 
     225    version (D_Version1) 
     226        bool isNaN () 
     227        { 
     228            if (isFloat (type)) 
     229            { 
     230                real stored = force!(real); 
     231                return (.isNaN (stored)) != 0; 
     232            } 
     233            return false; 
     234        } 
    185235 
    186236    int opCmp (Variant other) 
     
    251301    } 
    252302 
     303    version (D_Version1) version (Tango) 
    253304    char[] toString () 
    254305    { 
     
    256307        { 
    257308            char[] value = "\""; 
    258             auto array = VariantArray.fromVariant (*this); 
     309            version (D_Version2) 
     310            { 
     311                auto array = VariantArray.fromVariant (this); 
     312            } 
     313            else 
     314            { 
     315                auto array = VariantArray.fromVariant (*this); 
     316            } 
    259317            foreach (i, v; array) 
    260318                value ~= v.toString; 
     
    269327    } 
    270328 
    271     char[] typeString () 
     329    string typeString () 
    272330    { 
    273331        if (obj) 
     
    332390        return var; 
    333391    } 
     392} 
     393 
     394Variant getRuntimeVariant (TypeInfo type, void* data) 
     395{ 
     396    return getRuntimeVariant (type, (cast(ubyte*)data)[0..type.tsize]); 
    334397} 
    335398 
  • reflect/reflect/variant_array.d

    r250 r264  
    2525 
    2626import reflect.RuntimeTraits; 
    27 import tango.core.Traits; 
    2827import reflect.variant; 
     28//import tango.core.Traits; 
    2929 
    3030struct VariantArray 
     
    4747        static if (!is (T == Variant)) 
    4848        { 
    49             static if (isDynamicArrayType!(T) || isStaticArrayType!(T)) 
     49            if (isArray(typeid(T))) 
    5050                // This is slow, but it's DRY. 
    5151                return opAssign (Variant (array)); 
    5252            else 
    53                 static assert (false, "cannot assign a non-array to a VariantArray"); 
     53                assert (false, "cannot assign a non-array to a VariantArray"); 
    5454        } 
    5555        else 
     
    8585                    this.data = (&this.data[0])[0 .. (this.length * this.size)]; 
    8686            } 
    87             return *this; 
     87            version (D_Version1) 
     88                return *this; 
     89            else 
     90                return this; 
    8891        } 
    8992    } 
     
    108111        for (uint i = 0; i < length; i++) 
    109112        { 
    110             result = dg (i, opIndex (i)); 
     113            auto v = opIndex(i); 
     114            result = dg (i, v); 
    111115            if (result) 
    112116                break; 
     
    119123        if (this.length != other.length) 
    120124            return false; 
    121         foreach (i, var; *this) 
     125        VariantArray self; 
     126        version (D_Version2) self = this; else self = *this; 
     127        foreach (i, var; self) 
    122128        { 
    123129            if (other[i] != var) 
     
    127133    } 
    128134 
     135    version (D_Version1) version(Tango) 
    129136    char[] toString () 
    130137    {