Skip to content

Seemingly incorrect error message when calling Luau function & casting to int #268

Description

@phonaputer

I noticed that when calling a Luau function that returns a number via luabridge::getGlobal(..).call(..), if the C++ code requests the result to be cast to int (i.e. call<int>(..)) the result contains an error with the message "The native floating point can't fit inside a lua number."

Interestingly, when calling value() the result also seems to contain the correct result in addition to the error.

Let me note that this behavior does not occur when changing to call<lua_Number>(..) or call<double>(..).

Not supporting int seems reasonable if lua_Number is double, but the error message could be more helpful in indicating that the problem is related to casting the return type.

Minimal reproducible example

Running the following source outputs...

'add(2, 3)' result: 5
'add(2, 3)' error: The native floating point can't fit inside a lua number

Source...

#include <iostream>
#include <luacode.h>
#include <lualib.h>
#include <string>

#include <LuaBridge/LuaBridge.h>

const std::string LUAU_FUNCTION = R"(function add(a: number, b: number): number
  return a + b
end)";

int main() {
  auto *L = luaL_newstate();

  size_t bytecode_size = 0;
  auto bytecode = luau_compile(LUAU_FUNCTION.c_str(), LUAU_FUNCTION.length(), nullptr, &bytecode_size);

  auto result = luau_load(L, "add", bytecode, bytecode_size, 0);
  free(bytecode);
  if (result != LUA_OK) {
    std::cerr << "Failed to load Luau script: " << lua_tostring(L, -1) << "\n";
    lua_close(L);
    return 1;
  }

  result = lua_pcall(L, 0, 0, 0);
  if (result != LUA_OK) {
    std::cerr << "Failed to execute Luau script: " << lua_tostring(L, -1) << "\n";
    lua_close(L);
    return 1;
  }

  auto call_result = luabridge::getGlobal(L, "add").call<int>(2, 3);

  std::cout << "'add(2, 3)' result: " << call_result.value() << "\n";
  if (call_result.error()) {
    std::cout << "'add(2, 3)' error: " << call_result.error_cstr() << "\n";
  }

  lua_close(L);

  return 0;
}

Luau, LuaBridge3, and compiler versions

Luau version: 7.13
LuaBridge3 version: 3.0-rc12
Compiler: clang++

> clang++ --version
Ubuntu clang version 20.1.8 (++20250804090239+87f0227cb601-1~exp1~20250804210352.139)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-20/bin

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions