• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    Lua
  • Created over 12 years ago
  • Updated about 10 years ago

Reviews

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

Repository Details

The Tiny Lua Cocoa Bridge

TLC - The Tiny Lua Cocoa Bridge

TLC is a very compact and minimal Objective-C bridge for LuaJIT. Written by Fjõlnir Ásgeirsson

Simple Example

local objc = require("objc")
objc.loadFramework("AppKit")
pool = objc.NSAutoreleasePool:new()
objc.NSSpeechSynthesizer:new():startSpeakingString(objc.NSStr("Hello From Lua!"))
os.execute("sleep "..2)

Mini-Documentation

TLC supports the following:

  • Loading frameworks
  • Accessing Objective-C objects
  • Creating Objective-C classes
  • Calling methods on said objects
  • Creating blocks from Lua functions
  • Converting the basic lua types to objects (Explicitly)
  • Loading BridgeSupport files

TLC Does not yet support the following:

  • Calling methods that take a variable number of arguments
  • Defining variadic blocks, methods or callbacks, or ones that take pass-by-value(non-pointer) structs or unions. (A limitation of LuaJIT FFI)

Loading TLC

local objc = require("objc")

Loading frameworks

objc.loadFramework("Foundation")

-- You can also use BridgeSupport
-- It is slower, but you get access to all C types, functions & constants automatically
local bs = require("objc.BridgeSupport")
bs.loadFramework("Foundation")
-- You can then access constants using bs
myView.center = bs.CGPointZero

Accessing Objective-C objects

local NSString = objc.NSString

Calling Methods

local myStr = NSString:stringWithUTF8String("I am an NSString.")

-- Calling selectors with multiple arguments requires replacing the colons with underscores
-- Except the ones at the end, they are optional.
-- Example selector:with:multiple:parameters: => selector_with_multiple_parameters()
--         selectorWithAnonymousParams:::: => selectorWithAnonymousParams()
local anObject = MyObject:selector_with_multiple_parameters(arg1, arg2, arg3, arg4)

Converting the Basic Lua Types to Objects

-- If you know the type of the variable you want to convert you should use these functions
local string     = NSStr("foobar")
local number     = NSNum(123)
local array      = NSArr({"a","b","c"})
local dictionary = NSDic({ a=1, b=2, c=3 })
-- If not,
-- The Obj() function takes an arbitrary value and determines the correct class to convert it to
local object = Obj(anyVariable)

Subclassing & Extending of Classes

-- This creates a class and registers it with the runtime (it is also accessible with objc.MyClass after creation)
-- The dictionary specifies an instance variable of type int(type encoding: i)
-- To learn about type encodings read https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
local MyClass = objc.createClass(objc.NSObject, "MyClass", { ivar="i" })

-- Creates an init method returning an object(@) and taking as arguments an object(@) and a selector(:)
-- All methods must take self and selector as their first two arguments
objc.addMethod(MyClass, objc.SEL("init"), function(self, sel)
	print("Creating an instance of", self:class())
	objc.setIvar(self, "anIvar", 123)
	return objc.callSuper(self, sel)
end, "@@:")

-- Add a getter for 'ivar'
objc.addMethod(MyClass, objc.SEL("ivar"), function(self, sel)
	return objc.getIvar(self, "ivar")
end, "i@:")
-- Add a setter for 'ivar'
objc.addMethod(MyClass, objc.SEL("setIvar:"), function(self, sel, anIvar)
	objc.setIvar(self, "ivar", anIvar)
end, "v@:i")

-- 'instance' is an object pointer usable on the lua side as well as the objective-c side
local instance = MyClass:alloc():init()
instance:setIvar(123)
print(instance:ivar())

Creating Blocks from Lua Functions

-- To create a block you call createBlock with it's type encoding (Default being void return and no argument)
-- A block returning an integer and taking one object and one double as arguments
local block = objc.createBlock(function(object, double)
	print("I was passed these arguments: ", object, double)
	return 123
end, "i@d")

(Dirty Secret Trick)

-- If you don't want to type 'objc.' before using a class you can set the global namespace to use it as a fallback
setmetatable(_G, {__index=objc})
-- And then you can simply write the class names without the 'objc.' prefix
obj = CoolClass:doThings()

More Repositories

1

xnomad

A tiling window manager for OS X, written in tranquil.
C
483
star
2

DatabaseKit

An Objective-C database abstraction framework.
Objective-C
383
star
3

Tranquil

A language built on top of the Objective-C runtime, and ABI compatible with Objective-C.
Objective-C++
323
star
4

HTTPKit

A minimal, high-performance Objective-C library to write self-sufficient web applications. Built on top of Mongoose.
Objective-C
210
star
5

CoreFoundation-Lite-Linux

CFLite fixed to build correctly on linux (Tested on debian with clang 3.0)
C
38
star
6

Menufela

A little SIMBL hack that enables you to toggle the menubar on/off.
Objective-C
34
star
7

Tranquil-Live

A Lua LiveCoding environment for Mac
Objective-C
16
star
8

CodeRunner-templates

My CodeRunner templates (See readme for list)
Shell
15
star
9

GLMath

Simple math toolkit for use in computer graphics
C
14
star
10

Blackshades

Blackshades fixed to make it build on osx (Blackshades was created by Wolfire games - http://www.wolfire.com)
C++
13
star
11

yaccviso

Yacc grammar visualizer –– This repo is no longer maintained, please use Kaplan's official repo instead.
C
10
star
12

sekwenser

A step sequencer for use with the Korg PadKONTROL
Objective-C
10
star
13

Dynamo

A little OpenGL powered 2d engine written in C (That is very much in progress)
C
8
star
14

Beets

A little iPhone utility that listens to music through the mic and displays its BPM
Objective-C
7
star
15

FABatching

A single header library for adding allocation batching&pooling to Objective-C classes.
C
3
star
16

ShaderToy

A little app that runs fragment shaders in a ShaderToy.com like environment locally
Objective-C
3
star
17

FConvenience

Nice to have macros, for writing less cluttered code
Objective-C
3
star
18

Foundation-Lite

A subset of Foundation for Linux [Not maintained]
Objective-C
3
star
19

CoreFoundation-Lite-OS-X

CoreFoundation fixed to build on retail OS X (See readme for build instructions)
C
2
star
20

Dynamo-Android-Sample

A simple Android sample for Dynamo
C
2
star
21

objc-autoinit

Automatic init methods for Objective-C
C
2
star
22

Dynamo-iOS-Sample

A simple iOS sample for Dynamo
Objective-C
1
star
23

qdnes

Quick&Dirty NES, a toy project I'm using to learn how to write emulators (Not even close to working, and may never)
C++
1
star
24

marklit

A little tool for writing literate C code using markdown
Ruby
1
star
25

FLazyObjects

Classes for lazily allocating objects.
Objective-C
1
star
26

Scroll-availability-indicators

A little hack that adds a shadow on the edges of scroll views if there is content out of view (in Lion)
Objective-C
1
star