Skip to content

Commit

Permalink
fix(ios): make JSValue nonatomic property, retain it under the hood, …
Browse files Browse the repository at this point in the history
…handle exception in creation
  • Loading branch information
sgtcoolguy committed Jan 26, 2021
1 parent 63bea10 commit 10c4acd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
JSValue *rejectFunc;
}

@property (readonly) JSValue *JSValue;
@property (readonly, nonatomic) JSValue *JSValue;

- (void)resolve:(NSArray *)arguments;
- (void)reject:(NSArray *)arguments;
Expand Down
21 changes: 18 additions & 3 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollPromise.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ - (KrollPromise *)initInContext:(JSContext *)context
JSObjectRef resolve;
JSObjectRef reject;
JSValueRef exception = NULL;
_JSValue = [JSValue valueWithJSValueRef:JSObjectMakeDeferredPromise(context.JSGlobalContextRef, &resolve, &reject, &exception) inContext:context];

JSObjectRef promiseRef = JSObjectMakeDeferredPromise(context.JSGlobalContextRef, &resolve, &reject, &exception);
if (exception) {
// FIXME: Randomly getting "null is not an object" and I don't know what is null here. The context? The Promise prototype?
// report exception
[TiExceptionHandler.defaultExceptionHandler reportScriptError:[JSValue valueWithJSValueRef:exception inContext:context] inJSContext:context];
JSValue *error = [JSValue valueWithJSValueRef:exception inContext:context];
NSLog(@"%@", error[@"message"]);
[context setException:error];
_JSValue = [[JSValue valueWithUndefinedInContext:context] retain];
resolveFunc = [[JSValue valueWithUndefinedInContext:context] retain];
rejectFunc = [[JSValue valueWithUndefinedInContext:context] retain];
return self; // all bets are off!
}

_JSValue = [[JSValue valueWithJSValueRef:promiseRef inContext:context] retain];
resolveFunc = [[JSValue valueWithJSValueRef:resolve inContext:context] retain];
rejectFunc = [[JSValue valueWithJSValueRef:reject inContext:context] retain];
} else {
Expand All @@ -31,7 +41,8 @@ - (KrollPromise *)initInContext:(JSContext *)context
if (exception != nil) {
[TiExceptionHandler.defaultExceptionHandler reportScriptError:exception inJSContext:context];
}
_JSValue = [createPromise callWithArguments:@[ executor ]];
// FIXME: Do we need to use a ManagedJSValue here rather than retain it? We do expose it to the JS Engine!
_JSValue = [[createPromise callWithArguments:@[ executor ]] retain];
resolveFunc = [executor[@"resolve"] retain];
rejectFunc = [executor[@"reject"] retain];
}
Expand Down Expand Up @@ -79,8 +90,12 @@ - (void)rejectWithErrorMessage:(NSString *)message

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

Expand Down

0 comments on commit 10c4acd

Please sign in to comment.