diff --git a/iphone/Classes/FilesystemModule.m b/iphone/Classes/FilesystemModule.m index 2e276bc21ef..bda7dcb8a8b 100644 --- a/iphone/Classes/FilesystemModule.m +++ b/iphone/Classes/FilesystemModule.m @@ -178,7 +178,7 @@ - (NSString *)lineEnding - (JSValue *)getFile { - NSArray *args = [JSContext currentArguments]; + NSArray *args = JSContext.currentArguments; NSString *newpath = [self pathFromComponents:args]; TiFile *fileProxy = [self getFileProxy:newpath]; return [self NativeToJSValue:fileProxy]; @@ -186,11 +186,14 @@ - (JSValue *)getFile - (TiFile *)getFileProxy:(NSString *)path { - if ([path hasPrefix:[self resourcesDirectory]] && ([path hasSuffix:@".js"] || [path hasSuffix:@".json"])) { - NSURL *url = [NSURL fileURLWithPath:path]; - NSData *data = [TiUtils loadAppResource:url]; - if (data != nil) { - return [[[TiFilesystemBlobProxy alloc] initWithURL:url data:data] autorelease]; + if ([path hasSuffix:@".js"] || [path hasSuffix:@".json"]) { + NSString *resourcesDir = [self resourcesDirectory]; + if ([path hasPrefix:resourcesDir] || [path hasPrefix:[resourcesDir stringByStandardizingPath]]) { + NSURL *url = [NSURL fileURLWithPath:path]; + NSData *data = [TiUtils loadAppResource:url]; + if (data != nil) { + return [[[TiFilesystemBlobProxy alloc] initWithURL:url data:data] autorelease]; + } } } @@ -199,25 +202,25 @@ - (TiFile *)getFileProxy:(NSString *)path - (TiBlob *)getAsset { - NSArray *args = [JSContext currentArguments]; + NSArray *args = JSContext.currentArguments; NSString *newpath = [self pathFromComponents:args]; - - if ([newpath hasPrefix:[self resourcesDirectory]] && ([newpath hasSuffix:@".jpg"] || [newpath hasSuffix:@".png"])) { - UIImage *image = nil; - NSRange range = [newpath rangeOfString:@".app"]; - NSString *imageArg = nil; - if (range.location != NSNotFound) { - imageArg = [newpath substringFromIndex:range.location + 5]; + if ([newpath hasSuffix:@".jpg"] || [newpath hasSuffix:@".png"]) { + NSString *resourcesDir = [self resourcesDirectory]; + if ([newpath hasPrefix:resourcesDir] || [newpath hasPrefix:[resourcesDir stringByStandardizingPath]]) { + NSRange range = [newpath rangeOfString:@".app"]; + if (range.location != NSNotFound) { + NSString *imageArg = [newpath substringFromIndex:range.location + 5]; + //remove suffixes. + imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@3x" withString:@""]; + imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@2x" withString:@""]; + imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~iphone" withString:@""]; + imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~ipad" withString:@""]; + + UIImage *image = [UIImage imageNamed:imageArg]; + + return [[[TiBlob alloc] initWithImage:image] autorelease]; + } } - //remove suffixes. - imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@3x" withString:@""]; - imageArg = [imageArg stringByReplacingOccurrencesOfString:@"@2x" withString:@""]; - imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~iphone" withString:@""]; - imageArg = [imageArg stringByReplacingOccurrencesOfString:@"~ipad" withString:@""]; - - image = [UIImage imageNamed:imageArg]; - - return [[[TiBlob alloc] initWithImage:image] autorelease]; } return nil; } diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m index a63a9fcb79c..015876e6c56 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m @@ -414,6 +414,8 @@ - (void)didStartNewContext:(KrollContext *)kroll JSPropertyDescriptorConfigurableKey : @NO, JSPropertyDescriptorValueKey : global }]; + // TODO: Move to real paths for __dirname/__filename, but that affects Android and may break users/debugging? + // NSString *dirname = [[TiHost resourcePath] stringByStandardizingPath]; // Set the __dirname and __filename for the app.js. // For other files, it will be injected via the `TitaniumModuleRequireFormat` property [global defineProperty:@"__dirname" @@ -423,6 +425,7 @@ - (void)didStartNewContext:(KrollContext *)kroll JSPropertyDescriptorConfigurableKey : @NO, JSPropertyDescriptorValueKey : @"/" }]; + // NSString *filename = [dirname stringByAppendingString:@"/app.js"]; [global defineProperty:@"__filename" descriptor:@{ JSPropertyDescriptorEnumerableKey : @NO, @@ -672,12 +675,22 @@ - (id)krollObjectForProxy:(id)proxy - (KrollWrapper *)loadCommonJSModule:(NSString *)code withSourceURL:(NSURL *)sourceURL { - // FIXME: Can we skip all of this now? Doesn't we already properly resolve paths? + // FIXME: Can we skip this now? Don't we already properly resolve paths? // This takes care of resolving paths like `../../foo.js` - sourceURL = [NSURL fileURLWithPath:[[sourceURL path] stringByStandardizingPath]]; - - // Get the relative path to the Resources directory - NSString *filename = [[sourceURL path] stringByReplacingOccurrencesOfString:[[[NSBundle mainBundle] resourceURL] path] withString:@""]; + sourceURL = [sourceURL URLByStandardizingPath]; + NSString *filename = [sourceURL path]; + + // TODO: We should likely move away from "faked" / root being resources dir + // And report the real full path in __filename/__dirname, but that may be a breaking change and would impact Android for parity + NSString *resourcesPath = [TiHost resourcePath]; + NSString *standardized = [resourcesPath stringByStandardizingPath]; + // Strip resources dir from prefix of file url (if it matches) for __filename + if ([filename hasPrefix:resourcesPath]) { + filename = [filename stringByReplacingOccurrencesOfString:resourcesPath withString:@""]; + } else if (![resourcesPath isEqualToString:standardized] && [filename hasPrefix:standardized]) { + filename = [filename stringByReplacingOccurrencesOfString:standardized withString:@""]; + } + // strip basename for __dirname NSString *dirname = [filename stringByDeletingLastPathComponent]; NSString *js = [[NSString alloc] initWithFormat:TitaniumModuleRequireFormat, dirname, filename, code]; diff --git a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m index e7aac71f3ac..443a865fe2a 100644 --- a/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m +++ b/iphone/TitaniumKit/TitaniumKit/Sources/API/TiUtils.m @@ -1560,15 +1560,21 @@ + (CGRect)viewPositionRect:(UIView *)view + (NSData *)loadAppResource:(NSURL *)url { BOOL app = [[url scheme] hasPrefix:@"app"]; + BOOL isFileURL = [url isFileURL]; - if ([url isFileURL] || app) { + if (isFileURL || app) { BOOL leadingSlashRemoved = NO; NSString *urlstring = [[url standardizedURL] path]; - NSString *resourceurl = [[NSBundle mainBundle] resourcePath]; - NSRange range = [urlstring rangeOfString:resourceurl]; NSString *appurlstr = urlstring; - if (range.location != NSNotFound) { - appurlstr = [urlstring substringFromIndex:range.location + range.length + 1]; + if (isFileURL) { + // strip resources dir filepath prefix (if it's there) + NSString *resourceurl = [[NSBundle mainBundle] resourcePath]; + NSString *standardized = [resourceurl stringByStandardizingPath]; + if ([urlstring hasPrefix:resourceurl]) { + appurlstr = [urlstring stringByReplacingOccurrencesOfString:resourceurl withString:@""]; + } else if (![resourceurl isEqualToString:standardized] && [urlstring hasPrefix:standardized]) { + appurlstr = [urlstring stringByReplacingOccurrencesOfString:standardized withString:@""]; + } } if ([appurlstr hasPrefix:@"/"]) { #ifndef __clang_analyzer__