Skip to content

Commit 41213bd

Browse files
VoltrexKeyvatargos
authored andcommitted
src: add JSDoc typings for v8
Added JSDoc typings for the `v8` lib module. PR-URL: #38944 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
1 parent a4d70ff commit 41213bd

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

lib/v8.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ const {
5858
} = internalBinding('heap_utils');
5959
const { HeapSnapshotStream } = require('internal/heap_utils');
6060

61+
/**
62+
* Generates a snapshot of the current V8 heap
63+
* and writes it to a JSON file.
64+
* @param {string} [filename]
65+
* @returns {string}
66+
*/
6167
function writeHeapSnapshot(filename) {
6268
if (filename !== undefined) {
6369
filename = getValidatedPath(filename);
@@ -66,6 +72,11 @@ function writeHeapSnapshot(filename) {
6672
return triggerHeapSnapshot(filename);
6773
}
6874

75+
/**
76+
* Generates a snapshot of the current V8 heap
77+
* and returns a Readable Stream.
78+
* @returns {import('./stream.js').Readable}
79+
*/
6980
function getHeapSnapshot() {
7081
const handle = createHeapSnapshotStream();
7182
assert(handle);
@@ -110,11 +121,32 @@ const {
110121

111122
const kNumberOfHeapSpaces = kHeapSpaces.length;
112123

124+
/**
125+
* Sets V8 command-line flags.
126+
* @param {string} flags
127+
* @returns {void}
128+
*/
113129
function setFlagsFromString(flags) {
114130
validateString(flags, 'flags');
115131
_setFlagsFromString(flags);
116132
}
117133

134+
/**
135+
* Gets the current V8 heap statistics.
136+
* @returns {{
137+
* total_heap_size: number;
138+
* total_heap_size_executable: number;
139+
* total_physical_size: number;
140+
* total_available_size: number;
141+
* used_heap_size: number;
142+
* heap_size_limit: number;
143+
* malloced_memory: number;
144+
* peak_malloced_memory: number;
145+
* does_zap_garbage: number;
146+
* number_of_native_contexts: number;
147+
* number_of_detached_contexts: number;
148+
* }}
149+
*/
118150
function getHeapStatistics() {
119151
const buffer = heapStatisticsBuffer;
120152

@@ -135,6 +167,16 @@ function getHeapStatistics() {
135167
};
136168
}
137169

170+
/**
171+
* Gets the current V8 heap space statistics.
172+
* @returns {{
173+
* space_name: string;
174+
* space_size: number;
175+
* space_used_size: number;
176+
* space_available_size: number;
177+
* physical_space_size: number;
178+
* }[]}
179+
*/
138180
function getHeapSpaceStatistics() {
139181
const heapSpaceStatistics = new Array(kNumberOfHeapSpaces);
140182
const buffer = heapSpaceStatisticsBuffer;
@@ -153,6 +195,14 @@ function getHeapSpaceStatistics() {
153195
return heapSpaceStatistics;
154196
}
155197

198+
/**
199+
* Gets the current V8 heap code statistics.
200+
* @returns {{
201+
* code_and_metadata_size: number;
202+
* bytecode_and_metadata_size: number;
203+
* external_script_source_size: number;
204+
* }}
205+
*/
156206
function getHeapCodeStatistics() {
157207
const buffer = heapCodeStatisticsBuffer;
158208

@@ -169,6 +219,11 @@ function getHeapCodeStatistics() {
169219
/* JS methods for the base objects */
170220
Serializer.prototype._getDataCloneError = Error;
171221

222+
/**
223+
* Reads raw bytes from the deserializer's internal buffer.
224+
* @param {number} length
225+
* @returns {Buffer}
226+
*/
172227
Deserializer.prototype.readRawBytes = function readRawBytes(length) {
173228
const offset = this._readRawBytes(length);
174229
// `this.buffer` can be a Buffer or a plain Uint8Array, so just calling
@@ -209,6 +264,12 @@ class DefaultSerializer extends Serializer {
209264
this._setTreatArrayBufferViewsAsHostObjects(true);
210265
}
211266

267+
/**
268+
* Used to write some kind of host object, i.e. an
269+
* object that is created by native C++ bindings.
270+
* @param {Object} abView
271+
* @returns {void}
272+
*/
212273
_writeHostObject(abView) {
213274
let i = 0;
214275
if (abView.constructor === Buffer) {
@@ -231,6 +292,11 @@ class DefaultSerializer extends Serializer {
231292
}
232293

233294
class DefaultDeserializer extends Deserializer {
295+
/**
296+
* Used to read some kind of host object, i.e. an
297+
* object that is created by native C++ bindings.
298+
* @returns {any}
299+
*/
234300
_readHostObject() {
235301
const typeIndex = this.readUint32();
236302
const ctor = arrayBufferViewTypes[typeIndex];
@@ -253,13 +319,25 @@ class DefaultDeserializer extends Deserializer {
253319
}
254320
}
255321

322+
/**
323+
* Uses a `DefaultSerializer` to serialize `value`
324+
* into a buffer.
325+
* @param {any} value
326+
* @returns {Buffer}
327+
*/
256328
function serialize(value) {
257329
const ser = new DefaultSerializer();
258330
ser.writeHeader();
259331
ser.writeValue(value);
260332
return ser.releaseBuffer();
261333
}
262334

335+
/**
336+
* Uses a `DefaultDeserializer` with default options
337+
* to read a JavaScript value from a buffer.
338+
* @param {Buffer | TypedArray | DataView} buffer
339+
* @returns {any}
340+
*/
263341
function deserialize(buffer) {
264342
const der = new DefaultDeserializer(buffer);
265343
der.readHeader();

0 commit comments

Comments
 (0)