test[javascript]: JavaScript test

This commit is contained in:
godotg
2024-04-23 16:08:04 +08:00
parent 884fe85fee
commit 0d5d668156
10 changed files with 34 additions and 38 deletions
@@ -124,7 +124,7 @@ public abstract class GenerateJsUtils {
var readObject = readObject(registration);
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName
, fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName
, fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName);
var outputPath = StringUtils.format("{}/{}/{}.js", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
var file = new File(outputPath);
@@ -144,7 +144,7 @@ public abstract class GenerateJsUtils {
var fieldName = field.getName();
// 生成注释
var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.JavaScript);
for(var fieldNote : fieldNotes) {
for (var fieldNote : fieldNotes) {
fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS);
}
var triple = jsSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
@@ -3,8 +3,10 @@ const {} = function() {
{}
};
{}.PROTOCOL_ID = {};
{}.prototype.protocolId = function() {
return {};
return {}.PROTOCOL_ID;
};
{}.write = function(buffer, packet) {
@@ -20,6 +20,10 @@ const decoder = new TextDecoder('utf-8');
// const encoder = new util.TextEncoder('utf-8');
// const decoder = new util.TextDecoder('utf-8');
// 现在所有主流浏览器都支持TextDecoder,只有微信小程序不支持TextDecoder(微信浏览器也支持,微信的小程序和浏览器不是同一个js环境)
// https://developers.weixin.qq.com/community/develop/doc/000ca85023ce78c8484e0d1d256400
// 如果在微信小程序中使用,需要按照上面的链接全局引入TextEncoder相关依赖
// 在js中long可以支持的最大值
// const maxLong = 9007199254740992;
// const minLong = -9007199254740992;
@@ -50,13 +50,15 @@ const ComplexObject = function() {
this.sss = new Set(); // Set<Set<ObjectA>>
this.ssss = new Set(); // Set<string>
this.sssss = new Set(); // Set<Map<number, string>>
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,按照增加的顺序添加order
// 如果要修改协议并且兼容老协议,需要加上Compatible注解,保持Compatible注解的value自增
this.myCompatible = 0; // number
this.myObject = null; // ObjectA | null
};
ComplexObject.PROTOCOL_ID = 100;
ComplexObject.prototype.protocolId = function() {
return 100;
return ComplexObject.PROTOCOL_ID;
};
ComplexObject.write = function(buffer, packet) {
@@ -3,8 +3,10 @@ const EmptyObject = function() {
};
EmptyObject.PROTOCOL_ID = 0;
EmptyObject.prototype.protocolId = function() {
return 0;
return EmptyObject.PROTOCOL_ID;
};
EmptyObject.write = function(buffer, packet) {
@@ -18,11 +18,12 @@ const NormalObject = function() {
this.mm = new Map(); // Map<number, ObjectA>
this.s = new Set(); // Set<number>
this.ssss = new Set(); // Set<string>
this.outCompatibleValue = 0; // number
};
NormalObject.PROTOCOL_ID = 101;
NormalObject.prototype.protocolId = function() {
return 101;
return NormalObject.PROTOCOL_ID;
};
NormalObject.write = function(buffer, packet) {
@@ -30,8 +31,7 @@ NormalObject.write = function(buffer, packet) {
buffer.writeInt(0);
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(854);
buffer.writeInt(-1);
buffer.writeByte(packet.a);
buffer.writeByteArray(packet.aaa);
buffer.writeShort(packet.b);
@@ -50,8 +50,6 @@ NormalObject.write = function(buffer, packet) {
buffer.writeIntPacketMap(packet.mm, 102);
buffer.writeIntSet(packet.s);
buffer.writeStringSet(packet.ssss);
buffer.writeInt(packet.outCompatibleValue);
buffer.adjustPadding(854, beforeWriteIndex);
};
NormalObject.read = function(buffer) {
@@ -97,10 +95,6 @@ NormalObject.read = function(buffer) {
packet.s = set16;
const set17 = buffer.readStringSet();
packet.ssss = set17;
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result18 = buffer.readInt();
packet.outCompatibleValue = result18;
}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
@@ -3,11 +3,12 @@ const ObjectA = function() {
this.a = 0; // number
this.m = new Map(); // Map<number, string>
this.objectB = null; // ObjectB | null
this.innerCompatibleValue = 0; // number
};
ObjectA.PROTOCOL_ID = 102;
ObjectA.prototype.protocolId = function() {
return 102;
return ObjectA.PROTOCOL_ID;
};
ObjectA.write = function(buffer, packet) {
@@ -15,13 +16,10 @@ ObjectA.write = function(buffer, packet) {
buffer.writeInt(0);
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(201);
buffer.writeInt(-1);
buffer.writeInt(packet.a);
buffer.writeIntStringMap(packet.m);
buffer.writePacket(packet.objectB, 103);
buffer.writeInt(packet.innerCompatibleValue);
buffer.adjustPadding(201, beforeWriteIndex);
};
ObjectA.read = function(buffer) {
@@ -37,10 +35,6 @@ ObjectA.read = function(buffer) {
packet.m = map1;
const result2 = buffer.readPacket(103);
packet.objectB = result2;
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result3 = buffer.readInt();
packet.innerCompatibleValue = result3;
}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
@@ -1,11 +1,12 @@
const ObjectB = function() {
this.flag = false; // boolean
this.innerCompatibleValue = 0; // number
};
ObjectB.PROTOCOL_ID = 103;
ObjectB.prototype.protocolId = function() {
return 103;
return ObjectB.PROTOCOL_ID;
};
ObjectB.write = function(buffer, packet) {
@@ -13,11 +14,8 @@ ObjectB.write = function(buffer, packet) {
buffer.writeInt(0);
return;
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(4);
buffer.writeInt(-1);
buffer.writeBoolean(packet.flag);
buffer.writeInt(packet.innerCompatibleValue);
buffer.adjustPadding(4, beforeWriteIndex);
};
ObjectB.read = function(buffer) {
@@ -29,10 +27,6 @@ ObjectB.read = function(buffer) {
const packet = new ObjectB();
const result0 = buffer.readBoolean();
packet.flag = result0;
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result1 = buffer.readInt();
packet.innerCompatibleValue = result1;
}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
@@ -4,8 +4,10 @@ const SimpleObject = function() {
this.g = false; // boolean
};
SimpleObject.PROTOCOL_ID = 104;
SimpleObject.prototype.protocolId = function() {
return 104;
return SimpleObject.PROTOCOL_ID;
};
SimpleObject.write = function(buffer, packet) {
@@ -3346,8 +3346,10 @@ const VeryBigObject = function() {
this.ssss88 = new Set(); // Set<string>
};
VeryBigObject.PROTOCOL_ID = 1;
VeryBigObject.prototype.protocolId = function() {
return 1;
return VeryBigObject.PROTOCOL_ID;
};
VeryBigObject.write = function(buffer, packet) {