Skip to content

Commit

Permalink
implement issue #181
Browse files Browse the repository at this point in the history
  • Loading branch information
mslavchev committed Aug 30, 2015
1 parent 33296ca commit 290a095
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
46 changes: 30 additions & 16 deletions src/jni/JsArgConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ using namespace v8;
using namespace std;
using namespace tns;

JsArgConverter::JsArgConverter(const v8::FunctionCallbackInfo<Value>& args, bool hasImplementationObject, const string& methodSignature) :
m_args(nullptr), m_methodSignature(methodSignature), m_isValid(true), m_error(Error())
JsArgConverter::JsArgConverter(const v8::FunctionCallbackInfo<Value>& args, bool hasImplementationObject, const string& methodSignature, MetadataEntry *entry) :
m_args(nullptr), m_methodSignature(methodSignature), m_isValid(true), m_error(Error()), m_tokens(nullptr)
{
m_argsLen = !hasImplementationObject ? args.Length() : args.Length() - 1;

Expand All @@ -26,8 +26,21 @@ JsArgConverter::JsArgConverter(const v8::FunctionCallbackInfo<Value>& args, bool
m_args = new jvalue[m_argsLen];
memset(m_args, 0, m_argsLen * sizeof(jvalue));

JniSignatureParser parser(m_methodSignature);
m_tokens = parser.Parse();
if (entry->isResolved)
{
if (entry->parsedSig.empty())
{
JniSignatureParser parser(m_methodSignature);
entry->parsedSig = parser.Parse();
}
m_tokens = &entry->parsedSig;
}
else
{
JniSignatureParser parser(m_methodSignature);
m_tokens2 = parser.Parse();
m_tokens = &m_tokens2;
}

for (int i = 0; i < m_argsLen; i++)
{
Expand All @@ -46,9 +59,10 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)
JEnv env;

bool success = false;
stringstream s;

string typeSignature = m_tokens[index];
char buff[1024];

const auto& typeSignature = m_tokens->at(index);

if (arg.IsEmpty())
{
Expand All @@ -65,7 +79,7 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)

if (!success)
{
s << "Cannot convert array to " << typeSignature << " at index " << index;
sprintf(buff, "Cannot convert array to %s at index %d", typeSignature.c_str(), index);
}
}
else if (arg->IsNumber() || arg->IsNumberObject())
Expand All @@ -74,7 +88,7 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)

if (!success)
{
s << "Cannot convert number to " << typeSignature << " at index " << index;
sprintf(buff, "Cannot convert number to %s at index %d", typeSignature.c_str(), index);
}
}
else if (arg->IsBoolean() || arg->IsBooleanObject())
Expand All @@ -83,7 +97,7 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)

if (!success)
{
s << "Cannot convert boolean to " << typeSignature << " at index " << index;
sprintf(buff, "Cannot convert boolean to %s at index %d", typeSignature.c_str(), index);
}
}
else if (arg->IsString() || arg->IsStringObject())
Expand All @@ -92,7 +106,7 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)

if (!success)
{
s << "Cannot convert string to " << typeSignature << " at index " << index;
sprintf(buff, "Cannot convert string to %s at index %d", typeSignature.c_str(), index);
}
}
else if (arg->IsObject())
Expand Down Expand Up @@ -202,7 +216,7 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)

if (!success)
{
s << "Cannot convert object to " << typeSignature << " at index " << index;
sprintf(buff, "Cannot convert object to %s at index %d", typeSignature.c_str(), index);
}
}
else if (arg->IsUndefined() || arg->IsNull())
Expand All @@ -219,7 +233,7 @@ bool JsArgConverter::ConvertArg(const Handle<Value>& arg, int index)
if (!success)
{
m_error.index = index;
m_error.msg = s.str();
m_error.msg = string(buff);
}

return success;
Expand Down Expand Up @@ -248,7 +262,7 @@ bool JsArgConverter::ConvertJavaScriptNumber(JEnv& env, const Handle<Value>& jsV

jvalue value = { 0 };

string typeSignature = m_tokens[index];
const auto& typeSignature = m_tokens->at(index);

const char typePrefix = typeSignature[0];

Expand Down Expand Up @@ -304,7 +318,7 @@ bool JsArgConverter::ConvertJavaScriptBoolean(JEnv& env, const Handle<Value>& js
{
bool success;

string typeSignature = m_tokens[index];
const auto& typeSignature = m_tokens->at(index);

if (typeSignature == "Z")
{
Expand Down Expand Up @@ -354,7 +368,7 @@ bool JsArgConverter::ConvertJavaScriptArray(JEnv& env, const Handle<Array>& jsAr

jsize arrLength = jsArr->Length();

string arraySignature = m_tokens[index];
const auto& arraySignature = m_tokens->at(index);

string elementType = arraySignature.substr(1);

Expand Down Expand Up @@ -469,7 +483,7 @@ bool JsArgConverter::ConvertFromCastFunctionObject(T value, int index)
{
bool success = false;

string typeSignature = m_tokens[index];
const auto& typeSignature = m_tokens->at(index);

const char typeSignaturePrefix = typeSignature[0];

Expand Down
6 changes: 4 additions & 2 deletions src/jni/JsArgConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <vector>
#include "JEnv.h"
#include "v8.h"
#include "MetadataEntry.h"

namespace tns
{
class JsArgConverter
{
public:
JsArgConverter(const v8::FunctionCallbackInfo<v8::Value>& args, bool hasImplementationObject, const std::string& methodSignature);
JsArgConverter(const v8::FunctionCallbackInfo<v8::Value>& args, bool hasImplementationObject, const std::string& methodSignature, MetadataEntry *entry);

~JsArgConverter();

Expand Down Expand Up @@ -60,7 +61,8 @@ namespace tns

std::string m_methodSignature;

std::vector<std::string> m_tokens;
std::vector<std::string> *m_tokens;
std::vector<std::string> m_tokens2;

std::vector<int> m_storedObjects;

Expand Down
1 change: 1 addition & 0 deletions src/jni/MetadataEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace tns
bool isResolved;
void *memberId;
jclass clazz;
std::vector<std::string> parsedSig;
};
}

Expand Down
6 changes: 1 addition & 5 deletions src/jni/NativeScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,6 @@ void NativeScriptRuntime::CallJavaMethod(const Handle<Object>& caller, const str
}






if (!isStatic)
{
DEBUG_WRITE("CallJavaMethod called %s.%s. Instance id: %d, isSuper=%d", className.c_str(), methodName.c_str(), caller.IsEmpty() ? -42 : caller->GetIdentityHash(), isSuper);
Expand All @@ -295,7 +291,7 @@ void NativeScriptRuntime::CallJavaMethod(const Handle<Object>& caller, const str
DEBUG_WRITE("CallJavaMethod called %s.%s. static method", className.c_str(), methodName.c_str());
}

JsArgConverter argConverter(args, false, sig);
JsArgConverter argConverter(args, false, sig, entry);

if (!argConverter.IsValid())
{
Expand Down

0 comments on commit 290a095

Please sign in to comment.