Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing message for C++ exceptions in certain lambdas and function objects #965

Closed
firas-assaad opened this issue May 6, 2020 · 3 comments
Assignees
Labels
Bug.Aaah! Pure horror.
Milestone

Comments

@firas-assaad
Copy link

Hi!

This might be is related to my last issue #906

For free C++ lambdas that capture variables and throw exceptions, the exception's what() message isn't returned in the sol error. Instead I might get something from the stack. If the lambda doesn't capture anything then everything works as expected.

This doesn't seem to be happen for lambdas registered on user types, but only when they're registered as global Lua functions. The error message returned when the function is wrapped in sol::yielding is slightly different, but the same issue occurs with or without sol::yielding.

I'm using Visual Studio 2019, latest sol single header (revision 82812c5 generated 2020-03-31 04:18:51.740568 UTC).

#define SOL_USING_CXX_LUA 1
#define SOL_ALL_SAFETIES_ON 1
#define SOL_EXCEPTIONS_ALWAYS_UNSAFE 1
#include <sol.hpp>
#include <iostream>
#include <exception>

int main() {
    try {
        sol::state lua;
        lua.open_libraries(sol::lib::base, sol::lib::coroutine);

        int x = 5;
        lua["f"] = [x](const std::string& text) {
            throw std::exception{"expected"};
        };

        sol::string_view code = R"(f("hi"))";
        auto result = lua.safe_script(code);
        if (!result.valid()) {
            sol::error err = result;
            throw std::runtime_error(err.what());
        }
    } catch (std::exception& e) {
        std::cout << "Exception: " << e.what() << "\n";
    }
}

This is what I get:

[sol3] An error occurred and has been passed to an error handler: sol: CRITICAL_EXCEPTION_FAILURE error: hi
Exception: sol: CRITICAL_EXCEPTION_FAILURE error: hi

This is what I expect:

[sol3] An exception occurred: expected
[sol3] An error occurred and has been passed to an error handler: sol: runtime error: expected
stack traceback:
        [C]: in function 'f'
        [string "f("hi")"]:1: in main chunk
Exception: sol: runtime error: expected
stack traceback:
        [C]: in function 'f'
        [string "f("hi")"]:1: in main chunk

I also noticed the same issue when using function objects:

#define SOL_USING_CXX_LUA 1
#define SOL_ALL_SAFETIES_ON 1
#define SOL_EXCEPTIONS_ALWAYS_UNSAFE 1
#include <sol.hpp>
#include <iostream>
#include <exception>

class functor {
public:
    void operator()(const std::string& message) {
        throw std::exception{"expected"};
    }
};

int main() {
    try {
        sol::state lua;
        lua.open_libraries(sol::lib::base, sol::lib::coroutine);

        functor f;
        lua["f"] = f;

        sol::string_view code = R"(f("hi"))";
        auto result = lua.safe_script(code);
        if (!result.valid()) {
            sol::error err = result;
            throw std::runtime_error(err.what());
        }
    } catch (std::exception& e) {
        std::cout << "Exception: " << e.what() << "\n";
    }
}
@ThePhD
Copy link
Owner

ThePhD commented May 6, 2020

I guess I have to proof the exception code.

For like the 8th time.

Oh booooy....!

@ThePhD ThePhD self-assigned this May 7, 2020
@ThePhD ThePhD added the Bug.Aaah! Pure horror. label May 7, 2020
@ThePhD ThePhD added this to the Bugs milestone May 7, 2020
@firas-assaad
Copy link
Author

firas-assaad commented Nov 8, 2020

Hi, I was investigating a similar error and found that by commenting out the catch(...) in trampoline.hpp I get the expected detailed message. It'll probably break something else, so I'm not sure if this helps in any way.

ThePhD added a commit that referenced this issue May 6, 2021
- Add a flag to allow size_t(-1) and similar shenanigans (SOL_ALL_INTEGER_VALUES_FIT)
- Half-fix, but not fully, for #1183, #1072, #1038, #965
- Fix #1126
- Prepare for #1061
@ThePhD
Copy link
Owner

ThePhD commented Oct 23, 2021

Part of the #1260 fixes now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug.Aaah! Pure horror.
Projects
None yet
Development

No branches or pull requests

2 participants