Skip to content

Commit 5031087

Browse files
committed
Merge pull request #152 from NativeScript/0-day-fix
fix method resolver to support the same behavior of throwing exceptio…
2 parents 4c19395 + dc910a2 commit 5031087

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

src/jni/NativeScriptRuntime.cpp

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,35 +197,95 @@ void NativeScriptRuntime::CallJavaMethod(const Handle<Object>& caller, const str
197197
if ((entry != nullptr) && entry->isResolved)
198198
{
199199
isStatic = entry->isStatic;
200+
200201
if (entry->memberId == nullptr)
201202
{
202203
entry->clazz = env.FindClass(className);
203204
if (entry->clazz == nullptr)
204205
{
205-
DEBUG_WRITE("Cannot resolve class=%s while calling method %s", className.c_str(), methodName.c_str());
206+
MetadataNode* callerNode = MetadataNode::GetNodeFromHandle(caller);
207+
const string callerClassName = callerNode->GetName();
208+
209+
DEBUG_WRITE("Cannot resolve class: %s while calling method: %s callerClassName: %s", className.c_str(), methodName.c_str(), callerClassName.c_str());
210+
clazz = env.FindClass(callerClassName);
211+
if (clazz == nullptr)
212+
{
213+
DEBUG_WRITE("Cannot resolve caller's class name: %s", callerClassName.c_str());
214+
return;
215+
}
216+
217+
mid = isStatic ?
218+
env.GetStaticMethodID(clazz, methodName, entry->sig) :
219+
env.GetMethodID(clazz, methodName, entry->sig);
220+
221+
if (mid == nullptr)
222+
{
223+
DEBUG_WRITE("Cannot resolve a method %s on caller class: %s", methodName.c_str(), callerClassName.c_str());
224+
return;
225+
}
226+
206227
}
228+
else
229+
{
230+
entry->memberId = isStatic ?
231+
env.GetStaticMethodID(entry->clazz, methodName, entry->sig) :
232+
env.GetMethodID(entry->clazz, methodName, entry->sig);
233+
234+
if (entry->memberId == nullptr)
235+
{
236+
DEBUG_WRITE("Cannot resolve a method %s on class: %s", methodName.c_str(), className.c_str());
237+
return;
238+
}
239+
}
240+
}
207241

208-
entry->memberId = isStatic
209-
? env.GetStaticMethodID(entry->clazz, methodName, entry->sig)
210-
: env.GetMethodID(entry->clazz, methodName, entry->sig);
242+
if (entry->clazz != nullptr)
243+
{
244+
clazz = entry->clazz;
245+
mid = reinterpret_cast<jmethodID>(entry->memberId);
211246
}
212-
clazz = entry->clazz;
213-
mid = reinterpret_cast<jmethodID>(entry->memberId);
247+
214248
sig = entry->sig;
215249
}
216250
else
217251
{
218-
auto mi = MethodCache::ResolveMethodSignature(className, methodName, args, isStatic);
219-
if (mi.mid == nullptr)
252+
DEBUG_WRITE("Resolving method: %s on className %s", methodName.c_str(), className.c_str());
253+
MethodCache::CacheMethodInfo mi;
254+
255+
clazz = env.FindClass(className);
256+
if (clazz != nullptr)
220257
{
221-
DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d", className.c_str(), methodName.c_str(), isStatic, isSuper);
222-
return;
258+
mi = MethodCache::ResolveMethodSignature(className, methodName, args, isStatic);
259+
if (mi.mid == nullptr)
260+
{
261+
DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d", className.c_str(), methodName.c_str(), isStatic, isSuper);
262+
return;
263+
}
223264
}
265+
else
266+
{
267+
MetadataNode* callerNode = MetadataNode::GetNodeFromHandle(caller);
268+
const string callerClassName = callerNode->GetName();
269+
DEBUG_WRITE("Resolving method on caller class: %s.%s on className %s", callerClassName.c_str(), methodName.c_str(), className.c_str());
270+
mi = MethodCache::ResolveMethodSignature(callerClassName, methodName, args, isStatic);
271+
if (mi.mid == nullptr)
272+
{
273+
DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d, callerClass=%s", className.c_str(), methodName.c_str(), isStatic, isSuper, callerClassName.c_str());
274+
return;
275+
}
276+
}
277+
278+
224279
clazz = mi.clazz;
225280
mid = mi.mid;
226281
sig = mi.signature;
227282
}
228283

284+
285+
286+
287+
288+
229289
if (!isStatic)
230290
{
231291
DEBUG_WRITE("CallJavaMethod called %s.%s. Instance id: %d, isSuper=%d", className.c_str(), methodName.c_str(), caller.IsEmpty() ? -42 : caller->GetIdentityHash(), isSuper);

0 commit comments

Comments
 (0)