Commit d63cd8ee authored by jan.koester's avatar jan.koester
Browse files

nodesjs included

parent 3007cce8
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for CMakeLists.txt: 1 added line, 1 removed line.
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.18)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra --warn-no-unused-parameter -Wstack-usage=4096")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wextra --warn-no-unused-parameter -Wstack-usage=4096 --std=c++20")

project(authdb)

+10 −0
Changes for bindings/CMakeLists.txt: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
set(NODE_MODULE_MINIMUM_ABI 83)

include("${CMAKE_SOURCE_DIR}/cmake/nodejs.cmake")

add_node_module(authdb-nds)
@@ -5,3 +7,11 @@ add_node_module(authdb-nds)
target_sources(authdb-nds INTERFACE
    ${CMAKE_CURRENT_SOURCE_DIR}/nodesjs.cpp
)

target_include_directories(
    authdb-nds BEFORE INTERFACE "${CMAKE_SOURCE_DIR}/client"
)

target_compile_options(authdb-nds INTERFACE -z,defs)

target_link_libraries(authdb-nds INTERFACE client)
+113 −0
Changes for bindings/nodesjs.cpp: 113 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <client.h>

#include <node.h>
#include <node_object_wrap.h>

namespace authdb {

using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Global;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

class AuthDBJS : public node::ObjectWrap , Client{
public:
    static void Init(v8::Isolate* isolate){
        // Prepare constructor template
        Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(isolate, New);
        tpl->SetClassName(String::NewFromUtf8(isolate, "AuthDBJS").ToLocalChecked());
        tpl->InstanceTemplate()->SetInternalFieldCount(1);

        Local<Context> context = isolate->GetCurrentContext();
        constructor.Reset(isolate, tpl->GetFunction(context).ToLocalChecked());

        node::AddEnvironmentCleanupHook(isolate, [](void*) {
            constructor.Reset();
        }, nullptr);
    }

    static void NewInstance(const v8::FunctionCallbackInfo<v8::Value>& args){
        Isolate* isolate = args.GetIsolate();

        const unsigned argc = 1;
        Local<Value> argv[argc] = { args[0] };
        Local<v8::Function> cons = Local<v8::Function>::New(isolate, constructor);
        Local<Context> context = isolate->GetCurrentContext();
        Local<Object> instance = cons->NewInstance(context, argc, argv).ToLocalChecked();
        args.GetReturnValue().Set(instance);
    };

private:
    explicit AuthDBJS(double value = 0);
    ~AuthDBJS();

    static void New(const v8::FunctionCallbackInfo<v8::Value>& args){
        Isolate* isolate = args.GetIsolate();
        Local<Context> context = isolate->GetCurrentContext();

        if (args.IsConstructCall()) {
            // Invoked as constructor: `new MyObject(...)`
            double value = args[0]->IsUndefined() ?
            0 : args[0]->NumberValue(context).FromMaybe(0);
            AuthDBJS* obj = new AuthDBJS(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<v8::Function> cons = Local<v8::Function>::New(isolate, constructor);
            Local<Object> instance =
            cons->NewInstance(context, argc, argv).ToLocalChecked();
            args.GetReturnValue().Set(instance);
        }
    }

    static v8::Global<v8::Function> constructor;
    double value_;
};

void CreateObject(const FunctionCallbackInfo<Value>& args) {
    AuthDBJS::NewInstance(args);
}

void InitAll(Local<Object> exports) {
  AuthDBJS::Init(exports->GetIsolate());

  NODE_SET_METHOD(exports, "createObject", CreateObject);
}

NODE_MODULE(authdb, InitAll)
};