- Timestamp:
- 04/18/09 12:43:16 (3 years ago)
- Files:
-
- reflect/reflect/invoke.d (modified) (10 diffs)
- reflect/reflect/typeinfoex.d (modified) (7 diffs)
- reflect/reflect/variant.d (modified) (10 diffs)
- reflect/reflect/variant_array.d (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
reflect/reflect/invoke.d
r263 r264 2 2 3 3 import reflect.RuntimeTraits; 4 import std.boxer;5 4 import std.stdio; 5 import reflect.variant; 6 6 7 7 enum StackAlign = 4; … … 43 43 } 44 44 45 Boxinvoke (void* func, TypeInfo returnType, ...)45 Variant invoke (void* func, TypeInfo returnType, ...) 46 46 { 47 47 return invokeDirect (func, returnType, _arguments, _argptr); 48 48 } 49 49 50 BoxinvokeDirect (void* func, TypeInfo returnType, TypeInfo[] _arguments, void* _argptr)50 Variant invokeDirect (void* func, TypeInfo returnType, TypeInfo[] _arguments, void* _argptr) 51 51 { 52 52 void*[] args; … … 54 54 { 55 55 args ~= _argptr; 56 //writefln ("item: %s %s ", a, *(cast(int*)_argptr));56 //writefln ("item: %s %s %s", a, *(cast(int*)_argptr), *(cast(double*)_argptr)); 57 57 _argptr += a.tsize; 58 58 } 59 59 auto ret = invokeDirect (func, returnType, returnType.tsize(), args, _arguments); 60 return box(returnType, ret.ptr);60 return getRuntimeVariant (returnType, ret.ptr); 61 61 } 62 62 … … 79 79 int eax, edx; 80 80 double st0; // TODO 81 if (ets == 3 || ets > 5 || et is typeid(float))81 if (ets == 3 || ets > 5 || isFloat(et)) 82 82 { 83 83 end = args.length; … … 89 89 for (int i = 0; i < end; i++) 90 90 { 91 auto pointer = cast(int*)args[i];91 auto pointer = args[i]; 92 92 auto size = types[i].tsize; 93 93 int tmp; … … 101 101 mov tmp, EBX; 102 102 } 103 //writefln("pushed %s as %s", * pointer, tmp);103 //writefln("pushed %s as %s", *cast(int*)pointer, tmp); 104 104 pointer++; 105 105 size -= 4; 106 106 } 107 /*108 size = types[i].tsize;109 size %= StackAlign;110 size = StackAlign - size;111 while (size)112 {113 size--;114 115 asm116 {117 mov EAX, pad;118 push [EAX];119 }120 }121 */122 107 } 123 108 if (end < args.length) … … 136 121 mov eax, EAX; 137 122 mov edx, EDX; 138 fst st0 , ST(0);123 fst st0; 139 124 } 140 125 … … 167 152 else if (isFloat(returnType)) 168 153 { 169 assert (false, "cannot yet handle floating point types");154 //assert (false, "cannot yet handle floating point types"); 170 155 if (returnType == typeid(float)) 171 156 { … … 213 198 auto box = invoke (func, typeid(int), 2, 3); 214 199 assert (invokeCount == 1); 215 auto val = unbox!(int)(box);200 auto val = box.to!(int); 216 201 if (val != 5) 217 202 { … … 233 218 assert (invokeCount == 1); 234 219 } 220 221 // test with floating point returns 222 double bar(int i, double j) 223 { 224 invokeCount++; 225 writefln("bar: i = %s j = %s", i, j); 226 return (4.5 + i) * j; 227 } 228 unittest 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 235 244 /* 236 245 unittest reflect/reflect/typeinfoex.d
r262 r264 4 4 module reflect.typeinfoex; 5 5 import reflect.invoke; 6 import reflect.variant; 6 7 7 8 import std.traits; … … 59 60 /** The argument types for this function. */ 60 61 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) 62 66 { 63 67 this.name = name; … … 69 73 // TODO invoke 70 74 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) 73 87 { 74 88 throw new Exception ("FuncInfo.invoke: not implemented"); … … 101 115 102 116 /** Get the value for this field for the given object. */ 103 Boxget (Object o)117 Variant get (Object o) 104 118 { 105 119 return get (*cast(void**)&o); … … 107 121 108 122 /// ditto 109 Boxget (void* ptr)123 Variant get (void* ptr) 110 124 { 111 125 ptr += offset; 112 return box (type, ptr);126 return getRuntimeVariant (type, (cast(ubyte*)ptr)[0..type.tsize]); 113 127 } 114 128 … … 254 268 alias typeof(__traits(getVirtualFunctions, This, name)[overload]) Type; 255 269 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); 257 271 funcs ~= info; 258 272 get_overloads!(This, name, overload + 1)(funcs); … … 347 361 auto field = tix.field("field1"); 348 362 auto box = field.get(t); 349 assert ( unbox!(int)(box) == 8);363 assert (box.to!(int) == 8); 350 364 } 351 365 reflect/reflect/variant.d
r250 r264 27 27 import reflect.variant_array; 28 28 import 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; 29 version (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 } 37 else 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 } 34 66 35 67 const MaxInline = 16; // I'll store 16 bytes in the variant itself … … 37 69 class VariantTypeMismatchException : Exception 38 70 { 39 this ( char[]message)71 this (string message) 40 72 { 41 73 super (message); … … 104 136 } 105 137 type = typeid(T); 106 return *this; 138 version (D_Version2) 139 { 140 return this; 141 } 142 else 143 { 144 return *this; 145 } 107 146 } 108 147 … … 150 189 bool comparable (Variant other) 151 190 { 152 return !(isNaN || other.isNaN); 191 version (D_Version1) 192 return !(isNaN || other.isNaN); 193 else 194 return true; 153 195 } 154 196 … … 159 201 if (isArray (type) && isArray (other.type)) 160 202 { 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 } 162 211 auto arrother = VariantArray.fromVariant (other); 163 212 return (arrthis == arrother); … … 174 223 } 175 224 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 } 185 235 186 236 int opCmp (Variant other) … … 251 301 } 252 302 303 version (D_Version1) version (Tango) 253 304 char[] toString () 254 305 { … … 256 307 { 257 308 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 } 259 317 foreach (i, v; array) 260 318 value ~= v.toString; … … 269 327 } 270 328 271 char[]typeString ()329 string typeString () 272 330 { 273 331 if (obj) … … 332 390 return var; 333 391 } 392 } 393 394 Variant getRuntimeVariant (TypeInfo type, void* data) 395 { 396 return getRuntimeVariant (type, (cast(ubyte*)data)[0..type.tsize]); 334 397 } 335 398 reflect/reflect/variant_array.d
r250 r264 25 25 26 26 import reflect.RuntimeTraits; 27 import tango.core.Traits;28 27 import reflect.variant; 28 //import tango.core.Traits; 29 29 30 30 struct VariantArray … … 47 47 static if (!is (T == Variant)) 48 48 { 49 static if (isDynamicArrayType!(T) || isStaticArrayType!(T))49 if (isArray(typeid(T))) 50 50 // This is slow, but it's DRY. 51 51 return opAssign (Variant (array)); 52 52 else 53 staticassert (false, "cannot assign a non-array to a VariantArray");53 assert (false, "cannot assign a non-array to a VariantArray"); 54 54 } 55 55 else … … 85 85 this.data = (&this.data[0])[0 .. (this.length * this.size)]; 86 86 } 87 return *this; 87 version (D_Version1) 88 return *this; 89 else 90 return this; 88 91 } 89 92 } … … 108 111 for (uint i = 0; i < length; i++) 109 112 { 110 result = dg (i, opIndex (i)); 113 auto v = opIndex(i); 114 result = dg (i, v); 111 115 if (result) 112 116 break; … … 119 123 if (this.length != other.length) 120 124 return false; 121 foreach (i, var; *this) 125 VariantArray self; 126 version (D_Version2) self = this; else self = *this; 127 foreach (i, var; self) 122 128 { 123 129 if (other[i] != var) … … 127 133 } 128 134 135 version (D_Version1) version(Tango) 129 136 char[] toString () 130 137 {
