Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
91cfb67
Resolves a compiler warning about accessing "self" within a block
tempelmann Jan 9, 2022
1f1008b
Adds LuaContext.parseResult property for retrieving a single return v…
tempelmann Jan 9, 2022
105cd4d
Fixes issue #9 by removing the "#if DEBUG" and "#endif" lines
tempelmann Jan 9, 2022
115e8a5
Fixes issue #2 by storing all added object references in an NSArray
tempelmann Jan 9, 2022
dceb6f6
Fixes issue #7, allowing blocks to be assiged as global functions
tempelmann Jan 9, 2022
2bc79cd
Adds unit tests for blocks and leaks. The Leaks test still fails due …
tempelmann Jan 9, 2022
591a0dc
Updates lua source files from 5.2.2 to v5.4.3
tempelmann Jan 9, 2022
821d528
Fixes issue #8 (caused leak when `luaWrapperNewIndex` or `setProperty…
tempelmann Jan 10, 2022
049de50
Fixes the examples to work with the current version, including the ne…
tempelmann Jan 10, 2022
20937fe
Adds link to Apple's JSC docs
tempelmann Jan 10, 2022
cb4e16d
Fixes a bug introduced by commit 1f1008b2 when refactoring the `parse…
tempelmann Jan 10, 2022
8aa2af2
Fixes retrieval of boolean values from Lua (we must use lua_toboolean…
tempelmann Jan 10, 2022
d5d6042
Updates fix for issue #8.
tempelmann Jan 10, 2022
6ac48fc
Comments out an NSLog I had activated for testing and forgot to undo.
tempelmann Jan 11, 2022
ffcc0b8
Fixes #11, along with test case
tempelmann Feb 1, 2022
f50e0ce
Adds a safety check because clang's analyzer complains otherwise for …
tempelmann Feb 2, 2022
a6298ca
More small syntax changes to make the clang analyzer happy
tempelmann Feb 2, 2022
f823a2c
When passing an NSDate to Lua, it now gets converted into an epoch va…
tempelmann Feb 16, 2022
6a21a96
Fixes the issue where "BOOL" (or "bool") type returns by an ObjC func…
tempelmann Feb 16, 2022
f01e558
Minor updates of project files for Xcode 10 (I think)
tempelmann Jan 12, 2024
910ba62
Making Xcode 15.4 happy
bdpedersen Aug 3, 2024
2522a85
Updating to LUA 5.4.7
bdpedersen Aug 3, 2024
e1f9f4b
[wip] first version of anonymous function support
bdpedersen Aug 3, 2024
c6f1e2c
Support for TFUNCTION as anonymous callable objects
bdpedersen Aug 3, 2024
3a4ce32
No need for the AnonCall category
bdpedersen Aug 3, 2024
5a40038
Adds support for BOOL type
tempelmann Apr 7, 2025
530b769
Merge pull request #1 from bdpedersen/Enhancements
tempelmann May 26, 2025
e63aa23
Reverts some changes from "Making Xcode 15.4 happy" (commit 910ba626e…
tempelmann May 26, 2025
ac1365b
Fixes some indentations (no code affected)
tempelmann May 26, 2025
ef62fd5
Uses a macro definition "LUA_EXPORT_TYPE" in place of the protocol na…
tempelmann May 26, 2025
9392ea8
Fixes a small issue inside `toObjC()`: The index value for a table (a…
tempelmann May 26, 2025
ba3625f
Fixes LuaCallable, not crashing any more when LuaCallable is dealloca…
tempelmann May 26, 2025
c902f6b
Upates some settings from Xcode 16.3.
tempelmann May 26, 2025
0985af4
Fixes a doc warning
tempelmann May 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 86 additions & 18 deletions LuaTests.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

31 changes: 15 additions & 16 deletions LuaTests.xcodeproj/xcshareddata/xcschemes/LuaTests.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "1630"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,10 +23,19 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E47E87251B04845B00108EAE"
BuildableName = "LuaTests.xctest"
BlueprintName = "LuaTests"
ReferencedContainer = "container:LuaTests.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -39,24 +48,16 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E47E87251B04845B00108EAE"
BuildableName = "LuaTests.xctest"
BlueprintName = "LuaTests"
ReferencedContainer = "container:LuaTests.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -67,14 +68,12 @@
ReferencedContainer = "container:LuaTests.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
13 changes: 6 additions & 7 deletions LuaTests.xcodeproj/xcshareddata/xcschemes/obj2lua-ios.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "1630"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,21 +23,22 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -48,14 +49,12 @@
ReferencedContainer = "container:LuaTests.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
36 changes: 22 additions & 14 deletions LuaTests.xcodeproj/xcshareddata/xcschemes/objc2lua.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "1630"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,10 +23,19 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D4A9E61718A95F6F002A9EA5"
BuildableName = "libobjc2lua.a"
BlueprintName = "objc2lua"
ReferencedContainer = "container:LuaTests.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -39,24 +48,18 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D4A9E61718A95F6F002A9EA5"
BuildableName = "libobjc2lua.a"
BlueprintName = "objc2lua"
ReferencedContainer = "container:LuaTests.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableAddressSanitizer = "YES"
enableUBSanitizer = "YES"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -68,13 +71,18 @@
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
59 changes: 38 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
ObjC-Lua
========

A simple Objective-C &lt;-> Lua bridge modeled after iOS 7's JavaScriptCore.
A simple Objective-C &lt;-> Lua bridge modeled after iOS 7's and macOS'
[JavaScriptCore](https://developer.apple.com/documentation/javascriptcore).

A trivial use might look like this:

```objective-c
static NSString *const myScript =
LUA_STRING(
globalVar = { 0.0, 1.0 }

function myFunction(parameter)
return parameter >= globalVar[1] and parameter <= globalVar[2]
end
globalVar = { 0.0, 1.0 }

function myFunction(parameter)
return parameter >= globalVar[1] and parameter <= globalVar[2]
end

return doubleThisValue(2) * 3
);

- (void)doLua {
LuaContext *ctx = [LuaContext new];

// install the global function `doubleThisValue` as a block
ctx[@"doubleThisValue"] = ^(NSNumber *v) {
// we receive and return NSNumber objects, not plain ints!
return @(v.integerValue * 2);
};

NSError *error = nil;
if( ! [ctx parse:myScript error:&error] ) {
NSLog(@"Error parsing lua script: %@", error);
return;
}

NSLog(@"the script returned: %@ (should be 12)", ctx.parseResult);

NSLog(@"globalVar is: %@", ctx[@"globalVar"]); // should print "globalVar is: [ 0.0, 1.0 ]"
NSLog(@"globalVar is: %@ (should be “(0,1)”)", ctx[@"globalVar"]);

id result = [ctx call:@"myFunction" args:@[ @0.5 ] error:&error];
id result = [ctx call:"myFunction" with:@[ @0.5 ] error:&error];
if( error ) {
NSLog(@"Error calling myFunction: %@", error);
return;
}
NSLog(@"myFunction returned: %@", result); // should print "myFunction returned: '1'"
NSLog(@"myFunction returned: %@ (should be 1)", result);

ctx[@"globalVar"] = @[ 0.2, 0.4 ];
ctx[@"globalVar"] = @[ @0.2, @0.4 ];
NSLog(@"globalVar is: %@ (should be “(0.2,0.4)”)", ctx[@"globalVar"]);

result = [ctx call:@"myFunction" args:@[ @0.5 ] error:&error];
result = [ctx call:"myFunction" with:@[ @0.5 ] error:&error];
if( error ) {
NSLog(@"Error calling myFunction: %@", error);
return;
}
NSLog(@"myFunction returned: %@", result); // should print "myFunction returned: '0'"
NSLog(@"myFunction returned: %@ (should be 0)", result);
}
```

A more complex use might be:
Here's a more complex use that involves an entire
class exposed to Lua (via the `LuaExport` protocol,
similar to [JSExport](https://developer.apple.com/documentation/javascriptcore/jsexport)).

```objective-c
static NSString *const myScript =
LUA_STRING(
function moveView(view)
local center = view.center
center.y = center.y + 5
center.x = center.x + 5
view.center = center
end
function moveView(view)
local center = view.center
center.y = center.y + 5
center.x = center.x + 5
view.center = center
end
);

@protcol UIViewLuaExports <LuaExport>
@protocol UIViewLuaExports <LuaExport>

@property(nonatomic) CGFloat alpha;
@property(nonatomic) CGRect bounds;
Expand All @@ -78,7 +95,7 @@ LUA_STRING(
return;
}

[ctx call:@"moveView" args:@[ onView ] error:&error];
[ctx call:"moveView" with:@[ onView ] error:&error];
if( error ) {
NSLog(@"Error calling myFunction: %@", error);
return;
Expand Down
28 changes: 28 additions & 0 deletions classes/LuaCallable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// LuaCallable.h
// LuaTests
//
// Created by Brian Pedersen on 03/08/2024.
// Copyright © 2024 Sean Meiners. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "lua.h"
#import "lauxlib.h"
#import "lualib.h"

NS_ASSUME_NONNULL_BEGIN

@class LuaContext;

@interface LuaCallable : NSObject

@property (readonly) int handle;
@property (readonly,assign) lua_State *L;

- (id)initWithState:(lua_State *)L index:(int)index;
- (void)destroy;

@end

NS_ASSUME_NONNULL_END
46 changes: 46 additions & 0 deletions classes/LuaCallable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// LuaCallable.m
// LuaTests
//
// Created by Brian Pedersen on 03/08/2024.
// Copyright © 2024 Sean Meiners. All rights reserved.
//

#import "LuaCallable.h"
#import "LuaContext.h"

@implementation LuaCallable

- (id)initWithState:(lua_State *)L index:(int)index {
lua_pushvalue(L, index);
lua_Integer ref = luaL_ref(L, LUA_REGISTRYINDEX);
if (self = [super init]) {
_handle = (int) ref;
_L = L;
} else {
_L = NULL;
}
return self;
}

- (void)dealloc {
[self destroy];
}

- (void)destroy {
if (_L) {
lua_State *L = _L;
_L = NULL;
luaL_unref(L, LUA_REGISTRYINDEX, (int)_handle);
}
}

- (NSString *)description {
return [NSString stringWithFormat:@"Anonymous callable object with handle %d",_handle];
}

- (NSString *)debugDescription {
return [self description];
}

@end
Loading