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

Crash when iterating over lua table that contains integer field #1645

Open
gamagan opened this issue Nov 12, 2024 · 0 comments
Open

Crash when iterating over lua table that contains integer field #1645

gamagan opened this issue Nov 12, 2024 · 0 comments

Comments

@gamagan
Copy link

gamagan commented Nov 12, 2024

I'm getting a crash that i don't understand. I'm using it from MSVC 2022, in C++. Script engine is Luajit 2.1

Problem:
From Lua side, I define a script that looks basically like this:

local this = {}

this.callback_1 = function() ... end
this.callback_2 = function() ... end

return this

On the C++ side, I iterate over the returned table and grab references to the functions in the table:

void Foo::ProcessTable(sol::table table) {
	for (const auto& itr : table) {
		auto nameOpt = itr.first.as<std::optional<std::string>>();
		// Since Lua tables are arrays *and* maps, some entries might not have string keys.
		if (not nameOpt) {
			continue;
		}

                // Ignore whatever is not a function. 
		auto fun = itr.second.as<std::optional<sol::function>>();
		if (not fun) {
			continue;
		}

		auto& name = *nameOpt;

		if (name == "callback_1") {
			this->callback_1 = *fun;
                } else if (name == "callback_2") {
			this->callback_2 = *fun;
                } else {
                    // We don't recognize this callback. Just ignore it.
                }
         }
}

This works well. However, if I add an integer field to the Lua table, from the Lua side, it crashes on the for (....) part, after the int field is processed (which is skipped via if (not fun)).

For example, this makes it crash:

local this = {}

this.callback_1 = function() ... end
this.value = 42
this.callback_2 = function() ... end

return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant