Skip to content

Commit 2c8e2ac

Browse files
committed
fix(ios): handle when new proxies are created with dictionary arguments
1 parent 3e2934b commit 2c8e2ac

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.h

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ JSExportAs(fireEvent,
118118
@deprecated Only here for backwards compatibility with SDK < 8.1.0. Use `init` instead.
119119
*/
120120
- (id)_initWithPageContext:(id<TiEvaluator>)context __attribute__((deprecated));
121+
- (id)_initWithPageContext:(id<TiEvaluator>)contextargs:(NSArray *)args __attribute__((deprecated));
121122

122123
// hooks for when an event listener gets added/removed
123124
- (void)_listenerAdded:(NSString *)typecount:(int)count;

iphone/TitaniumKit/TitaniumKit/Sources/API/ObjcProxy.m

+32
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,38 @@ - (id)_initWithPageContext:(id<TiEvaluator>)context
112112
return [selfinit];
113113
}
114114

115+
- (id)_initWithPageContext:(id<TiEvaluator>)context_args:(NSArray *)args
116+
{
117+
if (self = [self_initWithPageContext:context_]) {
118+
NSDictionary *a = nil;
119+
NSUInteger count = [args count];
120+
if (count > 0 && [[args objectAtIndex:0] isKindOfClass:[NSDictionaryclass]]) {
121+
a = [args objectAtIndex:0];
122+
}
123+
124+
// If we're being created by an old proxy/module but we're a new-style obj-c proxy
125+
// we need to handle assigning the properties object passed into the constructor
126+
if (a != nil) {
127+
// Get the JS object corresponding to "this" proxy
128+
// Note that [JSContext currentContext] is nil, so we need to hack and get the global context
129+
// TODO: Can we hack in a nice method that gets current context if available, falls back to global context?
130+
// Because a lot of the code in the proxy base class assumes current context is not nil
131+
KrollContext *krollContext = [context_ krollContext];
132+
JSGlobalContextRef ref = krollContext.context;
133+
JSValueRef jsValueRef = TiBindingTiValueFromNSObject(ref, self);
134+
JSContext *context = [JSContext contextWithJSGlobalContextRef:ref];
135+
JSValue *this = [JSValue valueWithJSValueRef:jsValueRef inContext:context];
136+
137+
// Go through the key/value pairs and set them on "this"
138+
for (NSString *key in a) {
139+
id value = a[key];
140+
this[key] = value;
141+
}
142+
}
143+
}
144+
return self;
145+
}
146+
115147
- (NSURL *)_baseURL
116148
{
117149
return baseURL;

0 commit comments

Comments
 (0)
close