forked from vladimirvivien/go-cshared-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
51 lines (43 loc) · 1.16 KB
/
client.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
var ref = require("ref");
var ffi = require("ffi");
var Struct = require("ref-struct")
var ArrayType = require("ref-array")
var longlong = ref.types.longlong;
var LongArray = ArrayType(longlong);
// define object GoSlice to map to:
// C type struct { void *data; GoInt len; GoInt cap; }
var GoSlice = Struct({
data: LongArray,
len: "longlong",
cap: "longlong"
});
// define object GoString to map:
// C type struct { const char *p; GoInt n; }
var GoString = Struct({
p: "string",
n: "longlong"
});
// define foreign functions
var awesome = ffi.Library("./awesome.so", {
Add: ["longlong", ["longlong", "longlong"]],
Cosine: ["double", ["double"]],
Sort: ["void", [GoSlice]],
Log: ["longlong", [GoString]]
});
// call Add
console.log("awesome.Add(12, 99) = ", awesome.Add(12, 99));
// call Cosine
console.log("awesome.Cosine(1) = ", awesome.Cosine(1));
// call Sort
nums = LongArray([12,54,0,423,9]);
var slice = new GoSlice();
slice["data"] = nums;
slice["len"] = 5;
slice["cap"] = 5;
awesome.Sort(slice);
console.log("awesome.Sort([12,54,9,423,9] = ", nums.toArray());
// call Log
str = new GoString();
str["p"] = "Hello Node!";
str["n"] = 11;
awesome.Log(str);