[feat] nodejs bindings

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-20 13:09:35 +02:00
parent 07a61af8e5
commit c665c1c2fb
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
8 changed files with 34 additions and 42 deletions

View file

@ -3,10 +3,10 @@
{ {
'target_name': 'dal', 'target_name': 'dal',
'sources': [ 'sources': [
'cgo/dal.h', 'binding/dal.h',
'cgo/dal.cc' 'binding/dal.cc'
], ],
'libraries': [ '../cgo/dal.a' ], 'libraries': [ '../binding/dal.a' ],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
"include_dirs": [ "include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")" "<!@(node -p \"require('node-addon-api').include\")"

View file

@ -1,17 +1,16 @@
#include <napi.h> #include <napi.h>
#include <stdio.h>
#include "dal.h" #include "dal.h"
static void _InitSQLite(const Napi::CallbackInfo& args) { static void _InitSQLite(const Napi::CallbackInfo& args) {
Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>(); Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>();
char * charstr = reinterpret_cast<char *>(buf.Data()); GoString charstr = {reinterpret_cast<char *>(buf.Data()), long(buf.Length())};
InitSQLite(charstr); InitSQLite(charstr);
} }
static Napi::Value Handle(const Napi::CallbackInfo& args) { static Napi::Value Handle(const Napi::CallbackInfo& args) {
Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>(); Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>();
char * charstr = reinterpret_cast<char *>(buf.Data()); GoSlice input = {reinterpret_cast<void *>(buf.Data()), long(buf.Length()), long(buf.Length())};
GoSlice result = HandleQuery(charstr); GoSlice result = HandleQuery(input);
return Napi::Buffer<char>::Copy(args.Env(), reinterpret_cast<char *>(result.data), result.len); return Napi::Buffer<char>::Copy(args.Env(), reinterpret_cast<char *>(result.data), result.len);
} }

24
binding/dal.go Normal file
View file

@ -0,0 +1,24 @@
package main
import "C"
import (
"strings"
_ "github.com/mattn/go-sqlite3"
"l12.xyz/dal/facade"
)
//export InitSQLite
func InitSQLite(pragmas string) {
pragmasArray := strings.Split(pragmas, ";")
facade.InitSQLite(pragmasArray)
}
//export HandleQuery
func HandleQuery(input []byte) []byte {
var out []byte
facade.HandleQuery(&input, &out)
return out
}
func main() {}

View file

@ -19,10 +19,6 @@ typedef struct { const char *p; ptrdiff_t n; } _GoString_;
/* Start of preamble from import "C" comments. */ /* Start of preamble from import "C" comments. */
#line 3 "dal.go"
#include <stdlib.h>
#line 1 "cgo-generated-wrapper"
/* End of preamble from import "C" comments. */ /* End of preamble from import "C" comments. */
@ -78,8 +74,8 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern "C" { extern "C" {
#endif #endif
extern void InitSQLite(char* pragmas); extern void InitSQLite(GoString pragmas);
extern GoSlice HandleQuery(char* input); extern GoSlice HandleQuery(GoSlice input);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -1,4 +1,4 @@
module srv module binding
go 1.22.6 go 1.22.6

View file

@ -1,27 +0,0 @@
package main
// #include <stdlib.h>
import "C"
import (
"strings"
_ "github.com/mattn/go-sqlite3"
"l12.xyz/dal/facade"
)
//export InitSQLite
func InitSQLite(pragmas *C.char) {
str := C.GoString(pragmas)
pragmasArray := strings.Split(str, ";")
facade.InitSQLite(pragmasArray)
}
//export HandleQuery
func HandleQuery(input *C.char) []byte {
var in, out []byte
in = []byte(C.GoString(input))
facade.HandleQuery(&in, &out)
return out
}
func main() {}

View file

@ -19,7 +19,7 @@
"test:dal": "bun test dal/__test__", "test:dal": "bun test dal/__test__",
"test:serve": "cd dal/__test__/srv && go run main.go", "test:serve": "cd dal/__test__/srv && go run main.go",
"fmt": "prettier --write .", "fmt": "prettier --write .",
"cgo:prepare":"cd cgo && go build -buildmode=c-archive -o dal.a ./dal.go", "preinstall":"cd binding && go build -buildmode=c-archive -o dal.a ./dal.go",
"cgo:build": "node-gyp configure build" "cgo:build": "node-gyp configure build"
} }
} }