Created
May 13, 2019 21:03
-
-
Save ccgus/5f677b899c98262d217f591061c20c2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (FJSValue*)evaluateModuleAtURL:(NSURL*)scriptURL { | |
if (scriptURL) { | |
NSError *error; | |
NSString *script = [NSString stringWithContentsOfURL:scriptURL encoding:NSUTF8StringEncoding error:&error]; | |
if (script) { | |
#define NODE_STYLE_WRAPPER 1 | |
#ifdef NODE_STYLE_WRAPPER | |
NSString *moduleWrapper = @"(function(__filename, __dirname) {\nvar module = { exports : {} }; var exports = module.exports;\n%@;\nreturn module.exports;})"; | |
moduleWrapper = [NSString stringWithFormat:moduleWrapper, script]; | |
FJSValue *moduleValueFunction = [self evaluateNoQueue:moduleWrapper withSourceURL:scriptURL]; | |
FJSValue *moduleValue = [moduleValueFunction callWithArguments:@[[scriptURL path], [[scriptURL URLByDeletingLastPathComponent] path]]]; | |
#else | |
id fn = self[@"__filename"]; | |
id dn = self[@"__dirname"]; | |
// __filename and __dirname are node things. I wish we could pass them in as arguments, but I can't seem to massage the js so that'll happen. | |
self[@"__filename"] = [scriptURL path]; | |
self[@"__dirname"] = [[scriptURL URLByDeletingLastPathComponent] path]; | |
NSString *module = [NSString stringWithFormat:@"(function() { var module = { exports : {} }; var exports = module.exports; %@;\nreturn module.exports; })()", script]; | |
FJSValue *moduleValue = [self evaluateNoQueue:module withSourceURL:scriptURL]; | |
self[@"__filename"] = [fn isUndefined] || [fn isNull] ? nil : fn; | |
self[@"__dirname"] = [dn isUndefined] || [dn isNull] ? nil : dn; | |
#endif | |
return moduleValue; | |
} | |
else if (error) { | |
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"Cannot find module %@", scriptURL.path] userInfo:nil]; | |
} | |
} | |
return nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment