Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access to `primordials` object in node?

I am learning Node.js and analyzing its source code. I notice Node.js invokes primordials many times in its source code. However, I do not know the implementation of primordials. After reading the file lib/internal/per_context/primordials.js, I find out some of properties of primordials, such as primordials.Array and primordials.ArrayBuffer are JS builtins:

[
  'Array',
  'ArrayBuffer',
  'BigInt',
  'BigInt64Array',
  'BigUint64Array',
  'Boolean',
  'DataView',
  'Date',
  'Error',
  'EvalError',
  'Float32Array',
  'Float64Array',
  'Function',
  'Int16Array',
  'Int32Array',
  'Int8Array',
  'Map',
  'Number',
  'Object',
  'RangeError',
  'ReferenceError',
  'RegExp',
  'Set',
  'String',
  'Symbol',
  'SyntaxError',
  'TypeError',
  'URIError',
  'Uint16Array',
  'Uint32Array',
  'Uint8Array',
  'Uint8ClampedArray',
  'WeakMap',
  'WeakSet',
].forEach((name) => {
  const original = global[name];
  primordials[name] = original;
  copyPropsRenamed(original, primordials, name);
  copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

However, I cannot find the implementation of other properties of primordials, such as primordials.FunctionPrototypeCall and primordials.ObjectDefineProperty. In order to understand all the properties of primordials, I want to access to primordials after compiling of Node.js's source code. Is there any method to access to primordials? Thx.

like image 688
xiaoning Avatar asked Jan 22 '26 17:01

xiaoning


1 Answers

primordials are the internal name for any built-in JS data types, you can access them from any JS code that you run with Node.js because these are global entities. e.g. if you have index.js file you can just call new Array or any other primordial and then run it with node ./index.js

Also as far as I know in you are writing some code in lib folder here you would need to access these in a special manner, but I think you are just wring the JS code so you don't care about it.

Most of these types come from V8 engine.

like image 104
Ayzrian Avatar answered Jan 25 '26 06:01

Ayzrian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!