Skip to content

Commit

Permalink
Added NanTryCatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Jul 4, 2015
1 parent 4b6404a commit 10f1ca4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
58 changes: 52 additions & 6 deletions nan.h
Expand Up @@ -85,7 +85,7 @@
NAN_DISALLOW_MOVE(CLASS)

#define NODE_0_10_MODULE_VERSION 11
#define NODE_0_12_MODULE_VERSION 12
#define NODE_0_12_MODULE_VERSION 14
#define ATOM_0_21_MODULE_VERSION 41
#define IOJS_1_0_MODULE_VERSION 42
#define IOJS_1_1_MODULE_VERSION 43
Expand Down Expand Up @@ -140,7 +140,6 @@ template<typename T, typename M = NanNonCopyablePersistentTraits<T> >
class NanPersistent;
#endif // NODE_MODULE_VERSION


#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
# include "nan_maybe_43_inl.h" // NOLINT(build/include)
Expand Down Expand Up @@ -413,6 +412,53 @@ class NanEscapableScope {
void operator delete(void *, size_t);
};

//=== TryCatch =================================================================

class NanTryCatch {
v8::TryCatch try_catch_;
friend void NanFatalException(const NanTryCatch&);

public:
#if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
NanTryCatch() : try_catch_(v8::Isolate::GetCurrent()) {}
#endif

NAN_INLINE bool HasCaught() const { return try_catch_.HasCaught(); }

NAN_INLINE bool CanContinue() const { return try_catch_.CanContinue(); }

NAN_INLINE v8::Handle<v8::Value> ReThrow() { return try_catch_.ReThrow(); }

NAN_INLINE v8::Local<v8::Value> Exception() const {
return try_catch_.Exception();
}

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
NAN_INLINE v8::MaybeLocal<v8::Value> StackTrace() const {
return try_catch_.StackTrace(NanGetCurrentContext());
}
#else
NAN_INLINE NanMaybeLocal<v8::Value> StackTrace() const {
return NanMaybeLocal<v8::Value>(try_catch_.StackTrace());
}
#endif

NAN_INLINE v8::Local<v8::Message> Message() const {
return try_catch_.Message();
}

NAN_INLINE void Reset() { try_catch_.Reset(); }

NAN_INLINE void SetVerbose(bool value) { try_catch_.SetVerbose(value); }

NAN_INLINE void SetCaptureMessage(bool value) {
try_catch_.SetCaptureMessage(value);
}
};

//============ =================================================================

/* node 0.12 */
#if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
NAN_INLINE
Expand Down Expand Up @@ -726,8 +772,8 @@ class NanEscapableScope {
v8::Isolate::GetCurrent(), target, method, argc, argv));
}

NAN_INLINE void NanFatalException(const v8::TryCatch& try_catch) {
node::FatalException(v8::Isolate::GetCurrent(), try_catch);
NAN_INLINE void NanFatalException(const NanTryCatch& try_catch) {
node::FatalException(v8::Isolate::GetCurrent(), try_catch.try_catch_);
}

NAN_INLINE v8::Local<v8::Value> NanErrnoException(
Expand Down Expand Up @@ -1060,8 +1106,8 @@ class NanEscapableScope {
return NanNew(node::MakeCallback(target, method, argc, argv));
}

NAN_INLINE void NanFatalException(const v8::TryCatch& try_catch) {
node::FatalException(const_cast<v8::TryCatch&>(try_catch));
NAN_INLINE void NanFatalException(const NanTryCatch& try_catch) {
node::FatalException(const_cast<v8::TryCatch &>(try_catch.try_catch_));
}

NAN_INLINE v8::Local<v8::Value> NanErrnoException(
Expand Down
4 changes: 4 additions & 0 deletions test/binding.gyp
Expand Up @@ -124,4 +124,8 @@
"target_name" : "converters"
, "sources" : [ "cpp/converters.cpp" ]
}
, {
"target_name" : "trycatch"
, "sources" : [ "cpp/trycatch.cpp" ]
}
]}
30 changes: 30 additions & 0 deletions test/cpp/trycatch.cpp
@@ -0,0 +1,30 @@
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2015 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/

#include <nan.h>

NAN_METHOD(TryCatch) {
NanTryCatch try_catch;
v8::Local<v8::Value> v =
NanRunScript(NanNew<NanUnboundScript>(
NanNew("throw 'waaa'").ToLocalChecked()).ToLocalChecked())
.ToLocalChecked();
if (v.IsEmpty()) {
assert(try_catch.HasCaught());
try_catch.ReThrow();
}
}

void Init (v8::Handle<v8::Object> target) {
NanSet(target
, NanNew<v8::String>("r").ToLocalChecked()
, NanNew<v8::FunctionTemplate>(TryCatch)->GetFunction()
);
}

NODE_MODULE(trycatch, Init)
21 changes: 21 additions & 0 deletions test/js/trycatch-test.js
@@ -0,0 +1,21 @@
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2015 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/

const test = require('tap').test
, testRoot = require('path').resolve(__dirname, '..')
, bindings = require('bindings')({ module_root: testRoot, bindings: 'trycatch' });

test('returnemptystring', function (t) {
t.plan(2);
t.type(bindings.r, 'function');
try {
bindings.r();
} catch (err) {
t.equal(err, 'waaa');
}
});

0 comments on commit 10f1ca4

Please sign in to comment.