satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

Detect a runtime error in a UserdataMetatable function call #65

Closed TimSC closed 7 years ago

TimSC commented 7 years ago

I would like to detect runtime errors when I call a lua function. I tried this, (similar to https://github.com/satoren/kaguya/blob/master/test/test_06_state.cpp ) but no exception is thrown.

luaState.setErrorHandler(kaguya::ErrorHandler::throwDefaultError);
kaguya::LuaFunction way_function = luaState["way_function"];
kaguya::LuaRef ret = way_function(&sharedData.osmObject);

I have a deliberate error in the lua code which I would expect to say variable not defined:

function way_function(way)
    local highway = way:Find("highway")
    local waterway = way:Find("waterway")
    if highway~="" then
        way:Layer("transportation", false)
        way:Attribute("class", highway)
    end
    if waterway~="" then
        way:Layer("waterway", false)
        way:Attribute("class", waterway)
    end
    if building~="" then
        way:Layer("building", true)
    end
end

Any ideas?

TimSC commented 7 years ago

"In Lua programs, typos in variable names can be hard to spot because, in general, Lua will not complain that a variable is undefined." Interesting language! That's news to me. http://lua-users.org/wiki/DetectingUndefinedVariables