Skip to content

Commit

Permalink
Merge pull request #4 from mogenson/type-annots
Browse files Browse the repository at this point in the history
Finish adding type annotations
  • Loading branch information
mogenson committed Feb 5, 2024
2 parents 0ecab89 + ce96b57 commit bfd4324
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
23 changes: 16 additions & 7 deletions LuaRepl.app/Contents/Resources/objc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ local type_encoding = setmetatable({
})

---convert a NULL pointer to nil
---@param p cdata
---@param p cdata pointer
---@return cdata | nil
local function ptr(p)
if p == nil then return nil else return p end
end

---return a Class from name or object
---@param name string | Class
---@param name string | Class | id
---@return Class
local function cls(name)
assert(name)
Expand Down Expand Up @@ -121,9 +121,9 @@ local function sel(name, num_args)
end

---call a method for a SEL on a Class or object
---@param receiver string | Class | id
---@param selector string | SEL
---@param ...? any
---@param receiver string | Class | id the class or object
---@param selector string | SEL name of method
---@param ...? any additional method parameters
---@return any
local function msgSend(receiver, selector, ...)
---return Method for Class or object and SEL
Expand Down Expand Up @@ -192,12 +192,16 @@ local function msgSend(receiver, selector, ...)
end

---load a Framework
---@param framework string
---@param framework string framework name without the '.framework' extension
local function loadFramework(framework)
-- on newer versions of MacOS this is a broken symbolic link, but dlopen() still succeeds
ffi.load(string.format("/System/Library/Frameworks/%s.framework/%s", framework, framework), true)
end

---create a new custom class from an optional base class
---@param name string name of new class
---@param super_class? string | Class parent class, or NSObject if omitted
---@return Class
local function newClass(name, super_class)
assert(name and type(name) == "string")
local super_class = cls(super_class or "NSObject")
Expand All @@ -206,9 +210,14 @@ local function newClass(name, super_class)
return class
end

---add a method to a custom class
---@param class string | Class class created with newClass()
---@param selector string | SEL name of method
---@types string Objective-C type encoded method arguments and return type
---@func function lua callback function for method implementation
local function addMethod(class, selector, types, func)
assert(type(func) == "function")
assert(type(types) == "string") -- and #types - 1 == debug.getinfo(func).nparams)
assert(type(types) == "string")

local class = cls(class)
local selector = sel(selector)
Expand Down
6 changes: 5 additions & 1 deletion LuaRepl.app/Contents/Resources/repl.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local repl = { buffer = "" }

function repl:eval(text) --> result, error
---evaluate a chunk of lua code
---@param text string code chunk
---@return string | nil result returned values of chunk in comma separated string
---@return string | nil error message if eval failed
function repl:eval(text)
local chunk = self.buffer .. text

local func, err = loadstring(chunk)
Expand Down

0 comments on commit bfd4324

Please sign in to comment.