dpwqicw157673 2013-12-22 10:07
浏览 82

与C ++相对,有人可以在Go中编写本机Node.js扩展吗?

That's all there is to my question, really, but I think it's an interesting thing to have answered.

  • 写回答

3条回答 默认 最新

  • douchuntang2827 2013-12-22 11:27
    关注

    Native module for node.js must deeply interact with V8 process which contains a lot of v8 concepts such as gc, javascript context, ...

    And I don't think V8 has exposed compatible and stable APIs for other language to interact with it. That is why node.js native addon should be built with C++ and always imports V8 C++ headers.


    But you can use GO to write node.js native addons by wrapping GO code with C++:

    file: module.go

    package main
    
    func Add(a, b int) int {
        return a + b
    }
    

    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)
    

    More about "how to call GO functionn from C/C++":

    Call Go functions from C


    Edit:

    Please see @jdi comments and the link: https://groups.google.com/forum/#!msg/golang-nuts/FzPbOwbTlPs/dAJVWQHx6m4J

    Quote: It might be doable for simple things like add (that don't generate garbage or require the runtime), but it's not supported (yet) by either compiler as far as I know. Part of the work is done for linux (see golang.org/issue/256), but there are a number of open questions (what happens when you load two shared objects? etc)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题