231103.1 · 7f4a9ed91bbecb84e39f4bc8030247ce5116ddf8
Reorganize examples into categorized folders (#341)
* refactor: create directory structure to organize exmaples
* test: update test_all to work with new directory structure
* fix: bug in test_all if no package.json scripts
* test: cleanup output of test_all
* fix: remove default test script that throws from thread_safe_function_with_object_wrap
* fix: remove build_with_cmake from .gitignore
* fix: swith to ' from " and remove subdir from getAllTests call
* test: add extra test cases for npm start and just running the pkgJson.main with node
* refactor: move all examples to src directory
* refactor: add numbers to categories so they are ordered in the src folder
* refactor: simplify getAllExamples in test_all.js
* docs: add directory structure to README.md
* docs: update directory structure to README.md
* docs: update directory structure to README.md
* docs: update directory structure to README.md
* docs: update directory structure example in readme
* add back build_with_cmake
---------
Co-authored-by: legendecas <legendecas@gmail.com>
reading declarations
Changes to src/1-getting-started/6_object_wrap/node-addon-api/myobject.cc. All files
src/1-getting-started/6_object_wrap/node-addon-api/myobject.cc
@@ -0,0 +1,58 @@+#include "myobject.h"++Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {+ Napi::Function func =+ DefineClass(env,+ "MyObject",+ {InstanceMethod("plusOne", &MyObject::PlusOne),+ InstanceMethod("value", &MyObject::GetValue),+ InstanceMethod("multiply", &MyObject::Multiply)});++ Napi::FunctionReference* constructor = new Napi::FunctionReference();+ *constructor = Napi::Persistent(func);+ env.SetInstanceData(constructor);++ exports.Set("MyObject", func);+ return exports;+}++MyObject::MyObject(const Napi::CallbackInfo& info)+ : Napi::ObjectWrap<MyObject>(info) {+ Napi::Env env = info.Env();++ int length = info.Length();++ if (length <= 0 || !info[0].IsNumber()) {+ Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();+ return;+ }++ Napi::Number value = info[0].As<Napi::Number>();+ this->value_ = value.DoubleValue();+}++Napi::Value MyObject::GetValue(const Napi::CallbackInfo& info) {+ double num = this->value_;++ return Napi::Number::New(info.Env(), num);+}++Napi::Value MyObject::PlusOne(const Napi::CallbackInfo& info) {+ this->value_ = this->value_ + 1;++ return MyObject::GetValue(info);+}++Napi::Value MyObject::Multiply(const Napi::CallbackInfo& info) {+ Napi::Number multiple;+ if (info.Length() <= 0 || !info[0].IsNumber()) {+ multiple = Napi::Number::New(info.Env(), 1);+ } else {+ multiple = info[0].As<Napi::Number>();+ }++ Napi::Object obj = info.Env().GetInstanceData<Napi::FunctionReference>()->New(+ {Napi::Number::New(info.Env(), this->value_ * multiple.DoubleValue())});++ return obj;+}