Skip to content

Lua Script validation #1744

@Iurysab

Description

@Iurysab

Hi! First of all, thank you for the sol2 library. It is incredibly useful and robust.

I have created several "Lua classes" using new_usertype. These classes have specific functions and attributes. My goal is to validate a user's Lua script to ensure it is correct regarding both syntax (Lua grammar) and semantics (checking if the called methods/attributes actually exist in my C++ bindings).

When I use lua.load_file (or lua.load), it only validates the Lua syntax. If the script contains a call to a non-existent method (e.g., a typo like myObj:mve() instead of myObj:move()), load considers it valid. The error only pops up at runtime when I actually execute the script.

Is there a recommended pattern or feature in sol2 to perform a "Dry Run" or a "Safety Check" that detects these binding errors (like "attempt to call method 'x' a nil value") without executing the script?

I am currently implementing a "Dry Run" flag in my C++ classes to bypass logic during validation, but I was wondering if sol2 has a more native or elegant solution for introspecting the script against the registered usertypes.

Example:

// Setup
lua.new_usertype<Robot>("Robot", "move", &Robot::move);

// Script with a typo ("mve" instead of "move")
std::string bad_script = "local r = Robot.new(); r:mve(10);";

// This passes (returns valid), which is expected for Lua
auto result = lua.load(bad_script); 

// I want to catch the error here, without running "real" game logic
if (result.valid()) {
    // How to validate that 'mve' does not exist safely?
}

Thanks in advance for any help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions