diff --git a/src/jni/Module.cpp b/src/jni/Module.cpp index a0952501d..bac9a5fa0 100644 --- a/src/jni/Module.cpp +++ b/src/jni/Module.cpp @@ -182,6 +182,41 @@ void Module::RequireCallback(const v8::FunctionCallbackInfo& args) } } +void Module::ResolveModulePath(const v8::FunctionCallbackInfo& args) +{ + try + { + if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) + { + throw NativeScriptException(string("__resolveModulePath should be called with two string parameters")); + } + + string path = ConvertToString(args[0].As()); + string baseDir = ConvertToString(args[1].As()); + + JEnv env; + jstring jsPath = env.NewStringUTF(path.c_str()); + jstring jsBaseDir = env.NewStringUTF(baseDir.c_str()); + jobject jsModulePath = env.CallStaticObjectMethod(MODULE_CLASS, RESOLVE_PATH_METHOD_ID, jsPath, jsBaseDir); + + args.GetReturnValue().Set(ArgConverter::jstringToV8String((jstring) jsModulePath)); + } + catch (NativeScriptException& e) + { + e.ReThrowToV8(); + } + catch (std::exception e) { + stringstream ss; + ss << "Error: c++ exception: " << e.what() << endl; + NativeScriptException nsEx(ss.str()); + nsEx.ReThrowToV8(); + } + catch (...) { + NativeScriptException nsEx(std::string("Error: c++ exception!")); + nsEx.ReThrowToV8(); + } +} + void Module::RequireNativeCallback(const v8::FunctionCallbackInfo& args) { auto ext = args.Data().As(); diff --git a/src/jni/Module.h b/src/jni/Module.h index c7331659a..79bb2a415 100644 --- a/src/jni/Module.h +++ b/src/jni/Module.h @@ -25,6 +25,7 @@ namespace tns static void RequireCallback(const v8::FunctionCallbackInfo& args); + static void ResolveModulePath(const v8::FunctionCallbackInfo& args); private: struct Cache; diff --git a/src/jni/Runtime.cpp b/src/jni/Runtime.cpp index 3c1befb3b..edef187db 100644 --- a/src/jni/Runtime.cpp +++ b/src/jni/Runtime.cpp @@ -392,6 +392,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, globalTemplate->Set(ConvertToV8String("__disableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::DisableVerboseLoggingMethodCallback)); globalTemplate->Set(ConvertToV8String("__exit"), FunctionTemplate::New(isolate, CallbackHandlers::ExitMethodCallback)); globalTemplate->Set(ConvertToV8String("__nativeRequire"), FunctionTemplate::New(isolate, Module::RequireCallback)); + globalTemplate->Set(ConvertToV8String("__resolveModulePath"), FunctionTemplate::New(isolate, Module::ResolveModulePath)); m_weakRef.Init(isolate, globalTemplate, m_objectManager);