@ 260528.1

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/4-references-and-handle-scope/function-reference-demo/node-addon-api/. All files

src/4-references-and-handle-scope/function-reference-demo/node-addon-api/binding.gyp

new file mode 100644@@ -0,0 +1,31 @@+{+  "targets": [+    {+      "target_name": "addon",+      "sources": [+        "src/binding.cc",+        "src/native-addon.cc"+      ],+      'cflags!': [ '-fno-exceptions' ],+      'cflags_cc!': [ '-fno-exceptions' ],+      'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],+      'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],+      'conditions': [+        ['OS=="win"', {+          "msvs_settings": {+            "VCCLCompilerTool": {+              "ExceptionHandling": 1+            }+          }+        }],+        ['OS=="mac"', {+          "xcode_settings": {+            "CLANG_CXX_LIBRARY": "libc++",+            'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',+            'MACOSX_DEPLOYMENT_TARGET': '10.7'+          }+        }]+      ]+    }+  ]+}

src/4-references-and-handle-scope/function-reference-demo/node-addon-api/index.js

new file mode 100644@@ -0,0 +1,27 @@+'use strict'++const { NativeAddon } = require('bindings')('addon')++function JSFnRef() {+    console.log('Hi! I\'m a JS function reference!')+}++function JSFn() {+    console.log('Hi! I\'m a JS function!')+}++const ITERATIONS = 5++const addon = new NativeAddon(JSFnRef, JSFn)++for (let i = 0; i < ITERATIONS; i++) {+    addon.tryCallByStoredReference()+}++try {+    addon.tryCallByStoredFunction()+} catch (err) {+    console.error('This code crashes because JSFn is valid only inside the constructor function.')+    console.error('The lifespan of the JSFn function is limited to the execution of the constructor function.')+    console.error('After that, the function stored in JSFn is ready to be garbage collected and it cannot be used anymore.')+}

src/4-references-and-handle-scope/function-reference-demo/node-addon-api/package.json

new file mode 100644@@ -0,0 +1,14 @@+{+  "name": "function-reference-demo",+  "version": "0.0.0",+  "description": "Tutorial example showing the use of function references.",+  "main": "index.js",+  "gypfile": true,+  "scripts": {+    "start": "node index.js"+  },+  "dependencies": {+    "node-addon-api": "*",+    "bindings": "*"+  }+}

src/4-references-and-handle-scope/function-reference-demo/node-addon-api/src/binding.cc

new file mode 100644@@ -0,0 +1,9 @@+#include <napi.h>+#include "native-addon.h"++Napi::Object Init(Napi::Env env, Napi::Object exports) {+  NativeAddon::Init(env, exports);+  return exports;+}++NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)\ No newline at end of file

src/4-references-and-handle-scope/function-reference-demo/node-addon-api/src/native-addon.cc

new file mode 100644@@ -0,0 +1,36 @@+#include "native-addon.h"+#include <iostream>++Napi::FunctionReference NativeAddon::constructor;++Napi::Object NativeAddon::Init(Napi::Env env, Napi::Object exports) {+  Napi::Function func =+      DefineClass(env,+                  "NativeAddon",+                  {InstanceMethod("tryCallByStoredReference",+                                  &NativeAddon::TryCallByStoredReference),+                   InstanceMethod("tryCallByStoredFunction",+                                  &NativeAddon::TryCallByStoredFunction)});++  constructor = Napi::Persistent(func);+  constructor.SuppressDestruct();++  exports.Set("NativeAddon", func);+  return exports;+}++NativeAddon::NativeAddon(const Napi::CallbackInfo& info)+    : Napi::ObjectWrap<NativeAddon>(info) {+  jsFnRef = Napi::Persistent(info[0].As<Napi::Function>());+  jsFn = info[1].As<Napi::Function>();+}++void NativeAddon::TryCallByStoredReference(const Napi::CallbackInfo& info) {+  // Napi::Env env = info.Env();+  jsFnRef.Call({});+}++void NativeAddon::TryCallByStoredFunction(const Napi::CallbackInfo& info) {+  // Napi::Env env = info.Env();+  jsFn.Call({});+}\ No newline at end of file

src/4-references-and-handle-scope/function-reference-demo/node-addon-api/src/native-addon.h

new file mode 100644@@ -0,0 +1,15 @@+#include <napi.h>++class NativeAddon : public Napi::ObjectWrap<NativeAddon> {+ public:+  static Napi::Object Init(Napi::Env env, Napi::Object exports);+  NativeAddon(const Napi::CallbackInfo& info);++ private:+  static Napi::FunctionReference constructor;+  Napi::FunctionReference jsFnRef;+  Napi::Function jsFn;++  void TryCallByStoredReference(const Napi::CallbackInfo& info);+  void TryCallByStoredFunction(const Napi::CallbackInfo& info);+};\ No newline at end of file