Skip to content

Commit

Permalink
feat(ios): add KrollPromise to expose ES6 promises natively
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Aug 31, 2020
1 parent 8c996cb commit d92f05d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions iphone/TitaniumKit/TitaniumKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
B7385F5624659F83007DB1F2 /* AssetsModule.h in Headers */ = {isa = PBXBuildFile; fileRef = B7385F5424659F83007DB1F2 /* AssetsModule.h */; };
B7385F592465A4B3007DB1F2 /* KrollModule.m in Sources */ = {isa = PBXBuildFile; fileRef = B7385F572465A4B3007DB1F2 /* KrollModule.m */; };
B7385F5A2465A4B3007DB1F2 /* KrollModule.h in Headers */ = {isa = PBXBuildFile; fileRef = B7385F582465A4B3007DB1F2 /* KrollModule.h */; };
B70ABB8424B79C9C0007D07A /* KrollPromise.h in Headers */ = {isa = PBXBuildFile; fileRef = B70ABB8324B79C9C0007D07A /* KrollPromise.h */; settings = {ATTRIBUTES = (Public, ); }; };
B70ABB8624B79DCF0007D07A /* KrollPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = B70ABB8524B79DCF0007D07A /* KrollPromise.m */; };
B7624B6C216663C7000C365A /* ObjcProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = B7624B6A216663C7000C365A /* ObjcProxy.h */; settings = {ATTRIBUTES = (Public, ); }; };
B7624B6D216663C7000C365A /* ObjcProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = B7624B6B216663C7000C365A /* ObjcProxy.m */; };
B7D114DB2465F48600B95107 /* Module.h in Headers */ = {isa = PBXBuildFile; fileRef = B7D114DA2465F48600B95107 /* Module.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -198,6 +200,8 @@
B7385F5424659F83007DB1F2 /* AssetsModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AssetsModule.h; sourceTree = "<group>"; };
B7385F572465A4B3007DB1F2 /* KrollModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KrollModule.m; sourceTree = "<group>"; };
B7385F582465A4B3007DB1F2 /* KrollModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KrollModule.h; sourceTree = "<group>"; };
B70ABB8324B79C9C0007D07A /* KrollPromise.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KrollPromise.h; sourceTree = "<group>"; };
B70ABB8524B79DCF0007D07A /* KrollPromise.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KrollPromise.m; sourceTree = "<group>"; };
B7624B6A216663C7000C365A /* ObjcProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjcProxy.h; sourceTree = "<group>"; };
B7624B6B216663C7000C365A /* ObjcProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ObjcProxy.m; sourceTree = "<group>"; };
B7D114DA2465F48600B95107 /* Module.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Module.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -437,6 +441,8 @@
DB258CDA1F0975F7000D0D8D /* Kroll */ = {
isa = PBXGroup;
children = (
B70ABB8324B79C9C0007D07A /* KrollPromise.h */,
B70ABB8524B79DCF0007D07A /* KrollPromise.m */,
DB258D1B1F097680000D0D8D /* KrollBridge.h */,
DB258D1C1F097680000D0D8D /* KrollBridge.m */,
DB15FC231F0A833100A82C45 /* KrollCallback.h */,
Expand Down Expand Up @@ -699,6 +705,7 @@
DB15FD0C1F0A894000A82C45 /* TiWindowProxy.h in Headers */,
DB258DB81F09770C000D0D8D /* TiUtils.h in Headers */,
DB15FC351F0A833100A82C45 /* KrollContext.h in Headers */,
B70ABB8424B79C9C0007D07A /* KrollPromise.h in Headers */,
DB258D9D1F097680000D0D8D /* TiUIView.h in Headers */,
DB15FD2C1F0A93EF00A82C45 /* TiStreamProxy.h in Headers */,
B7385F5A2465A4B3007DB1F2 /* KrollModule.h in Headers */,
Expand Down Expand Up @@ -858,6 +865,7 @@
DB15FC9D1F0A862D00A82C45 /* ListenerEntry.m in Sources */,
DB15FC3C1F0A833100A82C45 /* KrollMethodDelegate.m in Sources */,
DB15FD291F0A93A900A82C45 /* TiFilesystemFileStreamProxy.m in Sources */,
B70ABB8624B79DCF0007D07A /* KrollPromise.m in Sources */,
DB15FC5C1F0A84C000A82C45 /* TiHost.m in Sources */,
DB258D621F097680000D0D8D /* Bridge.m in Sources */,
DB15FD071F0A892500A82C45 /* Ti2DMatrix.m in Sources */,
Expand Down
30 changes: 30 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifndef KrollPromise_h
#define KrollPromise_h

#import <JavaScriptCore/JavaScriptCore.h>

@interface KrollPromise : NSObject {
@private
JSValue *resolveFunc;
JSValue *rejectFunc;
}

@property (readonly) JSValue *JSValue;

- (void)resolve:(NSArray *)arguments;
- (void)reject:(NSArray *)arguments;

- (KrollPromise *)initInContext:(JSContext *)context;

+ (JSValue *)resolved:(NSArray *)arguments inContext:(JSContext *)context;
+ (JSValue *)rejected:(NSArray *)arguments inContext:(JSContext *)context;

@end

#endif /* KrollPromise_h */
73 changes: 73 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#import "KrollPromise.h"
#import "TiExceptionHandler.h"

@implementation KrollPromise

- (KrollPromise *)initInContext:(JSContext *)context
{
if (self = [super init]) {
if (@available(iOS 13, *)) {
// Use iOS 13 APIs.
JSObjectRef resolve;
JSObjectRef reject;
JSValueRef exception = NULL;
_JSValue = [JSValue valueWithJSValueRef:JSObjectMakeDeferredPromise(context.JSGlobalContextRef, &resolve, &reject, &exception) inContext:context];
if (exception) {
// report exception
[TiExceptionHandler.defaultExceptionHandler reportScriptError:[JSValue valueWithJSValueRef:exception inContext:context] inJSContext:context];
}
resolveFunc = [[JSValue valueWithJSValueRef:resolve inContext:context] retain];
rejectFunc = [[JSValue valueWithJSValueRef:reject inContext:context] retain];
} else {
// Alternative code for earlier versions of iOS. We hack it by evaluating JS
// TODO: I assume this is pretty slow. Can we re-use eval'd values here?
JSValue *executor = [context evaluateScript:@"function executor(resolve, reject) { executor.resolve = resolve; executor.reject = reject; }\nexecutor;"];
JSValue *exception = context.exception;
if (exception != nil) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context];
}
JSValue *createPromise = [context evaluateScript:@"function createPromise(executor) { console.log(executor); return new Promise(executor); }\ncreatePromise;"];
exception = context.exception;
if (exception != nil) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context];
}
_JSValue = [createPromise callWithArguments:@[ executor ]];
resolveFunc = [executor[@"resolve"] retain];
rejectFunc = [executor[@"reject"] retain];
}
}
return self;
}

+ (JSValue *)resolved:(NSArray *)arguments inContext:(JSContext *)context
{
KrollPromise *promise = [[KrollPromise alloc] initInContext:context];
[promise resolve:arguments];
return promise.JSValue;
}

+ (JSValue *)rejected:(NSArray *)arguments inContext:(JSContext *)context
{
KrollPromise *promise = [[KrollPromise alloc] initInContext:context];
[promise reject:arguments];
return promise.JSValue;
}

- (void)resolve:(NSArray *)arguments
{
[resolveFunc callWithArguments:arguments];
}

- (void)reject:(NSArray *)arguments
{
[rejectFunc callWithArguments:arguments];
}

- (void)dealloc
{
[resolveFunc release];
[rejectFunc release];
[super dealloc];
}

@end

0 comments on commit d92f05d

Please sign in to comment.