Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Updated sample for Go 1.9 #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALL:
go tool cgo go-node-ffi.go
go build -buildmode=c-shared -o go-node-ffi.so go-node-ffi.go
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

An experiment using [Go 1.5](https://blog.golang.org/go1.5) [buildmode](https://golang.org/s/execmodes) to compile shared C libraries, and to subsequently access them through NodeJS bindings via [node-ffi](https://github.com/node-ffi/node-ffi).

Update: also tested on Go 1.9, see troubleshooting section for more info.

Simply put, let's try calling Go functions from JavaScript.


Expand Down Expand Up @@ -33,4 +35,20 @@ Tinker with the sample file `go-node-ffi.js` by running:

```shell
node go-node-ffi.js
```
```

## Troubleshooting

If you get:

```
panic: runtime error: cgo result has Go pointer
```

Then try setting the following environment var:

```
export GODEBUG=cgocheck=0
```

This still needs to be resolved.
15 changes: 9 additions & 6 deletions go-node-ffi.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Created by "go tool cgo" - DO NOT EDIT. */

/* package github.com/mrsaints/go-node-ffi */
/* package command-line-arguments */

/* Start of preamble from import "C" comments. */

Expand All @@ -11,6 +11,7 @@


/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
Expand All @@ -28,14 +29,16 @@ typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef __complex float GoComplex64;
typedef __complex double GoComplex128;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

// static assertion to make sure the file is being used on architecture
// at least with matching size of GoInt.
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef struct { char *p; GoInt n; } GoString;
typedef struct { const char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
Expand Down
20 changes: 12 additions & 8 deletions go-node-ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@ var FFI = require("ffi");
var Ref = require("ref");
var Struct = require("ref-struct");

var hw_path = "/home/mrsaints/Workspace/golang/src/github.com/mrsaints/go-node-ffi/go-node-ffi";
var hw_path = "./go-node-ffi";

/*
* GoString ABI-compliant struct
*/
var GoString = Struct({
p: "char *",
n: "int"
p: "string",
n: "int",
});

function NewGoString(v) {
var _gs = new GoString();
_gs.p = Ref.allocCString(v);
_gs.p = v
_gs.n = v.length;
return _gs;
}

function GoStringToJsString(gostring) {
return gostring.p.slice(0, gostring.n)
}

var hw = FFI.Library(hw_path, {
"HelloWorld": ["string", []],
"Greet": ["string", [GoString]],
"HelloWorld": [GoString, []],
"Greet": [GoString, [GoString]],
"Add": ["int", ["int", "int"]]
});

var helloWorld = hw.HelloWorld();
console.log(helloWorld);
console.log(GoStringToJsString(helloWorld));

var addition = hw.Add(2, 4);
console.log(addition);

var john = NewGoString("John");
var greeting = hw.Greet(john);
console.log(greeting);
console.log(GoStringToJsString(greeting));
Binary file modified go-node-ffi.so
Binary file not shown.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
"version": "1.0.0",
"description": "An experiment using Go 1.5 buildmode to compile shared C libraries, and to subsequently access them through NodeJS bindings via node-ffi.",
"main": "go-node-ffi.js",
"devDependencies": {
"ffi": "^1.3.2",
"ref": "^1.0.2",
"ref-struct": "^1.0.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node go-node-ffi.js"
},
"repository": {
"type": "git",
Expand All @@ -21,5 +17,10 @@
"bugs": {
"url": "https://github.com/MrSaints/go-node-ffi/issues"
},
"homepage": "https://github.com/MrSaints/go-node-ffi#readme"
"homepage": "https://github.com/MrSaints/go-node-ffi#readme",
"dependencies": {
"ffi": "^2.2.0",
"ref": "^1.3.5",
"ref-struct": "^1.1.0"
}
}