diff options
author | James M Snell <jasnell@gmail.com> | 2017-04-26 16:48:05 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-04-28 14:29:45 -0700 |
commit | 1052383f7ce4d151bdcc3e71f522c5f5b430bff6 (patch) | |
tree | 2d60bb34b4b54883755ccbeff780a40e9c2886fc /lib | |
parent | bed4612c408dddd86f82b0d939338b6b72a5e346 (diff) | |
download | node-new-1052383f7ce4d151bdcc3e71f522c5f5b430bff6.tar.gz |
v8: refactor struture of v8 module
Use the more efficient module.exports = {} pattern,
restructure imports from bindings, requires.
PR-URL: https://github.com/nodejs/node/pull/12681
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/v8.js | 116 |
1 files changed, 62 insertions, 54 deletions
@@ -14,44 +14,50 @@ 'use strict'; -const Buffer = require('buffer').Buffer; - -const v8binding = process.binding('v8'); -const serdesBinding = process.binding('serdes'); -const bufferBinding = process.binding('buffer'); - +const { Buffer } = require('buffer'); +const { Serializer, Deserializer } = process.binding('serdes'); +const { copy } = process.binding('buffer'); const { objectToString } = require('internal/util'); const { FastBuffer } = require('internal/buffer'); -// Properties for heap statistics buffer extraction. +const { + cachedDataVersionTag, + setFlagsFromString, + heapStatisticsArrayBuffer, + heapSpaceStatisticsArrayBuffer, + updateHeapStatisticsArrayBuffer, + updateHeapSpaceStatisticsArrayBuffer, + + // Properties for heap and heap space statistics buffer extraction. + kTotalHeapSizeIndex, + kTotalHeapSizeExecutableIndex, + kTotalPhysicalSizeIndex, + kTotalAvailableSize, + kUsedHeapSizeIndex, + kHeapSizeLimitIndex, + kDoesZapGarbageIndex, + kMallocedMemoryIndex, + kPeakMallocedMemoryIndex, + kHeapSpaces, + kHeapSpaceStatisticsPropertiesCount, + kSpaceSizeIndex, + kSpaceUsedSizeIndex, + kSpaceAvailableSizeIndex, + kPhysicalSpaceSizeIndex +} = process.binding('v8'); + +const kNumberOfHeapSpaces = kHeapSpaces.length; + const heapStatisticsBuffer = - new Float64Array(v8binding.heapStatisticsArrayBuffer); -const kTotalHeapSizeIndex = v8binding.kTotalHeapSizeIndex; -const kTotalHeapSizeExecutableIndex = v8binding.kTotalHeapSizeExecutableIndex; -const kTotalPhysicalSizeIndex = v8binding.kTotalPhysicalSizeIndex; -const kTotalAvailableSize = v8binding.kTotalAvailableSize; -const kUsedHeapSizeIndex = v8binding.kUsedHeapSizeIndex; -const kHeapSizeLimitIndex = v8binding.kHeapSizeLimitIndex; -const kDoesZapGarbageIndex = v8binding.kDoesZapGarbageIndex; -const kMallocedMemoryIndex = v8binding.kMallocedMemoryIndex; -const kPeakMallocedMemoryIndex = v8binding.kPeakMallocedMemoryIndex; - -// Properties for heap space statistics buffer extraction. + new Float64Array(heapStatisticsArrayBuffer); + const heapSpaceStatisticsBuffer = - new Float64Array(v8binding.heapSpaceStatisticsArrayBuffer); -const kHeapSpaces = v8binding.kHeapSpaces; -const kNumberOfHeapSpaces = kHeapSpaces.length; -const kHeapSpaceStatisticsPropertiesCount = - v8binding.kHeapSpaceStatisticsPropertiesCount; -const kSpaceSizeIndex = v8binding.kSpaceSizeIndex; -const kSpaceUsedSizeIndex = v8binding.kSpaceUsedSizeIndex; -const kSpaceAvailableSizeIndex = v8binding.kSpaceAvailableSizeIndex; -const kPhysicalSpaceSizeIndex = v8binding.kPhysicalSpaceSizeIndex; - -exports.getHeapStatistics = function() { + new Float64Array(heapSpaceStatisticsArrayBuffer); + +function getHeapStatistics() { const buffer = heapStatisticsBuffer; - v8binding.updateHeapStatisticsArrayBuffer(); + updateHeapStatisticsArrayBuffer(); return { 'total_heap_size': buffer[kTotalHeapSizeIndex], @@ -64,15 +70,12 @@ exports.getHeapStatistics = function() { 'peak_malloced_memory': buffer[kPeakMallocedMemoryIndex], 'does_zap_garbage': buffer[kDoesZapGarbageIndex] }; -}; - -exports.cachedDataVersionTag = v8binding.cachedDataVersionTag; -exports.setFlagsFromString = v8binding.setFlagsFromString; +} -exports.getHeapSpaceStatistics = function() { +function getHeapSpaceStatistics() { const heapSpaceStatistics = new Array(kNumberOfHeapSpaces); const buffer = heapSpaceStatisticsBuffer; - v8binding.updateHeapSpaceStatisticsArrayBuffer(); + updateHeapSpaceStatisticsArrayBuffer(); for (var i = 0; i < kNumberOfHeapSpaces; i++) { const propertyOffset = i * kHeapSpaceStatisticsPropertiesCount; @@ -86,17 +89,14 @@ exports.getHeapSpaceStatistics = function() { } return heapSpaceStatistics; -}; +} /* V8 serialization API */ -const Serializer = exports.Serializer = serdesBinding.Serializer; -const Deserializer = exports.Deserializer = serdesBinding.Deserializer; - /* JS methods for the base objects */ Serializer.prototype._getDataCloneError = Error; -Deserializer.prototype.readRawBytes = function(length) { +Deserializer.prototype.readRawBytes = function readRawBytes(length) { const offset = this._readRawBytes(length); // `this.buffer` can be a Buffer or a plain Uint8Array, so just calling // `.slice()` doesn't work. @@ -155,8 +155,6 @@ class DefaultSerializer extends Serializer { } } -exports.DefaultSerializer = DefaultSerializer; - class DefaultDeserializer extends Deserializer { constructor(buffer) { super(buffer); @@ -176,27 +174,37 @@ class DefaultDeserializer extends Deserializer { byteLength / BYTES_PER_ELEMENT); } else { // Copy to an aligned buffer first. - const copy = Buffer.allocUnsafe(byteLength); - bufferBinding.copy(this.buffer, copy, 0, - byteOffset, byteOffset + byteLength); - return new ctor(copy.buffer, - copy.byteOffset, + const buffer_copy = Buffer.allocUnsafe(byteLength); + copy(this.buffer, buffer_copy, 0, byteOffset, byteOffset + byteLength); + return new ctor(buffer_copy.buffer, + buffer_copy.byteOffset, byteLength / BYTES_PER_ELEMENT); } } } -exports.DefaultDeserializer = DefaultDeserializer; - -exports.serialize = function serialize(value) { +function serialize(value) { const ser = new DefaultSerializer(); ser.writeHeader(); ser.writeValue(value); return ser.releaseBuffer(); -}; +} -exports.deserialize = function deserialize(buffer) { +function deserialize(buffer) { const der = new DefaultDeserializer(buffer); der.readHeader(); return der.readValue(); +} + +module.exports = exports = { + cachedDataVersionTag, + getHeapStatistics, + getHeapSpaceStatistics, + setFlagsFromString, + Serializer, + Deserializer, + DefaultSerializer, + DefaultDeserializer, + deserialize, + serialize }; |