6 using namespace luabind
;
7 LUABIND_ANONYMOUS_FIX
int feedback1
= 0;
8 LUABIND_ANONYMOUS_FIX
int feedback2
= 0;
9 LUABIND_ANONYMOUS_FIX
int feedback3
= 0;
10 LUABIND_ANONYMOUS_FIX
int feedback4
= 0;
11 LUABIND_ANONYMOUS_FIX
int feedback5
= 0;
13 void test_object_param(const object
& table
)
15 object current_object
;
16 current_object
= table
;
18 if (table
.type() == LUA_TTABLE
)
22 feedback2
= object_cast
<int>(table
["oh"]);
25 for (object::array_iterator i
= table
.abegin(); i
!= table
.aend(); ++i
)
27 feedback3
+= object_cast
<int>(*i
);
31 for (object::iterator j
= table
.begin(); j
!= table
.end(); ++j
)
33 feedback4
+= object_cast
<int>(*j
);
37 for (object::raw_iterator j
= table
.raw_begin(); j
!= table
.raw_end(); ++j
)
39 feedback5
+= object_cast
<int>(*j
);
49 if (table
.type() != LUA_TNIL
)
74 void test_match(const luabind::object
& o
)
79 void test_match(int i
)
84 function_ multiret(object a, object b)
93 } // anonymous namespace
97 using namespace luabind
;
99 lua_State
* L
= lua_open();
100 int top
= lua_gettop(L
);
112 std::cout << object_cast<int>(a) << ", " << object_cast<int>(b) << "\n";
114 (a, b, c) = multiret(a, b);
116 std::cout << object_cast<int>(a) << ", " << object_cast<int>(b) << ", " << object_cast<int>(c) << "\n";
121 def("test_object_param", &test_object_param
),
122 def("test_fun", &test_fun
),
123 def("test_match", (void(*)(const luabind::object
&))&test_match
),
124 def("test_match", (void(*)(int))&test_match
),
126 class_
<test_param
>("test_param")
127 .def(constructor
<>())
128 .def_readwrite("obj", &test_param::obj
)
129 .def_readonly("obj2", &test_param::obj2
)
132 dostring(L
, "t = 2");
133 dostring(L
, "test_object_param(t)");
134 if (feedback1
!= 2) return false;
135 if (feedback2
!= 1) return false;
137 dostring(L
, "test_object_param(nil)");
138 if (feedback1
!= 2) return false;
139 if (feedback2
!= 2) return false;
141 dostring(L
, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
142 dostring(L
, "test_object_param(t)");
143 if (feedback1
!= 1) return false;
144 if (feedback2
!= 4) return false;
145 if (feedback3
!= 28) return false;
146 if (feedback4
!= 32) return false;
147 if (feedback5
!= 32) return false;
149 object g
= get_globals(L
);
152 if (t
.type() != LUA_TTABLE
) return false;
154 object blurp
= t
["blurp"];
155 if (object_cast
<int>(blurp
) != 5) return false;
157 object fun
= g
["test_fun"];
159 if (object_cast
<int>(ret
) != 42) return false;
160 if (feedback1
!= 3) return false;
162 dostring(L
, "function test_param_policies(x, y) end");
163 object test_param_policies
= g
["test_param_policies"];
164 int a
= test_param_policies
.type();
165 if (a
!= LUA_TFUNCTION
) return false;
166 // call the function and tell lua to adopt the pointer passed as first argument
167 test_param_policies(5, new test_param())[adopt(_2
)];
169 dostring(L
, "test_match(7)");
170 if (feedback1
!= 27) return false;
171 dostring(L
, "test_match('oo')");
172 if (feedback1
!= 28) return false;
174 dostring(L
, "t = test_param()\n"
176 "if t.obj == 'foo' then test_fun() end\n");
177 if (feedback1
!= 3) return false;
179 dostring(L
, "if t.obj2 == nil then test_fun() end\n");
180 if (feedback1
!= 3) return false;
182 dostring(L
, "function test_object_policies(a) glob = a\nreturn 6\nend");
183 object test_object_policies
= g
["test_object_policies"];
184 object ret_val
= test_object_policies("teststring")[detail::null_type()];
185 if (object_cast
<int>(ret_val
) != 6) return false;
186 if (object_cast
<std::string
>(g
["glob"]) != "teststring") return false;
187 if (object_cast
<std::string
>(g
.at("glob")) != "teststring") return false;
188 if (object_cast
<std::string
>(g
.raw_at("glob")) != "teststring") return false;
191 if (top
!= lua_gettop(L
)) return false;
195 // make sure lua adopted the pointer by checking that it has been deleted
196 if (feedback1
!= 30) return false;