Created
July 26, 2016 17:22
-
-
Save kevinswiber/494df3fe71efc81d6f28cddbaf23e34e to your computer and use it in GitHub Desktop.
Interop file for using libnode from Golang.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interop | |
/* | |
#cgo CFLAGS: -I${SRCDIR}/../ | |
#cgo LDFLAGS: -L${SRCDIR}/../ -lhello | |
#include <stdlib.h> | |
#include <string.h> | |
#include <node_c.h> | |
static char** create_argv_array(int array_size) { | |
char** argv = calloc(sizeof(char*), array_size); | |
return argv; | |
} | |
// libuv requires that argv memory is adjacent | |
static char* create_argv_placeholder(int total_length) { | |
char* placeholder = calloc(sizeof(char), total_length); | |
return placeholder; | |
} | |
static char* set_argv_string(char** argv, int index, char* cursor, char* str) { | |
argv[index] = cursor; | |
strcpy(cursor, str); | |
return cursor + strlen(str) + 1; | |
} | |
*/ | |
import "C" | |
func NodeMain(argv []string) { | |
argc := C.int(len(argv)) | |
args := C.create_argv_array(argc) | |
var totalLength int | |
for _, s := range argv { | |
totalLength += len(s) | |
} | |
cursor := C.create_argv_placeholder(C.int(totalLength)) | |
for i, s := range argv { | |
cursor = C.set_argv_string(args, C.int(i), cursor, C.CString(s)) | |
} | |
C.node_start(argc, args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment