javascript Nodejs:“process.binding”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24042861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:03:05  来源:igfitidea点击:

Nodejs: What does `process.binding` mean?

javascriptc++node.jsv8undocumented-behavior

提问by laconbass

I've seen process.binding('...')many times while researching through the node.js source code on github.

process.binding('...')在研究github 上node.js 源代码时见过很多次。

Can anybody explain me what this function does?

谁能解释一下这个函数的作用是什么?

采纳答案by vkurchatkin

This function returns internal module, like require. It's not public, so you shouldn't rely on it in your code, but you can use it to play with node's low level objects, if you want to understand how things work.

此函数返回内部模块,如 require。它不是公开的,所以你不应该在你的代码中依赖它,但是如果你想了解它是如何工作的,你可以用它来处理节点的低级对象。

For example, heretimer_wrapbinding is registered. It exportsTimerconstructor. In lib/timers.jsit's imported

例如,此处timer_wrap注册绑定。它导出Timer构造函数。在lib/timers.js它的进口

回答by Mohamed Ben HEnda

It's a feature that essentially goes out and grab the C++ feature and make it available inside the javascript . Take this example process.binding('zlib')that is used in zlib

这是一个本质上可以获取 C++ 功能并使其在 javascript 中可用的功能。以zlib中process.binding('zlib')使用的这个例子为例

This is essentially going out and getting the zlib C++ object and then it's being used the rest of the time in the javascript code.

这本质上是出去并获取 zlib C++ 对象,然后在 javascript 代码中的其余时间使用它。

So when you use zlib you're not actually going out and grabbing the C++ library, you're using the Javascript library that wraps the C++ feature for you.

因此,当您使用 zlib 时,您实际上并没有出去获取 C++ 库,而是使用了为您包装了 C++ 功能的 Javascript 库。

It makes it easier to use

它使使用更容易

回答by Yilmaz

process.binding connects the javascript side of Node.js to the C++ side of the Node.js. C++ side of node.js is where a lot of the internal work of everything that node does, is actually implemented. So a lot of your code relies upon ultimately C++ code. Node.js is using the power of C++.

process.binding 将 Node.js 的 javascript 端连接到 Node.js 的 C++ 端。node.js 的 C++ 端是 node 所做的所有事情的许多内部工作实际实现的地方。因此,您的很多代码最终都依赖于 C++ 代码。Node.js 正在使用 C++ 的强大功能。

Here is an example:

下面是一个例子:

const crypto=require(“crypto”)
const start=Date.now()
crypto.pbkdf2(“a”, “b”, 100000,512,sha512,()=>{
console.log(“1”:Date.now()-start)
})

Crypto is a built-in module in Node.js for hashing and saving passwords. This is how we implement it in Node.js but actual hashing process takes place in C++ side of node.js.

Crypto 是 Node.js 中的一个内置模块,用于散列和保存密码。这就是我们在 Node.js 中实现它的方式,但实际的散列过程发生在 node.js 的 C++ 端。

when node.js runs this function, actually inside of this function, it passes all the arguments to the PBKDF2()function which is the c++ code. this function does all the calculations and returns the result. this is how PBKDF imported to the javascript side of the node.js

当 node.js 运行这个函数时,实际上在这个函数内部,它将所有参数传递给PBKDF2()函数,即 c++ 代码。此函数执行所有计算并返回结果。这就是 PBKDF 如何导入到 node.js 的 javascript 端

const {PBKDF2}=process.binding(“crypto”)

So this is how javascript side of node.js is connected to the c++ side of node.js. in the c++ side of node.js, V8 is going to translate the node.js values into their c++ equivalents.

所以这就是 node.js 的 javascript 端如何连接到 node.js 的 c++ 端。在 node.js 的 c++ 端,V8 将把 node.js 值转换为它们的 c++ 等效项。