| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42fc754 commit 255f017
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,22 @@ const { | |||
| 35 | 35 | swap32: _swap32, | |
| 36 | 36 | swap64: _swap64, | |
| 37 | 37 | kMaxLength, | |
| 38 | - kStringMaxLength | ||
| 38 | + kStringMaxLength, | ||
| 39 | + zeroFill: bindingZeroFill, | ||
| 40 | + | ||
| 41 | + // Additional Buffer methods | ||
| 42 | + asciiSlice, | ||
| 43 | + base64Slice, | ||
| 44 | + latin1Slice, | ||
| 45 | + hexSlice, | ||
| 46 | + ucs2Slice, | ||
| 47 | + utf8Slice, | ||
| 48 | + asciiWrite, | ||
| 49 | + base64Write, | ||
| 50 | + latin1Write, | ||
| 51 | + hexWrite, | ||
| 52 | + ucs2Write, | ||
| 53 | + utf8Write | ||
| 39 | 54 | } = internalBinding('buffer'); | |
| 40 | 55 | const { | |
| 41 | 56 | getOwnNonIndexProperties, | |
@@ -75,10 +90,6 @@ const { validateString } = require('internal/validators'); | |||
| 75 | 90 | ||
| 76 | 91 | const internalBuffer = require('internal/buffer'); | |
| 77 | 92 | ||
| 78 | - const { setupBufferJS } = internalBuffer; | ||
| 79 | - | ||
| 80 | - const bindingObj = {}; | ||
| 81 | - | ||
| 82 | 93 | class FastBuffer extends Uint8Array {} | |
| 83 | 94 | FastBuffer.prototype.constructor = Buffer; | |
| 84 | 95 | internalBuffer.FastBuffer = FastBuffer; | |
@@ -89,6 +100,19 @@ for (const [name, method] of Object.entries(internalBuffer.readWrites)) { | |||
| 89 | 100 | Buffer.prototype[name] = method; | |
| 90 | 101 | } | |
| 91 | 102 | ||
| 103 | + Buffer.prototype.asciiSlice = asciiSlice; | ||
| 104 | + Buffer.prototype.base64Slice = base64Slice; | ||
| 105 | + Buffer.prototype.latin1Slice = latin1Slice; | ||
| 106 | + Buffer.prototype.hexSlice = hexSlice; | ||
| 107 | + Buffer.prototype.ucs2Slice = ucs2Slice; | ||
| 108 | + Buffer.prototype.utf8Slice = utf8Slice; | ||
| 109 | + Buffer.prototype.asciiWrite = asciiWrite; | ||
| 110 | + Buffer.prototype.base64Write = base64Write; | ||
| 111 | + Buffer.prototype.latin1Write = latin1Write; | ||
| 112 | + Buffer.prototype.hexWrite = hexWrite; | ||
| 113 | + Buffer.prototype.ucs2Write = ucs2Write; | ||
| 114 | + Buffer.prototype.utf8Write = utf8Write; | ||
| 115 | + | ||
| 92 | 116 | const constants = Object.defineProperties({}, { | |
| 93 | 117 | MAX_LENGTH: { | |
| 94 | 118 | value: kMaxLength, | |
@@ -105,11 +129,11 @@ const constants = Object.defineProperties({}, { | |||
| 105 | 129 | Buffer.poolSize = 8 * 1024; | |
| 106 | 130 | let poolSize, poolOffset, allocPool; | |
| 107 | 131 | ||
| 108 | - setupBufferJS(Buffer.prototype, bindingObj); | ||
| 109 | - | ||
| 132 | + // A toggle used to access the zero fill setting of the array buffer allocator | ||
| 133 | + // in C++. | ||
| 110 | 134 | // |zeroFill| can be undefined when running inside an isolate where we | |
| 111 | 135 | // do not own the ArrayBuffer allocator. Zero fill is always on in that case. | |
| 112 | - const zeroFill = bindingObj.zeroFill || [0]; | ||
| 136 | + const zeroFill = bindingZeroFill || [0]; | ||
| 113 | 137 | ||
| 114 | 138 | function createUnsafeBuffer(size) { | |
| 115 | 139 | return new FastBuffer(createUnsafeArrayBuffer(size)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -653,11 +653,15 @@ function setupGlobalVariables() { | |||
| 653 | 653 | } | |
| 654 | 654 | }); | |
| 655 | 655 | ||
| 656 | - // This, as side effect, removes `setupBufferJS` from the buffer binding, | ||
| 657 | - // and exposes it on `internal/buffer`. | ||
| 658 | - NativeModule.require('internal/buffer'); | ||
| 656 | + const { Buffer } = NativeModule.require('buffer'); | ||
| 657 | + const bufferBinding = internalBinding('buffer'); | ||
| 659 | 658 | ||
| 660 | - global.Buffer = NativeModule.require('buffer').Buffer; | ||
| 659 | + // Only after this point can C++ use Buffer::New() | ||
| 660 | + bufferBinding.setBufferPrototype(Buffer.prototype); | ||
| 661 | + delete bufferBinding.setBufferPrototype; | ||
| 662 | + delete bufferBinding.zeroFill; | ||
| 663 | + | ||
| 664 | + global.Buffer = Buffer; | ||
| 661 | 665 | process.domain = null; | |
| 662 | 666 | process._exiting = false; | |
| 663 | 667 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,17 +1,11 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const binding = internalBinding('buffer'); | ||
| 4 | 3 | const { | |
| 5 | 4 | ERR_BUFFER_OUT_OF_BOUNDS, | |
| 6 | 5 | ERR_INVALID_ARG_TYPE, | |
| 7 | 6 | ERR_OUT_OF_RANGE | |
| 8 | 7 | } = require('internal/errors').codes; | |
| 9 | 8 | const { validateNumber } = require('internal/validators'); | |
| 10 | - const { setupBufferJS } = binding; | ||
| 11 | - | ||
| 12 | - // Remove from the binding so that function is only available as exported here. | ||
| 13 | - // (That is, for internal use only.) | ||
| 14 | - delete binding.setupBufferJS; | ||
| 15 | 9 | ||
| 16 | 10 | // Temporary buffers to convert numbers. | |
| 17 | 11 | const float32Array = new Float32Array(1); | |
@@ -779,7 +773,6 @@ function writeFloatBackwards(val, offset = 0) { | |||
| 779 | 773 | ||
| 780 | 774 | // FastBuffer wil be inserted here by lib/buffer.js | |
| 781 | 775 | module.exports = { | |
| 782 | - setupBufferJS, | ||
| 783 | 776 | // Container to export all read write functions. | |
| 784 | 777 | readWrites: { | |
| 785 | 778 | readUIntLE, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1055,38 +1055,12 @@ static void EncodeUtf8String(const FunctionCallbackInfo<Value>& args) { | |||
| 1055 | 1055 | } | |
| 1056 | 1056 | ||
| 1057 | 1057 | ||
| 1058 | - // pass Buffer object to load prototype methods | ||
| 1059 | - void SetupBufferJS(const FunctionCallbackInfo<Value>& args) { | ||
| 1058 | + void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) { | ||
| 1060 | 1059 | Environment* env = Environment::GetCurrent(args); | |
| 1061 | 1060 | ||
| 1062 | 1061 | CHECK(args[0]->IsObject()); | |
| 1063 | 1062 | Local<Object> proto = args[0].As<Object>(); | |
| 1064 | 1063 | env->set_buffer_prototype_object(proto); | |
| 1065 | - | ||
| 1066 | - env->SetMethodNoSideEffect(proto, "asciiSlice", StringSlice<ASCII>); | ||
| 1067 | - env->SetMethodNoSideEffect(proto, "base64Slice", StringSlice<BASE64>); | ||
| 1068 | - env->SetMethodNoSideEffect(proto, "latin1Slice", StringSlice<LATIN1>); | ||
| 1069 | - env->SetMethodNoSideEffect(proto, "hexSlice", StringSlice<HEX>); | ||
| 1070 | - env->SetMethodNoSideEffect(proto, "ucs2Slice", StringSlice<UCS2>); | ||
| 1071 | - env->SetMethodNoSideEffect(proto, "utf8Slice", StringSlice<UTF8>); | ||
| 1072 | - | ||
| 1073 | - env->SetMethod(proto, "asciiWrite", StringWrite<ASCII>); | ||
| 1074 | - env->SetMethod(proto, "base64Write", StringWrite<BASE64>); | ||
| 1075 | - env->SetMethod(proto, "latin1Write", StringWrite<LATIN1>); | ||
| 1076 | - env->SetMethod(proto, "hexWrite", StringWrite<HEX>); | ||
| 1077 | - env->SetMethod(proto, "ucs2Write", StringWrite<UCS2>); | ||
| 1078 | - env->SetMethod(proto, "utf8Write", StringWrite<UTF8>); | ||
| 1079 | - | ||
| 1080 | - if (auto zero_fill_field = env->isolate_data()->zero_fill_field()) { | ||
| 1081 | - CHECK(args[1]->IsObject()); | ||
| 1082 | - auto binding_object = args[1].As<Object>(); | ||
| 1083 | - auto array_buffer = ArrayBuffer::New(env->isolate(), | ||
| 1084 | - zero_fill_field, | ||
| 1085 | - sizeof(*zero_fill_field)); | ||
| 1086 | - auto name = FIXED_ONE_BYTE_STRING(env->isolate(), "zeroFill"); | ||
| 1087 | - auto value = Uint32Array::New(array_buffer, 0, 1); | ||
| 1088 | - CHECK(binding_object->Set(env->context(), name, value).FromJust()); | ||
| 1089 | - } | ||
| 1090 | 1064 | } | |
| 1091 | 1065 | ||
| 1092 | 1066 | ||
@@ -1096,7 +1070,7 @@ void Initialize(Local<Object> target, | |||
| 1096 | 1070 | void* priv) { | |
| 1097 | 1071 | Environment* env = Environment::GetCurrent(context); | |
| 1098 | 1072 | ||
| 1099 | - env->SetMethod(target, "setupBufferJS", SetupBufferJS); | ||
| 1073 | + env->SetMethod(target, "setBufferPrototype", SetBufferPrototype); | ||
| 1100 | 1074 | env->SetMethodNoSideEffect(target, "createFromString", CreateFromString); | |
| 1101 | 1075 | ||
| 1102 | 1076 | env->SetMethodNoSideEffect(target, "byteLengthUtf8", ByteLengthUtf8); | |
@@ -1121,6 +1095,32 @@ void Initialize(Local<Object> target, | |||
| 1121 | 1095 | target->Set(env->context(), | |
| 1122 | 1096 | FIXED_ONE_BYTE_STRING(env->isolate(), "kStringMaxLength"), | |
| 1123 | 1097 | Integer::New(env->isolate(), String::kMaxLength)).FromJust(); | |
| 1098 | + | ||
| 1099 | + env->SetMethodNoSideEffect(target, "asciiSlice", StringSlice<ASCII>); | ||
| 1100 | + env->SetMethodNoSideEffect(target, "base64Slice", StringSlice<BASE64>); | ||
| 1101 | + env->SetMethodNoSideEffect(target, "latin1Slice", StringSlice<LATIN1>); | ||
| 1102 | + env->SetMethodNoSideEffect(target, "hexSlice", StringSlice<HEX>); | ||
| 1103 | + env->SetMethodNoSideEffect(target, "ucs2Slice", StringSlice<UCS2>); | ||
| 1104 | + env->SetMethodNoSideEffect(target, "utf8Slice", StringSlice<UTF8>); | ||
| 1105 | + | ||
| 1106 | + env->SetMethod(target, "asciiWrite", StringWrite<ASCII>); | ||
| 1107 | + env->SetMethod(target, "base64Write", StringWrite<BASE64>); | ||
| 1108 | + env->SetMethod(target, "latin1Write", StringWrite<LATIN1>); | ||
| 1109 | + env->SetMethod(target, "hexWrite", StringWrite<HEX>); | ||
| 1110 | + env->SetMethod(target, "ucs2Write", StringWrite<UCS2>); | ||
| 1111 | + env->SetMethod(target, "utf8Write", StringWrite<UTF8>); | ||
| 1112 | + | ||
| 1113 | + // It can be a nullptr when running inside an isolate where we | ||
| 1114 | + // do not own the ArrayBuffer allocator. | ||
| 1115 | + if (uint32_t* zero_fill_field = env->isolate_data()->zero_fill_field()) { | ||
| 1116 | + Local<ArrayBuffer> array_buffer = ArrayBuffer::New( | ||
| 1117 | + env->isolate(), zero_fill_field, sizeof(*zero_fill_field)); | ||
| 1118 | + CHECK(target | ||
| 1119 | + ->Set(env->context(), | ||
| 1120 | + FIXED_ONE_BYTE_STRING(env->isolate(), "zeroFill"), | ||
| 1121 | + Uint32Array::New(array_buffer, 0, 1)) | ||
| 1122 | + .FromJust()); | ||
| 1123 | + } | ||
| 1124 | 1124 | } | |
| 1125 | 1125 | ||
| 1126 | 1126 | } // anonymous namespace | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -233,6 +233,7 @@ v8::MaybeLocal<v8::Uint8Array> New(Environment* env, | |||
| 233 | 233 | size_t byte_offset, | |
| 234 | 234 | size_t length) { | |
| 235 | 235 | v8::Local<v8::Uint8Array> ui = v8::Uint8Array::New(ab, byte_offset, length); | |
| 236 | + CHECK(!env->buffer_prototype_object().IsEmpty()); | ||
| 236 | 237 | v8::Maybe<bool> mb = | |
| 237 | 238 | ui->SetPrototype(env->context(), env->buffer_prototype_object()); | |
| 238 | 239 | if (mb.IsNothing()) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments