-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (47 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const path = require('path');
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const wchar_t = require('ref-wchar-napi');
const wchar = wchar_t.string;
const wcharPtr = ref.refType(wchar);
const LPCWSTR = ref.refType(wcharPtr);
const PWideChar = LPCWSTR;
try {
console.log('[Start]');
const libaryPath = path.resolve(__dirname, 'libsample-pascal-lib.so');
const lib = ffi.Library(libaryPath, {
MySucc: [ ffi.types.int64, [ ffi.types.int64 ] ],
MyPred: [ ffi.types.int64, [ ffi.types.int64 ] ],
WriteToLog: [ ffi.types.bool, [ ffi.types.CString, ref.refType(ffi.types.int32) ] ],
});
const result1 = lib.MySucc(10);
console.log("result1: " + result1);
const result2 = lib.MyPred(10);
console.log("result2: " + result2);
const { result: result3, strReceveid } = callStrMethod(lib, 'WriteToLog', '[Node]: A Caça é amanhã?');
console.log("result3: " + result3);
console.log("lib output: " + strReceveid + ";");
console.log('[End]');
}
catch (error) {
console.log(error.message);
}
function callStrMethod(lib, methodName, strInput) {
const charset = 'utf8';
const charsetBytesSize = 4;
const resultStringMaxLenth = Math.max((strInput?.length ?? 0) + 1, 50);
const bufferSize = charsetBytesSize * resultStringMaxLenth;
const bufferSizeRef = ref.alloc(ffi.types.int32, bufferSize);
const strBuffer = Buffer.alloc(bufferSize, '\0', charset);
if (strInput)
strBuffer.write(strInput, charset);
const strBufferPtr = ref.alloc(PWideChar, strBuffer);
const result = lib[methodName](strBufferPtr, bufferSizeRef);
const bufferSizeRecevied = bufferSizeRef.deref();
const strBufferReceveid = ref.reinterpretUntilZeros(strBufferPtr.deref(), charsetBytesSize ?? 0);
const strReceveid = strBufferReceveid.toString(charset, 0, bufferSizeRecevied - 1);
return {
result,
strReceveid
};
}