Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 802875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:40:11+00:00 2026-05-14T23:40:11+00:00

I am trying to create an objective c interface that encapsulates the functionality of

  • 0

I am trying to create an objective c interface that encapsulates the functionality of storing and running a lua script (compiled or not.) My code for the script interface is as follows:

#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

Here is the main method:
#import “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);
}

And the lua script is just:
print(“O Hai World!”)

Loading the file is correct, but I think it messes up at pcall.

Any Help is greatly appreciated.

Heading

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:40:12+00:00Added an answer on May 14, 2026 at 11:40 pm

    Your code is so complex that there are no obvious faults. However, your next step should be to check the return value from lua_pcall. If it is nonzero, there will be an error message on the top of the stack which you can print via

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

    If you don’t get a useful error message, my next step would be to dump the Lua stack. How many elements are on it? What is the (Lua) type of each element? What is the value. Functions lua_top, and luaL_typename will be useful. To print the value you will have to switch on the results of lua_type. Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer writerow's argument is a sequence... and a string, which is… May 16, 2026 at 5:27 pm
  • Editorial Team
    Editorial Team added an answer I wouldn't put those fields (which I generally call audit… May 16, 2026 at 5:27 pm
  • Editorial Team
    Editorial Team added an answer The recursion stops when it x is less than 1… May 16, 2026 at 5:27 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is my first time working with Objective-C, and I keep trying to create
Trying to create an app that does some socket communication (Writing only). I can
I am currently trying to create a menu system for a game and cannot
Hi I am trying to create a table-view programatically using a cocoa lisp bridge
Is Apple declaring method [CIImage initWithImage:(CIImage*)] that I'm not aware of? The only method
I'm new to Objective-c. For learning purposes I'm trying to build something like a
As a disclaimer I'd like to state that I'm fairly new to Objective-C and
Ok, so I have the code below (Objective-C FYI) and I was wondering if
I am trying to learn Objective-C and have run into a problem with pushing
I'm currently stuck trying to get my objective c generated files to compile in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.