Last active
July 31, 2018 05:53
-
-
Save bdash/8450420 to your computer and use it in GitHub Desktop.
Expose an Objective-C class to JavaScriptCore, with automatic JavaScript constructor generation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cc -framework JavaScriptCore -fobjc-arc -o javascriptcore-constructor-test javascriptcore-constructor-test.m | |
#include <JavaScriptCore/JavaScriptCore.h> | |
@protocol TestInterface <JSExport> | |
- (instancetype)initWithString:(NSString *)string; | |
@property (readonly) NSString *string; | |
@end | |
@interface TestClass : NSObject <TestInterface> | |
@end | |
@implementation TestClass | |
@synthesize string = _string; | |
- (instancetype)initWithString:(NSString *)string | |
{ | |
if (!(self = [super init])) | |
return nil; | |
_string = [string copy]; | |
return self; | |
} | |
@end | |
int main(int argc, char **argv) | |
{ | |
JSContext *context = [[JSContext alloc] init]; | |
context[@"TestClass"] = [TestClass class]; | |
JSValue *result = [context evaluateScript:@"(new TestClass(\"Hello, world!\")).string"]; | |
NSLog(@"Result: %@", result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works on iOS 7.1 and 8.0, outputing "Result: Hello, world!".