Skip to content

Commit

Permalink
fix(ios): delay js object creation to prevent accidental GC (8_0_X) (#…
Browse files Browse the repository at this point in the history
…10772)

* fix(ios): invoke event callbacks handling synchronously

* fix: schedule event firing on main-thread

* schedule blocks synchronously

* lazy create jsobject

* lazy create propsObject

* fix linting issue

* perform sync/async depending on thread

* remove debug logs

* re-add proxy protection

* fix linting issues
  • Loading branch information
janvennemann authored and lokeshchdhry committed May 10, 2019
1 parent 0328afa commit af14065
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 108 deletions.
6 changes: 1 addition & 5 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,7 @@ - (id)registerProxy:(id)proxy
}

ourKrollObject = [[KrollObject alloc] initWithTarget:proxy context:context];
#ifdef USE_JSCORE_FRAMEWORK
if (![proxy isKindOfClass:[TiModule class]]) {
[ourKrollObject applyGarbageCollectionSafeguard];
}
#endif
[ourKrollObject applyGarbageCollectionSafeguard];

[self registerProxy:proxy
krollObject:ourKrollObject];
Expand Down
17 changes: 8 additions & 9 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiBindingEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,14 @@ void TiBindingEventProcess(TiBindingRunLoop runloop, void *payload)
|| !JSObjectIsFunction(context, (JSObjectRef)currentCallback)) {
continue;
}
TiThreadPerformOnMainThread(^{
JSValueRef exception = NULL;
JSObjectCallAsFunction(context, (JSObjectRef)currentCallback, (JSObjectRef)eventTargetRef, 1, (JSValueRef *)&eventObjectRef, &exception);
if (exception != NULL) {
id excm = TiBindingTiValueToNSObject(context, exception);
[[TiExceptionHandler defaultExceptionHandler] reportScriptError:[TiUtils scriptErrorValue:excm]];
}
},
[NSThread isMainThread]);

JSValueRef exception = NULL;
JSObjectCallAsFunction(context, (JSObjectRef)currentCallback, (JSObjectRef)eventTargetRef, 1, (JSValueRef *)&eventObjectRef, &exception);
if (exception != NULL) {
id excm = TiBindingTiValueToNSObject(context, exception);
[[TiExceptionHandler defaultExceptionHandler] reportScriptError:[TiUtils scriptErrorValue:excm]];
}

// Note cancel bubble
cancelBubbleValue = JSObjectGetProperty(context, eventObjectRef, jsEventCancelBubbleStringRef, NULL);
if (JSValueToBoolean(context, cancelBubbleValue)) {
Expand Down
5 changes: 2 additions & 3 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiBindingTiValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ JSValueRef TiBindingTiValueFromProxy(JSContextRef jsContext, TiProxy *obj)
return [[ourBridge registerProxy:obj] jsobject];
}
KrollObject *objKrollObject = [ourBridge krollObjectForProxy:obj];
#ifdef USE_JSCORE_FRAMEWORK
JSValueRef valueRef = [objKrollObject jsobject];
[objKrollObject removeGarbageCollectionSafeguard];
#endif
return [objKrollObject jsobject];
return valueRef;
}

DebugLog(@"[WARN] Generating a new JSObject for KrollObject %@ because the contexts %@ and its context %@ differed.", obj, context, ourBridge);
Expand Down
47 changes: 27 additions & 20 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -889,16 +889,20 @@ - (void)fireEvent:(NSString *)type withObject:(id)obj propagate:(BOOL)propagate
if (![self _hasListeners:type]) {
return;
}
TiBindingEvent ourEvent;
ourEvent = TiBindingEventCreateWithNSObjects(self, self, type, obj);
if (report || (code != 0)) {
TiBindingEventSetErrorCode(ourEvent, code);
}
if (message != nil) {
TiBindingEventSetErrorMessageWithNSString(ourEvent, message);
}
TiBindingEventSetBubbles(ourEvent, propagate);
TiBindingEventFire(ourEvent);

TiThreadPerformOnMainThread(^{
TiBindingEvent ourEvent;
ourEvent = TiBindingEventCreateWithNSObjects(self, self, type, obj);
if (report || (code != 0)) {
TiBindingEventSetErrorCode(ourEvent, code);
}
if (message != nil) {
TiBindingEventSetErrorMessageWithNSString(ourEvent, message);
}
TiBindingEventSetBubbles(ourEvent, propagate);
TiBindingEventFire(ourEvent);
},
NSThread.isMainThread);
}

//Temporary method until source is removed, for our subclasses.
Expand All @@ -908,17 +912,20 @@ - (void)fireEvent:(NSString *)type withObject:(id)obj withSource:(id)source prop
return;
}

TiBindingEvent ourEvent;
TiThreadPerformOnMainThread(^{
TiBindingEvent ourEvent;

ourEvent = TiBindingEventCreateWithNSObjects(self, source, type, obj);
if (report || (code != 0)) {
TiBindingEventSetErrorCode(ourEvent, code);
}
if (message != nil) {
TiBindingEventSetErrorMessageWithNSString(ourEvent, message);
}
TiBindingEventSetBubbles(ourEvent, propagate);
TiBindingEventFire(ourEvent);
ourEvent = TiBindingEventCreateWithNSObjects(self, source, type, obj);
if (report || (code != 0)) {
TiBindingEventSetErrorCode(ourEvent, code);
}
if (message != nil) {
TiBindingEventSetErrorMessageWithNSString(ourEvent, message);
}
TiBindingEventSetBubbles(ourEvent, propagate);
TiBindingEventFire(ourEvent);
},
NSThread.isMainThread);
}

- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
Expand Down
6 changes: 1 addition & 5 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ bool KrollDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef prope
@private
NSMutableDictionary *properties;
NSMutableDictionary *statics;
JSObjectRef jsobject;
JSObjectRef propsObject;
JSObjectRef _jsobject;
BOOL targetable;
BOOL finalized;
BOOL protecting;
Expand Down Expand Up @@ -59,7 +58,6 @@ bool KrollDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef prope
//TODO: Lots of copypasted code in these methods could be refactored out.
@property (nonatomic, assign) JSObjectRef propsObject;
- (JSObjectRef)jsobject;
- (void)invalidateJsobject;
- (JSValueRef)jsvalueForUndefinedKey:(NSString *)key;

- (void)noteKeylessKrollObject:(KrollObject *)value;
Expand All @@ -83,9 +81,7 @@ bool KrollDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef prope
- (void)removeListener:(KrollCallback *)eventCallback forEvent:(NSString *)eventName;
- (void)triggerEvent:(NSString *)eventName withObject:(NSDictionary *)eventData thisObject:(KrollObject *)thisObject;

#ifdef USE_JSCORE_FRAMEWORK
- (void)applyGarbageCollectionSafeguard;
- (void)removeGarbageCollectionSafeguard;
#endif

@end

0 comments on commit af14065

Please sign in to comment.