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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.

You Might Also Like