Skip to content

Commit 36a7795

Browse files
vkurchatkintrevnorris
authored andcommitted
buffer: expose internals on binding
Remove internal object and expose functions directly on binding. This makes possible to simply use internal functions in other builtin modules. PR-URL: #770 Reviewed-by: Trevor Norris <[email protected]> Reviewed-by: Ben Noordhuis <[email protected]>
1 parent 8aed9d6 commit 36a7795

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

lib/buffer.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22

3-
const buffer = process.binding('buffer');
3+
const binding = process.binding('buffer');
44
const smalloc = process.binding('smalloc');
55
const util = require('util');
66
const alloc = smalloc.alloc;
77
const truncate = smalloc.truncate;
88
const sliceOnto = smalloc.sliceOnto;
99
const kMaxLength = smalloc.kMaxLength;
10-
var internal = {};
1110

1211
exports.Buffer = Buffer;
1312
exports.SlowBuffer = SlowBuffer;
@@ -124,7 +123,7 @@ NativeBuffer.prototype = Buffer.prototype;
124123

125124

126125
// add methods to Buffer prototype
127-
buffer.setupBufferJS(NativeBuffer, internal);
126+
binding.setupBufferJS(NativeBuffer);
128127

129128

130129
// Static methods
@@ -142,7 +141,7 @@ Buffer.compare = function compare(a, b) {
142141
if (a === b)
143142
return 0;
144143

145-
return internal.compare(a, b);
144+
return binding.compare(a, b);
146145
};
147146

148147

@@ -215,7 +214,7 @@ Buffer.byteLength = function(str, enc) {
215214
ret = str.length >>> 1;
216215
break;
217216
default:
218-
ret = internal.byteLength(str, enc);
217+
ret = binding.byteLength(str, enc);
219218
}
220219
return ret;
221220
};
@@ -274,7 +273,7 @@ Buffer.prototype.equals = function equals(b) {
274273
if (this === b)
275274
return true;
276275

277-
return internal.compare(this, b) === 0;
276+
return binding.compare(this, b) === 0;
278277
};
279278

280279

@@ -298,7 +297,7 @@ Buffer.prototype.compare = function compare(b) {
298297
if (this === b)
299298
return 0;
300299

301-
return internal.compare(this, b);
300+
return binding.compare(this, b);
302301
};
303302

304303

@@ -319,7 +318,7 @@ Buffer.prototype.fill = function fill(val, start, end) {
319318
val = code;
320319
}
321320

322-
internal.fill(this, val, start, end);
321+
binding.fill(this, val, start, end);
323322

324323
return this;
325324
};
@@ -663,31 +662,31 @@ Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
663662
offset = offset >>> 0;
664663
if (!noAssert)
665664
checkOffset(offset, 4, this.length);
666-
return internal.readFloatLE(this, offset);
665+
return binding.readFloatLE(this, offset);
667666
};
668667

669668

670669
Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
671670
offset = offset >>> 0;
672671
if (!noAssert)
673672
checkOffset(offset, 4, this.length);
674-
return internal.readFloatBE(this, offset);
673+
return binding.readFloatBE(this, offset);
675674
};
676675

677676

678677
Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
679678
offset = offset >>> 0;
680679
if (!noAssert)
681680
checkOffset(offset, 8, this.length);
682-
return internal.readDoubleLE(this, offset);
681+
return binding.readDoubleLE(this, offset);
683682
};
684683

685684

686685
Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
687686
offset = offset >>> 0;
688687
if (!noAssert)
689688
checkOffset(offset, 8, this.length);
690-
return internal.readDoubleBE(this, offset);
689+
return binding.readDoubleBE(this, offset);
691690
};
692691

693692

@@ -910,7 +909,7 @@ Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) {
910909
offset = offset >>> 0;
911910
if (!noAssert)
912911
checkFloat(this, val, offset, 4);
913-
internal.writeFloatLE(this, val, offset);
912+
binding.writeFloatLE(this, val, offset);
914913
return offset + 4;
915914
};
916915

@@ -920,7 +919,7 @@ Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) {
920919
offset = offset >>> 0;
921920
if (!noAssert)
922921
checkFloat(this, val, offset, 4);
923-
internal.writeFloatBE(this, val, offset);
922+
binding.writeFloatBE(this, val, offset);
924923
return offset + 4;
925924
};
926925

@@ -930,7 +929,7 @@ Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) {
930929
offset = offset >>> 0;
931930
if (!noAssert)
932931
checkFloat(this, val, offset, 8);
933-
internal.writeDoubleLE(this, val, offset);
932+
binding.writeDoubleLE(this, val, offset);
934933
return offset + 8;
935934
};
936935

@@ -940,7 +939,7 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
940939
offset = offset >>> 0;
941940
if (!noAssert)
942941
checkFloat(this, val, offset, 8);
943-
internal.writeDoubleBE(this, val, offset);
942+
binding.writeDoubleBE(this, val, offset);
944943
return offset + 8;
945944
};
946945

src/node_buffer.cc

+17-21
Original file line numberDiff line numberDiff line change
@@ -636,34 +636,30 @@ void SetupBufferJS(const FunctionCallbackInfo<Value>& args) {
636636
proto->ForceSet(env->offset_string(),
637637
Uint32::New(env->isolate(), 0),
638638
v8::ReadOnly);
639-
640-
CHECK(args[1]->IsObject());
641-
642-
Local<Object> internal = args[1].As<Object>();
643-
ASSERT(internal->IsObject());
644-
645-
env->SetMethod(internal, "byteLength", ByteLength);
646-
env->SetMethod(internal, "compare", Compare);
647-
env->SetMethod(internal, "fill", Fill);
648-
649-
env->SetMethod(internal, "readDoubleBE", ReadDoubleBE);
650-
env->SetMethod(internal, "readDoubleLE", ReadDoubleLE);
651-
env->SetMethod(internal, "readFloatBE", ReadFloatBE);
652-
env->SetMethod(internal, "readFloatLE", ReadFloatLE);
653-
654-
env->SetMethod(internal, "writeDoubleBE", WriteDoubleBE);
655-
env->SetMethod(internal, "writeDoubleLE", WriteDoubleLE);
656-
env->SetMethod(internal, "writeFloatBE", WriteFloatBE);
657-
env->SetMethod(internal, "writeFloatLE", WriteFloatLE);
658639
}
659640

660641

661642
void Initialize(Handle<Object> target,
662643
Handle<Value> unused,
663644
Handle<Context> context) {
664645
Environment* env = Environment::GetCurrent(context);
665-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "setupBufferJS"),
666-
env->NewFunctionTemplate(SetupBufferJS)->GetFunction());
646+
647+
env->SetMethod(target, "setupBufferJS", SetupBufferJS);
648+
649+
env->SetMethod(target, "byteLength", ByteLength);
650+
env->SetMethod(target, "byteLength", ByteLength);
651+
env->SetMethod(target, "compare", Compare);
652+
env->SetMethod(target, "fill", Fill);
653+
654+
env->SetMethod(target, "readDoubleBE", ReadDoubleBE);
655+
env->SetMethod(target, "readDoubleLE", ReadDoubleLE);
656+
env->SetMethod(target, "readFloatBE", ReadFloatBE);
657+
env->SetMethod(target, "readFloatLE", ReadFloatLE);
658+
659+
env->SetMethod(target, "writeDoubleBE", WriteDoubleBE);
660+
env->SetMethod(target, "writeDoubleLE", WriteDoubleLE);
661+
env->SetMethod(target, "writeFloatBE", WriteFloatBE);
662+
env->SetMethod(target, "writeFloatLE", WriteFloatLE);
667663
}
668664

669665

0 commit comments

Comments
 (0)