Syntax Error When Loading Lua Script

Im trying to use lua in my application, but when trying to load a script using lua_loadBuffer, I get an error message.

LuaContext := lua_newstate(@Alloc, nil);
  try
    luaL_openlibs(LuaContext);
    s := 'print("hi")';
    lua_register(LuaContext, 'print', @print_func);  
    if luaL_loadbuffer(LuaContext, PChar(s), Length(s), PChar('sample1')) <> 0 then
    begin
      raise Exception.Create(lua_tostring(LuaContext, -1));  //<- I get the following error message here:  [string "sample1"]:1: syntax error
    end;
    if lua_pcall(LuaContext, 0, 0, 0) <> 0 then
      Exception.Create('');
  except
      Debugln('Error: ' + lua_tostring(LuaContext, -1));
  end;

Afaik the lua code is valid, right? Just a "syntax error" isn't very descriptive, and me not having any expierience with the lua, I don't even know where to look for mistakes.

4

1 Answer

Lua does not work with UTF-16 strings.
To ensure your data is encoded with 1-byte codepage, use AnsiString and PAnsiChar instead of String and PChar.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest