ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 493 forks source link

Sometimes it is not possible to catch sol::error #1469

Open wzhengsen opened 1 year ago

wzhengsen commented 1 year ago

My IDE: Microsoft Visual Studio Community 2022 v17.4.5

The code 1:

#define SOL_ALL_SAFETIES_ON 1
#define SOL_SAFE_FUNCTIONS 0
#include <sol/sol.hpp>
#include <iostream>

int main() {
    sol::state lua;
    try {
        sol::function f = lua[""];
        f();
    }
    catch (const sol::error& e) {
        std::cout << e.what() << std::endl;// Yes,I catched it.
    }
    return 0;
}

The code 2:

#define SOL_ALL_SAFETIES_ON 1
#define SOL_SAFE_FUNCTIONS 0
#include <sol/sol.hpp>
#include <iostream>

int main() {
    sol::state lua;
    try {
        sol::table t = lua[""];// An uncatchable error was raised here.
    }
    catch (const sol::error& e) {
        std::cout << e.what() << std::endl;
    }
    return 0;
}
Rochet2 commented 1 year ago

I tried the codes in godbolt and the first code does not throw at all. The second code throws. lua: error: stack index -1, expected table, received nil: value is not a table or a userdata that can behave like one (type check failed in constructor) GCC 10.1, Lua 5.3.5, Sol 3.2.1

So sounds like both codes may work differently in different environments. Or maybe just different sol or lua versions handle the cases differently,

  1. https://godbolt.org/z/6anYEq6h5
  2. https://godbolt.org/z/ad7frnrEY
wzhengsen commented 1 year ago

@Rochet2 I have provided a screen recording to reproduce the bug: screen

This is my code:

#define SOL_ALL_SAFETIES_ON 1
#define SOL_SAFE_FUNCTIONS 0
#include <sol/sol.hpp>
#include <iostream>

int main() {
    std::cout << SOL_VERSION << std::endl;
    std::cout << LUA_VERSION << std::endl;

    sol::state lua;
    try {
        sol::function f = lua[""];
        f();
    }
    catch (const sol::error& e) {
        std::cout << e.what() << std::endl;
    }

    try {
       sol::table t = lua[""];
    }
    catch (const sol::error& e) {
       std::cout << e.what() << std::endl;
    }

    return 0;
}