What are the Node.JS getHiddenValue and setHiddenValue functions that wrap V8 GetPrivate and SetPrivate?

For very complex reasons, I am studying to better understand Node.JS internals and discovered two functions of an unknown purpose.

These are functions and ways to access them.

process.binding('util').setHiddenValue process.binding('util').getHiddenValue 

From their own code declarations , it is clear that they wrap the following V8 functions:

 v8::Object::SetPrivate v8::Object::GetPrivate 

I also made a small snippet that shows what they can do.

 'use strict'; var binding = process.binding('util'); var o = {}; binding.setHiddenValue(o, 7, 'testing123'); console.log(binding.getHiddenValue(o, 7)); // returns 'testing123' 

However, I could not find the documentation of what the so-called "hidden values" are for, or otherwise determine why they are needed in Node or in V8.

Can anyone shed light on their true purpose?

For reference, these are apparently the only valid values ​​that can be used to bind data (with an excess of 7):

 alpn_buffer_private_symbol: 0, arrow_message_private_symbol: 1, contextify_context_private_symbol: 2, contextify_global_private_symbol: 3, decorated_private_symbol: 4, npn_buffer_private_symbol: 5, processed_private_symbol: 6, selected_npn_buffer_private_symbol: 7, 
+6
source share
1 answer

From https://v8docs.nodesource.com/io.js-3.0/db/d85/classv8_1_1_object.html#a98ad2e2a82b457a733bee13e4c2ba876 :

Access to hidden properties of JavaScript objects. These properties are hidden from executable JavaScript and are only available through the V8 C ++ API. Hidden properties entered internally by V8 (for example, a hash identifier) ​​are prefixed with "v8 ::".

However, they will not be used in versions v7 and higher - https://github.com/nodejs/node/commit/924cc6c6335e58f61b04d2f41d348bd6b8be98a1

For comparison with vanilla JS:

 const foo = Object.create({}, { privBar: {value: 'private'}, publBar: {value: 'public', enumerable: true} }) console.log(foo.privBar) // 'private' console.log(foo.publBar) // 'public' 

Please note that we can still access privBar because it is not private. But if we are JSON.stringify(foo) , then only publBar will be displayed in serialization. With v8 features, you get an even more private privBar .

+3
source

Source: https://habr.com/ru/post/1013302/


All Articles