简体   繁体   English

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

[英]Nodejs: What does `process.binding` mean?

I've seen process.binding('...') many times while researching through the node.js source code on github .在研究github 上node.js 源代码时,我已经多次看到process.binding('...')

Can anybody explain me what this function does?谁能解释一下这个函数的作用是什么?

This function returns internal module, like require.此函数返回内部模块,如 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.它不是公开的,所以你不应该在你的代码中依赖它,但是如果你想了解它是如何工作的,你可以用它来处理节点的低级对象。

For example, here timer_wrap binding is registered.例如, 这里注册timer_wrap绑定。 It exports Timer constructor.导出Timer构造函数。 In lib/timers.js it's importedlib/timers.js它被导入

It's a feature that essentially goes out and grab the C++ feature and make it available inside the javascript .这是一个本质上可以获取 C++ 功能并使其在 javascript 中可用的功能。 Take this example process.binding('zlib') that is used in zlib借此示例process.binding('zlib')其中所使用的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它使使用更容易

process.binding connects the javascript side of Node.js to the C++ side of the Node.js. process.binding 将 Node.js 的 javascript 端连接到 Node.js 的 C++ 端。 C++ side of node.js is where a lot of the internal work of everything that node does, is actually implemented. node.js 的 C++ 端是 node 所做的所有事情的许多内部工作实际实现的地方。 So a lot of your code relies upon ultimately C++ code.所以你的很多代码最终都依赖于 C++ 代码。 Node.js is using the power of 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. Crypto 是 Node.js 中的一个内置模块,用于散列和保存密码。 This is how we implement it in Node.js but actual hashing process takes place in C++ side of 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.当 node.js 运行这个函数时,实际上是在这个函数内部,它将所有参数传递给PBKDF2()函数,它是 C++ 代码。 this function does all the calculations and returns the result.此函数执行所有计算并返回结果。 this is how PBKDF imported to the javascript side of the node.js这就是 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.所以这就是 node.js 的 javascript 端如何连接到 node.js 的 c++ 端。 in the c++ side of node.js, V8 is going to translate the node.js values into their c++ equivalents.在 node.js 的 c++ 端,V8 将把 node.js 值转换为它们的 c++ 等效项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM