Tuesday 15 March 2011

c++ - Assign value to Chaiscript variable through user-type object gives unexpect behaviour -



c++ - Assign value to Chaiscript variable through user-type object gives unexpect behaviour -

i've got peculiar problem c++ , chaiscript, hope can help me it, , i'll seek give much info needed.

basically, calling c++ function defined in c++ through chaiscript, returns vector2 object (user-type object), homecoming crazy values when seek value straight fellow member variable of vector2 (x or y). if assign whole vector2 variable, work expected, , accessing fellow member variables give me expected value.

i managed reproduce problem minimum code could, getting rid of physics engine , else using.

here c++ code:

//test.cpp #include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript_stdlib.hpp> template<typename t> struct vector2 { vector2() : x(0), y(0) {}; vector2(t px, t py) : x(px), y(py) {}; vector2(const vector2& cp) : x(cp.x), y(cp.y) {}; vector2& operator+=(const vector2& vec_r) { x += vec_r.x; y += vec_r.y; homecoming *this; } vector2 operator+(const vector2& vec_r) { homecoming vector2(*this += vec_r); } void operator=(const vector2& ver_r) { x = ver_r.x; y = ver_r.y; } t x; t y; }; vector2<float> getvalue() { homecoming vector2<float>(10,15); } int main() { chaiscript::chaiscript _script(chaiscript::std_lib::library()); //registering stuff _script.add(chaiscript::user_type<vector2<float>>(), "vector2f"); _script.add(chaiscript::constructor<vector2<float> ()>(), "vector2f"); _script.add(chaiscript::constructor<vector2<float> (float, float)>(), "vector2f"); _script.add(chaiscript::constructor<vector2<float> (const vector2<float>&)>(), "vector2f"); _script.add(chaiscript::fun(&vector2<float>::x), "x"); _script.add(chaiscript::fun(&vector2<float>::y), "y"); _script.add(chaiscript::fun(&vector2<float>::operator +), "+"); _script.add(chaiscript::fun(&vector2<float>::operator +=), "+="); _script.add(chaiscript::fun(&vector2<float>::operator =), "="); _script.add(chaiscript::fun(&getvalue), "getvalue"); _script.eval_file("test.chai"); homecoming 0; }

and here script:

//test.chai var test = 0.0 var test2 = vector2f(10,10) test = getvalue().x print(test) print(test2.x)

this code reproduce following: 6.52556e-38 10

this value getvalue().x (6.52556e-38) different each time run code, crazy value between 1.f~8.f goes exponential -36~-38.

i can around problem using "var = getvalue()", , accessing "something.x", helpful understand why happens , how fixed.

this bug in chaiscript has been fixed on develop branch , in next release.

https://github.com/chaiscript/chaiscript/commit/4f972bcf67826591423149c006f80d43a026bd15

c++ chaiscript

No comments:

Post a Comment