13 #include <luabind/luabind.hpp>
14 #include <luabind/scope.hpp>
16 bool dostring(lua_State
* L
, const char* str
)
18 if (luaL_loadbuffer(L
, str
, std::strlen(str
), str
) || lua_pcall(L
, 0, 0, 0))
20 const char* a
= lua_tostring(L
, -1);
21 std::cout
<< a
<< "\n";
28 int dostring2(lua_State
* L
, const char* str
)
30 if (luaL_loadbuffer(L
, str
, std::strlen(str
), str
) || lua_pcall(L
, 0, 0, 0))
37 bool report_success(bool result
, const char* name
)
41 if (result
) std::cout
<< ": passed\n";
42 else std::cout
<< ": failed\n";
44 if (result
) std::cout
<< ": \033[32mpassed\033[0m\n";
45 else std::cout
<< ": \033[31mfailed\033[0m\n";
52 #include <luabind/runtime_converters.hpp>
56 using namespace luabind::rt_converter
;
61 { std::cout
<< "constructed\n"; }
63 my_type(const my_type
&)
64 { std::cout
<< "copied\n"; }
67 { std::cout
<< "destructed\n"; }
70 void int_from_lua(lua_State
* L
, int index
, rvalue_data
* data
)
72 data
->result
= new (data
->data
) int(lua_tonumber(L
, index
));
75 void float_from_lua(lua_State
* L
, int index
, rvalue_data
* data
)
77 data
->result
= new (data
->data
) float(lua_tonumber(L
, index
));
80 void string_from_lua(lua_State
* L
, int index
, rvalue_data
* data
)
82 data
->result
= new (data
->data
) std::string(lua_tostring(L
, index
));
85 void my_type_from_lua(lua_State
* L
, int index
, rvalue_data
* data
)
87 data
->result
= new (data
->data
) my_type
;
90 void f(int a
, float b
, const std::vector
<float>& s
)
95 template<class Source
, class Target
>
96 struct implicit_conversion
98 typedef typename
boost::add_reference
<
99 typename
boost::add_const
<Target
>::type
102 typedef typename
boost::add_reference
<Source
>::type source_t
;
104 implicit_conversion()
106 insert_converter(&convert
, typeid(target_t
));
109 static void convert(lua_State
* L
, int index
, rvalue_data
* data
)
111 typename
boost::python::detail::referent_storage
<source_t
>::type storage
;
114 tmp
.data
= storage
.bytes
;
116 rvalue_convert(L
, index
, &tmp
, registered
<Source
>::converters
);
118 new (data
->data
) Target(void_ptr_to_reference(tmp
.result
, (source_t(*)())0));
119 data
->result
= data
->data
;
121 if (tmp
.result
== tmp
.data
)
122 boost::python::detail::destroy_referent(tmp
.result
, (source_t(*)())0);
127 { int n
; virtual void f() {} };
130 { float e
; virtual void f() {} };
134 lua_State
* L
= lua_open();
136 using namespace luabind
;
139 insert_converter(&int_from_lua
, typeid(const int&));
140 insert_converter(&float_from_lua
, typeid(const float&));
141 // insert_converter(&string_from_lua, typeid(const std::string&));
142 insert_converter(&my_type_from_lua
, typeid(const my_type
&));
144 implicit_conversion
<int, std::vector
<float> >();
148 def("f", &f
, runtime(_1
) + runtime(_2
) + runtime(_3
))
162 passed
&= report_success(test_construction(), "construction");
163 passed
&= report_success(test_attributes(), "attributes");
164 passed
&= report_success(test_operators(), "operators");
165 passed
&= report_success(test_implicit_cast(), "implicit cast");
166 passed
&= report_success(test_const(), "const");
167 #ifndef LUABIND_NO_EXCEPTIONS
168 passed
&= report_success(test_exceptions(), "exceptions");
170 std::cout
<< "exceptions: skipped \n";
172 passed
&= report_success(test_null_pointer(), "null pointer");
173 passed
&= report_success(test_policies(), "policies");
174 passed
&= report_success(test_lua_classes(), "lua classes");
175 passed
&= report_success(test_free_functions(), "free functions");
176 passed
&= report_success(test_object(), "object");
177 passed
&= report_success(test_held_type(), "held type");
178 passed
&= report_success(test_iterator(), "iterator");
179 passed
&= report_success(test_scope(), "scopes");
180 passed
&= report_success(test_yield(), "yield");
182 if (passed
) std::cout
<< "\n\nall tests passed\n";
183 else std::cout
<< "\n\nsome tests failed\n";