From 2c8e2ac07e07a49434bded23324d8e7f9492ae8a Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Tue, 3 Dec 2019 17:09:01 -0500 Subject: [PATCH] fix(ios): handle when new proxies are created with dictionary arguments --- .../TitaniumKit/Sources/API/ObjcProxy.h | 1 + .../TitaniumKit/Sources/API/ObjcProxy.m | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.h b/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.h index 85da2acea87..85950b3dcec 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.h +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.h @@ -118,6 +118,7 @@ JSExportAs(fireEvent, @deprecated Only here for backwards compatibility with SDK < 8.1.0. Use `init` instead. */ - (id)_initWithPageContext:(id)context __attribute__((deprecated)); +- (id)_initWithPageContext:(id)context args:(NSArray *)args __attribute__((deprecated)); // hooks for when an event listener gets added/removed - (void)_listenerAdded:(NSString *)type count:(int)count; diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.m index 597566d454e..358a1d0d04c 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.m @@ -112,6 +112,38 @@ - (id)_initWithPageContext:(id)context return [self init]; } +- (id)_initWithPageContext:(id)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;