Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Created November 4, 2013 18:43
Show Gist options
  • Save bjouhier/7307287 to your computer and use it in GitHub Desktop.
Save bjouhier/7307287 to your computer and use it in GitHub Desktop.
exception broken in osx addon
{
"targets": [
{
"conditions": [
["OS=='mac'", {
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES"
}
}],
],
"target_name": "hello",
"sources": [ "hello.cc" ]
}
]
}
#include <node.h>
#include <v8.h>
#include <stdexcept>
using namespace v8;
void fooBar(const Arguments& args) {
Local<Array> loc = Local<Array>::Cast(args[0]);
for (uint32_t i = 0; i < loc->Length(); i++) {
printf("%d\n", i);
throw std::invalid_argument("foo");
}
}
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
try {
fooBar(args);
return scope.Close(String::New("world"));
} catch (const std::invalid_argument& ex) {
return scope.Close(String::New(ex.what()));
}
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
var addon = require('./build/Release/hello');
console.log(addon.hello(["abc", "def"])); // 'world'
@bjouhier
Copy link
Author

bjouhier commented Nov 4, 2013

Compile it:

$ node-gyp configure build

Run it:

$ node hello
0
terminate called after throwing an instance of 'std::invalid_argument'
Abort trap: 6

But it works if the throw is moved outside of the loop.

Versions:

  • osx 10.8.5
  • gcc 4.7.3.0
  • node v0.10.20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment