Luabridge binding overloaded operators

心不动则不痛 提交于 2019-12-10 22:14:49

问题


I've written a simple vec3 class, which implements the */+- operators:

class vec3
{
public:
    vec3():
    x(0.0f),y(0.0f),z(0.0f)
    {}
    vec3(float ix, float iy, float iz):
    x(ix),y(iy),z(iz)
    {}
    vec3 operator+(const vec3& v){
        vec3 vec(x+v.x,y+v.y,z+v.z);
        return vec;
    }
    vec3 operator-(const vec3& v){
        vec3 vec(x-v.x,y-v.y,z-v.z);
        return vec;
    }
    vec3 operator*(const float& s){
        vec3 vec(x*s,y*s,z*s);
        return vec;
    }
    vec3 operator/(const float& d){
        flot div = 1.0f/d;
        vec3 vec(x*div,y*div,z*div);
        return vec;
    }
    float x, y, z;
};

I wish to bind these operators via luabridge, in some way similar to:

getGlobalNamespace(L)
    .beginClass<vec3>("vec3");
        .addConstructor<void (*) (const float&, const float& const float&)>()
        .addData ("x", &vec3::x)
        .addData ("z", &vec3::z)
        .addData ("z", &vec3::y)
        .addFunction("__add",(vec3*(vec3::*)(vec3)) &vec3::operator+);
        .addFunction("__sub",(vec3*(vec3::*)(vec3)) &vec3::operator-);
        .addFunction("__mul",(vec3*(vec3::*)(float)) &vec3::operator*);
        .addFunction("__div",(vec3*(vec3::*)(float)) &vec3::operator/);
    .endClass();

So that I can then call the functions in lua like so:

onUpdate = function (e, dt)
    e.position = e.position + e.velocity * dt;
end

When I place a breakpoint in the *operator for the vec3 class, it does hit it, but the rhs float is undefined.

If I change the lua code to:

    onUpdate = function (e, dt)
        e.position = e.position + e.velocity
    end

Then the rhs vec3 is also undefined. So it looks like the argument is not being passed correctly.

I then changed the registration to:

getGlobalNamespace(L)
    .beginClass<vec3>("vec3");
        .addConstructor<void (*) (const float&, const float& const float&)>()
        .addData ("x", &vec3::x)
        .addData ("z", &vec3::z)
        .addData ("z", &vec3::y)
        .addFunction("__add",(vec3*(vec3::*)(const vec3&)) &vec3::operator+);
        .addFunction("__sub",(vec3*(vec3::*)(const vec3&)) &vec3::operator-);
        .addFunction("__mul",(vec3*(vec3::*)(const float&)) &vec3::operator*);
        .addFunction("__div",(vec3*(vec3::*)(const float&)) &vec3::operator/);
    .endClass();

But now the member data of the vec3, or the float argument is undefined. If I move past this breakpoint, luabdridge throws the following exception:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

The only thing I can think of is that I have somehow not registered the function correctly.

So my question is this: How do I bind the operator correctly, and ensure that the argument type is correct, and how do I correctly call it in lua?


回答1:


So as I mentioned in my question, I suspected that the registration was not being done correctly. I still don;t fully understand the specifics, but I suspect that the function pointer cast is somehow changing the calling convention, so I changed the code to:

.addFunction("__mul",&vec3::operator*);

And it worked perfectly.



来源:https://stackoverflow.com/questions/49490076/luabridge-binding-overloaded-operators

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!