| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1230062 commit 408a585
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,21 +91,29 @@ Object.setPrototypeOf(Buffer, Uint8Array); | |||
| 91 | 91 | /** | |
| 92 | 92 | * Creates a new filled Buffer instance. | |
| 93 | 93 | * alloc(size[, fill[, encoding]]) | |
| 94 | + * | ||
| 95 | + * Only pay attention to encoding if it's a string. This | ||
| 96 | + * prevents accidentally sending in a number that would | ||
| 97 | + * be interpreted as a start offset. | ||
| 98 | + * Also, don't apply encoding if fill is a number. | ||
| 99 | + * | ||
| 100 | + * These comments are placed before the function to keep the text length | ||
| 101 | + * down, to ensure that it remains inlineable by V8. | ||
| 94 | 102 | **/ | |
| 95 | 103 | Buffer.alloc = function(size, fill, encoding) { | |
| 96 | 104 | if (typeof size !== 'number') | |
| 97 | 105 | throw new TypeError('"size" argument must be a number'); | |
| 98 | 106 | if (size <= 0) | |
| 99 | 107 | return createBuffer(size); | |
| 100 | 108 | if (fill !== undefined) { | |
| 101 | - // Only pay attention to encoding if it's a string. This | ||
| 102 | - // prevents accidentally sending in a number that would | ||
| 103 | - // be interpretted as a start offset. | ||
| 104 | - // Also, don't apply encoding if fill is a number. | ||
| 105 | 109 | if (typeof fill !== 'number' && typeof encoding === 'string') | |
| 106 | 110 | fill = Buffer.from(fill, encoding); | |
| 107 | 111 | ||
| 108 | - return createBuffer(size, true).fill(fill); | ||
| 112 | + const buf = createBuffer(size, true); | ||
| 113 | + // Buffer.prototype.fill does not support filling with other buffers in v4. | ||
| 114 | + // Instead, call binding.fill directly. | ||
| 115 | + binding.fill(buf, fill, 0, buf.length); | ||
| 116 | + return buf; | ||
| 109 | 117 | } | |
| 110 | 118 | return createBuffer(size); | |
| 111 | 119 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1060,6 +1060,19 @@ assert.throws(function() { | |||
| 1060 | 1060 | Buffer.allocUnsafe(0xFFFFFFFFF); | |
| 1061 | 1061 | }, RangeError); | |
| 1062 | 1062 | ||
| 1063 | + assert(Buffer.alloc.toString().length < 600, 'Buffer.alloc is not inlineable'); | ||
| 1064 | + | ||
| 1065 | + // https://github.com/nodejs/node/issues/9226 | ||
| 1066 | + { | ||
| 1067 | + const buf = Buffer.alloc(4, 'YQ==', 'base64'); | ||
| 1068 | + const expectedBuf = Buffer.from([97, 97, 97, 97]); | ||
| 1069 | + assert(buf.equals(expectedBuf)); | ||
| 1070 | + } | ||
| 1071 | + { | ||
| 1072 | + const buf = Buffer.alloc(4, 'ab', 'ascii'); | ||
| 1073 | + const expectedBuf = Buffer.from([97, 98, 97, 98]); | ||
| 1074 | + assert(buf.equals(expectedBuf)); | ||
| 1075 | + } | ||
| 1063 | 1076 | ||
| 1064 | 1077 | // attempt to overflow buffers, similar to previous bug in array buffers | |
| 1065 | 1078 | assert.throws(function() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments