我们可以在Go中编写一个本地的Node.js扩展,而不是C ++吗? [英] Could one write a native Node.js extension in Go, as opposed to C++?
本文介绍了我们可以在Go中编写一个本地的Node.js扩展,而不是C ++吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我的问题,真的,但我认为这是一个有趣的事情,得到了答案。
在这里增加对共享库的支持,现在这是可能的。
file:calculator.go
//程序包名称:calculator
程序包main
导入C
//导出Sum
func Sum(x,y float64)float64 {
return x + y
}
func main(){
}
file:node-calculator.cc
#includecalculator.h
#include< node.h>
命名空间calc {
使用v8 :: FunctionCallbackInfo;
使用v8 :: Isolate;
使用v8 :: Local;
使用v8 :: Object;
使用v8 :: String;
使用v8 :: Value;
使用v8 :: Number;
使用v8 :: Exception;
$ b $ void add(const FunctionCallbackInfo< Value>& args){
隔离* isolate = args.GetIsolate();
//检查传递的参数数量。
if(args.Length()<2){
//抛出传回给JavaScript的错误
isolate-> ThrowException(Exception :: TypeError(
String :: NewFromUtf8(isolate,Wrong number of arguments)));
return;
//检查参数类型
if(!args [0] - &>; IsNumber()||!args [1] - &> IsNumber()) {
isolate-> ThrowException(Exception :: TypeError(
String :: NewFromUtf8(isolate,Wrong arguments)));
return;
}
//执行操作
Local< Number> num = Number :: New(isolate,Sum(args [0] - > NumberValue(),args [1] - > NumberValue()));
//设置返回值(使用传入的
// FunctionCallbackInfo< Value>&)
args.GetReturnValue()。Set(num);
}
void init(Local< Object> exports){
NODE_SET_METHOD(exports,add,add);
NODE_MODULE(calculator,init)
}
file:test.cc
const calculator = require('./ build / Release / node-calculator');
console.log('4 + 5 =',calculator.add(4,5));
file:binding.gyp
<$ p $
targets:[
{
target_name:节点计算器,
sources:[
node-calculator.cc
],
libraries:[
../calculator.a
,
},
],
}
建立:
go build -buildmode c-archive -o calculator.a calculator.go
node-gyp configure
node-gyp build
输出:
#> ; node test.js
4 + 5 = 9
That's all there is to my question, really, but I think it's an interesting thing to have answered.
解决方案
With the addition of support for shared libraries in go, this is possible now.
file: calculator.go
// package name: calculator
package main
import "C"
//export Sum
func Sum(x, y float64) float64 {
return x + y
}
func main() {
}
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)
}
file: test.cc
const calculator = require('./build/Release/node-calculator');
console.log('4+5=', calculator.add(4, 5));
file: binding.gyp
{
"targets": [
{
"target_name": "node-calculator",
"sources": [
"node-calculator.cc"
],
"libraries": [
"../calculator.a"
],
},
],
}
Build:
go build -buildmode c-archive -o calculator.a calculator.go
node-gyp configure
node-gyp build
Output:
#> node test.js
4+5= 9
这篇关于我们可以在Go中编写一个本地的Node.js扩展,而不是C ++吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文