Skip to content

Commit 22fd3b1

Browse files
committed
runtime: handle internal modules, support require("lib.node");
1 parent 56aa07e commit 22fd3b1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

test-app/runtime/src/main/cpp/runtime/module/ModuleInternal.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ void ModuleInternal::CheckFileExists(napi_env env, const std::string& path, cons
219219
jEnv.CallStaticObjectMethod(MODULE_CLASS, RESOLVE_PATH_METHOD_ID, (jstring) jsModulename, (jstring) jsBaseDir);
220220
}
221221

222+
napi_value ModuleInternal::LoadInternalModule(napi_env env, const std::string& moduleName) {
223+
if (moduleName == "url") {
224+
napi_value moduleObj;
225+
napi_create_object(env, &moduleObj);
226+
napi_value url;
227+
napi_value exports;
228+
napi_create_object(env, &exports);
229+
napi_get_named_property(env, napi_util::global(env), "URL", &url);
230+
napi_set_named_property(env, exports, "URL", url);
231+
napi_set_named_property(env, moduleObj, "exports", exports);
232+
napi_util::napi_set_function(env, exports, "pathToFileURL", [](napi_env env, napi_callback_info info) -> napi_value {
233+
return ArgConverter::convertToJsString(env, "file://");
234+
});
235+
return moduleObj;
236+
}
237+
return nullptr;
238+
}
239+
222240
napi_value ModuleInternal::LoadImpl(napi_env env, const std::string& moduleName, const std::string& baseDir, bool& isData) {
223241
auto pathKind = GetModulePathKind(moduleName);
224242
auto cachePathKey = (pathKind == ModulePathKind::Global) ? moduleName : (baseDir + "*" + moduleName);
@@ -229,6 +247,13 @@ napi_value ModuleInternal::LoadImpl(napi_env env, const std::string& moduleName,
229247

230248
auto it = m_loadedModules.find(cachePathKey);
231249

250+
/**
251+
* Load internal modules like url,fs etc directly if someone does
252+
* require('url');
253+
*/
254+
napi_value moduleObj = ModuleInternal::LoadInternalModule(env, moduleName);
255+
if (moduleObj) return moduleObj;
256+
232257
if (it == m_loadedModules.end()) {
233258
std::string path;
234259

@@ -240,6 +265,10 @@ napi_value ModuleInternal::LoadImpl(napi_env env, const std::string& moduleName,
240265
path.replace(pos, sys_lib.length(), "");
241266
} else if (Util::EndsWith(moduleName, ".so")) {
242267
path = "lib" + moduleName;
268+
} else if (Util::EndsWith(moduleName, ".node")) {
269+
std::string libName = moduleName;
270+
Util::ReplaceAll(libName, ".node", "");
271+
path = "lib" + libName + ".so";
243272
} else {
244273
JEnv jenv;
245274
JniLocalRef jsModulename(jenv.NewStringUTF(moduleName.c_str()));

test-app/runtime/src/main/cpp/runtime/module/ModuleInternal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ModuleInternal {
5454
};
5555

5656
static napi_value RequireCallback(napi_env env, napi_callback_info info);
57+
static napi_value LoadInternalModule(napi_env env, const std::string& moduleName);
5758

5859
static napi_value RequireNativeCallback(napi_env env, napi_callback_info info);
5960

0 commit comments

Comments
 (0)