4

Node.js调用原生代码

 3 years ago
source link: http://blog.ilibrary.me/2017/01/25/node-js%E8%B0%83%E7%94%A8%E5%8E%9F%E7%94%9F%E4%BB%A3%E7%A0%81
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

Node js怎样

nan(Native Abstraction for Node.js)是node.js官方出的调用原生库的方案。使用这个库需要有一定的C/C++基础,能写C/C++代码。好处是它可以直接用node-gyp来编译原生代码,可以访问V8所有的API。灵活,方便。

FFI是一个调用动态库接口的库,可以不用写任何C++代码就可以调用原生接口,使用非常方便。

var ffi = require('ffi');

var libm = ffi.Library('libm', {
  'ceil': [ 'double', [ 'double' ] ]
});
libm.ceil(1.5); // 2

// You can also access just functions in the current process by passing a null
var current = ffi.Library(null, {
  'atoi': [ 'int', [ 'string' ] ]
});
current.atoi('1234'); // 1234

#比较 FFI使用简单,不需要C/C++知识基础。FFI相当于是在nan基础之上再封装了一层,把C/C++的那些东西隔离开来了。推荐使用FFI,在FFI不够用的时候再考虑用nan.

扫描二维码分享到微信朋友圈Loading...Please waitqrcode.php?url=%2F2017%2F01%2F25%2Fnode-js%25E8%25B0%2583%25E7%2594%25A8%25E5%258E%259F%25E7%2594%259F%25E4%25BB%25A3%25E7%25A0%2581

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK