代码之家  ›  专栏  ›  技术社区  ›  beta

Lua和Objective C不运行脚本

  •  0
  • beta  · 技术社区  · 15 年前

    我试图创建一个目标C接口,它封装了存储和运行Lua脚本(编译或不编译)的功能。我的脚本接口代码如下:

    #import <Cocoa/Cocoa.h>
    #import "Types.h"
    #import "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    
    @interface Script : NSObject<NSCoding> {
    @public
      s32 size;
      s8* data;
      BOOL done;
    }
    
    @property s32 size;
    @property s8* data;
    @property BOOL done;
    
    - (id) initWithScript: (u8*)data andSize:(s32)size;
    - (id) initFromFile: (const char*)file;
    - (void) runWithState: (lua_State*)state;
    - (void) encodeWithCoder: (NSCoder*)coder;
    - (id) initWithCoder: (NSCoder*)coder;
    
    @end
    
    #import "Script.h"
    
    @implementation Script
    
    @synthesize size;
    @synthesize data;
    @synthesize done;
    
    - (id) initWithScript: (s8*)d andSize:(s32)s
    {
            self = [super init];
     self->size = s;
     self->data = d;
     return self;
    }
    
    - (id) initFromFile:(const char *)file
    {
            FILE* p;
         p = fopen(file, "rb");
         if(p == NULL) return [super init];
          fseek(p, 0, SEEK_END);
            s32 fs = ftell(p);
         rewind(p);
         u8* buffer = (u8*)malloc(fs);
         fread(buffer, 1, fs, p);
          fclose(p);
    
         return [self initWithScript:buffer andSize:size];
    }
    
     - (void) runWithState: (lua_State*)state
     {
      if(luaL_loadbuffer(state, [self data], [self size], "Script") != 0)
      {
       NSLog(@"Error loading lua chunk.");
       return;
      }
      lua_pcall(state, 0, LUA_MULTRET, 0);
    
    }
    
    - (void) encodeWithCoder: (NSCoder*)coder
    {
      [coder encodeInt: size forKey: @"Script.size"];
     [coder encodeBytes:data length:size forKey:@"Script.data"];
    }
    
    - (id) initWithCoder: (NSCoder*)coder
    {
     self = [super init];
     NSUInteger actualSize;
     size = [coder decodeIntForKey: @"Script.size"];
     data = [[coder decodeBytesForKey:@"Script.data" returnedLength:&actualSize] retain];
     return self;
    }
    
    @end
    

    主要方法如下: #导入“script.h”

    int main(int argc, char* argv[])
    {
     Script* script = [[Script alloc] initFromFile:"./test.lua"];
     lua_State* state = luaL_newstate();
     luaL_openlibs(state);
     luaL_dostring(state, "print(_VERSION)");
     [script runWithState:state];
     luaL_dostring(state, "print(_VERSION)");
     lua_close(state);
    }
    

    Lua脚本只是: 印刷品(欧海世界)

    加载文件是正确的,但我认为它在pcall上是混乱的。

    非常感谢您的帮助。

    航向

    2 回复  |  直到 15 年前
        1
  •  2
  •   Norman Ramsey    15 年前

    您的代码非常复杂,没有明显的错误。但是,您的下一步应该是 检查返回值 lua_pcall . 如果不为零,则在堆栈顶部会出现一条错误消息,您可以通过该消息进行打印

    fprintf(stderr, "Pcall failed: %s\n", lua_tostring(state, -1));
    

    如果您没有收到有用的错误消息,我的下一步将是转储Lua堆栈。上面有多少个元素?每个元素的(lua)类型是什么?价值是什么。功能 lua_top luaL_typename 会有用的。要打印值,必须打开 lua_type . 祝你好运。

        2
  •  0
  •   torus    15 年前

    我没有运行你的代码。但乍一看,我发现initwithscript:的签名在头文件(使用u8*)和源文件(使用s8*)之间是不同的。