Skip to content

Expose resolvePath to JavaScript #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/jni/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ void Module::RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
}
}

void Module::ResolveModulePath(const v8::FunctionCallbackInfo<v8::Value>& 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>());
string baseDir = ConvertToString(args[1].As<String>());

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<v8::Value>& args)
{
auto ext = args.Data().As<External>();
Expand Down
1 change: 1 addition & 0 deletions src/jni/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace tns

static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args);

static void ResolveModulePath(const v8::FunctionCallbackInfo<v8::Value>& args);
private:
struct Cache;

Expand Down
1 change: 1 addition & 0 deletions src/jni/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down