diff options
Diffstat (limited to 'test/addons/01_function_arguments/binding.cc')
-rw-r--r-- | test/addons/01_function_arguments/binding.cc | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/addons/01_function_arguments/binding.cc b/test/addons/01_function_arguments/binding.cc new file mode 100644 index 0000000000..1379699453 --- /dev/null +++ b/test/addons/01_function_arguments/binding.cc @@ -0,0 +1,52 @@ +// Auto-generated by `node tools/doc/addon-verify.js` +// binding.cc +#include <node.h> + +namespace demo { + +using v8::Exception; +using v8::FunctionCallbackInfo; +using v8::Isolate; +using v8::Local; +using v8::Number; +using v8::Object; +using v8::String; +using v8::Value; + +// This is the implementation of the "add" method +// Input arguments are passed using the +// const FunctionCallbackInfo<Value>& args struct +void Add(const FunctionCallbackInfo<Value>& args) { + Isolate* isolate = args.GetIsolate(); + + // Check the number of arguments passed. + if (args.Length() < 2) { + // Throw an Error that is passed back to JavaScript + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8(isolate, "Wrong number of arguments"))); + return; + } + + // Check the argument types + if (!args[0]->IsNumber() || !args[1]->IsNumber()) { + isolate->ThrowException(Exception::TypeError( + String::NewFromUtf8(isolate, "Wrong arguments"))); + return; + } + + // Perform the operation + double value = args[0]->NumberValue() + args[1]->NumberValue(); + Local<Number> num = Number::New(isolate, value); + + // Set the return value (using the passed in + // FunctionCallbackInfo<Value>&) + args.GetReturnValue().Set(num); +} + +void Init(Local<Object> exports) { + NODE_SET_METHOD(exports, "add", Add); +} + +NODE_MODULE(NODE_GYP_MODULE_NAME, Init) + +} // namespace demo |