not for production.
You can ignore this step if xcodegen
has been installed already.
$ brew install xcodegen
$ cd project-dir
$ xcodegen
open the project, select simulator as the target, then press Command+U to run the tests.
- add pod dependency
pod 'QuickJS_iOS'
- add header import
#import <QuickJS_iOS/QuickJS_iOS.h>
Object for inject to javascript
@interface TestObject : NSObject
@end
@implementation TestObject
- (NSArray *)test:(NSNumber *)a :(NSString *)b :(NSNumber *)c {
NSLog(@"%@ %@ %@", a, b, c);
return @[@"a", @NO, @(123)];
}
@end
Sample for invoke ObjectiveC api from js
// prepare runtime & context
QJSRuntime *runtime = [[QJSRuntime alloc] init];
QJSContext *context = [runtime newContext];
// get global object
QJSValue *globalValue = [context getGlobalValue];
// set global variable
[globalValue setObject:[TestObject new] forKey:@"testval"];
// invoke objc instance api from javascript
[context eval:@"testval.test(1, 'a', false);"]
Sample for call javascript function in ObjectiveC
@protocol TestProtocol<NSObject>
- (id)javascriptAddFunc:(id)arg1 :(id)arg2
@end
QJSRuntime *runtime = [[QJSRuntime alloc] init];
QJSContext *context = [runtime newContext];
QJSValue *destObject = [context eval:@"var a = {javascriptAddFunc: function(a, b){return a * 10 + b;}}; a;"];
id<TestProtocol> obj = [destObject asProtocol:@protocol(TestProtocol)];
id retValue = [obj javascriptAddFunc:@(1):@(2)];
You can find more samples in file: QuickJS_iOSTest.m