peacalm / cpp-luaw

Lua Wrapper. Lua Binder. Interaction helper between C++ and Lua. Lua as config file for C++. Config parser. Dynamic expression evaluator.
MIT License
2 stars 1 forks source link

crash #29

Closed warsark closed 1 month ago

warsark commented 2 months ago

` void ceLog(std::string s) { printf(s.c_str()); } struct Obj { int i = 1; const int ci = 1;

Obj() 
{
    ceLog("ctor");
}
Obj(int v, int cv = 1) : i(v), ci(cv) {}

int abs() const { return std::abs(i); }

int plus() {
    return i; 
}
std::tuple<int, int, int> plus2(int d) {        
    return std::make_tuple(1,2,3);
}

};

int testLuaw() { peacalm::luaw l;

    l.register_ctor<Obj()>("NewObj");           // default constructor
    //l.register_ctor<Obj(int)>("NewObj1");       // constructor with 1 argument
    //l.register_ctor<Obj(int, int)>("NewObj2");  // constructor with 2 argument

    //// the constructors will generate a const instance of Obj in Lua
    //using ConstObj = const Obj;
    //l.register_ctor<ConstObj()>("NewConstObj");  // default constructor
    //l.register_ctor<ConstObj(int)>(
    //  "NewConstObj1");  // constructor with 1 argument
    //l.register_ctor<ConstObj(int, int)>(
    //  "NewConstObj2");  // constructor with 2 argument

    l.register_member("i", &Obj::i);
    l.register_member("ci", &Obj::ci);  // const member
    l.register_member("abs", &Obj::abs);

    // Register overloaded member functions.
    l.register_member("plus", &Obj::plus);
    l.register_member("plusby", &Obj::plus2);

    //my code
    l.set("ceLog", ceLog);
    Obj o;
    o.i = 100;
    int retcode =
        l.dostring("function test(oi) if (oi.i==100)then ceLog('ok'); end local a=oi.plus(); local x,y,z =oi.plusby(12); ceLog(string.format('result is:%d-%d',a,z)); local retMap={}; retMap[2] = 3; return retMap; end");
    std::map<uint64_t, uint64_t> mapRet = l.callf<std::map<uint64_t, uint64_t>>("test", &o);
    if (retcode != LUA_OK) {
        l.log_error_out();
        return 1;
    }

    return 0;

} `

peacalm commented 2 months ago

In Lua, to call a member function you should use ":", not "."

Try to change: local a=oi.plus() -> local a=oi:plus() local x,y,z =oi.plusby(12); -> local x,y,z =oi:plusby(12);

peacalm commented 2 months ago

Hi! I have updated code to handle this wrong usage, now it won't abort but give an error info!

see PR

Thanks for report!