buffer: move setupBufferJS to internal

Stashing it away in internal/buffer so that it can't be used in
userland, but can still be used in internals.

PR-URL: https://github.com/nodejs/node/pull/16391
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Bryan English 2017-10-21 23:25:59 -07:00 committed by James M Snell
parent 3621889c80
commit 8172f4547e
4 changed files with 26 additions and 16 deletions

View File

@ -35,7 +35,6 @@ const {
readDoubleLE: _readDoubleLE,
readFloatBE: _readFloatBE,
readFloatLE: _readFloatLE,
setupBufferJS,
swap16: _swap16,
swap32: _swap32,
swap64: _swap64,
@ -63,6 +62,8 @@ const errors = require('internal/errors');
const internalBuffer = require('internal/buffer');
const { setupBufferJS } = internalBuffer;
const bindingObj = {};
class FastBuffer extends Uint8Array {

View File

@ -292,6 +292,10 @@
}
});
// This, as side effect, removes `setupBufferJS` from the buffer binding,
// and exposes it on `internal/buffer`.
NativeModule.require('internal/buffer');
global.Buffer = NativeModule.require('buffer').Buffer;
process.domain = null;
process._exiting = false;

View File

@ -1,4 +1,13 @@
'use strict';
// This is needed still for FastBuffer
module.exports = {};
const binding = process.binding('buffer');
const { setupBufferJS } = binding;
// Remove from the binding so that function is only available as exported here.
// (That is, for internal use only.)
delete binding.setupBufferJS;
// FastBuffer wil be inserted here by lib/buffer.js
module.exports = {
setupBufferJS
};

View File

@ -11,13 +11,11 @@ const assert = require('assert');
const buffer = require('buffer');
// Monkey-patch setupBufferJS() to have an undefined zeroFill.
const process = require('process');
const originalBinding = process.binding;
const internalBuffer = require('internal/buffer');
const binding = originalBinding('buffer');
const originalSetup = binding.setupBufferJS;
const originalSetup = internalBuffer.setupBufferJS;
binding.setupBufferJS = (proto, obj) => {
internalBuffer.setupBufferJS = (proto, obj) => {
originalSetup(proto, obj);
assert.strictEqual(obj.zeroFill[0], 1);
delete obj.zeroFill;
@ -25,19 +23,14 @@ binding.setupBufferJS = (proto, obj) => {
const bindingObj = {};
binding.setupBufferJS(Buffer.prototype, bindingObj);
internalBuffer.setupBufferJS(Buffer.prototype, bindingObj);
assert.strictEqual(bindingObj.zeroFill, undefined);
process.binding = (bindee) => {
if (bindee === 'buffer')
return binding;
return originalBinding(bindee);
};
// Load from file system because internal buffer is already loaded and we're
// testing code that runs on first load only.
// Do not move this require() to top of file. It is important that
// `process.binding('buffer').setupBufferJS` be monkey-patched before this runs.
// `require('internal/buffer').setupBufferJS` be monkey-patched before this
// runs.
const monkeyPatchedBuffer = require('../../lib/buffer');
// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always
@ -51,3 +44,6 @@ while (uninitialized.every((val) => val === 0))
// zero-fill in that case.
const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
assert(zeroFilled.every((val) => val === 0));
// setupBufferJS shouldn't still be exposed on the binding
assert(!('setupBufferJs' in process.binding('buffer')));