Lua错误:"尝试调用一个字符串值"在Android NDK下?

我的 Lua 脚本是由 LOOP 编译的,编译后的脚本模块在桌面机器(OSX 10.7.5)上运行良好。但是在 Android 手机(Android 2.3.6)上运行时,总是失败并抱怨“尝试调用字符串值”。同样的脚本也可以在同一手机上以脚本方式(未编译)无问题运行。

测试中使用了 Lua 5.1.5 和 Android NDK r8b。

logcat 中的错误:

12-26 09:40:26.934: E/libb22luapre(8190): Failed to run script: attempt to call a string value

C 代码片段(为简单起见删除了错误处理代码):

const char script[] = "require \"hello.world\"\n"
            "require \"anothermodule\"\n"
            "hello.world.test2()";
luaL_loadstring(L, script);
lua_pcall(L, 0, LUA_MULTRET, 0);

我将预加载的表格转储了出来,这些所需的模块已经存在(已成功加载)。

我在网上搜索了一下,仍然找不到解决方案。如果有任何建议将不胜感激。

[更新] 我编译了 Android 的 lua 命令,并将编译后的 Lua 脚本嵌入其中。错误与上述相同。

$ adb shell
$ cd /data/local
$ ls
tmp
lua
dump_preload.lua
$ ./lua dump_preload.lua
--- print table --- preload
    test    function: 0x376f0
    anothermodule   function: 0x37718
    hello.world function: 0x376b0
--- print table --- loaded
    string  table: 0x33828
    debug   table: 0x37098
    package table: 0x33d30
    _G  table: 0x32528
    io  table: 0x34e80
    os  table: 0x357b8
    table   table: 0x332c0
    math    table: 0x36530
    coroutine   table: 0x33988
--- print table --- loaders
    1   function: 0x33ed8
    2   function: 0x33ef8
    3   function: 0x33f18
    4   function: 0x33f38
$ ./lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require 'test'
attempt to call a string value
stack traceback:
    [C]: ?
    [C]: in function 'require'
    stdin:1: in main chunk
    [C]: ?
> require 'hello.world'
attempt to call a string value
stack traceback:
    [C]: ?
    [C]: in function 'require'
    stdin:1: in main chunk
    [C]: ?
>
点赞
用户618981
用户618981

问题已解决。原来编译的 Lua 代码不是可移植的。必须针对目标机器编译 Lua 脚本。

步骤简要如下:

  1. 为 Android 编译 Lua 命令
  2. 将 Lua 命令文件和 LOOP 的 Lua 文件以及你的应用程序的 Lua 文件推送到目标手机的文件夹中,例如 /data/local
  3. 打开 adb shell 并编译 Lua 脚本文件
  4. 拉出生成的.h.c文件。
2012-12-28 02:34:22