[feat] nodejs bindings
Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
parent
07a61af8e5
commit
c665c1c2fb
|
@ -3,10 +3,10 @@
|
|||
{
|
||||
'target_name': 'dal',
|
||||
'sources': [
|
||||
'cgo/dal.h',
|
||||
'cgo/dal.cc'
|
||||
'binding/dal.h',
|
||||
'binding/dal.cc'
|
||||
],
|
||||
'libraries': [ '../cgo/dal.a' ],
|
||||
'libraries': [ '../binding/dal.a' ],
|
||||
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
|
||||
"include_dirs": [
|
||||
"<!@(node -p \"require('node-addon-api').include\")"
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
#include <napi.h>
|
||||
#include <stdio.h>
|
||||
#include "dal.h"
|
||||
|
||||
static void _InitSQLite(const Napi::CallbackInfo& args) {
|
||||
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);
|
||||
}
|
||||
|
||||
static Napi::Value Handle(const Napi::CallbackInfo& args) {
|
||||
Napi::Buffer<uint8_t> buf = args[0].As<Napi::Buffer<uint8_t>>();
|
||||
char * charstr = reinterpret_cast<char *>(buf.Data());
|
||||
GoSlice result = HandleQuery(charstr);
|
||||
GoSlice input = {reinterpret_cast<void *>(buf.Data()), long(buf.Length()), long(buf.Length())};
|
||||
GoSlice result = HandleQuery(input);
|
||||
return Napi::Buffer<char>::Copy(args.Env(), reinterpret_cast<char *>(result.data), result.len);
|
||||
}
|
||||
|
24
binding/dal.go
Normal file
24
binding/dal.go
Normal 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() {}
|
|
@ -19,10 +19,6 @@ typedef struct { const char *p; ptrdiff_t n; } _GoString_;
|
|||
/* 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. */
|
||||
|
@ -78,8 +74,8 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern void InitSQLite(char* pragmas);
|
||||
extern GoSlice HandleQuery(char* input);
|
||||
extern void InitSQLite(GoString pragmas);
|
||||
extern GoSlice HandleQuery(GoSlice input);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
module srv
|
||||
module binding
|
||||
|
||||
go 1.22.6
|
||||
|
27
cgo/dal.go
27
cgo/dal.go
|
@ -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() {}
|
|
@ -19,7 +19,7 @@
|
|||
"test:dal": "bun test dal/__test__",
|
||||
"test:serve": "cd dal/__test__/srv && go run main.go",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue