|
| 1 | +#include <string> |
| 2 | +#include "js_native_api.h" |
| 3 | +#include "NapiUtil.h" |
| 4 | + |
| 5 | +#import <Foundation/Foundation.h> |
| 6 | + |
| 7 | +class Require { |
| 8 | +public: |
| 9 | + Require(std::string path) : path(path) {} |
| 10 | + |
| 11 | + static napi_value createRequire(napi_env env, std::string &path) { |
| 12 | + Require *require = new Require(path); |
| 13 | + napi_value result; |
| 14 | + napi_create_function(env, "require", NAPI_AUTO_LENGTH, Require::requireCallback, require, &result); |
| 15 | + napi_ref ref; |
| 16 | + napi_wrap(env, result, require, Require::finalize, nullptr, &ref); |
| 17 | + return result; |
| 18 | + } |
| 19 | + |
| 20 | + static void finalize(napi_env env, void *data, void *hint) { |
| 21 | + Require *require = (Require *) data; |
| 22 | + delete require; |
| 23 | + } |
| 24 | + |
| 25 | + std::string resolve(std::string &spec) { |
| 26 | + if (spec.find("/") == 0) { |
| 27 | + return spec; |
| 28 | + } |
| 29 | + |
| 30 | + size_t dotpos = spec.find("."); |
| 31 | + size_t tildepos = spec.find("~"); |
| 32 | + std::string result; |
| 33 | + if (tildepos == 0) { |
| 34 | + result = [NSBundle.mainBundle.resourcePath UTF8String]; |
| 35 | + result += "/app"; |
| 36 | + } else { |
| 37 | + result = path; |
| 38 | + } |
| 39 | + |
| 40 | + if (dotpos == 0 || tildepos == 0) { |
| 41 | + std::string relativeSpec = spec.substr(spec.find("./") == 0 || spec.find("~/") == 0 ? 2 : 1); |
| 42 | + if (relativeSpec.empty()) { |
| 43 | + result += "/index.js"; |
| 44 | + } else { |
| 45 | + result += "/" + relativeSpec; |
| 46 | + } |
| 47 | + } else { |
| 48 | + result += "/" + spec; |
| 49 | + } |
| 50 | + |
| 51 | + size_t pos = result.rfind("/"); |
| 52 | + size_t jspos = result.find(".js"); |
| 53 | + if (jspos < pos || jspos == std::string::npos) { |
| 54 | + result += result.ends_with("/") ? "index.js" : "/index.js"; |
| 55 | + } |
| 56 | + |
| 57 | + return result; |
| 58 | + } |
| 59 | + |
| 60 | + napi_value require(napi_env env, std::string &spec) { |
| 61 | + std::string path = resolve(spec); |
| 62 | + NSError *error = nil; |
| 63 | + |
| 64 | + NSString *source = [NSString stringWithContentsOfFile:[NSString stringWithUTF8String:path.c_str()] encoding:NSUTF8StringEncoding error:&error]; |
| 65 | + if (error != nil) { |
| 66 | + NSLog(@"error: %@", error); |
| 67 | + napi_throw_error(env, nullptr, [[error localizedDescription] UTF8String]); |
| 68 | + return nullptr; |
| 69 | + } |
| 70 | + |
| 71 | + if (spec.ends_with(".json")) { |
| 72 | + napi_value script, result; |
| 73 | + napi_create_string_utf8(env, [source UTF8String], NAPI_AUTO_LENGTH, &script); |
| 74 | + napi_run_script(env, script, &result); |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + std::string bootstrap; |
| 79 | + bootstrap = "let cjsModule; try { cjsModule = function cjsModule(exports, require, module, __filename, __dirname) { try { "; |
| 80 | + bootstrap += [source UTF8String]; |
| 81 | + bootstrap += "\n } catch(e) { console.log('evaluate module failed:', e.stack); } }; Object.defineProperty(cjsModule, \"name\", { value: `" + path + "` }); } catch (e) { console.log(e.stack) }\n cjsModule"; |
| 82 | + |
| 83 | + napi_status status; |
| 84 | + napi_value func, script, module, exports, require, __filename, __dirname, global, result; |
| 85 | + |
| 86 | + napi_create_string_utf8(env, bootstrap.c_str(), NAPI_AUTO_LENGTH, &script); |
| 87 | + |
| 88 | + status = napi_run_script(env, script, &func); |
| 89 | + if (status != napi_ok) { |
| 90 | + const napi_extended_error_info *info; |
| 91 | + napi_get_last_error_info(env, &info); |
| 92 | + NSLog(@"error in run script: %d, %s", status, info->error_message); |
| 93 | + return nullptr; |
| 94 | + } |
| 95 | + |
| 96 | + napi_create_object(env, &module); |
| 97 | + napi_create_object(env, &exports); |
| 98 | + |
| 99 | + napi_set_named_property(env, module, "exports", exports); |
| 100 | + |
| 101 | + napi_get_global(env, &global); |
| 102 | + |
| 103 | + napi_create_string_utf8(env, path.c_str(), NAPI_AUTO_LENGTH, &__filename); |
| 104 | + |
| 105 | + std::string dirname = path.substr(0, path.rfind("/")); |
| 106 | + |
| 107 | + require = Require::createRequire(env, dirname); |
| 108 | + |
| 109 | + napi_create_string_utf8(env, dirname.c_str(), NAPI_AUTO_LENGTH, &__dirname); |
| 110 | + |
| 111 | + napi_value argv[5] = {exports, require, module, __filename, __dirname}; |
| 112 | + |
| 113 | + status = napi_call_function(env, global, func, 5, argv, &result); |
| 114 | + if (status != napi_ok) { |
| 115 | + const napi_extended_error_info *info; |
| 116 | + napi_get_last_error_info(env, &info); |
| 117 | + NSLog(@"error in evaluate module: %d, %s", status, info->error_message); |
| 118 | + return nullptr; |
| 119 | + } |
| 120 | + |
| 121 | + napi_get_named_property(env, module, "exports", &exports); |
| 122 | + return exports; |
| 123 | + } |
| 124 | + |
| 125 | + static napi_value requireCallback(napi_env env, napi_callback_info cbinfo) { |
| 126 | + napi_value arg; |
| 127 | + Require *require; |
| 128 | + size_t argc = 1; |
| 129 | + napi_get_cb_info(env, cbinfo, &argc, &arg, nullptr, (void**) &require); |
| 130 | + std::string spec = getStringValue(env, arg); |
| 131 | + return require->require(env, spec); |
| 132 | + } |
| 133 | + |
| 134 | + std::string path; |
| 135 | +}; |
0 commit comments