diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2018-01-20 01:00:34 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-01-21 02:19:46 +0100 |
commit | d9b59def72c718aaad3eefb6bf43f409ccefe4d2 (patch) | |
tree | ec02442a3153500b91e368c873db4d1a3763e567 /test/addons/05_wrapping_c_objects/myobject.cc | |
parent | c6682636be4955e2181c00e7d4868fbf6682a5c5 (diff) | |
download | android-node-v8-d9b59def72c718aaad3eefb6bf43f409ccefe4d2.tar.gz android-node-v8-d9b59def72c718aaad3eefb6bf43f409ccefe4d2.tar.bz2 android-node-v8-d9b59def72c718aaad3eefb6bf43f409ccefe4d2.zip |
build,test: make building addon tests less fragile
* Get rid of recursive `make` when building the node binary. An earlier
commit makes GYP write out rules that we can use for proper dependency
tracking.
* Use module name 'binding' in addons.md and addons-napi/*/binding.gyp.
This massively simplifies the logic for generating the build rules.
* Check in auto-generated add-on tests from `doc/api/addons.md`. The
files change rarely and generating them dynamically causes no end of
race conditions and special-casing during the build.
PR-URL: https://github.com/nodejs/node/pull/17407
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Diffstat (limited to 'test/addons/05_wrapping_c_objects/myobject.cc')
-rw-r--r-- | test/addons/05_wrapping_c_objects/myobject.cc | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/addons/05_wrapping_c_objects/myobject.cc b/test/addons/05_wrapping_c_objects/myobject.cc new file mode 100644 index 0000000000..4383ec44ed --- /dev/null +++ b/test/addons/05_wrapping_c_objects/myobject.cc @@ -0,0 +1,73 @@ +// Auto-generated by `node tools/doc/addon-verify.js` +// myobject.cc +#include "myobject.h" + +namespace demo { + +using v8::Context; +using v8::Function; +using v8::FunctionCallbackInfo; +using v8::FunctionTemplate; +using v8::Isolate; +using v8::Local; +using v8::Number; +using v8::Object; +using v8::Persistent; +using v8::String; +using v8::Value; + +Persistent<Function> MyObject::constructor; + +MyObject::MyObject(double value) : value_(value) { +} + +MyObject::~MyObject() { +} + +void MyObject::Init(Local<Object> exports) { + Isolate* isolate = exports->GetIsolate(); + + // Prepare constructor template + Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); + tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + + // Prototype + NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); + + constructor.Reset(isolate, tpl->GetFunction()); + exports->Set(String::NewFromUtf8(isolate, "MyObject"), + tpl->GetFunction()); +} + +void MyObject::New(const FunctionCallbackInfo<Value>& args) { + Isolate* isolate = args.GetIsolate(); + + if (args.IsConstructCall()) { + // Invoked as constructor: `new MyObject(...)` + double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue(); + MyObject* obj = new MyObject(value); + obj->Wrap(args.This()); + args.GetReturnValue().Set(args.This()); + } else { + // Invoked as plain function `MyObject(...)`, turn into construct call. + const int argc = 1; + Local<Value> argv[argc] = { args[0] }; + Local<Context> context = isolate->GetCurrentContext(); + Local<Function> cons = Local<Function>::New(isolate, constructor); + Local<Object> result = + cons->NewInstance(context, argc, argv).ToLocalChecked(); + args.GetReturnValue().Set(result); + } +} + +void MyObject::PlusOne(const FunctionCallbackInfo<Value>& args) { + Isolate* isolate = args.GetIsolate(); + + MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder()); + obj->value_ += 1; + + args.GetReturnValue().Set(Number::New(isolate, obj->value_)); +} + +} // namespace demo |