![Page 1: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/1.jpg)
These are confidential sessions—please refrain from streaming, blogging, or taking pictures
Session 615
Integrating JavaScript into Native Applications
Mark HahnenbergJavaScriptCore engineer
![Page 2: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/2.jpg)
JavaScriptCore.framework
Mac iOS
C API ?
![Page 3: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/3.jpg)
JavaScriptCore.framework
Mac iOS
C API C API
Objective-C API Objective-C API
![Page 4: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/4.jpg)
API GoalsOverview
Automatic
Safety
Fidelity
![Page 5: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/5.jpg)
API GoalsOverview
Automatic
Safety
Fidelity
![Page 6: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/6.jpg)
API GoalsOverview
Automatic
Safety
Fidelity
![Page 7: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/7.jpg)
API GoalsOverview
Automatic
Safety
Fidelity
![Page 8: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/8.jpg)
What You Will Learn
![Page 9: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/9.jpg)
What You Will Learn
• Objective-C → JavaScript
![Page 10: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/10.jpg)
What You Will Learn
• Objective-C → JavaScript
• JavaScript → Objective-C
![Page 11: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/11.jpg)
What You Will Learn
• Objective-C → JavaScript
• JavaScript → Objective-C• Memory management
![Page 12: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/12.jpg)
What You Will Learn
• Objective-C → JavaScript
• JavaScript → Objective-C• Memory management• Threading
![Page 13: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/13.jpg)
What You Will Learn
• Objective-C → JavaScript
• JavaScript → Objective-C• Memory management• Threading• JavaScriptCore C API
![Page 14: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/14.jpg)
What You Will Learn
• Objective-C → JavaScript
• JavaScript → Objective-C• Memory management• Threading• JavaScriptCore C API• JavaScriptCore with a WebView
![Page 15: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/15.jpg)
DemoColorMyWords application
![Page 16: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/16.jpg)
Objective-C → JavaScript
![Page 17: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/17.jpg)
Evaluating JavaScript Code
#import <JavaScriptCore/JavaScriptCore.h>
int main () {JSContext *context = [[JSContext alloc] init];
JSValue *result = [context evaluateScript:@"2 + 2"];
NSLog(@"2 + 2 = %d", [result toInt32]);return 0;
}
![Page 18: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/18.jpg)
Evaluating JavaScript Code
#import <JavaScriptCore/JavaScriptCore.h>
int main () {JSContext *context = [[JSContext alloc] init];
JSValue *result = [context evaluateScript:@"2 + 2"];
NSLog(@"2 + 2 = %d", [result toInt32]);return 0;
}
![Page 19: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/19.jpg)
Evaluating JavaScript Code
#import <JavaScriptCore/JavaScriptCore.h>
int main () {JSContext *context = [[JSContext alloc] init];
JSValue *result = [context evaluateScript:@"2 + 2"];
NSLog(@"2 + 2 = %d", [result toInt32]);return 0;
}
![Page 20: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/20.jpg)
Evaluating JavaScript Code
#import <JavaScriptCore/JavaScriptCore.h>
int main () {JSContext *context = [[JSContext alloc] init];
JSValue *result = [context evaluateScript:@"2 + 2"];
NSLog(@"2 + 2 = %d", [result toInt32]);return 0;
}
![Page 21: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/21.jpg)
JSContext
![Page 22: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/22.jpg)
JSContext
• Context for evaluating code
![Page 23: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/23.jpg)
JSContext
• Context for evaluating code• Global object
![Page 24: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/24.jpg)
JSValue
JSValue
![Page 25: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/25.jpg)
JSValue
• Reference to JavaScript value
JSValue
![Page 26: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/26.jpg)
JSValue
• Reference to JavaScript value■ Strong reference
JSValue
![Page 27: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/27.jpg)
JSValue
• Reference to JavaScript value■ Strong reference
• Tied to a JSContext JSValue
![Page 28: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/28.jpg)
JSValue
• Reference to JavaScript value■ Strong reference
• Tied to a JSContext■ Strong reference
JSValue
![Page 29: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/29.jpg)
Creating JSValues
+ valueWithBool:(BOOL)value inContext:(JSContext *)context;+ valueWithDouble:(double)value inContext:(JSContext *)context;+ valueWithInt32:(int32_t)value inContext:(JSContext *)context;+ valueWithUInt32:(uint32_t)value inContext:(JSContext *)context;+ valueWithNullInContext:(JSContext *)context;+ valueWithUndefinedInContext:(JSContext *)context;
+ valueWithNewObjectInContext:(JSContext *)context;+ valueWithNewArrayInContext:(JSContext *)context;+ valueWithNewRegularExpressionFromPattern: (NSString *)pattern flags:(NSString *)flags inContext:(JSContext *)context;+ valueWithNewErrorFromMessage: (NSString *)message inContext:(JSContext *)context;
![Page 30: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/30.jpg)
Creating JSValues
+ valueWithBool:(BOOL)value inContext:(JSContext *)context;+ valueWithDouble:(double)value inContext:(JSContext *)context;+ valueWithInt32:(int32_t)value inContext:(JSContext *)context;+ valueWithUInt32:(uint32_t)value inContext:(JSContext *)context;+ valueWithNullInContext:(JSContext *)context;+ valueWithUndefinedInContext:(JSContext *)context;
+ valueWithNewObjectInContext:(JSContext *)context;+ valueWithNewArrayInContext:(JSContext *)context;+ valueWithNewRegularExpressionFromPattern: (NSString *)pattern flags:(NSString *)flags inContext:(JSContext *)context;+ valueWithNewErrorFromMessage: (NSString *)message inContext:(JSContext *)context;
![Page 31: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/31.jpg)
BridgingCreating JSValues
+ valueWithObject:(id)value inContext:(JSContext *)context;
![Page 32: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/32.jpg)
Accessing JavaScript Values
- (BOOL)toBool;- (double)toDouble;- (int32_t)toInt32;- (uint32_t)toUInt32;- (NSNumber *)toNumber;- (NSString *)toString;
- (NSDate *)toDate;- (NSArray *)toArray;- (NSDictionary *)toDictionary;
- (id)toObject;- (id)toObjectOfClass:(Class)expectedClass;
![Page 33: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/33.jpg)
Calling JavaScript Functions
var factorial = function(n) {if (n < 0) return;if (n === 0) return 1;return n * factorial(n - 1)
};
![Page 34: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/34.jpg)
Calling JavaScript Functions
NSString *factorialScript = loadFactorialScript();[_context evaluateScript:factorialScript];
JSValue *function = _context[@"factorial"];
JSValue *result = [function callWithArguments:@[@5]];
NSLog(@"factorial(5) = %d", [result toInt32]);
![Page 35: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/35.jpg)
NSString *factorialScript = loadFactorialScript();[_context evaluateScript:factorialScript];
JSValue *function = _context[@"factorial"];
JSValue *result = [function callWithArguments:@[@5]];
NSLog(@"factorial(5) = %d", [result toInt32]);
Calling JavaScript Functions
![Page 36: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/36.jpg)
NSString *factorialScript = loadFactorialScript();[_context evaluateScript:factorialScript];
JSValue *function = _context[@"factorial"];
JSValue *result = [function callWithArguments:@[@5]];
NSLog(@"factorial(5) = %d", [result toInt32]);
Calling JavaScript Functions
![Page 37: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/37.jpg)
NSString *factorialScript = loadFactorialScript();[_context evaluateScript:factorialScript];
JSValue *function = _context[@"factorial"];
JSValue *result = [function callWithArguments:@[@5]];
NSLog(@"factorial(5) = %d", [result toInt32]);
Calling JavaScript Functions
![Page 38: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/38.jpg)
DemoColorMyWords recap
![Page 39: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/39.jpg)
JavaScript → Objective-C
![Page 40: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/40.jpg)
JavaScript → Objective-C
![Page 41: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/41.jpg)
JavaScript → Objective-C
• Two ways to interact with Objective-C from JavaScript
![Page 42: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/42.jpg)
JavaScript → Objective-C
• Two ways to interact with Objective-C from JavaScript■ Blocks
![Page 43: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/43.jpg)
JavaScript → Objective-C
• Two ways to interact with Objective-C from JavaScript■ Blocks
■ JS functions
![Page 44: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/44.jpg)
JavaScript → Objective-C
• Two ways to interact with Objective-C from JavaScript■ Blocks
■ JS functions■ JSExport protocol
![Page 45: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/45.jpg)
JavaScript → Objective-C
• Two ways to interact with Objective-C from JavaScript■ Blocks
■ JS functions■ JSExport protocol
■ JS objects
![Page 46: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/46.jpg)
Blocks
![Page 47: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/47.jpg)
Blocks
![Page 48: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/48.jpg)
Blocks
• Easy way to expose Objective-C code to JavaScript
![Page 49: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/49.jpg)
Blocks
• Easy way to expose Objective-C code to JavaScript• Automatically wraps Objective-C block inside callable JavaScript function
![Page 50: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/50.jpg)
Blocks
context[@"makeNSColor"] = ^(NSDictionary *rgb){ float r = [colors[@"red"] floatValue]; float g = [colors[@"green"] floatValue]; float b = [colors[@"blue"] floatValue]; return [NSColor colorWithRed:(r / 255.0) green:(g / 255.0f) blue:(b / 255.0f) alpha:1.0];}; Objective-C Block
![Page 51: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/51.jpg)
Blocks
context[@"makeNSColor"] = ^(NSDictionary *rgb){ float r = [colors[@"red"] floatValue]; float g = [colors[@"green"] floatValue]; float b = [colors[@"blue"] floatValue]; return [NSColor colorWithRed:(r / 255.0) green:(g / 255.0f) blue:(b / 255.0f) alpha:1.0];};
makeNSColor
Objective-C Block
![Page 52: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/52.jpg)
Blocks
var colorForWord = function(word) { if (!colorMap[word]) return; return makeNSColor(colorMap[word]);};
colorForWord
makeNSColor
Objective-C Block
![Page 53: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/53.jpg)
Blocks
var colorForWord = function(word) { if (!colorMap[word]) return; return makeNSColor(colorMap[word]);};
colorForWord
makeNSColor
Objective-C Block
![Page 54: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/54.jpg)
BlocksCaveats
![Page 55: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/55.jpg)
Blocks
• Avoid capturing JSValues
Caveats
![Page 56: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/56.jpg)
Blocks
• Avoid capturing JSValues■ Prefer passing as arguments
Caveats
![Page 57: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/57.jpg)
Blocks
• Avoid capturing JSValues■ Prefer passing as arguments
• Avoid capturing JSContexts
Caveats
![Page 58: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/58.jpg)
Blocks
• Avoid capturing JSValues■ Prefer passing as arguments
• Avoid capturing JSContexts■ Use + [JSContext currentContext]
Caveats
![Page 59: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/59.jpg)
Bad
JSContext *context = [[JSContext alloc] init];
context[@"callback"] = ^{JSValue *object = [JSValue valueWithNewObjectInContext:context];object[@"x"] = 2;object[@"y"] = 3;return object;
};
Blocks
![Page 60: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/60.jpg)
Good
JSContext *context = [[JSContext alloc] init];
context[@"callback"] = ^{JSValue *object = [JSValue valueWithNewObjectInContext: [JSContext currentContext]];object[@"x"] = 2;object[@"y"] = 3;return object;
};
Blocks
![Page 61: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/61.jpg)
JSExport
![Page 62: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/62.jpg)
JSExport
• Easy way for JavaScript to interact with Objective-C objects
![Page 63: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/63.jpg)
Objective-C JavaScript
@interface MyPoint
@property double x;@property double y;
- (NSString *)description;
+ (MyPoint *)makePointWithX:(double)x y:(double)y;
@end
JSExport
![Page 64: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/64.jpg)
Objective-C JavaScript
@interface MyPoint
@property double x;@property double y;
- (NSString *)description;
+ (MyPoint *)makePointWithX:(double)x y:(double)y;
@end
point.x;point.x = 10;
JSExport
![Page 65: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/65.jpg)
Objective-C JavaScript
@interface MyPoint
@property double x;@property double y;
- (NSString *)description;
+ (MyPoint *)makePointWithX:(double)x y:(double)y;
@end
point.x;point.x = 10;
point.description();
JSExport
![Page 66: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/66.jpg)
Objective-C JavaScript
@interface MyPoint
@property double x;@property double y;
- (NSString *)description;
+ (MyPoint *)makePointWithX:(double)x y:(double)y;
@end
point.x;point.x = 10;
point.description();
MyPoint.makePointWithXY(0, 0);
JSExport
![Page 67: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/67.jpg)
Objective-C JavaScript
@protocol MyPointExports <JSExport>
@property double x;@property double y;
- (NSString *)description;
+ (MyPoint *)makePointWithX:(double)x y:(double)y;
@end
point.x;point.x = 10;
point.description();
MyPoint.makePointWithXY(0, 0);
JSExport
![Page 68: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/68.jpg)
@interface MyPoint : NSObject <MyPointExports>
- (void)myPrivateMethod; // Not visible to JavaScript code.
@end
@implementation MyPoint
@end
JSExport
...
![Page 69: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/69.jpg)
JSExport
![Page 70: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/70.jpg)
JSExport
• Enumeration of methods and properties to export to JavaScript
![Page 71: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/71.jpg)
JSExport
• Enumeration of methods and properties to export to JavaScript
• @property → JavaScript getter/setter
![Page 72: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/72.jpg)
JSExport
• Enumeration of methods and properties to export to JavaScript
• @property → JavaScript getter/setter
• Instance method → JavaScript function
![Page 73: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/73.jpg)
JSExport
• Enumeration of methods and properties to export to JavaScript
• @property → JavaScript getter/setter
• Instance method → JavaScript function
• Class methods → JavaScript functions on global class object
![Page 74: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/74.jpg)
JSContext *context = [[JSContext alloc] init];[context evaluateScript:geometryScript];
MyPoint *point1 = [[MyPoint alloc] initWithX:0.0 y:0.0];MyPoint *point2 = [[MyPoint alloc] initWithX:1.0 y:1.0];
JSValue *function = context[@"euclideanDistance"];JSValue *result = [function callWithArguments:@[point1, point2]];
JSExport
![Page 75: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/75.jpg)
JSContext *context = [[JSContext alloc] init];[context evaluateScript:geometryScript];
MyPoint *point1 = [[MyPoint alloc] initWithX:0.0 y:0.0];MyPoint *point2 = [[MyPoint alloc] initWithX:1.0 y:1.0];
JSValue *function = context[@"euclideanDistance"];JSValue *result = [function callWithArguments:@[point1, point2]];
JSExport
![Page 76: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/76.jpg)
JSContext *context = [[JSContext alloc] init];[context evaluateScript:geometryScript];
MyPoint *point1 = [[MyPoint alloc] initWithX:0.0 y:0.0];MyPoint *point2 = [[MyPoint alloc] initWithX:1.0 y:1.0];
JSValue *function = context[@"euclideanDistance"];JSValue *result = [function callWithArguments:@[point1, point2]];
JSExport
![Page 77: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/77.jpg)
context[@"MyPoint"] = [MyPoint class];
JSValue *function = context[@"midpoint"];JSValue *jsResult = [function callWithArguments:@[point1, point2]];MyPoint *midpoint = [jsResult toObject];
JSExport
![Page 78: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/78.jpg)
context[@"MyPoint"] = [MyPoint class];
JSValue *function = context[@"midpoint"];JSValue *jsResult = [function callWithArguments:@[point1, point2]];MyPoint *midpoint = [jsResult toObject];
JSExport
![Page 79: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/79.jpg)
// geometry.js
var euclideanDistance = function(p1, p2) {var xDelta = p2.x - p1.x;var yDelta = p2.y - p1.y;return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
};
var midpoint = function(p1, p2) {var xDelta = (p2.x - p1.x) / 2;var yDelta = (p2.y - p1.y) / 2;return MyPoint.makePointWithXY(p1.x + xDelta, p1.y + yDelta);
};
JSExport
![Page 80: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/80.jpg)
Advanced API Topics
![Page 81: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/81.jpg)
Memory Management
![Page 82: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/82.jpg)
Memory Management
![Page 83: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/83.jpg)
Memory Management
• Objective-C uses ARC
![Page 84: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/84.jpg)
Memory Management
• Objective-C uses ARC• JavaScriptCore uses garbage collection
![Page 85: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/85.jpg)
Memory Management
• Objective-C uses ARC• JavaScriptCore uses garbage collection
■ All references are strong
![Page 86: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/86.jpg)
Memory Management
• Objective-C uses ARC• JavaScriptCore uses garbage collection
■ All references are strong
• API memory management is mostly automatic
![Page 87: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/87.jpg)
Memory Management
• Objective-C uses ARC• JavaScriptCore uses garbage collection
■ All references are strong
• API memory management is mostly automatic• Two situations that require extra attention:
![Page 88: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/88.jpg)
Memory Management
• Objective-C uses ARC• JavaScriptCore uses garbage collection
■ All references are strong
• API memory management is mostly automatic• Two situations that require extra attention:
■ Storing JavaScript values in Objective-C objects
![Page 89: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/89.jpg)
Memory Management
• Objective-C uses ARC• JavaScriptCore uses garbage collection
■ All references are strong
• API memory management is mostly automatic• Two situations that require extra attention:
■ Storing JavaScript values in Objective-C objects■ Adding JavaScript fields to Objective-C objects
![Page 90: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/90.jpg)
function ClickHandler(button, callback) {this.button = button;this.button.onClickHandler = this;this.handleEvent = callback;
};
Retain Cycles
![Page 91: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/91.jpg)
Bad
@implementation MyButton
- (void)setOnClickHandler:(JSValue *)handler{
_onClickHandler = handler; // Retain cycle}
@end
JSManagedValue
![Page 92: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/92.jpg)
Retain Cycles
Objective-C JavaScript
MyButton ClickHandler
_onClickHandler
this.button
Strong
Weak
![Page 93: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/93.jpg)
Retain Cycles
Objective-C JavaScript
MyButton ClickHandler
_onClickHandler
this.button
Strong
Weak
![Page 94: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/94.jpg)
Good
@implementation MyButton
- (void)setOnClickHandler:(JSValue *)handler{
_onClickHandler = [JSManagedValue managedValueWithValue:handler];[_context.virtualMachine addManagedReference:_onClickHandler withOwner:self]
}
@end
JSManagedValue
![Page 95: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/95.jpg)
Good
@implementation MyButton
- (void)setOnClickHandler:(JSValue *)handler{
_onClickHandler = [JSManagedValue managedValueWithValue:handler];[_context.virtualMachine addManagedReference:_onClickHandler withOwner:self]
}
@end
JSManagedValue
![Page 96: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/96.jpg)
JSManagedValue
Objective-C JavaScript
MyButton ClickHandler
_onClickHandler
this.button
Strong
Weak
Garbage collected
![Page 97: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/97.jpg)
Managed References
![Page 98: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/98.jpg)
Managed References
• JSManagedValue by itself is a weak reference to a JavaScript value
![Page 99: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/99.jpg)
Managed References
• JSManagedValue by itself is a weak reference to a JavaScript value• -addManagedReference:withOwner: turns JSManagedValue into a "garbage collected" reference
![Page 100: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/100.jpg)
Managed References
![Page 101: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/101.jpg)
Managed References
• If JavaScript can find the Objective-C owner of a managed reference■ Reference is kept alive
![Page 102: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/102.jpg)
Managed References
• If JavaScript can find the Objective-C owner of a managed reference■ Reference is kept alive
• Otherwise■ Reference is released
![Page 103: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/103.jpg)
Threading
![Page 104: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/104.jpg)
JSVirtualMachine
![Page 105: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/105.jpg)
JSVirtualMachine
![Page 106: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/106.jpg)
JSVirtualMachine
![Page 107: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/107.jpg)
JSVirtualMachine
JSVirtualMachine
![Page 108: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/108.jpg)
JSVirtualMachine
JSVirtualMachine
![Page 109: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/109.jpg)
JSVirtualMachine
JSVirtualMachine
![Page 110: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/110.jpg)
JSVirtualMachine
JSVirtualMachine
![Page 111: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/111.jpg)
JSVirtualMachine
JSVirtualMachine
![Page 112: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/112.jpg)
Threading
![Page 113: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/113.jpg)
Threading
• API is thread safe
![Page 114: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/114.jpg)
Threading
• API is thread safe• Locking granularity is JSVirtualMachine
![Page 115: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/115.jpg)
Threading
• API is thread safe• Locking granularity is JSVirtualMachine
■ Use separate JSVirtualMachines for concurrency/parallelism
![Page 116: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/116.jpg)
Interfacing with the JavaScriptCore C API
![Page 117: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/117.jpg)
• JSValue ↔ JSValueRef• JSContext ↔ JSGlobalContextRef
Interfacing with the C API
![Page 118: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/118.jpg)
JSGlobalContextRef ctx = JSContext *context = [JSContext contextWithJSGlobalContextRef:ctx];
JSContext *context = JSGlobalContextRef ctx = [context JSGlobalContextRef];
JSContext ↔ JSGlobalContextRef
...
...
![Page 119: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/119.jpg)
JSValueRef valueRef = JSValue *value = [JSValue valueWithJSValueRef:valueRef inContext:context];
JSValue *value = JSValueRef valueRef = [value JSValueRef];
JSValue ↔ JSValueRef
...
...
![Page 120: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/120.jpg)
DemoColorMyCode
![Page 121: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/121.jpg)
WebKit WebView
![Page 122: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/122.jpg)
WebKit WebView (Mac)
![Page 123: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/123.jpg)
WebKit WebView (Mac)
• -webView:didCreateJavaScriptContext:forFrame:
WebView
JavaScriptCore
WebFrameLoadDelegate
![Page 124: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/124.jpg)
WebKit WebView (Mac)
• -webView:didCreateJavaScriptContext:forFrame:• Install custom objects using context argument
WebView
JavaScriptCore
WebFrameLoadDelegate
![Page 125: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/125.jpg)
WebKit WebView (Mac)
• -webView:didCreateJavaScriptContext:forFrame:• Install custom objects using context argument
• Replaces old callbacks gracefully
WebView
JavaScriptCore
WebFrameLoadDelegate
![Page 126: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/126.jpg)
WebKit WebView (Mac)
• -webView:didCreateJavaScriptContext:forFrame:• Install custom objects using context argument
• Replaces old callbacks gracefully• iTunes store uses WebView
![Page 127: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/127.jpg)
@implementation MyFrameLoadDelegate
- (void)webView:(WebView *)webView didCreateJavaScriptContext(JSContext *)context forFrame:(WebFrame *)frame{
MyConsole *console = [[MyConsole alloc] init];context[@"myConsole"] = console;
}
@end
WebKit WebView (Mac)
![Page 128: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/128.jpg)
<html><head> <script> var onClickHandler = function() { myConsole.log("clicked!"); }; </script></head><body> <button onclick="onClickHandler()">Click Me!</button></body>
</html>
WebKit WebView (Mac)
![Page 129: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/129.jpg)
Summary
![Page 130: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/130.jpg)
What We’ve Covered
• Objective-C → JavaScript• JavaScript → Objective-C
• Interfacing with the JavaScriptCore C API
• Reference counting vs. Garbage collection
• Threading
• Custom objects in WebKit WebViews
![Page 131: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/131.jpg)
Call to Action
• Convert one bit of old code to use the new Objective-C API• Add a small snippet of JavaScript to your app
![Page 132: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/132.jpg)
John GeleynseDirector, Technology [email protected]
DocumentationJavaScriptCore.framework Headershttps://developer.apple.com/library/mac/#documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/
Apple Developer Forumshttp://devforums.apple.com
More Information
![Page 133: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/133.jpg)
Related Sessions
Getting to Know Web Inspector Russian HillTuesday 10:15AM
Getting the Most Out of Web Inspector Russian HillTuesday 11:30AM
Power and Performance: Optimizing Your Website for Great Battery Life and Responsive Scrolling
Russian HillWednesday 9:00AM
Preparing and Presenting Media for Accessibility Nob HillWednesday 10:15AM
Implementing OS X Push Notifications for Websites MarinaFriday 9:00AM
Integrating JavaScript into Native Apps MarinaFriday 10:15AM
![Page 134: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/134.jpg)
Labs
Integrating Web Technologies into Native Apps Lab Media Lab AFriday 11:30 AM
Safari and Web Tools Lab Media Lab AFriday 2:00 PM
![Page 135: Integrating JavaScript into Native Applications · JavaScriptCore.framework Mac iOS C API C API Objective-C API Objective-C API](https://reader035.vdocuments.us/reader035/viewer/2022062306/5f6224fe83a24c70c0289616/html5/thumbnails/135.jpg)