sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Sometimes it is not possible to catch sol::error

Open wzhengsen opened this issue 2 years ago • 2 comments

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;
}

wzhengsen avatar Mar 28 '23 10:03 wzhengsen

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

Rochet2 avatar Mar 28 '23 17:03 Rochet2

@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;
}

wzhengsen avatar Mar 29 '23 03:03 wzhengsen