加载lua脚本时出现语法错误
我试图在我的应用程序中使用lua,但当尝试使用lua_loadBuffer加载脚本时,我收到一条错误消息加载lua脚本时出现语法错误,lua,pascal,freepascal,Lua,Pascal,Freepascal,我试图在我的应用程序中使用lua,但当尝试使用lua_loadBuffer加载脚本时,我收到一条错误消息 LuaContext := lua_newstate(@Alloc, nil); try luaL_openlibs(LuaContext); s := 'print("hi")'; lua_register(LuaContext, 'print', @print_func); if luaL_loadbuffer(LuaContext, PChar(
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;
LuaContext:=lua_newstate(@Alloc,nil);
尝试
luaL_openlibs(LuaContext);
s:=“打印”(“hi”);
lua_寄存器(LuaContext,'print',@print_func);
如果luaL_loadbuffer(LuaContext,PChar,Length,PChar('sample1'))为0,则
开始
引发异常。创建(lua_tostring(LuaContext,-1))// Lua不适用于UTF-16字符串。
为确保数据采用1字节代码页编码,请使用AnsiString
和PAnsiChar
而不是字符串和PChar。尝试var s:AnsiString代码>@EgorSkriptunoff谢谢,这很有效。你可能想发布一个答案,这样我就可以接受了;答案通常有长度限制;)。请随意发布并接受您自己的答案。@PaulKulchenko-有时我打破了这个传统:-)Freepascal默认模式pchar=pansichar和string=ansistring@MarcovandeVoort-是,默认情况下。