Skip to content

Commit

Permalink
refactor: move all examples to src directory
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Oct 13, 2023
1 parent 23fc82c commit d2b2da5
Show file tree
Hide file tree
Showing 272 changed files with 50 additions and 198 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Napi::Value DoHeavyMath(const Napi::CallbackInfo& info) {
return env.Undefined();
}
uint32_t num_1 = info[0].As<Napi::Number>().Uint32Value();

if (!info[1].IsNumber()) {
Napi::TypeError::New(env, "num2 must be a number")
.ThrowAsJavaScriptException();
return env.Undefined();
}
uint32_t num_2 = info[1].As<Napi::Number>().Uint32Value();

DoHeavyMathWorker* worker = new DoHeavyMathWorker(env, num_1, num_2);
worker->Queue();
return worker->GetPromise();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
class HelloAddon : public Napi::Addon<HelloAddon> {
public:
HelloAddon(Napi::Env env, Napi::Object exports) {
DefineAddon(exports, {
InstanceMethod("hello", &HelloAddon::Hello, napi_enumerable)
});
DefineAddon(exports,
{InstanceMethod("hello", &HelloAddon::Hello, napi_enumerable)});
}

private:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ void MyObject::Init(Napi::Env env, Napi::Object exports) {

Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor); //NOTE: this assumes only 1 class is exported
//for multiple exported classes, need a struct or other mechanism
env.SetInstanceData(constructor); // NOTE: this assumes only 1 class is
// exported for multiple exported classes,
// need a struct or other mechanism

exports.Set("MyObject", func);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class NamedInterceptor : public ObjectWrap {
char buf[256];

public:
NamedInterceptor() { std::strncpy(this->buf, "foo", sizeof (this->buf)); }
NamedInterceptor() { std::strncpy(this->buf, "foo", sizeof(this->buf)); }
static NAN_MODULE_INIT(Init);
static v8::Local<v8::Value> NewInstance ();
static v8::Local<v8::Value> NewInstance();
static NAN_METHOD(New);

static NAN_PROPERTY_GETTER(PropertyGetter);
Expand All @@ -35,33 +35,32 @@ NAN_METHOD(CreateNew) {

NAN_MODULE_INIT(NamedInterceptor::Init) {
v8::Local<v8::FunctionTemplate> tpl =
Nan::New<v8::FunctionTemplate>(NamedInterceptor::New);
Nan::New<v8::FunctionTemplate>(NamedInterceptor::New);
namedinterceptors_constructor.Reset(tpl);
tpl->SetClassName(Nan::New("NamedInterceptor").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
v8::Local<v8::ObjectTemplate> inst = tpl->InstanceTemplate();

SetNamedPropertyHandler(
inst
, NamedInterceptor::PropertyGetter
, NamedInterceptor::PropertySetter
, NamedInterceptor::PropertyQuery
, NamedInterceptor::PropertyDeleter
, NamedInterceptor::PropertyEnumerator);
SetNamedPropertyHandler(inst,
NamedInterceptor::PropertyGetter,
NamedInterceptor::PropertySetter,
NamedInterceptor::PropertyQuery,
NamedInterceptor::PropertyDeleter,
NamedInterceptor::PropertyEnumerator);

v8::Local<v8::Function> createnew =
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(CreateNew))
.ToLocalChecked();
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(CreateNew))
.ToLocalChecked();
Set(target, Nan::New("create").ToLocalChecked(), createnew);
}

v8::Local<v8::Value> NamedInterceptor::NewInstance () {
v8::Local<v8::Value> NamedInterceptor::NewInstance() {
EscapableHandleScope scope;
v8::Local<v8::FunctionTemplate> constructorHandle =
Nan::New(namedinterceptors_constructor);
v8::Local<v8::Object> instance =
Nan::NewInstance(GetFunction(constructorHandle).ToLocalChecked())
.ToLocalChecked();
Nan::NewInstance(GetFunction(constructorHandle).ToLocalChecked())
.ToLocalChecked();
return scope.Escape(instance);
}

Expand All @@ -71,10 +70,9 @@ NAN_METHOD(NamedInterceptor::New) {
info.GetReturnValue().Set(info.This());
}


NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) {
NamedInterceptor* interceptor =
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
if (!std::strcmp(*Nan::Utf8String(property), "prop")) {
info.GetReturnValue().Set(Nan::New(interceptor->buf).ToLocalChecked());
} else {
Expand All @@ -84,12 +82,10 @@ NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) {

NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) {
NamedInterceptor* interceptor =
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
if (!std::strcmp(*Nan::Utf8String(property), "prop")) {
std::strncpy(
interceptor->buf
, *Nan::Utf8String(value)
, sizeof (interceptor->buf));
interceptor->buf, *Nan::Utf8String(value), sizeof(interceptor->buf));
info.GetReturnValue().Set(info.This());
} else {
info.GetReturnValue().Set(info.This());
Expand All @@ -104,8 +100,8 @@ NAN_PROPERTY_ENUMERATOR(NamedInterceptor::PropertyEnumerator) {

NAN_PROPERTY_DELETER(NamedInterceptor::PropertyDeleter) {
NamedInterceptor* interceptor =
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf));
ObjectWrap::Unwrap<NamedInterceptor>(info.Holder());
std::strncpy(interceptor->buf, "goober", sizeof(interceptor->buf));
info.GetReturnValue().Set(True());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <memory>
#include <cstring>
#include <memory>
#include <string>
#include "proxy-template.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ static Napi::Value CreateByteArray(const Napi::CallbackInfo& info) {
// unique_ptr ownership.
nativeArray.release();

Napi::Uint8Array byteArray = Napi::Uint8Array::New(info.Env(), arrayLength, arrayBuffer, 0);

Napi::Uint8Array byteArray =
Napi::Uint8Array::New(info.Env(), arrayLength, arrayBuffer, 0);

return byteArray;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include <chrono>
#include <napi.h>
#include <chrono>
#include <thread>

using namespace Napi;

using Context = Reference<Value>;
using DataType = int;
void CallJs(Napi::Env env, Function callback, Context *context, DataType *data);
void CallJs(Napi::Env env, Function callback, Context* context, DataType* data);
using TSFN = TypedThreadSafeFunction<Context, DataType, CallJs>;
using FinalizerDataType = void;

std::thread nativeThread;
TSFN tsfn;

Value Start(const CallbackInfo &info) {
Value Start(const CallbackInfo& info) {
Napi::Env env = info.Env();

if (info.Length() < 2) {
Expand All @@ -28,18 +28,19 @@ Value Start(const CallbackInfo &info) {

// Create a new context set to the the receiver (ie, `this`) of the function
// call
Context *context = new Reference<Value>(Persistent(info.This()));
Context* context = new Reference<Value>(Persistent(info.This()));

// Create a ThreadSafeFunction
tsfn = TSFN::New(
env,
info[0].As<Function>(), // JavaScript function called asynchronously
"Resource Name", // Name
0, // Unlimited queue
1, // Only one thread will use this initially
info[0].As<Function>(), // JavaScript function called asynchronously
"Resource Name", // Name
0, // Unlimited queue
1, // Only one thread will use this initially
context,
[](Napi::Env, FinalizerDataType *,
Context *ctx) { // Finalizer used to clean threads up
[](Napi::Env,
FinalizerDataType*,
Context* ctx) { // Finalizer used to clean threads up
nativeThread.join();
delete ctx;
});
Expand All @@ -48,7 +49,7 @@ Value Start(const CallbackInfo &info) {
nativeThread = std::thread([count] {
for (int i = 0; i < count; i++) {
// Create new data
int *value = new int(clock());
int* value = new int(clock());

// Perform a blocking call
napi_status status = tsfn.BlockingCall(value);
Expand All @@ -69,13 +70,15 @@ Value Start(const CallbackInfo &info) {

// Transform native data into JS data, passing it to the provided
// `callback` -- the TSFN's JavaScript function.
void CallJs(Napi::Env env, Function callback, Context *context,
DataType *data) {
void CallJs(Napi::Env env,
Function callback,
Context* context,
DataType* data) {
// Is the JavaScript environment still available to call into, eg. the TSFN is
// not aborted
if (env != nullptr) {
// On Node-API 5+, the `callback` parameter is optional; however, this example
// does ensure a callback is provided.
// On Node-API 5+, the `callback` parameter is optional; however, this
// example does ensure a callback is provided.
if (callback != nullptr) {
callback.Call(context->Value(), {Number::New(env, *data)});
}
Expand Down
3 changes: 2 additions & 1 deletion test_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chalk = require("chalk");
const semver = require("semver");

const excludeFolder = ["node_modules", "website"];
const examplesFolder = path.join(__dirname, "src");

function getAllExamples(pathToCheck) {
const directoriesToTest = [];
Expand All @@ -28,7 +29,7 @@ const passed = [];
const failedInstalls = [];
const noTest = [];
const failedTests = [];
for (directoryToTest of getAllExamples(path.join(__dirname))) {
for (directoryToTest of getAllExamples(examplesFolder)) {
console.log(chalk.green(`testing: ${directoryToTest}`));
const pkgJson = require(path.join(directoryToTest, "package.json"));
if (pkgJson.engines && pkgJson.engines.node) {
Expand Down
33 changes: 0 additions & 33 deletions tooling/build_with_cmake/README.md

This file was deleted.

13 changes: 0 additions & 13 deletions tooling/build_with_cmake/napi/CMakeLists.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tooling/build_with_cmake/napi/hello.c

This file was deleted.

3 changes: 0 additions & 3 deletions tooling/build_with_cmake/napi/hello.js

This file was deleted.

14 changes: 0 additions & 14 deletions tooling/build_with_cmake/napi/package.json

This file was deleted.

22 changes: 0 additions & 22 deletions tooling/build_with_cmake/node-addon-api/CMakeLists.txt

This file was deleted.

23 changes: 0 additions & 23 deletions tooling/build_with_cmake/node-addon-api/hello.cc

This file was deleted.

3 changes: 0 additions & 3 deletions tooling/build_with_cmake/node-addon-api/hello.js

This file was deleted.

Loading

0 comments on commit d2b2da5

Please sign in to comment.