Skip to content

Instantly share code, notes, and snippets.

@preslavrachev
Created September 8, 2012 11:18
Show Gist options
  • Save preslavrachev/3673663 to your computer and use it in GitHub Desktop.
Save preslavrachev/3673663 to your computer and use it in GitHub Desktop.
Executing Objective-C code from Haxe (Using NME's native extensions)
#in the Extension folder:
haxelib run hxcpp Build.xml -Diphoneos #device
#or
haxelib run hxcpp Build.xml -Diphonesim #simulator
#or
haxelib run hxcpp Build.xml #desktop - Mac or Windows respectively
#then build the Haxe project, and use XCode to compile and deploy the C++ code
<!-- PATH: Extension/project/Build.xml-->
<!-- .... -->
!-- Find the "files" section -->
<files id="common">
<compilerflag value="-Iinclude"/>
<file name="common/ExternalInterface.cpp"/>
<file name="common/Test.cpp"/>
!-- Insert the ObjCTest.mm file there -->
<file name="common/ObjCTest.mm"/>
</files>
<!-- .... -->
<!-- PATH: Project/Extension Test.nmml -->
<!-- change the following line -->
<include path="../Extension" />
<!-- to -->
<extension name="test" path="../Extension" />
<!-- PATH: Project/ExtensionTest.hx -->
private function construct ():Void {
Test.nslog();
}
<!-- PATH: Extension/project/common/ExternalInterface.cpp -->
#include "ObjCTest.h"
//....
using namespace test;
static value test_nslog () {
return alloc_int (testNSLog ());
}
DEFINE_PRIM (test_nslog, 0);
<!-- PATH: Extension/project/include/ObjCTest.h-->
#import <hx/CFFI.h>
namespace test {
int testNSLog();
}
<!-- PATH: Extension/project/common/ObjCTest.mm-->
<!-- NOTE: It is important to use the extension ".mm" (Objective-C++)
rather than ".m" (Objective-C)
-->
#import "ObjCTest.h"
#import <Foundation/Foundation.h>
namespace test {
int testNSLog(value str) {
//I know, the example is super silly, but easy enough to
//demonstrate the idea
NSString *msg = [[NSString alloc] initWithCString:"Testing..."];
NSLog(msg);
return 0;
}
}
<!-- PATH: Extension/Test.hx -->
public static function nslog (message:String):Void {
objc_call_nslog (message);
}
//...
private static var objc_call_nslog = Lib.load ("test", "test_nslog", 0);
// the "0" is for the number of arguments that the function accepts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment