Changeset 237
- Timestamp:
- 11/17/07 00:24:58 (1 year ago)
- Files:
-
- trunk/minid/bind.d (modified) (17 diffs)
- trunk/minid/utils.d (modified) (1 diff)
- trunk/samples/simple.md (modified) (4 diffs)
- trunk/test.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/minid/bind.d
r236 r237 46 46 { 47 47 dchar[] name = utf.toUtf32(member.Name); 48 ns[name] = MDValue(new MDClosure(ns, &member. Function, name));48 ns[name] = MDValue(new MDClosure(ns, &member.WrappedFunc, name)); 49 49 } 50 50 else static if(is(typeof(member.isClass))) 51 51 { 52 52 dchar[] name = utf.toUtf32(member.Name); 53 ns[name] = MDValue( new member.Class());53 ns[name] = MDValue(member.makeClass()); 54 54 } 55 55 else static if(is(typeof(member.isStruct))) … … 80 80 This wraps a function, and is meant to be used as a parameter to WrapModule. 81 81 */ 82 public struct WrapFunc(alias func , char[] name = NameOfFunc!(func), funcType = typeof(&func))82 public struct WrapFunc(alias func) 83 83 { 84 84 const bool isFunc = true; 85 alias WrappedFunc!(func, name, funcType) Function;86 const char[] Name = name;85 const char[] Name = NameOfFunc!(func); 86 mixin WrappedFunc!(func, Name, typeof(&func)); 87 87 } 88 88 … … 90 90 public template WrapFunc(alias func, funcType) 91 91 { 92 alias WrapFunc!(func, NameOfFunc!(func), funcType) WrapFunc; 92 const bool isFunc = true; 93 const char[] Name = NameOfFunc!(func); 94 mixin WrappedFunc!(func, Name, funcType); 95 } 96 97 /// ditto 98 public struct WrapFunc(alias func, char[] name) 99 { 100 const bool isFunc = true; 101 const char[] Name = name; 102 mixin WrappedFunc!(func, Name, typeof(&func)); 103 } 104 105 /// ditto 106 public struct WrapFunc(alias func, char[] name, funcType) 107 { 108 const bool isFunc = true; 109 const char[] Name = name; 110 mixin WrappedFunc!(func, Name, funcType); 93 111 } 94 112 … … 116 134 } 117 135 136 private class WrappedClass : MDClass 137 { 138 private this(dchar[] name, MDClass base) 139 { 140 super(name, base); 141 } 142 143 public override WrappedInstance newInstance() 144 { 145 return new WrappedInstance(this); 146 } 147 } 148 118 149 private class WrappedInstance : MDInstance 119 150 { … … 128 159 private class WrappedStruct(T) : MDInstance 129 160 { 130 // alias FieldNames!(T) fieldNames;131 161 private T inst; 132 162 … … 135 165 super(owner); 136 166 } 137 138 // private void updateFields()139 // {140 // foreach(i, v; fieldNames)141 // mFields[mixin("\"" ~ fieldNames[i] ~ "\"d")] = MDValue(inst.tupleof[i]);142 // }143 //144 // private T* toStruct()145 // {146 // foreach(i, v; fieldNames)147 // inst.tupleof[i] = mFields[mixin("\"" ~ fieldNames[i] ~ "\"d")].to!(typeof(inst.tupleof[i]));148 //149 // return &inst;150 // }151 167 } 152 168 153 169 /** 154 170 Wraps a D class of the given type with the given members. name is the name that will be given to the class in MiniD. 171 This is meant to be used as a parameter to WrapModule. 155 172 */ 156 173 public struct WrapClass(ClassType, char[] name = ClassType.stringof, Members...) … … 161 178 const char[] Name = name; 162 179 const CtorCount = CountCtors!(Members); 180 const className = ToUTF32!(name); 163 181 164 182 static assert(CtorCount <= 1, "Cannot have more than one WrapCtors instance in wrapped class parameters for class " ~ ClassType.stringof); 165 183 166 static class Class : MDClass 167 { 168 private this() 169 { 170 alias BaseTypeTupleOf!(ClassType) Bases; 184 public static WrappedClass makeClass() 185 { 186 alias BaseTypeTupleOf!(ClassType) Bases; 187 188 if(typeid(ClassType) in WrappedClasses) 189 throw new MDException("Native class " ~ ClassType.stringof ~ " cannot be wrapped more than once"); 190 191 WrappedClass self; 192 193 static if(!is(Bases[0] == Object)) 194 { 195 self = new WrappedClass(className, GetWrappedClass(typeid(Bases[0]))); 196 } 197 else 198 { 199 self = new WrappedClass(className, null); 200 } 201 202 WrappedClasses[typeid(ClassType)] = self; 203 204 foreach(i, member; Members) 205 { 206 static if(is(typeof(member.isMethod))) 207 { 208 const name = ToUTF32!(member.Name); 209 self.mMethods[name] = MDValue(new MDClosure(self.mMethods, &WrappedMethod!(member.Func, member.FuncType, ClassType), className ~ "." ~ name)); 210 } 211 else static if(is(typeof(member.isProperty))) 212 { 213 const name = ToUTF32!(member.Name); 214 self.mMethods[name] = MDValue(new MDClosure(self.mMethods, &WrappedProperty!(member.Func, member.Name, member.FuncType, ClassType), className ~ "." ~ name)); 215 } 216 else static if(is(typeof(member.isCtors))) 217 { 218 self.mMethods["constructor"d] = MDValue(new MDClosure(self.mMethods, &constructor!(member.Types), className ~ ".constructor")); 219 } 220 else 221 static assert(false, "Invalid member type '" ~ typeof(member).stringof ~ "' in wrapped class '" ~ name ~ "'"); 222 } 223 224 static if(CtorCount == 0) 225 self.mMethods["constructor"d] = MDValue(new MDClosure(self.mMethods, &defaultCtor, className ~ ".constructor")); 171 226 172 if(typeid(ClassType) in WrappedClasses) 173 throw new MDException("Native class " ~ ClassType.stringof ~ " cannot be wrapped more than once"); 174 175 WrappedClasses[typeid(ClassType)] = this; 176 dchar[] className = utf.toUtf32(name); 177 178 static if(!is(Bases[0] == Object)) 179 { 180 super(className, GetWrappedClass(typeid(Bases[0]))); 181 } 182 else 183 { 184 super(className, null); 185 } 186 187 foreach(i, member; Members) 188 { 189 static if(is(typeof(member.isMethod))) 190 { 191 dchar[] name = utf.toUtf32(member.Name); 192 mMethods[name] = MDValue(new MDClosure(mMethods, &WrappedMethod!(member.Func, member.FuncType, ClassType), className ~ "." ~ name)); 193 } 194 else static if(is(typeof(member.isProperty))) 195 { 196 dchar[] name = utf.toUtf32(member.Name); 197 mMethods[name] = MDValue(new MDClosure(mMethods, &WrappedProperty!(member.Func, member.Name, member.FuncType, ClassType), className ~ "." ~ name)); 198 } 199 else static if(is(typeof(member.isCtors))) 200 { 201 mMethods["constructor"d] = MDValue(new MDClosure(mMethods, &constructor!(member.Types), className ~ ".constructor")); 202 } 203 else 204 static assert(false, "Invalid member type '" ~ typeof(member).stringof ~ "' in wrapped class '" ~ name ~ "'"); 205 } 206 207 static if(CtorCount == 0) 208 mMethods["constructor"d] = MDValue(new MDClosure(mMethods, &defaultCtor, className ~ ".constructor")); 209 } 210 211 public override WrappedInstance newInstance() 212 { 213 return new WrappedInstance(this); 214 } 215 216 static if(CtorCount == 0) 217 { 218 static assert(is(typeof(new ClassType())), "Cannot call default constructor for class " ~ ClassType.stringof ~ "; please wrap a constructor explicitly"); 219 220 private int defaultCtor(MDState s, uint numParams) 221 { 222 auto self = s.getContext!(WrappedInstance); 223 assert(self !is null, "Invalid 'this' parameter passed to " ~ ClassType.stringof ~ ".constructor"); 224 225 self.inst = new ClassType(); 226 return 0; 227 } 228 } 229 230 private int constructor(Ctors...)(MDState s, uint numParams) 227 return self; 228 } 229 230 static if(CtorCount == 0) 231 { 232 static assert(is(typeof(new ClassType())), "Cannot call default constructor for class " ~ ClassType.stringof ~ "; please wrap a constructor explicitly"); 233 234 private static int defaultCtor(MDState s, uint numParams) 235 { 236 auto self = s.getContext!(WrappedInstance); 237 assert(self !is null, "Invalid 'this' parameter passed to " ~ ClassType.stringof ~ ".constructor"); 238 239 self.inst = new ClassType(); 240 return 0; 241 } 242 } 243 else 244 { 245 private static int constructor(Ctors...)(MDState s, uint numParams) 231 246 { 232 247 auto self = s.getContext!(WrappedInstance); … … 256 271 } 257 272 } 258 273 259 274 for(uint i = 0; i < numParams; i++) 260 275 args[i] = s.getParam(i); … … 268 283 { 269 284 typeString ~= s.getParam(0u).typeString(); 270 285 271 286 for(uint i = 1; i < numParams; i++) 272 287 typeString ~= ", " ~ s.getParam(i).typeString(); 273 288 } 274 289 275 290 typeString ~= ")"; 276 291 … … 387 402 Wrap a class method given an alias to the method (like A.foo). To be used as a parameter to one of the class wrapping templates. 388 403 */ 389 public struct WrapMethod(alias func , char[] name = NameOfFunc!(func), funcType = typeof(&func))404 public struct WrapMethod(alias func) 390 405 { 391 406 const bool isMethod = true; 407 const char[] Name = NameOfFunc!(func); 392 408 alias func Func; 409 alias typeof(&func) FuncType; 410 } 411 412 /// ditto 413 public struct WrapMethod(alias func, char[] name) 414 { 415 const bool isMethod = true; 393 416 const char[] Name = name; 417 alias func Func; 418 alias typeof(&func) FuncType; 419 } 420 421 /// ditto 422 public struct WrapMethod(alias func, funcType) 423 { 424 const bool isMethod = true; 425 const char[] Name = NameOfFunc!(func); 426 alias func Func; 394 427 alias funcType FuncType; 395 428 } 396 429 397 430 /// ditto 398 public template WrapMethod(alias func, funcType) 399 { 400 alias WrapMethod!(func, NameOfFunc!(func), funcType) WrapMethod; 431 public struct WrapMethod(alias func, char[] name, funcType) 432 { 433 const bool isMethod = true; 434 const char[] Name = name; 435 alias func Func; 436 alias funcType FuncType; 401 437 } 402 438 … … 408 444 or getter as appropriate based on how many parameters were passed to the MiniD method. 409 445 */ 410 public struct WrapProperty(alias func, char[] name = NameOfFunc!(func), funcType = typeof(&func)) 446 public struct WrapProperty(alias func) 447 { 448 const bool isProperty = true; 449 alias func Func; 450 const char[] Name = NameOfFunc!(func); 451 alias typeof(&func) FuncType; 452 } 453 454 /// ditto 455 public struct WrapProperty(alias func, char[] name) 456 { 457 const bool isProperty = true; 458 alias func Func; 459 const char[] Name = name; 460 alias typeof(&func) FuncType; 461 } 462 463 /// ditto 464 public struct WrapProperty(alias func, funcType) 465 { 466 const bool isProperty = true; 467 alias func Func; 468 const char[] Name = NameOfFunc!(func); 469 alias funcType FuncType; 470 } 471 472 /// ditto 473 public struct WrapProperty(alias func, char[] name, funcType) 411 474 { 412 475 const bool isProperty = true; … … 441 504 static class Class : MDClass 442 505 { 443 p rivatethis()506 public this() 444 507 { 445 508 if(typeid(StructType) in WrappedClasses) … … 447 510 448 511 WrappedClasses[typeid(StructType)] = this; 449 dchar[] structName = utf.toUtf32(name);450 512 const structName = ToUTF32!(name); 513 451 514 super(structName, null); 452 453 //foreach(i, n; FieldNames!(StructType))454 // mFields[mixin("\"" ~ FieldNames!(StructType)[i] ~ "\"d")] = MDValue(StructType.init.tupleof[i]);455 515 456 516 mMethods["opIndex"d] = MDValue(new MDClosure(mMethods, &getField, structName ~ ".opIndex")); … … 526 586 } 527 587 } 528 588 529 589 for(uint i = 0; i < numParams; i++) 530 590 args[i] = s.getParam(i); … … 542 602 typeString ~= ", " ~ s.getParam(i).typeString(); 543 603 } 544 604 545 605 typeString ~= ")"; 546 606 547 607 s.throwRuntimeException("Parameter list {} passed to constructor does not match any wrapped constructors", typeString); 548 608 } 549 609 550 610 private int getField(MDState s, uint numParams) 551 611 { … … 656 716 } 657 717 658 private int WrappedFunc(alias func, char[] name, funcType)(MDState s, uint numParams) 659 { 660 ParameterTupleOf!(funcType) args; 661 const minArgs = MinArgs!(func); 662 const maxArgs = args.length; 663 664 if(numParams < minArgs) 665 s.throwRuntimeException("At least " ~ itoa!(minArgs) ~ " parameter" ~ (minArgs == 1 ? "" : "s") ~ " expected, not {}", numParams); 666 667 if(numParams > maxArgs) 668 numParams = maxArgs; 669 670 static if(args.length == 0) 671 { 672 static if(is(ReturnTypeOf!(funcType) == void)) 673 { 674 func(); 675 return 0; 718 private template WrappedFunc(alias func, char[] name, funcType) 719 { 720 static int WrappedFunc(MDState s, uint numParams) 721 { 722 ParameterTupleOf!(funcType) args; 723 const minArgs = MinArgs!(func); 724 const maxArgs = args.length; 725 726 if(numParams < minArgs) 727 s.throwRuntimeException("At least " ~ itoa!(minArgs) ~ " parameter" ~ (minArgs == 1 ? "" : "s") ~ " expected, not {}", numParams); 728 729 if(numParams > maxArgs) 730 numParams = maxArgs; 731 732 static if(args.length == 0) 733 { 734 static if(is(ReturnTypeOf!(funcType) == void)) 735 { 736 func(); 737 return 0; 738 } 739 else 740 { 741 s.push(ToMiniDType(func())); 742 return 1; 743 } 676 744 } 677 745 else 678 746 { 679 s.push(ToMiniDType(func())); 680 return 1; 681 } 682 } 683 else 684 { 685 static if(minArgs == 0) 686 { 687 if(numParams == 0) 688 { 689 static if(is(ReturnTypeOf!(funcType) == void)) 690 { 691 func(); 692 return 0; 693 } 694 else 695 { 696 s.push(ToMiniDType(func())); 697 return 1; 698 } 699 } 700 } 701 702 foreach(i, arg; args) 703 { 704 const argNum = i + 1; 705 706 if(i < numParams) 707 args[i] = GetParameter!(typeof(arg))(s, i); 708 709 static if(argNum >= minArgs && argNum <= maxArgs) 710 { 711 if(argNum == numParams) 747 static if(minArgs == 0) 748 { 749 if(numParams == 0) 712 750 { 713 751 static if(is(ReturnTypeOf!(funcType) == void)) 714 752 { 715 func( args[0 .. argNum]);753 func(); 716 754 return 0; 717 755 } 718 756 else 719 757 { 720 s.push(ToMiniDType(func( args[0 .. argNum])));758 s.push(ToMiniDType(func())); 721 759 return 1; 722 760 } 723 761 } 724 762 } 725 } 726 } 727 728 assert(false, "WrappedFunc should never ever get here."); 763 764 foreach(i, arg; args) 765 { 766 const argNum = i + 1; 767 768 if(i < numParams) 769 args[i] = GetParameter!(typeof(arg))(s, i); 770 771 static if(argNum >= minArgs && argNum <= maxArgs) 772 { 773 if(argNum == numParams) 774 { 775 static if(is(ReturnTypeOf!(funcType) == void)) 776 { 777 func(args[0 .. argNum]); 778 return 0; 779 } 780 else 781 { 782 s.push(ToMiniDType(func(args[0 .. argNum]))); 783 return 1; 784 } 785 } 786 } 787 } 788 } 789 790 assert(false, "WrappedFunc should never ever get here."); 791 } 729 792 } 730 793 … … 906 969 if(!v.canCastTo!(WrappedInstance)) 907 970 return false; 908 971 909 972 return (cast(T)v.as!(WrappedInstance).inst) !is null; 910 973 } trunk/minid/utils.d
r225 r237 706 706 alias QSort_greater!(Pred, List[0], List[2 .. $]) QSort_greater; 707 707 } 708 709 /** 710 Convert string literals between unicode encodings at compile time. I swear, this should be 711 built into the compiler or something. 712 */ 713 public template ToUTF8(char[] s) 714 { 715 const char[] ToUTF8 = s; 716 } 717 718 /// ditto 719 public template ToUTF8(wchar[] s) 720 { 721 const char[] ToUTF8 = mixin("\"" ~ s ~ "\"c"); 722 } 723 724 /// ditto 725 public template ToUTF8(dchar[] s) 726 { 727 const char[] ToUTF8 = mixin("\"" ~ s ~ "\"c"); 728 } 729 730 /// ditto 731 public template ToUTF16(char[] s) 732 { 733 const wchar[] ToUTF16 = mixin("\"" ~ s ~ "\"w"); 734 } 735 736 /// ditto 737 public template ToUTF16(wchar[] s) 738 { 739 const wchar[] ToUTF16 = s; 740 } 741 742 /// ditto 743 public template ToUTF16(dchar[] s) 744 { 745 const wchar[] ToUTF16 = mixin("\"" ~ s ~ "\"w"); 746 } 747 748 /// ditto 749 public template ToUTF32(char[] s) 750 { 751 const dchar[] ToUTF32 = mixin("\"" ~ s ~ "\"d"); 752 } 753 754 /// ditto 755 public template ToUTF32(wchar[] s) 756 { 757 const dchar[] ToUTF32 = mixin("\"" ~ s ~ "\"d"); 758 } 759 760 /// ditto 761 public template ToUTF32(dchar[] s) 762 { 763 const dchar[] ToUTF32 = s; 764 } trunk/samples/simple.md
r236 r237 23 23 local origin = Point(0.0, 0.0); 24 24 local white = Color(255, 255, 255); 25 local p1 = Point(100.0, 100.0); 25 26 26 27 while(!arc.input.keyDown(key.Quit) && !arc.input.keyDown(key.Esc)) … … 30 31 arc.time.process(); 31 32 32 col.a = (math.sin(((arc.time.getTime() % 1000) / 500.0) * math.pi) + 1) / 4.0 + 0.5;33 34 33 local p = arc.input.mousePos(); 35 34 36 35 font.draw(p.toString(), p, col); 37 font.draw(toString( col.a), origin, white);36 font.draw(toString(p1.distance(p)), origin, white); 38 37 font.draw(toString(arc.time.fps()), Point(0.0, 16.0), white); 39 38 … … 67 66 { 68 67 mX; 69 68 70 69 this() 71 70 { … … 79 78 } 80 79 } 81 80 82 81 class B : A 83 82 { 84 83 mY; 85 84 86 85 this() 87 86 { 88 87 mY = 10; 89 88 90 89 writefln("B ctor"); 91 90 super(); 92 91 } 93 92 } 94 93 95 94 class C : B 96 95 { trunk/test.d
r236 r237 116 116 arc.font.Font, 117 117 WrapCtors!(void function(char[], int)), 118 WrapMethod!(Font.draw) 118 /* WrapMethod!(Font.getWidth!(dchar)), 119 WrapMethod!(Font.getWidthLastLine!(dchar)), 120 WrapMethod!(Font.getHeight), 121 WrapMethod!(Font.getLineSkip), 122 WrapMethod!(Font.setLineGap), */ 123 WrapMethod!(Font.draw, void function(dchar[], Point, Color))//, 124 //WrapMethod!(Font.calculateIndex!(dchar)) 119 125 ) 120 126 )(ctx); … … 134 140 WrapMethod!(Point.minComponent), 135 141 WrapMethod!(Point.opNeg), 136 // cross, dot 142 WrapMethod!(Point.cross), 143 WrapMethod!(Point.dot), 137 144 WrapMethod!(Point.scale), 138 145 // apply … … 140 147 WrapMethod!(Point.normalise, "normalize"), 141 148 WrapMethod!(Point.normaliseCopy, "normalizeCopy"), 142 // angle143 149 WrapMethod!(Point.rotate), 150 WrapMethod!(Point.rotateCopy), 144 151 WrapMethod!(Point.abs), 145 152 WrapMethod!(Point.absCopy), 146 // clamp153 WrapMethod!(Point.clamp), 147 154 WrapMethod!(Point.randomise, "randomize"), 148 // distance, distanceSquared149 WrapMethod!(Point. rotateCopy),155 WrapMethod!(Point.distance), 156 WrapMethod!(Point.distanceSquared), 150 157 WrapMethod!(Point.getX), 151 158 WrapMethod!(Point.getY), … … 154 161 WrapMethod!(Point.addX), 155 162 WrapMethod!(Point.addY) 163 // above, below, left, right 156 164 ) 157 165 )(ctx);
