mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-14 00:23:38 +00:00
ref[javascript]: rename get set method
This commit is contained in:
+2
-2
@@ -34,14 +34,14 @@ public class JsBoolSerializer implements IJsSerializer {
|
||||
@Override
|
||||
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("buffer.writeBoolean({});", objectStr)).append(LS);
|
||||
builder.append(StringUtils.format("buffer.writeBool({});", objectStr)).append(LS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
|
||||
String result = "result" + GenerateProtocolFile.localVariableId++;
|
||||
GenerateProtocolFile.addTab(builder, deep);
|
||||
builder.append(StringUtils.format("const {} = buffer.readBoolean(); ", result)).append(LS);
|
||||
builder.append(StringUtils.format("const {} = buffer.readBool(); ", result)).append(LS);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,23 @@ const ByteBuffer = function() {
|
||||
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
|
||||
}
|
||||
|
||||
this.getBuffer = function() {
|
||||
return this.buffer;
|
||||
};
|
||||
|
||||
this.writeBytes = function(byteArray) {
|
||||
const length = byteArray.byteLength;
|
||||
this.ensureCapacity(length);
|
||||
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
|
||||
this.writeOffset += length;
|
||||
};
|
||||
|
||||
this.toBytes = function() {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
|
||||
this.getWriteOffset = function() {
|
||||
return this.writeOffset;
|
||||
}
|
||||
@@ -120,7 +137,7 @@ const ByteBuffer = function() {
|
||||
return this.writeOffset > this.readOffset;
|
||||
};
|
||||
|
||||
this.writeBoolean = function(value) {
|
||||
this.writeBool = function(value) {
|
||||
if (!(value === true || value === false)) {
|
||||
throw new Error('value must be true of false');
|
||||
}
|
||||
@@ -133,25 +150,12 @@ const ByteBuffer = function() {
|
||||
this.writeOffset++;
|
||||
};
|
||||
|
||||
this.readBoolean = function() {
|
||||
this.readBool = function() {
|
||||
const value = this.bufferView.getInt8(this.readOffset);
|
||||
this.readOffset++;
|
||||
return (value === 1);
|
||||
};
|
||||
|
||||
this.writeBytes = function(byteArray) {
|
||||
const length = byteArray.byteLength;
|
||||
this.ensureCapacity(length);
|
||||
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
|
||||
this.writeOffset += length;
|
||||
};
|
||||
|
||||
this.toBytes = function() {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
|
||||
this.writeByte = function(value) {
|
||||
this.ensureCapacity(1);
|
||||
this.bufferView.setInt8(this.writeOffset, value);
|
||||
@@ -386,7 +390,7 @@ const ByteBuffer = function() {
|
||||
|
||||
this.writePacketFlag = function(value) {
|
||||
const flag = (value === null) || (value === undefined);
|
||||
this.writeBoolean(!flag);
|
||||
this.writeBool(!flag);
|
||||
return flag;
|
||||
};
|
||||
|
||||
@@ -400,23 +404,23 @@ const ByteBuffer = function() {
|
||||
return protocolRegistration.read(this);
|
||||
};
|
||||
|
||||
this.writeBooleanArray = function(array) {
|
||||
this.writeBoolArray = function(array) {
|
||||
if (array === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(array.length);
|
||||
array.forEach(element => {
|
||||
this.writeBoolean(element);
|
||||
this.writeBool(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readBooleanArray = function() {
|
||||
this.readBoolArray = function() {
|
||||
const array = [];
|
||||
const length = this.readInt();
|
||||
if (length > 0) {
|
||||
for (let index = 0; index < length; index++) {
|
||||
array.push(this.readBoolean());
|
||||
array.push(this.readBool());
|
||||
}
|
||||
}
|
||||
return array;
|
||||
@@ -601,12 +605,12 @@ const ByteBuffer = function() {
|
||||
};
|
||||
|
||||
// ---------------------------------------------list-------------------------------------------
|
||||
this.writeBooleanList = function(list) {
|
||||
this.writeBooleanArray(list);
|
||||
this.writeBoolList = function(list) {
|
||||
this.writeBoolArray(list);
|
||||
};
|
||||
|
||||
this.readBooleanList = function() {
|
||||
return this.readBooleanArray();
|
||||
this.readBoolList = function() {
|
||||
return this.readBoolArray();
|
||||
};
|
||||
|
||||
this.writeByteList = function(list) {
|
||||
@@ -674,19 +678,19 @@ const ByteBuffer = function() {
|
||||
};
|
||||
|
||||
// ---------------------------------------------set-------------------------------------------
|
||||
this.writeBooleanSet = function(set) {
|
||||
this.writeBoolSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeBoolean(element);
|
||||
this.writeBool(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readBooleanSet = function() {
|
||||
return new Set(this.readBooleanArray());
|
||||
this.readBoolSet = function() {
|
||||
return new Set(this.readBoolArray());
|
||||
};
|
||||
|
||||
this.writeByteSet = function(set) {
|
||||
|
||||
@@ -8,11 +8,11 @@ function assert(flag) {
|
||||
}
|
||||
}
|
||||
|
||||
const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
|
||||
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');
|
||||
const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
|
||||
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
|
||||
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
|
||||
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
|
||||
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');
|
||||
|
||||
const arrayBytes = new Uint8Array(data.length);
|
||||
data.copy(arrayBytes, 0, 0, data.length);
|
||||
@@ -43,8 +43,8 @@ function byteBufferTest() {
|
||||
// boolean
|
||||
const testBoolean = [false, true];
|
||||
testBoolean.forEach((value) => {
|
||||
buffer.writeBoolean(value);
|
||||
assert(buffer.readBoolean() == value);
|
||||
buffer.writeBool(value);
|
||||
assert(buffer.readBool() == value);
|
||||
});
|
||||
|
||||
assert(buffer.writeOffset == testBoolean.length);
|
||||
|
||||
@@ -75,6 +75,23 @@ const ByteBuffer = function() {
|
||||
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
|
||||
}
|
||||
|
||||
this.getBuffer = function() {
|
||||
return this.buffer;
|
||||
};
|
||||
|
||||
this.writeBytes = function(byteArray) {
|
||||
const length = byteArray.byteLength;
|
||||
this.ensureCapacity(length);
|
||||
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
|
||||
this.writeOffset += length;
|
||||
};
|
||||
|
||||
this.toBytes = function() {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
|
||||
this.getWriteOffset = function() {
|
||||
return this.writeOffset;
|
||||
}
|
||||
@@ -120,7 +137,7 @@ const ByteBuffer = function() {
|
||||
return this.writeOffset > this.readOffset;
|
||||
};
|
||||
|
||||
this.writeBoolean = function(value) {
|
||||
this.writeBool = function(value) {
|
||||
if (!(value === true || value === false)) {
|
||||
throw new Error('value must be true of false');
|
||||
}
|
||||
@@ -133,25 +150,12 @@ const ByteBuffer = function() {
|
||||
this.writeOffset++;
|
||||
};
|
||||
|
||||
this.readBoolean = function() {
|
||||
this.readBool = function() {
|
||||
const value = this.bufferView.getInt8(this.readOffset);
|
||||
this.readOffset++;
|
||||
return (value === 1);
|
||||
};
|
||||
|
||||
this.writeBytes = function(byteArray) {
|
||||
const length = byteArray.byteLength;
|
||||
this.ensureCapacity(length);
|
||||
new Uint8Array(this.buffer).set(new Uint8Array(byteArray), this.writeOffset);
|
||||
this.writeOffset += length;
|
||||
};
|
||||
|
||||
this.toBytes = function() {
|
||||
const result = new ArrayBuffer(this.writeOffset);
|
||||
new Uint8Array(result).set(new Uint8Array(this.buffer.slice(0, this.writeOffset)));
|
||||
return result;
|
||||
};
|
||||
|
||||
this.writeByte = function(value) {
|
||||
this.ensureCapacity(1);
|
||||
this.bufferView.setInt8(this.writeOffset, value);
|
||||
@@ -386,7 +390,7 @@ const ByteBuffer = function() {
|
||||
|
||||
this.writePacketFlag = function(value) {
|
||||
const flag = (value === null) || (value === undefined);
|
||||
this.writeBoolean(!flag);
|
||||
this.writeBool(!flag);
|
||||
return flag;
|
||||
};
|
||||
|
||||
@@ -400,23 +404,23 @@ const ByteBuffer = function() {
|
||||
return protocolRegistration.read(this);
|
||||
};
|
||||
|
||||
this.writeBooleanArray = function(array) {
|
||||
this.writeBoolArray = function(array) {
|
||||
if (array === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(array.length);
|
||||
array.forEach(element => {
|
||||
this.writeBoolean(element);
|
||||
this.writeBool(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readBooleanArray = function() {
|
||||
this.readBoolArray = function() {
|
||||
const array = [];
|
||||
const length = this.readInt();
|
||||
if (length > 0) {
|
||||
for (let index = 0; index < length; index++) {
|
||||
array.push(this.readBoolean());
|
||||
array.push(this.readBool());
|
||||
}
|
||||
}
|
||||
return array;
|
||||
@@ -601,12 +605,12 @@ const ByteBuffer = function() {
|
||||
};
|
||||
|
||||
// ---------------------------------------------list-------------------------------------------
|
||||
this.writeBooleanList = function(list) {
|
||||
this.writeBooleanArray(list);
|
||||
this.writeBoolList = function(list) {
|
||||
this.writeBoolArray(list);
|
||||
};
|
||||
|
||||
this.readBooleanList = function() {
|
||||
return this.readBooleanArray();
|
||||
this.readBoolList = function() {
|
||||
return this.readBoolArray();
|
||||
};
|
||||
|
||||
this.writeByteList = function(list) {
|
||||
@@ -674,19 +678,19 @@ const ByteBuffer = function() {
|
||||
};
|
||||
|
||||
// ---------------------------------------------set-------------------------------------------
|
||||
this.writeBooleanSet = function(set) {
|
||||
this.writeBoolSet = function(set) {
|
||||
if (set === null) {
|
||||
this.writeInt(0);
|
||||
} else {
|
||||
this.writeInt(set.size);
|
||||
set.forEach(element => {
|
||||
this.writeBoolean(element);
|
||||
this.writeBool(element);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.readBooleanSet = function() {
|
||||
return new Set(this.readBooleanArray());
|
||||
this.readBoolSet = function() {
|
||||
return new Set(this.readBoolArray());
|
||||
};
|
||||
|
||||
this.writeByteSet = function(set) {
|
||||
|
||||
@@ -92,10 +92,10 @@ ComplexObject.write = function(buffer, packet) {
|
||||
buffer.writeDouble(packet.ff);
|
||||
buffer.writeDoubleArray(packet.fff);
|
||||
buffer.writeDoubleArray(packet.ffff);
|
||||
buffer.writeBoolean(packet.g);
|
||||
buffer.writeBoolean(packet.gg);
|
||||
buffer.writeBooleanArray(packet.ggg);
|
||||
buffer.writeBooleanArray(packet.gggg);
|
||||
buffer.writeBool(packet.g);
|
||||
buffer.writeBool(packet.gg);
|
||||
buffer.writeBoolArray(packet.ggg);
|
||||
buffer.writeBoolArray(packet.gggg);
|
||||
buffer.writeString(packet.jj);
|
||||
buffer.writeStringArray(packet.jjj);
|
||||
buffer.writePacket(packet.kk, 102);
|
||||
@@ -290,13 +290,13 @@ ComplexObject.read = function(buffer) {
|
||||
packet.fff = array22;
|
||||
const array23 = buffer.readDoubleArray();
|
||||
packet.ffff = array23;
|
||||
const result24 = buffer.readBoolean();
|
||||
const result24 = buffer.readBool();
|
||||
packet.g = result24;
|
||||
const result25 = buffer.readBoolean();
|
||||
const result25 = buffer.readBool();
|
||||
packet.gg = result25;
|
||||
const array26 = buffer.readBooleanArray();
|
||||
const array26 = buffer.readBoolArray();
|
||||
packet.ggg = array26;
|
||||
const array27 = buffer.readBooleanArray();
|
||||
const array27 = buffer.readBoolArray();
|
||||
packet.gggg = array27;
|
||||
const result28 = buffer.readString();
|
||||
packet.jj = result28;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
// 常规的对象,取所有语言语法的交集,基本上所有语言都支持下面的语法
|
||||
const NormalObject = function() {
|
||||
this.a = 0; // number
|
||||
this.aaa = []; // Array<number>
|
||||
this.b = 0; // number
|
||||
// 整数类型
|
||||
this.c = 0; // number
|
||||
this.d = 0; // number
|
||||
this.e = 0; // number
|
||||
@@ -42,7 +43,7 @@ NormalObject.write = function(buffer, packet) {
|
||||
buffer.writeLong(packet.d);
|
||||
buffer.writeFloat(packet.e);
|
||||
buffer.writeDouble(packet.f);
|
||||
buffer.writeBoolean(packet.g);
|
||||
buffer.writeBool(packet.g);
|
||||
buffer.writeString(packet.jj);
|
||||
buffer.writePacket(packet.kk, 102);
|
||||
buffer.writeIntList(packet.l);
|
||||
@@ -79,7 +80,7 @@ NormalObject.read = function(buffer) {
|
||||
packet.e = result5;
|
||||
const result6 = buffer.readDouble();
|
||||
packet.f = result6;
|
||||
const result7 = buffer.readBoolean();
|
||||
const result7 = buffer.readBool();
|
||||
packet.g = result7;
|
||||
const result8 = buffer.readString();
|
||||
packet.jj = result8;
|
||||
|
||||
@@ -17,7 +17,7 @@ ObjectB.write = function(buffer, packet) {
|
||||
}
|
||||
const beforeWriteIndex = buffer.getWriteOffset();
|
||||
buffer.writeInt(4);
|
||||
buffer.writeBoolean(packet.flag);
|
||||
buffer.writeBool(packet.flag);
|
||||
buffer.writeInt(packet.innerCompatibleValue);
|
||||
buffer.adjustPadding(4, beforeWriteIndex);
|
||||
};
|
||||
@@ -29,7 +29,7 @@ ObjectB.read = function(buffer) {
|
||||
}
|
||||
const beforeReadIndex = buffer.getReadOffset();
|
||||
const packet = new ObjectB();
|
||||
const result0 = buffer.readBoolean();
|
||||
const result0 = buffer.readBool();
|
||||
packet.flag = result0;
|
||||
if (buffer.compatibleRead(beforeReadIndex, length)) {
|
||||
const result1 = buffer.readInt();
|
||||
|
||||
@@ -17,7 +17,7 @@ SimpleObject.write = function(buffer, packet) {
|
||||
}
|
||||
buffer.writeInt(-1);
|
||||
buffer.writeInt(packet.c);
|
||||
buffer.writeBoolean(packet.g);
|
||||
buffer.writeBool(packet.g);
|
||||
};
|
||||
|
||||
SimpleObject.read = function(buffer) {
|
||||
@@ -29,7 +29,7 @@ SimpleObject.read = function(buffer) {
|
||||
const packet = new SimpleObject();
|
||||
const result0 = buffer.readInt();
|
||||
packet.c = result0;
|
||||
const result1 = buffer.readBoolean();
|
||||
const result1 = buffer.readBool();
|
||||
packet.g = result1;
|
||||
if (length > 0) {
|
||||
buffer.setReadOffset(beforeReadIndex + length);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user