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:
parent
3621889c80
commit
8172f4547e
@ -35,7 +35,6 @@ const {
|
|||||||
readDoubleLE: _readDoubleLE,
|
readDoubleLE: _readDoubleLE,
|
||||||
readFloatBE: _readFloatBE,
|
readFloatBE: _readFloatBE,
|
||||||
readFloatLE: _readFloatLE,
|
readFloatLE: _readFloatLE,
|
||||||
setupBufferJS,
|
|
||||||
swap16: _swap16,
|
swap16: _swap16,
|
||||||
swap32: _swap32,
|
swap32: _swap32,
|
||||||
swap64: _swap64,
|
swap64: _swap64,
|
||||||
@ -63,6 +62,8 @@ const errors = require('internal/errors');
|
|||||||
|
|
||||||
const internalBuffer = require('internal/buffer');
|
const internalBuffer = require('internal/buffer');
|
||||||
|
|
||||||
|
const { setupBufferJS } = internalBuffer;
|
||||||
|
|
||||||
const bindingObj = {};
|
const bindingObj = {};
|
||||||
|
|
||||||
class FastBuffer extends Uint8Array {
|
class FastBuffer extends Uint8Array {
|
||||||
|
4
lib/internal/bootstrap_node.js
vendored
4
lib/internal/bootstrap_node.js
vendored
@ -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;
|
global.Buffer = NativeModule.require('buffer').Buffer;
|
||||||
process.domain = null;
|
process.domain = null;
|
||||||
process._exiting = false;
|
process._exiting = false;
|
||||||
|
@ -1,4 +1,13 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// This is needed still for FastBuffer
|
const binding = process.binding('buffer');
|
||||||
module.exports = {};
|
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
|
||||||
|
};
|
||||||
|
@ -11,13 +11,11 @@ const assert = require('assert');
|
|||||||
const buffer = require('buffer');
|
const buffer = require('buffer');
|
||||||
|
|
||||||
// Monkey-patch setupBufferJS() to have an undefined zeroFill.
|
// Monkey-patch setupBufferJS() to have an undefined zeroFill.
|
||||||
const process = require('process');
|
const internalBuffer = require('internal/buffer');
|
||||||
const originalBinding = process.binding;
|
|
||||||
|
|
||||||
const binding = originalBinding('buffer');
|
const originalSetup = internalBuffer.setupBufferJS;
|
||||||
const originalSetup = binding.setupBufferJS;
|
|
||||||
|
|
||||||
binding.setupBufferJS = (proto, obj) => {
|
internalBuffer.setupBufferJS = (proto, obj) => {
|
||||||
originalSetup(proto, obj);
|
originalSetup(proto, obj);
|
||||||
assert.strictEqual(obj.zeroFill[0], 1);
|
assert.strictEqual(obj.zeroFill[0], 1);
|
||||||
delete obj.zeroFill;
|
delete obj.zeroFill;
|
||||||
@ -25,19 +23,14 @@ binding.setupBufferJS = (proto, obj) => {
|
|||||||
|
|
||||||
const bindingObj = {};
|
const bindingObj = {};
|
||||||
|
|
||||||
binding.setupBufferJS(Buffer.prototype, bindingObj);
|
internalBuffer.setupBufferJS(Buffer.prototype, bindingObj);
|
||||||
assert.strictEqual(bindingObj.zeroFill, undefined);
|
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
|
// Load from file system because internal buffer is already loaded and we're
|
||||||
// testing code that runs on first load only.
|
// testing code that runs on first load only.
|
||||||
// Do not move this require() to top of file. It is important that
|
// 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');
|
const monkeyPatchedBuffer = require('../../lib/buffer');
|
||||||
|
|
||||||
// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always
|
// 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.
|
// zero-fill in that case.
|
||||||
const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
|
const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
|
||||||
assert(zeroFilled.every((val) => val === 0));
|
assert(zeroFilled.every((val) => val === 0));
|
||||||
|
|
||||||
|
// setupBufferJS shouldn't still be exposed on the binding
|
||||||
|
assert(!('setupBufferJs' in process.binding('buffer')));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user