Skip to content

Commit

Permalink
fix(ios): handle when new proxies are created with dictionary arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Dec 4, 2019
1 parent 3e2934b commit 2c8e2ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ JSExportAs(fireEvent,
@deprecated Only here for backwards compatibility with SDK < 8.1.0. Use `init` instead.
*/
- (id)_initWithPageContext:(id<TiEvaluator>)context __attribute__((deprecated));
- (id)_initWithPageContext:(id<TiEvaluator>)context args:(NSArray *)args __attribute__((deprecated));

// hooks for when an event listener gets added/removed
- (void)_listenerAdded:(NSString *)type count:(int)count;
Expand Down
32 changes: 32 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,38 @@ - (id)_initWithPageContext:(id<TiEvaluator>)context
return [self init];
}

- (id)_initWithPageContext:(id<TiEvaluator>)context_ args:(NSArray *)args
{
if (self = [self _initWithPageContext:context_]) {
NSDictionary *a = nil;
NSUInteger count = [args count];
if (count > 0 && [[args objectAtIndex:0] isKindOfClass:[NSDictionary class]]) {
a = [args objectAtIndex:0];
}

// If we're being created by an old proxy/module but we're a new-style obj-c proxy
// we need to handle assigning the properties object passed into the constructor
if (a != nil) {
// Get the JS object corresponding to "this" proxy
// Note that [JSContext currentContext] is nil, so we need to hack and get the global context
// TODO: Can we hack in a nice method that gets current context if available, falls back to global context?
// Because a lot of the code in the proxy base class assumes current context is not nil
KrollContext *krollContext = [context_ krollContext];
JSGlobalContextRef ref = krollContext.context;
JSValueRef jsValueRef = TiBindingTiValueFromNSObject(ref, self);
JSContext *context = [JSContext contextWithJSGlobalContextRef:ref];
JSValue *this = [JSValue valueWithJSValueRef:jsValueRef inContext:context];

// Go through the key/value pairs and set them on "this"
for (NSString *key in a) {
id value = a[key];
this[key] = value;
}
}
}
return self;
}

- (NSURL *)_baseURL
{
return baseURL;
Expand Down

0 comments on commit 2c8e2ac

Please sign in to comment.