Skip to content

Instantly share code, notes, and snippets.

@stephenmathieson
Created March 10, 2015 14:27
Show Gist options
  • Save stephenmathieson/6593406b5d791db4345b to your computer and use it in GitHub Desktop.
Save stephenmathieson/6593406b5d791db4345b to your computer and use it in GitHub Desktop.
NanThrowError & iojs 1.5.1
#include <iostream>
#include <nan.h>
using namespace v8;
NAN_METHOD(Wut);
void
Init(Handle<Object> exports){
exports->Set(
NanNew<String>("wut")
, NanNew<FunctionTemplate>(Wut)->GetFunction()
);
}
NODE_MODULE(wut, Init);
NAN_METHOD(Wut){
NanScope();
Local<String> foo;
Local<Object> options;
if (args.Length()) {
options = Local<Object>::Cast(args[0]);
if (options->Has(NanNew("foo"))) {
std::cout << "has 'foo'" << std::endl;
Local<Value> foo_ = options->Get(NanNew("foo"));
if (!foo_->IsString()) {
std::cout << "'foo' is not a string" << std::endl;
NanThrowError("foo must be a string");
std::cout << "we didnt throw or return???" << std::endl;
}
std::cout << "'foo' is a string" << std::endl;
foo = foo_->ToString();
}
}
NanReturnValue(foo);
}
{
'targets': [
{
'target_name': 'wut',
'include_dirs': [
"<!(node -e \"require('nan')\")",
],
'sources': ['binding.cc'],
},
],
}
BIN := node_modules/.bin
GYP := $(BIN)/node-gyp
SRC = binding.cc
build: $(SRC) node_modules
$(GYP) rebuild
node_modules: package.json
@npm install
@touch $@
clean: node_modules
$(GYP) clean
test: build
node test
.PHONY: clean test
{
"name": "wut",
"dependencies": {
"bindings": "^1.2.1",
"nan": "^1.7.0",
"node-gyp": "^1.0.3"
},
"main": "index",
"scripts": {
"install": "node-gyp rebuild"
},
"gypfile": true
}
require('bindings')('wut').wut({
foo: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment