Skip to content

Commit 6f3cd44

Browse files
author
Mihail Slavchev
committed
merge master branch
2 parents bece3ae + 485600f commit 6f3cd44

File tree

9 files changed

+222
-117
lines changed

9 files changed

+222
-117
lines changed

src/jni/JEnv.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ using namespace std;
1313

1414

1515

16-
JEnv::JEnv(bool detach)
17-
: m_detach(detach), m_env(nullptr)
16+
JEnv::JEnv()
17+
: m_env(nullptr)
1818
{
1919
JNIEnv *env = nullptr;
2020
jint ret = s_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
@@ -29,29 +29,12 @@ JEnv::JEnv(bool detach)
2929
}
3030

3131
JEnv::JEnv(JNIEnv *jniEnv)
32-
: m_env(jniEnv), m_detach(false)
32+
: m_env(jniEnv)
3333
{
3434
}
3535

3636
JEnv::~JEnv()
3737
{
38-
if (m_detach)
39-
{
40-
pid_t pid = getpid();
41-
pid_t tid = gettid();
42-
43-
if (pid != tid)
44-
{
45-
JNIEnv *env = nullptr;
46-
jint ret = s_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
47-
48-
if ((ret == JNI_OK) && (env != nullptr))
49-
{
50-
jint ret = s_jvm->DetachCurrentThread();
51-
assert(ret == JNI_OK);
52-
}
53-
}
54-
}
5538
}
5639

5740
JEnv::operator JNIEnv*() const

src/jni/JEnv.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace tns
1010
class JEnv
1111
{
1212
public:
13-
JEnv(bool detach = false);
13+
JEnv();
1414

1515
JEnv(JNIEnv *jniEnv);
1616

@@ -324,7 +324,6 @@ namespace tns
324324
void CheckForJavaException();
325325

326326
JNIEnv *m_env;
327-
bool m_detach;
328327

329328
static JavaVM *s_jvm;
330329

src/jni/Module.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,6 @@ void Module::RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
128128

129129
// cache the required modules by full path, not name only, since there might be some collisions with relative paths and names
130130
string modulePath = ArgConverter::jstringToString((jstring) jsModulePath);
131-
if(modulePath == ""){
132-
// module not found
133-
stringstream ss;
134-
ss << "Module \"" << moduleName << "\" not found";
135-
string exception = ss.str();
136-
throw NativeScriptException(exception);
137-
}
138-
if (modulePath == "EXTERNAL_FILE_ERROR")
139-
{
140-
// module not found
141-
stringstream ss;
142-
ss << "Module \"" << moduleName << "\" is located on the external storage. Modules can be private application files ONLY";
143-
string exception = ss.str();
144-
throw NativeScriptException(exception);
145-
}
146131

147132
auto it = s_loadedModules.find(modulePath);
148133

src/jni/NativeScriptException.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,20 @@ Local<Value> NativeScriptException::WrapJavaException(JEnv& env)
173173
{
174174
jfieldID fieldID = env.GetFieldID(env.GetObjectClass(exc), "jsValueAddress", "J");
175175
jlong addr = env.GetLongField(exc, fieldID);
176+
177+
jmethodID gmId = env.GetMethodID(env.GetObjectClass(exc), "getMessage", "()Ljava/lang/String;");
178+
jobject javaMessage = env.CallObjectMethod(exc, gmId);
179+
176180
Local<Value> v;
177181
if (addr != 0)
178182
{
179183
auto pv = (Persistent<Value> *) addr;
180184
v = Local<Value>::New(isolate, *pv);
181185
pv->Reset();
182186
}
187+
else if(javaMessage != nullptr) {
188+
v = GetJavaExceptionFromEnv(exc, env);
189+
}
183190
else
184191
{
185192
v = Null(isolate);
@@ -189,25 +196,30 @@ Local<Value> NativeScriptException::WrapJavaException(JEnv& env)
189196
}
190197
else
191198
{
192-
auto errMsg = GetExceptionMessage(env, exc);
193-
DEBUG_WRITE("Error during java interop errorMessage %s", errMsg.c_str());
199+
auto errObj = GetJavaExceptionFromEnv(exc, env);
200+
return errObj;
201+
}
202+
}
194203

195-
auto msg = ConvertToV8String(errMsg);
196-
auto errObj = Exception::Error(msg).As<Object>();
204+
Local<Value> NativeScriptException::GetJavaExceptionFromEnv(const JniLocalRef& exc, JEnv& env) {
205+
auto errMsg = GetExceptionMessage(env, exc);
206+
DEBUG_WRITE("Error during java interop errorMessage %s", errMsg.c_str());
197207

198-
jint javaObjectID = objectManager->GetOrCreateObjectId((jobject) exc);
199-
auto nativeExceptionObject = objectManager->GetJsObjectByJavaObject(javaObjectID);
208+
auto msg = ConvertToV8String(errMsg);
209+
auto errObj = Exception::Error(msg).As<Object>();
200210

201-
if (nativeExceptionObject.IsEmpty())
202-
{
203-
string className = objectManager->GetClassName((jobject)exc);
204-
nativeExceptionObject = objectManager->CreateJSWrapper(javaObjectID, className);
205-
}
206-
207-
errObj->Set(V8StringConstants::GetNativeException(), nativeExceptionObject);
211+
jint javaObjectID = objectManager->GetOrCreateObjectId((jobject) exc);
212+
auto nativeExceptionObject = objectManager->GetJsObjectByJavaObject(javaObjectID);
208213

209-
return errObj;
214+
if (nativeExceptionObject.IsEmpty())
215+
{
216+
string className = objectManager->GetClassName((jobject)exc);
217+
nativeExceptionObject = objectManager->CreateJSWrapper(javaObjectID, className);
210218
}
219+
220+
errObj->Set(V8StringConstants::GetNativeException(), nativeExceptionObject);
221+
222+
return errObj;
211223
}
212224

213225
void NativeScriptException::ThrowExceptionToV8(const string& exceptionMessage)

src/jni/NativeScriptException.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ namespace tns
3434
static string GetExceptionMessage(JEnv& env, jthrowable exception);
3535
static Local<Value> WrapJavaException(JEnv& env);
3636
static void ThrowExceptionToV8(const string& exceptionMessage);
37+
static Local<Value> GetJavaExceptionFromEnv(const JniLocalRef& exc, JEnv& env);
3738

3839
string GetFullMessage(const TryCatch& tc, bool isExceptionEmpty, bool isMessageEmpty, const string& prependMessage = "");
39-
JniLocalRef GetJavaException(const TryCatch& tc, bool isExceptionEmpty, bool isMessageEmpty, const string& prependMessage = "");
4040

41+
JniLocalRef GetJavaException(const TryCatch& tc, bool isExceptionEmpty, bool isMessageEmpty, const string& prependMessage = "");
4142
Local<Value> m_javascriptException;
4243
JniLocalRef m_javaException;
4344
string m_message;

src/src/com/tns/Module.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -57,40 +57,17 @@ static String getApplicationFilesPath()
5757
@RuntimeCallable
5858
private static String resolvePath(String path, String baseDir)
5959
{
60-
// This method is called my the NativeScriptRuntime.cpp RequireCallback method.
6160
// The baseDir is the directory path of the calling module.
6261
checkForExternalPath = true;
6362
File file = resolvePathHelper(path, baseDir);
6463

65-
if (file != null && file.exists())
66-
{
67-
File projectRootDir = new File(RootPackageDir);
68-
if (checkForExternalPath && isFileExternal(file, projectRootDir))
69-
{
70-
if (logger.isEnabled())
71-
{
72-
logger.write("Module " + path + " is on external path");
73-
}
74-
return "EXTERNAL_FILE_ERROR";
75-
}
76-
else
77-
{
78-
return file.getAbsolutePath();
79-
}
80-
}
81-
82-
// empty path will be handled by the NativeScriptRuntime.cpp and a JS error will be thrown
83-
if (logger.isEnabled())
84-
{
85-
logger.write("Module " + path + " not found. required from directory " + baseDir);
86-
}
87-
return "";
64+
return file.getAbsolutePath();
8865
}
8966

9067
private static boolean isFileExternal(File source, File target)
9168
{
9269
File currentParentDir = source.getParentFile();
93-
70+
9471
while (currentParentDir != null)
9572
{
9673
if (currentParentDir.equals(target))
@@ -108,6 +85,7 @@ private static File resolvePathHelper(String path, String baseDir)
10885
{
10986
File directory = null;
11087
File file = null;
88+
String possibleException = null;
11189

11290
if (baseDir == null || baseDir.isEmpty())
11391
{
@@ -119,19 +97,37 @@ private static File resolvePathHelper(String path, String baseDir)
11997
// absolute path
12098
directory = new File(path);
12199
file = getFileWithExtension(path);
100+
if(!file.exists()) {
101+
possibleException = "Failed to find module with absolute path: \"" + path + "\".";
102+
}
122103
}
123104
else if (path.startsWith("./") || path.startsWith("../") || path.startsWith("~/"))
124105
{
125106
// same or up directory
126107
String resolvedPath = FileSystem.resolveRelativePath(ApplicationFilesPath, path, baseDir);
127108
directory = new File(resolvedPath);
128109
file = getFileWithExtension(directory.getAbsolutePath());
110+
111+
if(!file.exists()) {
112+
if(path.startsWith("~/")) {
113+
possibleException = "Failed to find module: \"" + path + "\", relative to: " + ModulesFilesPath;
114+
}
115+
else {
116+
String paretnFolderPath = file.getParentFile().getAbsolutePath();
117+
if(ApplicationFilesPath.length() <= paretnFolderPath.length()) {
118+
possibleException = "Failed to find module: \"" + path + "\", relative to: " + paretnFolderPath.substring(ApplicationFilesPath.length()) + "/";
119+
}
120+
}
121+
}
129122
}
130123
else
131124
{
132125
// search for tns_module
133126
directory = new File(ApplicationFilesPath + NativeScriptModulesFilesPath, path);
134127
file = getFileWithExtension(directory.getPath());
128+
if(!file.exists()) {
129+
possibleException = "Failed to find module: \"" + path + "\", relative to: " + NativeScriptModulesFilesPath;
130+
}
135131
}
136132

137133
if (!file.exists() && directory.exists() && directory.isDirectory())
@@ -161,14 +157,14 @@ else if (path.startsWith("./") || path.startsWith("../") || path.startsWith("~/"
161157
}
162158
catch (IOException e)
163159
{
164-
// json read failed
165-
file = null;
160+
throw new NativeScriptException(e.getMessage());
166161
}
167162
catch (JSONException e)
168163
{
169-
file = null;
164+
throw new NativeScriptException(e.getMessage());
170165
}
171166
}
167+
172168
if (!found)
173169
{
174170
// search for index.js
@@ -177,19 +173,29 @@ else if (path.startsWith("./") || path.startsWith("../") || path.startsWith("~/"
177173

178174
// TODO: search for <folderName>.js ?
179175

180-
if(file != null) {
181-
// cache the main file for later use
182-
folderAsModuleCache.put(folderPath, file.getAbsolutePath());
183-
}
176+
// cache the main file for later use
177+
folderAsModuleCache.put(folderPath, file.getAbsolutePath());
184178
}
185179
else {
186180
// do not check whether this path is external for the application - it is already checked
187181
checkForExternalPath = false;
188182
file = new File(cachedPath);
189183
}
190184
}
191-
192-
return file;
185+
186+
File projectRootDir = new File(ApplicationFilesPath);
187+
if (checkForExternalPath && isFileExternal(file, projectRootDir))
188+
{
189+
throw new NativeScriptException("Module " + path + " is an external path. You can only load modules inside the application!");
190+
}
191+
192+
if (file.exists())
193+
{
194+
return file;
195+
}
196+
else {
197+
throw new NativeScriptException(possibleException);
198+
}
193199
}
194200

195201
private static File getFileWithExtension(String path)

test-app/assets/app/mainpage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ require("./tests/stringConversionTests");
1717
require("./tests/testsForTypescript");
1818
require("./tests/testGC");
1919
require("./tests/testsMemoryManagement");
20-
require("./tests/testIfAbleToRunExternalFile");
2120
require("./tests/testFieldGetSet");
2221
require("./tests/extendedClassesTests");
2322
require("./tests/extendClassNameTests");
2423
require("./tests/testJniReferenceLeak");
2524
require("./tests/testRequireJSON");
2625
require("./tests/testNativeModules");
26+
require("./tests/requireExceptionTests");
2727

2828
var MainActivity = {
2929
onCreate: function (bundle) {

0 commit comments

Comments
 (0)