Create a gist now

Instantly share code, notes, and snippets.

@fjolnir /tlc.lua
Last active Nov 5, 2016

What would you like to do?
LuaJIT ObjC bridge
The bridge is now located at

This is awesome, thanks for sharing it. Some comments:

Switch the top to this (keep ffi in a local and don't depend on a global):
local ffi = require("ffi") -- this will error() if FFI is not available

Make a local table to store your api and return that to the requirer at the end:
local tlc = {}
function tlc.objc_loadClass(aClassName) ... end
return tlc

Also, read the "To Cache or not to cache" at the bottom of this:
You should consider caching ffi.C not the individual functions


fjolnir commented Mar 29, 2012

Thanks for the feedback, you should find the latest version more pleasing :)


fjolnir commented Mar 29, 2012

Added fix for snow leopard.
Updated example.


fjolnir commented Mar 29, 2012

Cleaned up, added more functions to aid in objc introspection

pygy commented Mar 29, 2012

Cool stuff (even though I don't own a Mac right now, so I can't test it).

You may want to add memoization to the __index metamethods.

You can use weak tables to implement it efficiently. See for the details.

Here's the link to that thread with Mike Pall on it:

pygy commented Mar 29, 2012

To address another of Mike Pall's criticism:

  • ... And all those log() calls always evaluate their arguments ...
local _log = false

if objc.debug == true then
    _log = function(...)
                local args = {...}
                for i = 1, #args do args[i] = tostring(args[i]); end
        io.stderr:write("[objc] " .. table.concat(args, ", ") .. "\n")

if _log then _log(some, stuff) end

Regarding the "unspeakable horrific things" in __indexes, I think that most of the logic before the pcall could be placed before the function you return (and chached in weak tables, as pointed above).


fjolnir commented Mar 29, 2012

Pretty major overhaul,
Removed the initial "class loading" in favor of memoizing method implementations on usage. (thanks pygy) Should be a bit more performant now.
This required me to switch from the . syntax to using : for method calls, (so that the GC wouldn't hold onto object references forever).

There should be no log calls evaluated except on the first call to a method anymore, so that's not a major speed concern.

drahosp commented Mar 29, 2012

Much better now. It would be best to move this into a repository so it can be distributed using LuaRocks/LuaDist. I would also welcome automated conversion between NSString <-> String and NSArray/NSDictionary <-> Table for convenience.

pygy commented Mar 29, 2012

You're welcome :-)

Some more suggestions:

I removed all assignments from the fast path, and added a cache for the class names as well (with weak keys).

I also rewrote the column appending code, with the assumption that there is at most one trailing column. More readable and efficient this way, but it doesn't notice when people pass too many parameters (beside the first one). Since this check is only performed the first time a method is called, I supposed that I could drop it.

On the same topic:

I'm not very familiar with ObjC, so I may be wrong, but isn't it possible to have both msg and msg: as distinct messages on the same class? If yes, then the current caching behavior is flawed, and you'd have to make trailing underscores mandatory (if you want to keep the doc simple g), witch is ugly, but you may not have the choice...

This is 100% untested (typed in the browser).

local _empty = {}
local _cacheMT = {__mode = 'k'}
local _classNameCache = setmetatable({}, _cacheMT) 
-- you may want to do the same for the other caches.

ffi.metatype("struct objc_class", {
    __index = function(self,selArg)
        local cached = (
            or _empty --no need to create a new table each time.
        if cached ~= nil then
            return cached

        -- Else
        return function(...)
            local selStr = selArg:gsub("_", ":")

            -- Append missing colons to the selector
            if select('#',...) ~= 0 and selStr:sub(-1, -1) ~= ":" then 
                selStr = selStr .. ":"

            if _log then _log("Calling +["..className.." "..selStr.."]") end

            local method
            local methodDesc = C.class_getClassMethod(self, SEL(selStr))
            if methodDesc ~= nil then
                method = _readMethod(methodDesc)
                method = C.objc_msgSend

            -- Cache the calling block and execute it
            _classNameCache[self] = _classNameCache[self] or ffi.string(C.class_getName(self))
            local className = _classNameCache[self] 
            _classMethodCache[className] = _classMethodCache[className] or {}
            _classMethodCache[className][selArg] = function(self, ...)
                if self == nil then
                    return nil -- Passing nil to self means crashing

                local success, ret = pcall(method, ffi.cast("id", self), SEL(selStr), ...)
                if success == false then

                if ffi.istype("struct objc_object*", ret) and ret ~= nil then
                    if (selStr:sub(1,5) ~= "alloc" and selStr ~= "new")  then
                    if selStr:sub(1,5) ~= "alloc" then
                        ret = ffi.gc(ret, C.CFRelease)
                return ret
            return _classMethodCache[className][selArg](...)

Edit: I also updated my log function suggestion above to make it more robust. table.concat doesn't generate the intermediate strings, which otherwise have to be interned and collected.

Edit2 : These are still globals (and should be defined with ffi.typeof() according to Mike Pall. I'm not familiar with the FFI, but I suppose he's right g)

CGPoint = ffi.metatype("CGPoint", {})
CGSize = ffi.metatype("CGSize", {})
CGRect = ffi.metatype("CGRect", {})
CGAffineTransform = ffi.metatype("CGAffineTransform", {})
NSRange = ffi.metatype("NSRange", {})

fjolnir commented Mar 30, 2012

Fixed cache.
Added option to toggle automatically appending underscores on selectors(enabled by default)
Moved CG types from global to objc namespace
Added __tostring to objects and selectors so you can now print() them directly


fjolnir commented Mar 30, 2012

Added convenience functions to convert lua values to objects. NSArr(), NSDic(), NSNum(), NSStr() and the general Obj() which type checks and calls the correct function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment