Skip to content

Instantly share code, notes, and snippets.

@matt-curtis
Last active August 22, 2019 07:13
Show Gist options
  • Save matt-curtis/4d0a64048307fb54f874d665aac7e0e1 to your computer and use it in GitHub Desktop.
Save matt-curtis/4d0a64048307fb54f874d665aac7e0e1 to your computer and use it in GitHub Desktop.
MOJavaScriptObject function execution
@import JavaScriptCore;
/// Retaining MOJavaScriptObject retains your JSContext/JSObject as well
@interface MOJavaScriptObject : NSObject
@property (readonly) JSObjectRef JSObject;
@property (readonly) JSContextRef JSContext;
@end
@implementation MOJavaScriptObject ()
- (JSValue*) callWithArguments:(NSArray*)argumentsArray {
JSContext *context = [JSContext contextWithJSGlobalContextRef:(JSGlobalContextRef)self.JSContext];
JSValue *function = [JSValue valueWithJSValueRef:self.JSObject inContext:context];
return [function callWithArguments:argumentsArray];
}
@end
@matt-curtis
Copy link
Author

For anyone else who finds this gist and has the same questions as @websiddu and @nikogu:

The problem in James's case was that his JSContext was being released by Sketch before the function could execute. The solution was to use coscript.setShouldKeepAround(true) to prevent Sketch from releasing the context early.

Copy link

ghost commented Aug 22, 2019

@jamztang can you paste your closure code witch is how you trigger the runSomething method.

fn1(a, b) { return a + b; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment