1 module testdll; 2 3 import deimos.python.Python; 4 import pyd.pyd; 5 import std.stdio, std..string; 6 7 void foo() { 8 writeln("20 Monkey"); 9 } 10 11 void foo(int i) { 12 writefln("You entered %s", i); 13 } 14 15 string bar(int i) { 16 if (i > 10) { 17 return "It's greater than 10!"; 18 } else { 19 return "It's less than 10!"; 20 } 21 } 22 23 void baz(int i=10, string s="moo") { 24 writefln("i = %s\ns = %s", i, s); 25 } 26 27 class Foo { 28 int m_i; 29 this() { } 30 this(int i) { 31 m_i = i; 32 } 33 this(int i, int j) { 34 m_i = i + j; 35 } 36 void foo() { 37 writefln("Foo.foo(): i = %s", m_i); 38 } 39 int length() { return 10; } 40 int opSlice(size_t i1, size_t i2) { 41 writeln(i1, " ", i2); 42 return 12; 43 } 44 int opIndex(int x, int y) { 45 writeln(x, " ", y); 46 return x+y; 47 } 48 Foo opBinary(string op)(Foo f) if(op == "+") 49 { 50 return new Foo(m_i + f.m_i); 51 } 52 53 struct Range { 54 int i = 0; 55 56 @property bool empty() { 57 return i >= 10; 58 } 59 @property int front() { 60 return i+1; 61 } 62 void popFront() { 63 i++; 64 } 65 } 66 67 Range opSlice() { 68 return Range(); 69 } 70 @property int i() { return m_i; } 71 @property void i(int j) { m_i = j; } 72 void a() {} 73 void b() {} 74 void c() {} 75 void d() {} 76 void e() {} 77 void f() {} 78 void g() {} 79 void h() {} 80 void j() {} 81 void k() {} 82 void l() {} 83 void m() {} 84 void n() {} 85 void o() {} 86 void p() {} 87 void q() {} 88 void r() {} 89 void s() {} 90 void t() {} 91 void u() {} 92 void v() {} 93 void w() {} 94 void x() {} 95 void y() {} 96 void z() {} 97 } 98 99 void delegate() func_test() { 100 return { writeln("Delegate works!"); }; 101 } 102 103 void dg_test(void delegate() dg) { 104 dg(); 105 } 106 107 class Bar { 108 int[] m_a; 109 this() { } 110 this(int[] i ...) { m_a = i; } 111 } 112 113 struct S { 114 int i; 115 char[] s; 116 void write_s() { 117 writeln(s); 118 } 119 } 120 121 122 struct A { 123 int i; 124 } 125 126 Foo spam(Foo f) { 127 f.foo(); 128 Foo g = new Foo(f.i + 10); 129 return g; 130 } 131 132 void throws() { 133 throw new Exception("Yay! An exception!"); 134 } 135 136 A conv1() { 137 A a; 138 a.i = 12; 139 return a; 140 } 141 void conv2(A a) { 142 writeln(a.i); 143 } 144 145 extern(C) void PydMain() { 146 pragma(msg, "testdll.PydMain"); 147 ex_d_to_python(delegate int(A a) { return a.i; }); 148 ex_python_to_d(delegate A(int i) { A a; a.i = i; return a; }); 149 150 def!(foo)(); 151 // Python does not support function overloading. This requires us to wrap 152 // an overloading function under a different name. Note that if the 153 // overloaded function is not the lexically first, the type of the function 154 // must be specified 155 def!(foo, PyName!"foo2", void function(int))(); 156 pragma(msg, bar.mangleof); 157 def!(bar)(); 158 // Default argument support - Now implicit! 159 def!(baz)(); 160 def!(spam)(); 161 def!(func_test)(); 162 def!(dg_test)(); 163 def!(throws)(); 164 def!(conv1)(); 165 def!(conv2)(); 166 167 module_init(); 168 wrap_class!( 169 Foo, 170 PyName!"Foo", 171 Docstring!"A sample class.", 172 Init!(int), 173 Init!(int, int), 174 Property!(Foo.i, Docstring!"A sample property of Foo."), 175 OpBinary!("+"), 176 Def!(Foo.opSlice, PyName!"__iter__", Foo.Range function()), 177 Def!(Foo.foo, Docstring!"A sample method of Foo."), 178 Def!(Foo.a), 179 Def!(Foo.b), 180 Def!(Foo.c), 181 Def!(Foo.d), 182 Def!(Foo.e), 183 Def!(Foo.f), 184 Def!(Foo.g), 185 Def!(Foo.h), 186 Def!(Foo.j), 187 Def!(Foo.k), 188 Def!(Foo.l), 189 Def!(Foo.m), 190 Def!(Foo.n)/*, // Maximum length 191 Def!(Foo.o), 192 Def!(Foo.p), 193 Def!(Foo.q), 194 Def!(Foo.r), 195 Def!(Foo.s), 196 Def!(Foo.t), 197 Def!(Foo.u), 198 Def!(Foo.v), 199 Def!(Foo.w), 200 Def!(Foo.x), 201 Def!(Foo.y), 202 Def!(Foo.z), */ 203 )(); 204 205 wrap_struct!( 206 S, 207 Docstring!"A sample struct.", 208 Def!(S.write_s, Docstring!"A struct member function."), 209 Member!("i", Docstring!"One sample data member of S."), 210 Member!("s", Docstring!"Another sample data member of S."), 211 )(); 212 213 } 214