Skip to content

Commit

Permalink
replaced ASSERT_FAIL with c++ exceptions: fix for #221
Browse files Browse the repository at this point in the history
  • Loading branch information
Plamen5kov committed Nov 27, 2015
1 parent cd51a51 commit 8cf9094
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/jni/FieldAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "NativeScriptAssert.h"
#include "Util.h"
#include "V8GlobalHelpers.h"
#include "NativeScriptException.h"
#include <assert.h>
#include <sstream>

using namespace v8;
using namespace std;
Expand Down Expand Up @@ -186,9 +188,9 @@ Local<Value> FieldAccessor::GetJavaField(const Local<Object>& target, FieldCallb
}
default:
{
// TODO:
ASSERT_FAIL("Unknown field type");
break;
stringstream ss;
ss << "(InternalError): in FieldAccessor::GetJavaField: Unknown field type: '" << fieldTypeName[0] << "'";
throw NativeScriptException(ss.str());
}
}
}
Expand Down Expand Up @@ -397,9 +399,9 @@ void FieldAccessor::SetJavaField(const Local<Object>& target, const Local<Value>
}
default:
{
// TODO:
ASSERT_FAIL("Unknown field type");
break;
stringstream ss;
ss << "(InternalError): in FieldAccessor::SetJavaField: Unknown field type: '" << fieldTypeName[0] << "'";
throw NativeScriptException(ss.str());
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/jni/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ Local<Object> MetadataNode::CreateWrapper(Isolate *isolate)
}
else
{
ASSERT_FAIL("Can't create proxy for this type=%d", nodeType);
stringstream ss;
ss << "(InternalError): Can't create proxy for this type=" << static_cast<int>(nodeType);
throw NativeScriptException(ss.str());
}

return handle_scope.Escape(obj);
Expand Down Expand Up @@ -272,7 +274,6 @@ void MetadataNode::FieldAccessorGetterCallback(Local<String> property, const Pro
try {
auto thiz = info.This();
auto fieldCallbackData = reinterpret_cast<FieldCallbackData*>(info.Data().As<External>()->Value());

auto value = NativeScriptRuntime::GetJavaField(thiz, fieldCallbackData);
info.GetReturnValue().Set(value);
} catch (NativeScriptException& e) {
Expand Down Expand Up @@ -816,7 +817,9 @@ void MetadataNode::InterfaceConstructorCallback(const v8::FunctionCallbackInfo<v
{
if (!extendLocationFound)
{
ASSERT_FAIL("Invalid extend() call. No name specified for extend. Location: %s", extendLocation.c_str());
stringstream ss;
ss << "(InternalError): Invalid extend() call. No name specified for extend. Location: " << extendLocation.c_str();
throw NativeScriptException(ss.str());
}

if (!info[0]->IsObject())
Expand Down
2 changes: 0 additions & 2 deletions src/jni/NativeScriptAssert.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace tns
#define DEBUG_WRITE(fmt, args...) if (tns::LogEnabled) __android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", fmt, ##args)
#define DEBUG_WRITE_FORCE(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", fmt, ##args)
#define DEBUG_WRITE_FATAL(fmt, args...) __android_log_print(ANDROID_LOG_FATAL, "TNS.Native", fmt, ##args)
#define ASSERT(cond) ((cond)?(void)0:__android_log_assert("##cond", "TNS.Native", ""))
#define ASSERT_FAIL(fmt, args...) (__android_log_assert(fmt, "TNS.Native", fmt, ##args))
}

#endif /* NATIVESCRIPTASSERT_H_ */
1 change: 0 additions & 1 deletion src/jni/NativeScriptException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ NativeScriptException::NativeScriptException(JEnv& env)
NativeScriptException::NativeScriptException(const std::string& message)
:m_javascriptException(Local<Value>()), m_message(message)
{
DEBUG_WRITE("NativeScriptException-1");
}

NativeScriptException::NativeScriptException(const v8::Local<v8::Value>& jsException, const std::string& message)
Expand Down
7 changes: 4 additions & 3 deletions src/jni/NativeScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,10 @@ void NativeScriptRuntime::DisableVerboseLoggingMethodCallback(const v8::Function
void NativeScriptRuntime::ExitMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
{
try {
auto msg = ConvertToString(args[0].As<String>());
ASSERT_FAIL("%s", msg.c_str());
exit(-1);
auto msg = ConvertToString(args[0].As<String>());
stringstream ss;
ss << "You crashed the app on purpose with: __exit('" << msg << "')";
throw NativeScriptException(ss.str());
} catch (NativeScriptException& e) {
e.ReThrowToV8();
}
Expand Down
6 changes: 5 additions & 1 deletion src/jni/ObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "NativeScriptException.h"
#include <assert.h>
#include <algorithm>
#include <sstream>

using namespace v8;
using namespace std;
Expand Down Expand Up @@ -357,9 +358,12 @@ void ObjectManager::ReleaseJSInstance(Persistent<Object> *po, JSInstanceInfo *js
int javaObjectID = jsInstanceInfo->JavaObjectID;

auto it = idToObject.find(javaObjectID);

if (it == idToObject.end())
{
ASSERT_FAIL("js object with id:%d not found", javaObjectID);
stringstream ss;
ss << "(InternalError): Js object with id: " << javaObjectID << " not found";
throw NativeScriptException(ss.str());
}

assert(po == it->second);
Expand Down

0 comments on commit 8cf9094

Please sign in to comment.