• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    C
  • Created over 12 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bridge between Lua and Objective-C which allows you to call a Objective-C method from a Lua script using NSInvocation.

Lua-Objective-C Bridge

Join the chat at https://gitter.im/torus/Lua-Objective-C-Bridge

Synopsis

local ctx = objc.context:create()
local img = ctx:wrap(objc.class.UIImage)("imageNamed:", "spaceship.png")
local ship = ctx:wrap(objc.class.UIImageView)("alloc")("initWithImage:", -img)

The Lua code above is equivalent to following Objective-C code:

UIImage *img = [UIImage imageNamed:@"spaceship.png"];
UIImageView *ship = [[UIImageView alloc] initWithImage:img];

More Example: https://github.com/torus/ios-lua-lander/blob/master/LuaLander/LuaLander/bootstrap.lua

Requirements

  • Lua 5.2.x or Lua 5.3.x
  • iOS 8.0 or later (probably works on older iOS)
  • or macOS 10.13 or later (probably works on older macOS)

Usage

Just add following files to your project:

  • LuaBridge.h
  • LuaBridge.m
  • LuaBridgeInternal.h
  • utils.lua
  • Lua source code (excluding lua.c and luac.c)

Samples/Tests

Please see LuaObjCBridge and LuaObjCBridgeMac projects.

Objective-C API

First, import Lua and the bridge header files:

#import "lua.h"
#import "lualib.h"
#import "lauxlib.h"
#import "LuaBridge.h"

Then,

lua_State *L = [[LuaBridge instance] L];

gets the lua_State object to call Lua functions.

High-Level Lua API

local ctx = objc.context:create()

Creates a new context.

local wrapped_object = ctx:wrap(...)

Returns wrapped Objective-C object. Then, you can send a message to the object like:

wrapped_object("setOpaque:", false)

You need to unwrap the object to pass to a method using - (unary minus) like:

local webview = ctx:wrap(objc.class.UIWebView)("alloc")("initWithFrame:", -rect)

Any Objective-C class object can be obtained via objc.class table. For example

objc.class.UIWebView

Returns the UIWebView class object.

Extending the Bridge

LuaBridge uses a stack to share objects between Lua and Objective-C. And some operators are defined to handle stuff in the stack.

You can add user-defined operators to the LuaBridge class folloing this signature:

- (void)op_your_operator:(NSMutableArray*)stack

Low-Level Lua API

newstack

stack = objc.newstack()

Returns a new stack to pass and receive paramters between Lua and Objective-C.

push

objc.push(stack, arg, ...)

Pushes one or more arguments to the stack.

pop

val = objc.pop(stack)

Pops the last (top) value from the stack.

operate

objc.operate(stack, "operator_name")

Calls given operator with the content of the stack.

getclass

cls = objc.getclass(class_name)

Returns the class object for given class name.

Data Type Conversion

LuaObjective-C
nilNSNull (nil)
stringNSString
number, booleanNSNumber
userdataother NSObject-derived type
lightuserdatavoid *
table, function, threadLuaObjectReference

Pre-defined Operators

call

objc.push(stack, ..., arg2, arg1, target, selector)
objc.operate(stack, "call")
local ret = objc.pop(stack)

"call" operator sends a method specified by selector to the target, then pushes the returned object. Please note that the argments must be pushed in reverse order. This is equivalent to following Objective-C expression:

ret = [target selector arg1 arg2 ...]

Adding Operators

Create a category on the LuaBrige class and implement your operators in folloing signature:

- (void)op_your_operator:(NSMutableArray*)stack

Then from Lua script call the operator like this:

objc.operate(stack, "your_operator")

Note that in Objective-C, the method name should be "op_your_operator:".

License

The MIT License (MIT)

Copyright (c) 2015 Toru Hisai

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author

Toru Hisai [email protected] @torus

More Repositories

1

JavaScript-Objective-C-Bridge

Function call interface between JavaScript on UIWebView and Objective-C.
Objective-C
22
star
2

lua-call-cc

implementing call/cc on Lua script language.
Lua
11
star
3

embedding-lua

C
6
star
4

42-minishell

่ฒๆฎปใฎใ‚ˆใ†ใซ็พŽใ—ใ„ใ€‚
C
5
star
5

ios-lua-lander

Lunar Lander for iOS devices written in Lua.
Lua
4
star
6

opakapaka

A tiny Ajax and comet-based chat system.
Scheme
4
star
7

torus.github.io

My blog with Jekyll
CSS
3
star
8

gauche-violet

Libuv + Gauche
HTML
3
star
9

Java-CookieClicker

Java
2
star
10

XML-LibXML-LazyBuilder

XML::LibXML::LazyBuilder - easy and lazy way to create XML document for XML::LibXML
Perl
2
star
11

Data-Polipo

Perl module to read Polipo cache files.
Perl
2
star
12

webdict

Dictionary lookup CGI using the Sary suffix array utility library.
1
star
13

cont-cgi

Pseudo continuation for CGI.
JavaScript
1
star
14

blogasin

Gauche script to get book data via Amazon Web Service.
Scheme
1
star
15

Synker

Web-based state synchronizer.
JavaScript
1
star
16

raytracer

HTML
1
star
17

alloy-file-sharing

Alloy ใง Dropbox ใฟใŸใ„ใซใƒ•ใ‚กใ‚คใƒซใ‚’ใ‚ทใ‚งใ‚ขใ™ใ‚‹ใƒขใƒ‡ใƒซใ‚’ไฝœใฃใฆใฟใ‚‹ใƒ†ใ‚นใƒˆใ€‚
1
star
18

XML-LibXML-LazyMatcher

A simple XML matcher with lazy evaluation.
Perl
1
star
19

codejam

Scheme
1
star
20

gauche-xml-store

XML utility
Scheme
1
star
21

MEPHISTO

transfered from http://code.google.com/p/code-mephisto/
Scheme
1
star
22

vps-setup

VPS setup
1
star
23

scheme-human-resource-machine

Scheme
1
star
24

Java-Fractal

Java
1
star
25

Luna-Landa-on-CryENGINE2

a very simple physics-based Lunar Lander like game
Lua
1
star
26

map-js

Google maps-like map rendering system.
JavaScript
1
star
27

ipad-app-practice

iPad app practice
C
1
star
28

objc-runtime-book

Objective-C ใƒฉใƒณใ‚ฟใ‚คใƒ  API ใฎ่–„ใ„ๆœฌใ‚’็›ฎๆŒ‡ใ—ใฆๆ›ธใ„ใฆใฟใพใ™ใ€‚
1
star
29

Suffix-Array

Portable suffix array implementation in C++ and Lua
C
1
star