Skip to content

Commit

Permalink
buffer: proof of concept that everything is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Aug 11, 2016
1 parent ab3306a commit ec09d43
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,42 @@ const internalUtil = require('internal/util');

class FastBuffer extends Uint8Array {}

FastBuffer.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;
const kFromErrorMsg = 'First argument must be a string, Buffer, ' +
'ArrayBuffer, Array, or array-like object.';

exports.Buffer = Buffer;
exports.SlowBuffer = SlowBuffer;
exports.INSPECT_MAX_BYTES = 50;
exports.kMaxLength = binding.kMaxLength;

const kFromErrorMsg = 'First argument must be a string, Buffer, ' +
'ArrayBuffer, Array, or array-like object.';

Buffer.poolSize = 8 * 1024;
var poolSize, poolOffset, allocPool;

const BufferClass = ((BufferFn) => class Buffer extends FastBuffer {
constructor(arg, encodingOrOffset, length) {
let realbuf;

// Common case.
if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {
throw new Error(
'If encoding is specified then the first argument must be a string'
);
}
realbuf = BufferFn.allocUnsafe(arg);
} else {
realbuf = BufferFn.from(arg, encodingOrOffset, length);
}

super(realbuf.buffer, realbuf.byteOffset, realbuf.byteLength);
}
})(Buffer);

FastBuffer.prototype.constructor = Buffer;
BufferClass.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;

exports.Buffer = Buffer;
exports.SlowBuffer = SlowBuffer;
exports.BufferClass = BufferClass;

binding.setupBufferJS(Buffer.prototype, bindingObj);

Expand Down Expand Up @@ -65,16 +87,7 @@ function alignPool() {
* would ever actually be removed.
**/
function Buffer(arg, encodingOrOffset, length) {
// Common case.
if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {
throw new Error(
'If encoding is specified then the first argument must be a string'
);
}
return Buffer.allocUnsafe(arg);
}
return Buffer.from(arg, encodingOrOffset, length);
return new BufferClass(arg, encodingOrOffset, length);
}

/**
Expand Down

0 comments on commit ec09d43

Please sign in to comment.