kra*_*man 12
通过在go中添加对共享库的支持,现在可以实现.
档案:calculator.go
// package name: calculator
package main
import "C"
//export Sum
func Sum(x, y float64) float64 {
return x + y
}
func main() {
}
Run Code Online (Sandbox Code Playgroud)
file:node-calculator.cc
#include "calculator.h"
#include <node.h>
namespace calc {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Number;
using v8::Exception;
void add(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
// Check the number of arguments passed.
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
// Check the argument types
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
// Perform the operation
Local<Number> num = Number::New(isolate, Sum(args[0]->NumberValue(), args[1]->NumberValue()));
// Set the return value (using the passed in
// FunctionCallbackInfo<Value>&)
args.GetReturnValue().Set(num);
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "add", add);
}
NODE_MODULE(calculator, init)
}
Run Code Online (Sandbox Code Playgroud)
file:test.cc
const calculator = require('./build/Release/node-calculator');
console.log('4+5=', calculator.add(4, 5));
Run Code Online (Sandbox Code Playgroud)
file:binding.gyp
{
"targets": [
{
"target_name": "node-calculator",
"sources": [
"node-calculator.cc"
],
"libraries": [
"../calculator.a"
],
},
],
}
Run Code Online (Sandbox Code Playgroud)
建立:
go build -buildmode c-archive -o calculator.a calculator.go
node-gyp configure
node-gyp build
Run Code Online (Sandbox Code Playgroud)
输出:
#> node test.js
4+5= 9
Run Code Online (Sandbox Code Playgroud)
node.js的native模块必须与V8进程深度交互,V8进程包含很多v8概念,如gc,javascript context,...
而且我认为V8没有为其他语言提供兼容且稳定的API来与之交互.这就是为什么node.js本机插件应该用C++构建并始终导入V8 C++标头.
但是您可以使用GO通过使用C++包装GO代码来编写node.js本机插件:
file:module.go
package main
func Add(a, b int) int {
return a + b
}
Run Code Online (Sandbox Code Playgroud)
file:module.c
#include <node.h>
#include <v8.h>
using namespace v8;
extern int go_add(int, int) __asm__ ("example.main.Add");
void init(Handle<Object> exports) {
// call go_add
}
NODE_MODULE(module, init)
Run Code Online (Sandbox Code Playgroud)
更多关于"如何从C/C++调用GO函数":
编辑:
请参阅@jdi评论和链接:https://groups.google.com/forum/#!msg/golang -nuts/FzPbOwbTlPs/dAJVWQHx6m4J
Quote:它可能适用于简单的事情,如添加(不生成垃圾或需要运行时),但据我所知,任何编译器都不支持它.部分工作是针对linux完成的(参见golang.org/issue/256),但是有很多未解决的问题(当你加载两个共享对象时会发生什么?等等)
归档时间: |
|
查看次数: |
4811 次 |
最近记录: |