From 4c5fb4e2a1de8b7b737618d30fa70c2c4504be3f Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 20 Apr 2024 14:57:28 +0800 Subject: [PATCH] feat[es]: es support --- .../generate/GenerateProtocolFile.java | 10 + .../protocol/serializer/CodeLanguage.java | 14 +- .../serializer/CutDownArraySerializer.java | 32 + .../serializer/CutDownListSerializer.java | 18 + .../serializer/CutDownMapSerializer.java | 24 + .../serializer/CutDownSetSerializer.java | 18 + .../serializer/es/EsArraySerializer.java | 101 ++ .../serializer/es/EsBooleanSerializer.java | 47 + .../serializer/es/EsByteSerializer.java | 47 + .../serializer/es/EsDoubleSerializer.java | 47 + .../serializer/es/EsFloatSerializer.java | 47 + .../serializer/es/EsIntSerializer.java | 47 + .../serializer/es/EsListSerializer.java | 100 ++ .../serializer/es/EsLongSerializer.java | 47 + .../serializer/es/EsMapSerializer.java | 110 ++ .../es/EsObjectProtocolSerializer.java | 54 + .../serializer/es/EsSetSerializer.java | 101 ++ .../serializer/es/EsShortSerializer.java | 47 + .../serializer/es/EsStringSerializer.java | 47 + .../serializer/es/GenerateEsUtils.java | 199 +++ .../protocol/serializer/es/IEsSerializer.java | 35 + .../resources/es/ProtocolManagerTemplate.mjs | 32 + .../main/resources/es/ProtocolTemplate.mjs | 32 + .../main/resources/es/buffer/ByteBuffer.mjs | 1121 ++++++++++++++ .../src/main/resources/es/buffer/long.mjs | 1326 +++++++++++++++++ .../src/main/resources/es/buffer/longbits.mjs | 184 +++ 26 files changed, 3881 insertions(+), 6 deletions(-) create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsArraySerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsBooleanSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsByteSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsDoubleSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsFloatSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsIntSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsListSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsLongSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsMapSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsObjectProtocolSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsSetSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsShortSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/EsStringSerializer.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/GenerateEsUtils.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/es/IEsSerializer.java create mode 100644 protocol/src/main/resources/es/ProtocolManagerTemplate.mjs create mode 100644 protocol/src/main/resources/es/ProtocolTemplate.mjs create mode 100644 protocol/src/main/resources/es/buffer/ByteBuffer.mjs create mode 100644 protocol/src/main/resources/es/buffer/long.mjs create mode 100644 protocol/src/main/resources/es/buffer/longbits.mjs diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java index f0efc51d..5d63f820 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -20,6 +20,7 @@ import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.serializer.CodeLanguage; import com.zfoo.protocol.serializer.cpp.GenerateCppUtils; import com.zfoo.protocol.serializer.csharp.GenerateCsUtils; +import com.zfoo.protocol.serializer.es.GenerateEsUtils; import com.zfoo.protocol.serializer.gdscript.GenerateGdUtils; import com.zfoo.protocol.serializer.go.GenerateGoUtils; import com.zfoo.protocol.serializer.javascript.GenerateJsUtils; @@ -165,6 +166,15 @@ public abstract class GenerateProtocolFile { GenerateJsUtils.createProtocolManager(allSortedGenerateProtocols); } + // 生成Javascript协议 + if (generateLanguages.contains(CodeLanguage.ES)) { + GenerateEsUtils.init(generateOperation); + for (var protocolRegistration : allSortedGenerateProtocols) { + GenerateEsUtils.createJsProtocolFile((ProtocolRegistration) protocolRegistration); + } + GenerateEsUtils.createProtocolManager(allSortedGenerateProtocols); + } + // 生成TypeScript协议 if (generateLanguages.contains(CodeLanguage.TypeScript)) { GenerateTsUtils.init(generateOperation); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java index 7283095d..15090b8a 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java @@ -28,17 +28,19 @@ public enum CodeLanguage { JavaScript(1 << 3), - TypeScript(1 << 4), + ES(1 << 4), - Lua(1 << 5), + TypeScript(1 << 5), - CSharp(1 << 6), + Lua(1 << 10), - GdScript(1 << 7), + CSharp(1 << 11), - Python(1 << 8), + GdScript(1 << 12), - Protobuf(1 << 12); + Python(1 << 13), + + Protobuf(1 << 30); public final int id; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java index ef5569ad..eb373de3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -65,6 +65,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; @@ -92,6 +93,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS); break; @@ -119,6 +121,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; @@ -146,6 +149,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS); break; @@ -173,6 +177,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; @@ -200,6 +205,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS); break; @@ -227,6 +233,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; @@ -254,6 +261,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS); break; @@ -281,6 +289,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; @@ -308,6 +317,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS); break; @@ -335,6 +345,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; @@ -362,6 +373,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS); break; @@ -389,6 +401,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; @@ -416,6 +429,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS); break; @@ -443,6 +457,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS); break; @@ -471,6 +486,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("buffer.writePacketArray<{}>({}, {});", EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), objectStr, protocolId)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); break; @@ -520,6 +536,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); break; @@ -551,6 +568,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS); break; @@ -582,6 +600,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); break; @@ -613,6 +632,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS); break; @@ -644,6 +664,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); break; @@ -675,6 +696,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS); break; @@ -706,6 +728,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); break; @@ -737,6 +760,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS); break; @@ -768,6 +792,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); break; @@ -799,6 +824,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS); break; @@ -830,6 +856,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); break; @@ -861,6 +888,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS); break; @@ -892,6 +920,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); break; @@ -923,6 +952,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS); break; @@ -954,6 +984,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringArray();", array)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringArray();", array)).append(LS); break; @@ -982,6 +1013,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readPacketArray<{}>({});", array, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, protocolId)).append(LS); break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java index 17ec90e0..167cd50e 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -64,6 +64,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeBooleanList({});", objectStr)).append(LS); break; @@ -91,6 +92,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeByteList({});", objectStr)).append(LS); break; @@ -118,6 +120,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeShortList({});", objectStr)).append(LS); break; @@ -145,6 +148,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntList({});", objectStr)).append(LS); break; @@ -172,6 +176,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongList({});", objectStr)).append(LS); break; @@ -200,6 +205,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeFloatList({});", objectStr)).append(LS); break; @@ -228,6 +234,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeDoubleList({});", objectStr)).append(LS); break; @@ -256,6 +263,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringList({});", objectStr)).append(LS); break; @@ -283,6 +291,7 @@ public class CutDownListSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS); break; @@ -331,6 +340,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readBooleanList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readBooleanList();", list)).append(LS); break; @@ -362,6 +372,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readByteList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readByteList();", list)).append(LS); break; @@ -393,6 +404,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readShortList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readShortList();", list)).append(LS); break; @@ -424,6 +436,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntList();", list)).append(LS); break; @@ -455,6 +468,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongList();", list)).append(LS); break; @@ -486,6 +500,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readFloatList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readFloatList();", list)).append(LS); break; @@ -517,6 +532,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readDoubleList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readDoubleList();", list)).append(LS); break; @@ -548,6 +564,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringList();", list)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringList();", list)).append(LS); break; @@ -578,6 +595,7 @@ public class CutDownListSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketList({});", list, protocolId)).append(LS); break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java index 65e24a8d..cb87d06e 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -71,6 +71,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntIntMap({});", objectStr)).append(LS); return true; @@ -95,6 +96,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntLongMap({});", objectStr)).append(LS); return true; @@ -119,6 +121,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS); return true; @@ -140,6 +143,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -169,6 +173,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongIntMap({});", objectStr)).append(LS); return true; @@ -193,6 +198,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongLongMap({});", objectStr)).append(LS); return true; @@ -217,6 +223,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongStringMap({});", objectStr)).append(LS); return true; @@ -238,6 +245,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -267,6 +275,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringIntMap({});", objectStr)).append(LS); return true; @@ -291,6 +300,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringLongMap({});", objectStr)).append(LS); return true; @@ -315,6 +325,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringStringMap({});", objectStr)).append(LS); return true; @@ -336,6 +347,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { return true; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; @@ -385,6 +397,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntIntMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntIntMap();", map)).append(LS); return map; @@ -413,6 +426,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntLongMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntLongMap();", map)).append(LS); return map; @@ -441,6 +455,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntStringMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntStringMap();", map)).append(LS); return map; @@ -467,6 +482,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, protocolId)).append(LS); return map; @@ -499,6 +515,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongIntMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongIntMap();", map)).append(LS); return map; @@ -527,6 +544,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongLongMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongLongMap();", map)).append(LS); return map; @@ -555,6 +573,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongStringMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongStringMap();", map)).append(LS); return map; @@ -581,6 +600,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, protocolId)).append(LS); return map; @@ -614,6 +634,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringIntMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringIntMap();", map)).append(LS); return map; @@ -642,6 +663,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringLongMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringLongMap();", map)).append(LS); return map; @@ -670,6 +692,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringStringMap();", map)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringStringMap();", map)).append(LS); return map; @@ -696,6 +719,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); return map; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, protocolId)).append(LS); return map; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java index 0e40d7f8..2fabe4a5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -67,6 +67,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeBooleanSet({});", objectStr)).append(LS); break; @@ -96,6 +97,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeByteSet({});", objectStr)).append(LS); break; @@ -125,6 +127,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeShortSet({});", objectStr)).append(LS); break; @@ -154,6 +157,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeIntSet({});", objectStr)).append(LS); break; @@ -183,6 +187,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeLongSet({});", objectStr)).append(LS); break; @@ -212,6 +217,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeFloatSet({});", objectStr)).append(LS); break; @@ -241,6 +247,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeDoubleSet({});", objectStr)).append(LS); break; @@ -270,6 +277,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writeStringSet({});", objectStr)).append(LS); break; @@ -299,6 +307,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { break; case Cpp: case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS); break; @@ -347,6 +356,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readBooleanSet();", set)).append(LS); break; @@ -378,6 +388,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readByteSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readByteSet();", set)).append(LS); break; @@ -409,6 +420,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readShortSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readShortSet();", set)).append(LS); break; @@ -440,6 +452,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readIntSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntSet();", set)).append(LS); break; @@ -471,6 +484,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readLongSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongSet();", set)).append(LS); break; @@ -502,6 +516,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readFloatSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readFloatSet();", set)).append(LS); break; @@ -533,6 +548,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readDoubleSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readDoubleSet();", set)).append(LS); break; @@ -564,6 +580,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readStringSet();", set)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringSet();", set)).append(LS); break; @@ -594,6 +611,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { builder.append(StringUtils.format("auto {} = buffer.readPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS); break; case JavaScript: + case ES: case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketSet({});", set, protocolId)).append(LS); break; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsArraySerializer.java new file mode 100644 index 00000000..025de2fd --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsArraySerializer.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.ArrayField; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownArraySerializer; +import com.zfoo.protocol.serializer.typescript.GenerateTsUtils; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsArraySerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + var type = StringUtils.format("Array<{}>", GenerateTsUtils.toTsClassName(field.getType().getComponentType().getSimpleName())); + return new Triple<>(type, field.getName(), "[]"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.ES)) { + return; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; + + builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0);").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("} else {").append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS); + + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("{}.forEach({} => {", objectStr, element)).append(LS); + GenerateEsUtils.esSerializer(arrayField.getArrayElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("});").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.ES); + if (cutDown != null) { + return cutDown; + } + + ArrayField arrayField = (ArrayField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("const {} = [];", result)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + String readObject = GenerateEsUtils.esSerializer(arrayField.getArrayElementRegistration().serializer()) + .readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.push({});", result, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsBooleanSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsBooleanSerializer.java new file mode 100644 index 00000000..6f26f204 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsBooleanSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsBooleanSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("boolean", field.getName(), "false"); + } + + @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); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readBoolean(); ", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsByteSerializer.java new file mode 100644 index 00000000..d075f16a --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsByteSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsByteSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("number", field.getName(), "0"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeByte({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readByte();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsDoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsDoubleSerializer.java new file mode 100644 index 00000000..f96f1065 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsDoubleSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsDoubleSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("number", field.getName(), "0"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeDouble({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readDouble();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsFloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsFloatSerializer.java new file mode 100644 index 00000000..6bfdb533 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsFloatSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsFloatSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("number", field.getName(), "0"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeFloat({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readFloat();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsIntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsIntSerializer.java new file mode 100644 index 00000000..1cb73907 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsIntSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsIntSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("number", field.getName(), "0"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeInt({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readInt();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsListSerializer.java new file mode 100644 index 00000000..a258d0a8 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsListSerializer.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.ListField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownListSerializer; +import com.zfoo.protocol.serializer.typescript.GenerateTsUtils; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsListSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>(GenerateTsUtils.toTsClassName(field.getGenericType().toString()), field.getName(), "[]"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.ES)) { + return; + } + + ListField listField = (ListField) fieldRegistration; + + builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0);").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("} else {").append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS); + + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("{}.forEach({} => {", objectStr, element)).append(LS); + GenerateEsUtils.esSerializer(listField.getListElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, listField.getListElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("});").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.ES); + if (cutDown != null) { + return cutDown; + } + + ListField listField = (ListField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("const {} = [];", result)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + String readObject = GenerateEsUtils.esSerializer(listField.getListElementRegistration().serializer()) + .readObject(builder, deep + 2, field, listField.getListElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.push({});", result, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsLongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsLongSerializer.java new file mode 100644 index 00000000..95c1c629 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsLongSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsLongSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("number", field.getName(), "0"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeLong({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readLong();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsMapSerializer.java new file mode 100644 index 00000000..b10ed49f --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsMapSerializer.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.MapField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownMapSerializer; +import com.zfoo.protocol.serializer.typescript.GenerateTsUtils; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsMapSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>(GenerateTsUtils.toTsClassName(field.getGenericType().toString()), field.getName(), "new Map()"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.ES)) { + return; + } + + MapField mapField = (MapField) fieldRegistration; + builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0);").append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append("} else {").append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.size);", objectStr)).append(LS); + + String key = "key" + GenerateProtocolFile.index.getAndIncrement(); + String value = "value" + GenerateProtocolFile.index.getAndIncrement(); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("{}.forEach(({}, {}) => {", objectStr, value, key)).append(LS); + GenerateEsUtils.esSerializer(mapField.getMapKeyRegistration().serializer()) + .writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration()); + GenerateEsUtils.esSerializer(mapField.getMapValueRegistration().serializer()) + .writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("});").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.ES); + if (cutDown != null) { + return cutDown; + } + + MapField mapField = (MapField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("const {} = new Map();", result)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS); + + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + + String keyObject = GenerateEsUtils.esSerializer(mapField.getMapKeyRegistration().serializer()) + .readObject(builder, deep + 2, field, mapField.getMapKeyRegistration()); + + + String valueObject = GenerateEsUtils.esSerializer(mapField.getMapValueRegistration().serializer()) + .readObject(builder, deep + 2, field, mapField.getMapValueRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + + builder.append(StringUtils.format("{}.set({}, {});", result, keyObject, valueObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsObjectProtocolSerializer.java new file mode 100644 index 00000000..ae12a855 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsObjectProtocolSerializer.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.ObjectProtocolField; +import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsObjectProtocolSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()); + var type = StringUtils.format("{} | null", protocolSimpleName); + return new Triple<>(type, field.getName(), "null"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writePacket({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration; + var result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readPacket({});", result, objectProtocolField.getProtocolId())).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsSetSerializer.java new file mode 100644 index 00000000..b611649b --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsSetSerializer.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.registration.field.SetField; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CutDownSetSerializer; +import com.zfoo.protocol.serializer.typescript.GenerateTsUtils; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsSetSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>(GenerateTsUtils.toTsClassName(field.getGenericType().toString()), field.getName(), "new Set()"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.ES)) { + return; + } + + SetField setField = (SetField) fieldRegistration; + + builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("buffer.writeInt(0);").append(LS); + GenerateProtocolFile.addTab(builder, deep); + + builder.append("} else {").append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("buffer.writeInt({}.size);", objectStr)).append(LS); + + String element = "element" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append(StringUtils.format("{}.forEach({} => {", objectStr, element)).append(LS); + GenerateEsUtils.esSerializer(setField.getSetElementRegistration().serializer()) + .writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("});").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.ES); + if (cutDown != null) { + return cutDown; + } + + SetField setField = (SetField) fieldRegistration; + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + + builder.append(StringUtils.format("const {} = new Set();", result)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + String size = "size" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS); + + GenerateProtocolFile.addTab(builder, deep + 1); + String i = "index" + GenerateProtocolFile.index.getAndIncrement(); + builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS); + String readObject = GenerateEsUtils.esSerializer(setField.getSetElementRegistration().serializer()) + .readObject(builder, deep + 2, field, setField.getSetElementRegistration()); + GenerateProtocolFile.addTab(builder, deep + 2); + builder.append(StringUtils.format("{}.add({});", result, readObject)).append(LS); + GenerateProtocolFile.addTab(builder, deep + 1); + builder.append("}").append(LS); + GenerateProtocolFile.addTab(builder, deep); + builder.append("}").append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsShortSerializer.java new file mode 100644 index 00000000..3c99e89c --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsShortSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsShortSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("number", field.getName(), "0"); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeShort({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readShort();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsStringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsStringSerializer.java new file mode 100644 index 00000000..f336baef --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/EsStringSerializer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.util.StringUtils; + +import java.lang.reflect.Field; + +import static com.zfoo.protocol.util.FileUtils.LS; + +/** + * @author godotg + */ +public class EsStringSerializer implements IEsSerializer { + @Override + public Triple field(Field field, IFieldRegistration fieldRegistration) { + return new Triple<>("string", field.getName(), "\"\""); + } + + @Override + public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("buffer.writeString({});", objectStr)).append(LS); + } + + @Override + public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { + String result = "result" + GenerateProtocolFile.index.getAndIncrement(); + GenerateProtocolFile.addTab(builder, deep); + builder.append(StringUtils.format("const {} = buffer.readString();", result)).append(LS); + return result; + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/GenerateEsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/GenerateEsUtils.java new file mode 100644 index 00000000..27ec7b02 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/GenerateEsUtils.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.anno.Compatible; +import com.zfoo.protocol.generate.GenerateOperation; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.generate.GenerateProtocolNote; +import com.zfoo.protocol.generate.GenerateProtocolPath; +import com.zfoo.protocol.registration.IProtocolRegistration; +import com.zfoo.protocol.registration.ProtocolRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.reflect.*; +import com.zfoo.protocol.util.ClassUtils; +import com.zfoo.protocol.util.FileUtils; +import com.zfoo.protocol.util.ReflectionUtils; +import com.zfoo.protocol.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.zfoo.protocol.util.FileUtils.LS; +import static com.zfoo.protocol.util.StringUtils.TAB; + +/** + * @author godotg + */ +public abstract class GenerateEsUtils { + private static final Logger logger = LoggerFactory.getLogger(GenerateEsUtils.class); + // custom configuration + public static String protocolOutputRootPath = "zfooes"; + private static String protocolOutputPath = StringUtils.EMPTY; + + private static Map esSerializerMap; + + + public static IEsSerializer esSerializer(ISerializer serializer) { + return esSerializerMap.get(serializer); + } + + public static void init(GenerateOperation generateOperation) { + protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); + FileUtils.deleteFile(new File(protocolOutputPath)); + + esSerializerMap = new HashMap<>(); + esSerializerMap.put(BooleanSerializer.INSTANCE, new EsBooleanSerializer()); + esSerializerMap.put(ByteSerializer.INSTANCE, new EsByteSerializer()); + esSerializerMap.put(ShortSerializer.INSTANCE, new EsShortSerializer()); + esSerializerMap.put(IntSerializer.INSTANCE, new EsIntSerializer()); + esSerializerMap.put(LongSerializer.INSTANCE, new EsLongSerializer()); + esSerializerMap.put(FloatSerializer.INSTANCE, new EsFloatSerializer()); + esSerializerMap.put(DoubleSerializer.INSTANCE, new EsDoubleSerializer()); + esSerializerMap.put(StringSerializer.INSTANCE, new EsStringSerializer()); + esSerializerMap.put(ArraySerializer.INSTANCE, new EsArraySerializer()); + esSerializerMap.put(ListSerializer.INSTANCE, new EsListSerializer()); + esSerializerMap.put(SetSerializer.INSTANCE, new EsSetSerializer()); + esSerializerMap.put(MapSerializer.INSTANCE, new EsMapSerializer()); + esSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new EsObjectProtocolSerializer()); + } + + public static void clear() { + protocolOutputRootPath = null; + protocolOutputPath = null; + esSerializerMap = null; + } + + public static void createProtocolManager(List protocolList) throws IOException { + var list = List.of("es/buffer/ByteBuffer.mjs", "es/buffer/long.mjs", "es/buffer/longbits.mjs"); + for (var fileName : list) { + var fileInputStream = ClassUtils.getFileFromClassPath(fileName); + var outputPath = StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "es/")); + var createFile = new File(outputPath); + FileUtils.writeInputStreamToFile(createFile, fileInputStream); + } + + // 生成ProtocolManager.js文件 + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("es/ProtocolManagerTemplate.mjs"); + + var importBuilder = new StringBuilder(); + var initProtocolBuilder = new StringBuilder(); + for (var protocol : protocolList) { + var protocolId = protocol.protocolId(); + var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); + var path = GenerateProtocolPath.protocolAbsolutePath(protocol.protocolId(), CodeLanguage.ES); + importBuilder.append(StringUtils.format("import {} from './{}';", protocolName, path)).append(LS); + initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName)).append(LS); + } + + protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), StringUtils.EMPTY_JSON, initProtocolBuilder.toString().trim()); + var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.mjs")); + FileUtils.writeStringToFile(file, protocolManagerTemplate, true); + logger.info("Generated ES protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + } + + public static void createJsProtocolFile(ProtocolRegistration registration) throws IOException { + // 初始化index + GenerateProtocolFile.index.set(0); + + var protocolId = registration.protocolId(); + var registrationConstructor = registration.getConstructor(); + var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); + + var protocolTemplate = ClassUtils.getFileFromClassPathToString("es/ProtocolTemplate.mjs"); + + var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.ES, TAB, 0); + var fieldDefinition = fieldDefinition(registration); + var writeObject = writeObject(registration); + var readObject = readObject(registration); + + protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName + , fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName + , writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName); + var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName); + var file = new File(outputPath); + FileUtils.writeStringToFile(file, protocolTemplate, true); + logger.info("Generated ES protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + } + + private static String fieldDefinition(ProtocolRegistration registration) { + var protocolId = registration.getId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + // when generate source code fields, use origin fields sort + var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass()); + var fieldDefinitionBuilder = new StringBuilder(); + for (var field : sequencedFields) { + var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)]; + var fieldName = field.getName(); + // 生成注释 + var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.ES); + for(var fieldNote : fieldNotes) { + fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS); + } + var triple = esSerializer(fieldRegistration.serializer()).field(field, fieldRegistration); + fieldDefinitionBuilder.append(TAB) + .append(StringUtils.format("this.{} = {}; // {}", fieldName, triple.getRight(), triple.getLeft())) + .append(LS); + + } + return fieldDefinitionBuilder.toString(); + } + + private static String writeObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var jsBuilder = new StringBuilder(); + if (registration.isCompatible()) { + jsBuilder.append("const beforeWriteIndex = buffer.getWriteOffset();").append(LS); + jsBuilder.append(TAB).append(StringUtils.format("buffer.writeInt({});", registration.getPredictionLength())).append(LS); + } else { + jsBuilder.append(TAB).append("buffer.writeInt(-1);").append(LS); + } + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + esSerializer(fieldRegistration.serializer()).writeObject(jsBuilder, "packet." + field.getName(), 1, field, fieldRegistration); + } + if (registration.isCompatible()) { + jsBuilder.append(TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS); + } + return jsBuilder.toString(); + } + + private static String readObject(ProtocolRegistration registration) { + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var jsBuilder = new StringBuilder(); + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + if (field.isAnnotationPresent(Compatible.class)) { + jsBuilder.append(TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS); + var compatibleReadObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 2, field, fieldRegistration); + jsBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS); + jsBuilder.append(TAB).append("}").append(LS); + continue; + } + var readObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration); + jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS); + } + return jsBuilder.toString(); + } +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/es/IEsSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/es/IEsSerializer.java new file mode 100644 index 00000000..7fc69c08 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/es/IEsSerializer.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.protocol.serializer.es; + +import com.zfoo.protocol.model.Triple; +import com.zfoo.protocol.registration.field.IFieldRegistration; + +import java.lang.reflect.Field; + +/** + * @author godotg + */ +public interface IEsSerializer { + + /** + * 获取属性的类型,名称,默认值 + */ + Triple field(Field field, IFieldRegistration fieldRegistration); + + void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration); + + String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration); + +} diff --git a/protocol/src/main/resources/es/ProtocolManagerTemplate.mjs b/protocol/src/main/resources/es/ProtocolManagerTemplate.mjs new file mode 100644 index 00000000..89149a01 --- /dev/null +++ b/protocol/src/main/resources/es/ProtocolManagerTemplate.mjs @@ -0,0 +1,32 @@ +{} + +const protocols = new Map(); + +const ProtocolManager = {}; + +// initProtocol +{} + +ProtocolManager.getProtocol = function getProtocol(protocolId) { + const protocol = protocols.get(protocolId); + if (protocol === null) { + throw new Error('[protocolId:' + protocolId + ']协议不存在'); + } + return protocol; +}; + +ProtocolManager.write = function write(buffer, packet) { + const protocolId = packet.protocolId(); + buffer.writeShort(protocolId); + const protocol = ProtocolManager.getProtocol(protocolId); + protocol.write(buffer, packet); +}; + +ProtocolManager.read = function read(buffer) { + const protocolId = buffer.readShort(); + const protocol = ProtocolManager.getProtocol(protocolId); + const packet = protocol.read(buffer); + return packet; +}; + +export default ProtocolManager; diff --git a/protocol/src/main/resources/es/ProtocolTemplate.mjs b/protocol/src/main/resources/es/ProtocolTemplate.mjs new file mode 100644 index 00000000..14af85df --- /dev/null +++ b/protocol/src/main/resources/es/ProtocolTemplate.mjs @@ -0,0 +1,32 @@ +{} +const {} = function() { + {} +}; + +{}.prototype.protocolId = function() { + return {}; +}; + +{}.write = function(buffer, packet) { + if (packet === null) { + buffer.writeInt(0); + return; + } + {} +}; + +{}.read = function(buffer) { + const length = buffer.readInt(); + if (length === 0) { + return null; + } + const beforeReadIndex = buffer.getReadOffset(); + const packet = new {}(); + {} + if (length > 0) { + buffer.setReadOffset(beforeReadIndex + length); + } + return packet; +}; + +export default {}; diff --git a/protocol/src/main/resources/es/buffer/ByteBuffer.mjs b/protocol/src/main/resources/es/buffer/ByteBuffer.mjs new file mode 100644 index 00000000..292f8626 --- /dev/null +++ b/protocol/src/main/resources/es/buffer/ByteBuffer.mjs @@ -0,0 +1,1121 @@ +import { readInt64, writeInt64 } from './longbits'; +import ProtocolManager from '../ProtocolManager'; + +const empty_str = ''; +const initSize = 128; +const maxSize = 655537; + +const maxShort = 32767; +const minShort = -32768; + +const maxInt = 2147483647; +const minInt = -2147483648; + +// UTF-8编码与解码 +const encoder = new TextEncoder('utf-8'); +const decoder = new TextDecoder('utf-8'); + +// nodejs的测试环境需要用以下方式特殊处理 +// const util = require('util'); +// 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; + +const copy = function copy(original, newLength) { + if (original.byteLength > newLength) { + throw new Error('newLength is too small'); + } + const dst = new ArrayBuffer(newLength); + new Uint8Array(dst).set(new Uint8Array(original)); + return dst; +}; + +function encodeZigzagInt(n) { + // 有效位左移一位+符号位右移31位 + return (n << 1) ^ (n >> 31); +} + +function decodeZigzagInt(n) { + return (n >>> 1) ^ -(n & 1); +} + + +const ByteBuffer = function() { + this.writeOffset = 0; + this.readOffset = 0; + this.buffer = new ArrayBuffer(initSize); + this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength); + + this.adjustPadding = function(predictionLength, beforeWriteIndex) { + const currentWriteIndex = this.writeOffset; + const predictionCount = this.writeIntCount(predictionLength); + const length = currentWriteIndex - beforeWriteIndex - predictionCount; + const lengthCount = this.writeIntCount(length); + const padding = lengthCount - predictionCount; + if (padding === 0) { + this.setWriteOffset(beforeWriteIndex); + this.writeInt(length); + this.setWriteOffset(currentWriteIndex); + } else { + const retainedByteBuf = this.buffer.slice(currentWriteIndex - length, currentWriteIndex); + this.setWriteOffset(beforeWriteIndex); + this.writeInt(length); + this.writeBytes(retainedByteBuf); + } + } + + this.compatibleRead = function(beforeReadIndex, length) { + return length !== -1 && this.getReadOffset() < length + beforeReadIndex; + } + + this.getWriteOffset = function() { + return this.writeOffset; + } + + this.setWriteOffset = function(writeOffset) { + if (writeOffset > this.buffer.byteLength) { + throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset + + ', writerIndex: ' + this.writeOffset + + '(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength); + } + this.writeOffset = writeOffset; + }; + + this.getReadOffset = function() { + return this.readOffset; + } + + this.setReadOffset = function(readOffset) { + if (readOffset > this.writeOffset) { + throw new Error('index out of bounds exception: readerIndex: ' + this.readOffset + + ', writerIndex: ' + this.writeOffset + + '(expected: 0 <= readerIndex <= writerIndex <= capacity:' + this.buffer.byteLength); + } + this.readOffset = readOffset; + }; + + this.getCapacity = function() { + return this.buffer.byteLength - this.writeOffset; + }; + + this.ensureCapacity = function(minCapacity) { + while (minCapacity - this.getCapacity() > 0) { + const newSize = this.buffer.byteLength * 2; + if (newSize > maxSize) { + throw new Error('out of memory error'); + } + this.buffer = copy(this.buffer, newSize); + this.bufferView = new DataView(this.buffer, 0, this.buffer.byteLength); + } + }; + + this.isReadable = function() { + return this.writeOffset > this.readOffset; + }; + + this.writeBoolean = function(value) { + if (!(value === true || value === false)) { + throw new Error('value must be true of false'); + } + this.ensureCapacity(1); + if (value === true) { + this.bufferView.setInt8(this.writeOffset, 1); + } else { + this.bufferView.setInt8(this.writeOffset, 0); + } + this.writeOffset++; + }; + + this.readBoolean = 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); + this.writeOffset++; + }; + + this.readByte = function() { + const value = this.bufferView.getInt8(this.readOffset); + this.readOffset++; + return value; + }; + + this.writeShort = function(value) { + if (!(minShort <= value && value <= maxShort)) { + throw new Error('value must range between minShort:-32768 and maxShort:32767'); + } + this.ensureCapacity(2); + this.bufferView.setInt16(this.writeOffset, value); + this.writeOffset += 2; + }; + + this.readShort = function() { + const value = this.bufferView.getInt16(this.readOffset); + this.readOffset += 2; + return value; + }; + + this.writeRawInt = function(value) { + if (!(minInt <= value && value <= maxInt)) { + throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); + } + this.ensureCapacity(4); + this.bufferView.setInt32(this.writeOffset, value); + this.writeOffset += 4; + }; + + this.readRawInt = function() { + const value = this.bufferView.getInt32(this.readOffset); + this.readOffset += 4; + return value; + }; + + this.writeInt = function(value) { + if (!(minInt <= value && value <= maxInt)) { + throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); + } + this.ensureCapacity(5); + + value = encodeZigzagInt(value); + + if (value >>> 7 === 0) { + this.writeByte(value); + return; + } + + if (value >>> 14 === 0) { + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7)); + return; + } + + if (value >>> 21 === 0) { + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7 | 0x80)); + this.writeByte(value >>> 14); + return; + } + + if (value >>> 28 === 0) { + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7 | 0x80)); + this.writeByte((value >>> 14 | 0x80)); + this.writeByte(value >>> 21); + return; + } + + this.writeByte((value & 0x7F) | 0x80); + this.writeByte((value >>> 7 | 0x80)); + this.writeByte((value >>> 14 | 0x80)); + this.writeByte((value >>> 21 | 0x80)); + this.writeByte(value >>> 28); + }; + + this.writeIntCount = function(value) { + if (!(minInt <= value && value <= maxInt)) { + throw new Error('value must range between minInt:-2147483648 and maxInt:2147483647'); + } + value = encodeZigzagInt(value); + if (value >>> 7 === 0) { + return 1; + } + if (value >>> 14 === 0) { + return 2; + } + if (value >>> 21 === 0) { + return 3; + } + if (value >>> 28 === 0) { + return 4; + } + return 5; + } + + this.readInt = function() { + let b = this.readByte(); + let value = b & 0x7F; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 7; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 14; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 21; + if ((b & 0x80) !== 0) { + b = this.readByte(); + value |= (b & 0x7F) << 28; + } + } + } + } + + return decodeZigzagInt(value); + }; + + this.writeLong = function(value) { + if (value === null || value === undefined) { + throw new Error('value must not be null'); + } + this.ensureCapacity(9); + + writeInt64(this, value); + }; + + this.readLong = function() { + const buffer = new ArrayBuffer(9); + const bufferView = new DataView(buffer, 0, buffer.byteLength); + + let count = 0; + let b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + if ((b & 0x80) !== 0) { + b = this.readByte(); + bufferView.setUint8(count++, b); + } + } + } + } + } + } + } + } + return readInt64(new Uint8Array(buffer.slice(0, count))); + }; + + this.writeFloat = function(value) { + if (value === null || value === undefined) { + throw new Error('value must not be null'); + } + this.ensureCapacity(4); + this.bufferView.setFloat32(this.writeOffset, value); + this.writeOffset += 4; + }; + + this.readFloat = function() { + const value = this.bufferView.getFloat32(this.readOffset); + this.readOffset += 4; + return value; + }; + + this.writeDouble = function(value) { + if (value === null || value === undefined) { + throw new Error('value must not be null'); + } + this.ensureCapacity(8); + this.bufferView.setFloat64(this.writeOffset, value); + this.writeOffset += 8; + }; + + this.readDouble = function() { + const value = this.bufferView.getFloat64(this.readOffset); + this.readOffset += 8; + return value; + }; + + this.writeString = function(value) { + if (value === null || value === undefined || value.trim().length === 0) { + this.writeInt(0); + return; + } + + const uint8Array = encoder.encode(value); + + this.ensureCapacity(5 + uint8Array.length); + + this.writeInt(uint8Array.length); + uint8Array.forEach((value) => this.writeByte(value)); + }; + + this.readString = function() { + const length = this.readInt(); + if (length <= 0) { + return empty_str; + } + const uint8Array = new Uint8Array(this.buffer.slice(this.readOffset, this.readOffset + length)); + const value = decoder.decode(uint8Array); + this.readOffset += length; + return value; + }; + + this.writePacketFlag = function(value) { + const flag = (value === null) || (value === undefined); + this.writeBoolean(!flag); + return flag; + }; + + this.writePacket = function(packet, protocolId) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + protocolRegistration.write(this, packet); + }; + + this.readPacket = function(protocolId) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + return protocolRegistration.read(this); + }; + + this.writeBooleanArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeBoolean(element); + }); + } + }; + + this.readBooleanArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readBoolean()); + } + } + return array; + }; + + this.writeByteArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeByte(element); + }); + } + }; + + this.readByteArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readByte()); + } + } + return array; + }; + + this.writeShortArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeShort(element); + }); + } + }; + + this.readShortArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readShort()); + } + } + return array; + }; + + this.writeIntArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeInt(element); + }); + } + }; + + this.readIntArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readInt()); + } + } + return array; + }; + + this.writeLongArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeLong(element); + }); + } + }; + + this.readLongArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readLong()); + } + } + return array; + }; + + this.writeFloatArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeFloat(element); + }); + } + }; + + this.readFloatArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readFloat()); + } + } + return array; + }; + + this.writeDoubleArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeDouble(element); + }); + } + }; + + this.readDoubleArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readDouble()); + } + } + return array; + }; + + this.writeStringArray = function(array) { + if (array === null) { + this.writeInt(0); + } else { + this.writeInt(array.length); + array.forEach(element => { + this.writeString(element); + }); + } + }; + + this.readStringArray = function() { + const array = []; + const length = this.readInt(); + if (length > 0) { + for (let index = 0; index < length; index++) { + array.push(this.readString()); + } + } + return array; + }; + + this.writePacketArray = function(array, protocolId) { + if (array === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(array.length); + array.forEach(element => { + protocolRegistration.write(this, element); + }); + } + }; + + this.readPacketArray = function(protocolId) { + const array = []; + const length = this.readInt(); + if (length > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < length; index++) { + array.push(protocolRegistration.read(this)); + } + } + return array; + }; + + // ---------------------------------------------list------------------------------------------- + this.writeBooleanList = function(list) { + this.writeBooleanArray(list); + }; + + this.readBooleanList = function() { + return this.readBooleanArray(); + }; + + this.writeByteList = function(list) { + this.writeByteArray(list); + }; + + this.readByteList = function() { + return this.readByteArray(); + }; + + this.writeShortList = function(list) { + this.writeShortArray(list); + }; + + this.readShortList = function() { + return this.readShortArray(); + }; + + this.writeIntList = function(list) { + this.writeIntArray(list); + }; + + this.readIntList = function() { + return this.readIntArray(); + }; + + this.writeLongList = function(list) { + this.writeLongArray(list); + }; + + this.readLongList = function() { + return this.readLongArray(); + }; + + this.writeFloatList = function(list) { + this.writeFloatArray(list); + }; + + this.readFloatList = function() { + return this.readFloatArray(); + }; + + this.writeDoubleList = function(list) { + this.writeDoubleArray(list); + }; + + this.readDoubleList = function() { + return this.readDoubleArray(); + }; + + this.writeStringList = function(list) { + this.writeStringArray(list); + }; + + this.readStringList = function() { + return this.readStringArray(); + }; + + this.writePacketList = function(list, protocolId) { + this.writePacketArray(list, protocolId); + }; + + this.readPacketList = function(protocolId) { + return this.readPacketArray(protocolId); + }; + + // ---------------------------------------------set------------------------------------------- + this.writeBooleanSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeBoolean(element); + }); + } + }; + + this.readBooleanSet = function() { + return new Set(this.readBooleanArray()); + }; + + this.writeByteSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeByte(element); + }); + } + }; + + this.readByteSet = function() { + return new Set(this.readByteArray()); + }; + + this.writeShortSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeShort(element); + }); + } + }; + + this.readShortSet = function() { + return new Set(this.readShortArray()); + }; + + this.writeIntSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeInt(element); + }); + } + }; + + this.readIntSet = function() { + return new Set(this.readIntArray()); + }; + + this.writeLongSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeLong(element); + }); + } + }; + + this.readLongSet = function() { + return new Set(this.readLongArray()); + }; + + this.writeFloatSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeFloat(element); + }); + } + }; + + this.readFloatSet = function() { + return new Set(this.readFloatArray()); + }; + + this.writeDoubleSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeDouble(element); + }); + } + }; + + this.readDoubleSet = function() { + return new Set(this.readDoubleArray()); + }; + + this.writeStringSet = function(set) { + if (set === null) { + this.writeInt(0); + } else { + this.writeInt(set.size); + set.forEach(element => { + this.writeString(element); + }); + } + }; + + this.readStringSet = function() { + return new Set(this.readStringArray()); + }; + + this.writePacketSet = function(set, protocolId) { + if (set === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(set.size); + set.forEach(element => { + protocolRegistration.write(this, element); + }); + } + }; + + this.readPacketSet = function(protocolId) { + return new Set(this.readPacketArray(protocolId)); + }; + + // ---------------------------------------------map------------------------------------------- + this.writeIntIntMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + this.writeInt(value); + }); + } + }; + + this.readIntIntMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = this.readInt(); + map.set(key, value); + } + } + return map; + }; + + this.writeIntLongMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + this.writeLong(value); + }); + } + }; + + this.readIntLongMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = this.readLong(); + map.set(key, value); + } + } + return map; + }; + + this.writeIntStringMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + this.writeString(value); + }); + } + }; + + this.readIntStringMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = this.readString(); + map.set(key, value); + } + } + return map; + }; + + this.writeIntPacketMap = function(map, protocolId) { + if (map === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeInt(key); + protocolRegistration.write(this, value); + }); + } + }; + + this.readIntPacketMap = function(protocolId) { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < size; index++) { + const key = this.readInt(); + const value = protocolRegistration.read(this); + map.set(key, value); + } + } + return map; + }; + + this.writeLongIntMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + this.writeInt(value); + }); + } + }; + + this.readLongIntMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = this.readInt(); + map.set(key, value); + } + } + return map; + }; + + this.writeLongLongMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + this.writeLong(value); + }); + } + }; + + this.readLongLongMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = this.readLong(); + map.set(key, value); + } + } + return map; + }; + + this.writeLongStringMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + this.writeString(value); + }); + } + }; + + this.readLongStringMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = this.readString(); + map.set(key, value); + } + } + return map; + }; + + this.writeLongPacketMap = function(map, protocolId) { + if (map === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeLong(key); + protocolRegistration.write(this, value); + }); + } + }; + + this.readLongPacketMap = function(protocolId) { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < size; index++) { + const key = this.readLong(); + const value = protocolRegistration.read(this); + map.set(key, value); + } + } + return map; + }; + + this.writeStringIntMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + this.writeInt(value); + }); + } + }; + + this.readStringIntMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = this.readInt(); + map.set(key, value); + } + } + return map; + }; + + this.writeStringLongMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + this.writeLong(value); + }); + } + }; + + this.readStringLongMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = this.readLong(); + map.set(key, value); + } + } + return map; + }; + + this.writeStringStringMap = function(map) { + if (map === null) { + this.writeInt(0); + } else { + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + this.writeString(value); + }); + } + }; + + this.readStringStringMap = function() { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = this.readString(); + map.set(key, value); + } + } + return map; + }; + + this.writeStringPacketMap = function(map, protocolId) { + if (map === null) { + this.writeInt(0); + } else { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + this.writeInt(map.size); + map.forEach((value, key) => { + this.writeString(key); + protocolRegistration.write(this, value); + }); + } + }; + + this.readStringPacketMap = function(protocolId) { + const map = new Map(); + const size = this.readInt(); + if (size > 0) { + const protocolRegistration = ProtocolManager.getProtocol(protocolId); + for (let index = 0; index < size; index++) { + const key = this.readString(); + const value = protocolRegistration.read(this); + map.set(key, value); + } + } + return map; + }; +}; + +export default ByteBuffer; diff --git a/protocol/src/main/resources/es/buffer/long.mjs b/protocol/src/main/resources/es/buffer/long.mjs new file mode 100644 index 00000000..37026933 --- /dev/null +++ b/protocol/src/main/resources/es/buffer/long.mjs @@ -0,0 +1,1326 @@ +/* eslint-disable */ +// from https://github.com/dcodeIO/long.js/blob/master/src/long.js + +/** + * wasm optimizations, to do native i64 multiplication and divide + */ +var wasm = null; + +try { + wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([ + 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11 + ])), {}).exports; +} catch (e) { + // no wasm support :( +} + +/** + * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. + * See the from* functions below for more convenient ways of constructing Longs. + * @exports Long + * @class A Long class for representing a 64 bit two's-complement integer value. + * @param {number} low The low (signed) 32 bits of the long + * @param {number} high The high (signed) 32 bits of the long + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @constructor + */ +function Long(low, high, unsigned) { + + /** + * The low 32 bits as a signed value. + * @type {number} + */ + this.low = low | 0; + + /** + * The high 32 bits as a signed value. + * @type {number} + */ + this.high = high | 0; + + /** + * Whether unsigned or not. + * @type {boolean} + */ + this.unsigned = !!unsigned; +} + +// The internal representation of a long is the two given signed, 32-bit values. +// We use 32-bit pieces because these are the size of integers on which +// Javascript performs bit-operations. For operations like addition and +// multiplication, we split each number into 16 bit pieces, which can easily be +// multiplied within Javascript's floating-point representation without overflow +// or change in sign. +// +// In the algorithms below, we frequently reduce the negative case to the +// positive case by negating the input(s) and then post-processing the result. +// Note that we must ALWAYS check specially whether those values are MIN_VALUE +// (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as +// a positive number, it overflows back into a negative). Not handling this +// case would often result in infinite recursion. +// +// Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from* +// methods on which they depend. + +/** + * An indicator used to reliably determine if an object is a Long or not. + * @type {boolean} + * @const + * @private + */ +Long.prototype.__isLong__; + +Object.defineProperty(Long.prototype, "__isLong__", {value: true}); + +/** + * @function + * @param {*} obj Object + * @returns {boolean} + * @inner + */ +function isLong(obj) { + return (obj && obj["__isLong__"]) === true; +} + +/** + * Tests if the specified object is a Long. + * @function + * @param {*} obj Object + * @returns {boolean} + */ +Long.isLong = isLong; + +/** + * A cache of the Long representations of small integer values. + * @type {!Object} + * @inner + */ +var INT_CACHE = {}; + +/** + * A cache of the Long representations of small unsigned integer values. + * @type {!Object} + * @inner + */ +var UINT_CACHE = {}; + +/** + * @param {number} value + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ +function fromInt(value, unsigned) { + var obj, cachedObj, cache; + if (unsigned) { + value >>>= 0; + if (cache = (0 <= value && value < 256)) { + cachedObj = UINT_CACHE[value]; + if (cachedObj) + return cachedObj; + } + obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true); + if (cache) + UINT_CACHE[value] = obj; + return obj; + } else { + value |= 0; + if (cache = (-128 <= value && value < 128)) { + cachedObj = INT_CACHE[value]; + if (cachedObj) + return cachedObj; + } + obj = fromBits(value, value < 0 ? -1 : 0, false); + if (cache) + INT_CACHE[value] = obj; + return obj; + } +} + +/** + * Returns a Long representing the given 32 bit integer value. + * @function + * @param {number} value The 32 bit integer in question + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} The corresponding Long value + */ +Long.fromInt = fromInt; + +/** + * @param {number} value + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ +function fromNumber(value, unsigned) { + if (isNaN(value)) + return unsigned ? UZERO : ZERO; + if (unsigned) { + if (value < 0) + return UZERO; + if (value >= TWO_PWR_64_DBL) + return MAX_UNSIGNED_VALUE; + } else { + if (value <= -TWO_PWR_63_DBL) + return MIN_VALUE; + if (value + 1 >= TWO_PWR_63_DBL) + return MAX_VALUE; + } + if (value < 0) + return fromNumber(-value, unsigned).neg(); + return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); +} + +/** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * @function + * @param {number} value The number in question + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} The corresponding Long value + */ +Long.fromNumber = fromNumber; + +/** + * @param {number} lowBits + * @param {number} highBits + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ +function fromBits(lowBits, highBits, unsigned) { + return new Long(lowBits, highBits, unsigned); +} + +/** + * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is + * assumed to use 32 bits. + * @function + * @param {number} lowBits The low 32 bits + * @param {number} highBits The high 32 bits + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} The corresponding Long value + */ +Long.fromBits = fromBits; + +/** + * @function + * @param {number} base + * @param {number} exponent + * @returns {number} + * @inner + */ +var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) + +/** + * @param {string} str + * @param {(boolean|number)=} unsigned + * @param {number=} radix + * @returns {!Long} + * @inner + */ +function fromString(str, unsigned, radix) { + if (str.length === 0) + throw Error('empty string'); + if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") + return ZERO; + if (typeof unsigned === 'number') { + // For goog.math.long compatibility + radix = unsigned, + unsigned = false; + } else { + unsigned = !!unsigned; + } + radix = radix || 10; + if (radix < 2 || 36 < radix) + throw RangeError('radix'); + + var p; + if ((p = str.indexOf('-')) > 0) + throw Error('interior hyphen'); + else if (p === 0) { + return fromString(str.substring(1), unsigned, radix).neg(); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = fromNumber(pow_dbl(radix, 8)); + + var result = ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i), + value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = fromNumber(pow_dbl(radix, size)); + result = result.mul(power).add(fromNumber(value)); + } else { + result = result.mul(radixToPower); + result = result.add(fromNumber(value)); + } + } + result.unsigned = unsigned; + return result; +} + +/** + * Returns a Long representation of the given string, written using the specified radix. + * @function + * @param {string} str The textual representation of the Long + * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed + * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 + * @returns {!Long} The corresponding Long value + */ +Long.fromString = fromString; + +/** + * @function + * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val + * @param {boolean=} unsigned + * @returns {!Long} + * @inner + */ +function fromValue(val, unsigned) { + if (typeof val === 'number') + return fromNumber(val, unsigned); + if (typeof val === 'string') + return fromString(val, unsigned); + // Throws for non-objects, converts non-instanceof Long: + return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned); +} + +/** + * Converts the specified value to a Long using the appropriate from* function for its type. + * @function + * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {!Long} + */ +Long.fromValue = fromValue; + +// NOTE: the compiler should inline these constant values below and then remove these variables, so there should be +// no runtime penalty for these. + +/** + * @type {number} + * @const + * @inner + */ +var TWO_PWR_16_DBL = 1 << 16; + +/** + * @type {number} + * @const + * @inner + */ +var TWO_PWR_24_DBL = 1 << 24; + +/** + * @type {number} + * @const + * @inner + */ +var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; + +/** + * @type {number} + * @const + * @inner + */ +var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; + +/** + * @type {number} + * @const + * @inner + */ +var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; + +/** + * @type {!Long} + * @const + * @inner + */ +var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); + +/** + * @type {!Long} + * @inner + */ +var ZERO = fromInt(0); + +/** + * Signed zero. + * @type {!Long} + */ +Long.ZERO = ZERO; + +/** + * @type {!Long} + * @inner + */ +var UZERO = fromInt(0, true); + +/** + * Unsigned zero. + * @type {!Long} + */ +Long.UZERO = UZERO; + +/** + * @type {!Long} + * @inner + */ +var ONE = fromInt(1); + +/** + * Signed one. + * @type {!Long} + */ +Long.ONE = ONE; + +/** + * @type {!Long} + * @inner + */ +var UONE = fromInt(1, true); + +/** + * Unsigned one. + * @type {!Long} + */ +Long.UONE = UONE; + +/** + * @type {!Long} + * @inner + */ +var NEG_ONE = fromInt(-1); + +/** + * Signed negative one. + * @type {!Long} + */ +Long.NEG_ONE = NEG_ONE; + +/** + * @type {!Long} + * @inner + */ +var MAX_VALUE = fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0, false); + +/** + * Maximum signed value. + * @type {!Long} + */ +Long.MAX_VALUE = MAX_VALUE; + +/** + * @type {!Long} + * @inner + */ +var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF | 0, 0xFFFFFFFF | 0, true); + +/** + * Maximum unsigned value. + * @type {!Long} + */ +Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; + +/** + * @type {!Long} + * @inner + */ +var MIN_VALUE = fromBits(0, 0x80000000 | 0, false); + +/** + * Minimum signed value. + * @type {!Long} + */ +Long.MIN_VALUE = MIN_VALUE; + +/** + * @alias Long.prototype + * @inner + */ +var LongPrototype = Long.prototype; + +/** + * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. + * @returns {number} + */ +LongPrototype.toInt = function toInt() { + return this.unsigned ? this.low >>> 0 : this.low; +}; + +/** + * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). + * @returns {number} + */ +LongPrototype.toNumber = function toNumber() { + if (this.unsigned) + return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); + return this.high * TWO_PWR_32_DBL + (this.low >>> 0); +}; + +/** + * Converts the Long to a string written in the specified radix. + * @param {number=} radix Radix (2-36), defaults to 10 + * @returns {string} + * @override + * @throws {RangeError} If `radix` is out of range + */ +LongPrototype.toString = function toString(radix) { + radix = radix || 10; + if (radix < 2 || 36 < radix) + throw RangeError('radix'); + if (this.isZero()) + return '0'; + if (this.isNegative()) { // Unsigned Longs are never negative + if (this.eq(MIN_VALUE)) { + // We need to change the Long value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixLong = fromNumber(radix), + div = this.div(radixLong), + rem1 = div.mul(radixLong).sub(this); + return div.toString(radix) + rem1.toInt().toString(radix); + } else + return '-' + this.neg().toString(radix); + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), + rem = this; + var result = ''; + while (true) { + var remDiv = rem.div(radixToPower), + intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0, + digits = intval.toString(radix); + rem = remDiv; + if (rem.isZero()) + return digits + result; + else { + while (digits.length < 6) + digits = '0' + digits; + result = '' + digits + result; + } + } +}; + +/** + * Gets the high 32 bits as a signed integer. + * @returns {number} Signed high bits + */ +LongPrototype.getHighBits = function getHighBits() { + return this.high; +}; + +/** + * Gets the high 32 bits as an unsigned integer. + * @returns {number} Unsigned high bits + */ +LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() { + return this.high >>> 0; +}; + +/** + * Gets the low 32 bits as a signed integer. + * @returns {number} Signed low bits + */ +LongPrototype.getLowBits = function getLowBits() { + return this.low; +}; + +/** + * Gets the low 32 bits as an unsigned integer. + * @returns {number} Unsigned low bits + */ +LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() { + return this.low >>> 0; +}; + +/** + * Gets the number of bits needed to represent the absolute value of this Long. + * @returns {number} + */ +LongPrototype.getNumBitsAbs = function getNumBitsAbs() { + if (this.isNegative()) // Unsigned Longs are never negative + return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); + var val = this.high != 0 ? this.high : this.low; + for (var bit = 31; bit > 0; bit--) + if ((val & (1 << bit)) != 0) + break; + return this.high != 0 ? bit + 33 : bit + 1; +}; + +/** + * Tests if this Long's value equals zero. + * @returns {boolean} + */ +LongPrototype.isZero = function isZero() { + return this.high === 0 && this.low === 0; +}; + +/** + * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}. + * @returns {boolean} + */ +LongPrototype.eqz = LongPrototype.isZero; + +/** + * Tests if this Long's value is negative. + * @returns {boolean} + */ +LongPrototype.isNegative = function isNegative() { + return !this.unsigned && this.high < 0; +}; + +/** + * Tests if this Long's value is positive. + * @returns {boolean} + */ +LongPrototype.isPositive = function isPositive() { + return this.unsigned || this.high >= 0; +}; + +/** + * Tests if this Long's value is odd. + * @returns {boolean} + */ +LongPrototype.isOdd = function isOdd() { + return (this.low & 1) === 1; +}; + +/** + * Tests if this Long's value is even. + * @returns {boolean} + */ +LongPrototype.isEven = function isEven() { + return (this.low & 1) === 0; +}; + +/** + * Tests if this Long's value equals the specified's. + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.equals = function equals(other) { + if (!isLong(other)) + other = fromValue(other); + if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) + return false; + return this.high === other.high && this.low === other.low; +}; + +/** + * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.eq = LongPrototype.equals; + +/** + * Tests if this Long's value differs from the specified's. + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.notEquals = function notEquals(other) { + return !this.eq(/* validates */ other); +}; + +/** + * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.neq = LongPrototype.notEquals; + +/** + * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.ne = LongPrototype.notEquals; + +/** + * Tests if this Long's value is less than the specified's. + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.lessThan = function lessThan(other) { + return this.comp(/* validates */ other) < 0; +}; + +/** + * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.lt = LongPrototype.lessThan; + +/** + * Tests if this Long's value is less than or equal the specified's. + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { + return this.comp(/* validates */ other) <= 0; +}; + +/** + * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.lte = LongPrototype.lessThanOrEqual; + +/** + * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.le = LongPrototype.lessThanOrEqual; + +/** + * Tests if this Long's value is greater than the specified's. + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.greaterThan = function greaterThan(other) { + return this.comp(/* validates */ other) > 0; +}; + +/** + * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.gt = LongPrototype.greaterThan; + +/** + * Tests if this Long's value is greater than or equal the specified's. + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { + return this.comp(/* validates */ other) >= 0; +}; + +/** + * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.gte = LongPrototype.greaterThanOrEqual; + +/** + * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. + * @function + * @param {!Long|number|string} other Other value + * @returns {boolean} + */ +LongPrototype.ge = LongPrototype.greaterThanOrEqual; + +/** + * Compares this Long's value with the specified's. + * @param {!Long|number|string} other Other value + * @returns {number} 0 if they are the same, 1 if the this is greater and -1 + * if the given one is greater + */ +LongPrototype.compare = function compare(other) { + if (!isLong(other)) + other = fromValue(other); + if (this.eq(other)) + return 0; + var thisNeg = this.isNegative(), + otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) + return -1; + if (!thisNeg && otherNeg) + return 1; + // At this point the sign bits are the same + if (!this.unsigned) + return this.sub(other).isNegative() ? -1 : 1; + // Both are positive if at least one is unsigned + return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; +}; + +/** + * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}. + * @function + * @param {!Long|number|string} other Other value + * @returns {number} 0 if they are the same, 1 if the this is greater and -1 + * if the given one is greater + */ +LongPrototype.comp = LongPrototype.compare; + +/** + * Negates this Long's value. + * @returns {!Long} Negated Long + */ +LongPrototype.negate = function negate() { + if (!this.unsigned && this.eq(MIN_VALUE)) + return MIN_VALUE; + return this.not().add(ONE); +}; + +/** + * Negates this Long's value. This is an alias of {@link Long#negate}. + * @function + * @returns {!Long} Negated Long + */ +LongPrototype.neg = LongPrototype.negate; + +/** + * Returns the sum of this and the specified Long. + * @param {!Long|number|string} addend Addend + * @returns {!Long} Sum + */ +LongPrototype.add = function add(addend) { + if (!isLong(addend)) + addend = fromValue(addend); + + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = addend.high >>> 16; + var b32 = addend.high & 0xFFFF; + var b16 = addend.low >>> 16; + var b00 = addend.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); +}; + +/** + * Returns the difference of this and the specified Long. + * @param {!Long|number|string} subtrahend Subtrahend + * @returns {!Long} Difference + */ +LongPrototype.subtract = function subtract(subtrahend) { + if (!isLong(subtrahend)) + subtrahend = fromValue(subtrahend); + return this.add(subtrahend.neg()); +}; + +/** + * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}. + * @function + * @param {!Long|number|string} subtrahend Subtrahend + * @returns {!Long} Difference + */ +LongPrototype.sub = LongPrototype.subtract; + +/** + * Returns the product of this and the specified Long. + * @param {!Long|number|string} multiplier Multiplier + * @returns {!Long} Product + */ +LongPrototype.multiply = function multiply(multiplier) { + if (this.isZero()) + return ZERO; + if (!isLong(multiplier)) + multiplier = fromValue(multiplier); + + // use wasm support if present + if (wasm) { + var low = wasm.mul(this.low, + this.high, + multiplier.low, + multiplier.high); + return fromBits(low, wasm.get_high(), this.unsigned); + } + + if (multiplier.isZero()) + return ZERO; + if (this.eq(MIN_VALUE)) + return multiplier.isOdd() ? MIN_VALUE : ZERO; + if (multiplier.eq(MIN_VALUE)) + return this.isOdd() ? MIN_VALUE : ZERO; + + if (this.isNegative()) { + if (multiplier.isNegative()) + return this.neg().mul(multiplier.neg()); + else + return this.neg().mul(multiplier).neg(); + } else if (multiplier.isNegative()) + return this.mul(multiplier.neg()).neg(); + + // If both longs are small, use float multiplication + if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) + return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); + + // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high >>> 16; + var a32 = this.high & 0xFFFF; + var a16 = this.low >>> 16; + var a00 = this.low & 0xFFFF; + + var b48 = multiplier.high >>> 16; + var b32 = multiplier.high & 0xFFFF; + var b16 = multiplier.low >>> 16; + var b00 = multiplier.low & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); +}; + +/** + * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}. + * @function + * @param {!Long|number|string} multiplier Multiplier + * @returns {!Long} Product + */ +LongPrototype.mul = LongPrototype.multiply; + +/** + * Returns this Long divided by the specified. The result is signed if this Long is signed or + * unsigned if this Long is unsigned. + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Quotient + */ +LongPrototype.divide = function divide(divisor) { + if (!isLong(divisor)) + divisor = fromValue(divisor); + if (divisor.isZero()) + throw Error('division by zero'); + + // use wasm support if present + if (wasm) { + // guard against signed division overflow: the largest + // negative number / -1 would be 1 larger than the largest + // positive number, due to two's complement. + if (!this.unsigned && + this.high === -0x80000000 && + divisor.low === -1 && divisor.high === -1) { + // be consistent with non-wasm code path + return this; + } + var low = (this.unsigned ? wasm.div_u : wasm.div_s)( + this.low, + this.high, + divisor.low, + divisor.high + ); + return fromBits(low, wasm.get_high(), this.unsigned); + } + + if (this.isZero()) + return this.unsigned ? UZERO : ZERO; + var approx, rem, res; + if (!this.unsigned) { + // This section is only relevant for signed longs and is derived from the + // closure library as a whole. + if (this.eq(MIN_VALUE)) { + if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) + return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + else if (divisor.eq(MIN_VALUE)) + return ONE; + else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shr(1); + approx = halfThis.div(divisor).shl(1); + if (approx.eq(ZERO)) { + return divisor.isNegative() ? ONE : NEG_ONE; + } else { + rem = this.sub(divisor.mul(approx)); + res = approx.add(rem.div(divisor)); + return res; + } + } + } else if (divisor.eq(MIN_VALUE)) + return this.unsigned ? UZERO : ZERO; + if (this.isNegative()) { + if (divisor.isNegative()) + return this.neg().div(divisor.neg()); + return this.neg().div(divisor).neg(); + } else if (divisor.isNegative()) + return this.div(divisor.neg()).neg(); + res = ZERO; + } else { + // The algorithm below has not been made for unsigned longs. It's therefore + // required to take special care of the MSB prior to running it. + if (!divisor.unsigned) + divisor = divisor.toUnsigned(); + if (divisor.gt(this)) + return UZERO; + if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true + return UONE; + res = UZERO; + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + rem = this; + while (rem.gte(divisor)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2), + delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + approxRes = fromNumber(approx), + approxRem = approxRes.mul(divisor); + while (approxRem.isNegative() || approxRem.gt(rem)) { + approx -= delta; + approxRes = fromNumber(approx, this.unsigned); + approxRem = approxRes.mul(divisor); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) + approxRes = ONE; + + res = res.add(approxRes); + rem = rem.sub(approxRem); + } + return res; +}; + +/** + * Returns this Long divided by the specified. This is an alias of {@link Long#divide}. + * @function + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Quotient + */ +LongPrototype.div = LongPrototype.divide; + +/** + * Returns this Long modulo the specified. + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Remainder + */ +LongPrototype.modulo = function modulo(divisor) { + if (!isLong(divisor)) + divisor = fromValue(divisor); + + // use wasm support if present + if (wasm) { + var low = (this.unsigned ? wasm.rem_u : wasm.rem_s)( + this.low, + this.high, + divisor.low, + divisor.high + ); + return fromBits(low, wasm.get_high(), this.unsigned); + } + + return this.sub(this.div(divisor).mul(divisor)); +}; + +/** + * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. + * @function + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Remainder + */ +LongPrototype.mod = LongPrototype.modulo; + +/** + * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. + * @function + * @param {!Long|number|string} divisor Divisor + * @returns {!Long} Remainder + */ +LongPrototype.rem = LongPrototype.modulo; + +/** + * Returns the bitwise NOT of this Long. + * @returns {!Long} + */ +LongPrototype.not = function not() { + return fromBits(~this.low, ~this.high, this.unsigned); +}; + +/** + * Returns the bitwise AND of this Long and the specified. + * @param {!Long|number|string} other Other Long + * @returns {!Long} + */ +LongPrototype.and = function and(other) { + if (!isLong(other)) + other = fromValue(other); + return fromBits(this.low & other.low, this.high & other.high, this.unsigned); +}; + +/** + * Returns the bitwise OR of this Long and the specified. + * @param {!Long|number|string} other Other Long + * @returns {!Long} + */ +LongPrototype.or = function or(other) { + if (!isLong(other)) + other = fromValue(other); + return fromBits(this.low | other.low, this.high | other.high, this.unsigned); +}; + +/** + * Returns the bitwise XOR of this Long and the given one. + * @param {!Long|number|string} other Other Long + * @returns {!Long} + */ +LongPrototype.xor = function xor(other) { + if (!isLong(other)) + other = fromValue(other); + return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); +}; + +/** + * Returns this Long with bits shifted to the left by the given amount. + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shiftLeft = function shiftLeft(numBits) { + if (isLong(numBits)) + numBits = numBits.toInt(); + if ((numBits &= 63) === 0) + return this; + else if (numBits < 32) + return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); + else + return fromBits(0, this.low << (numBits - 32), this.unsigned); +}; + +/** + * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shl = LongPrototype.shiftLeft; + +/** + * Returns this Long with bits arithmetically shifted to the right by the given amount. + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shiftRight = function shiftRight(numBits) { + if (isLong(numBits)) + numBits = numBits.toInt(); + if ((numBits &= 63) === 0) + return this; + else if (numBits < 32) + return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); + else + return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); +}; + +/** + * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shr = LongPrototype.shiftRight; + +/** + * Returns this Long with bits logically shifted to the right by the given amount. + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { + if (isLong(numBits)) + numBits = numBits.toInt(); + numBits &= 63; + if (numBits === 0) + return this; + else { + var high = this.high; + if (numBits < 32) { + var low = this.low; + return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); + } else if (numBits === 32) + return fromBits(high, 0, this.unsigned); + else + return fromBits(high >>> (numBits - 32), 0, this.unsigned); + } +}; + +/** + * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shru = LongPrototype.shiftRightUnsigned; + +/** + * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. + * @function + * @param {number|!Long} numBits Number of bits + * @returns {!Long} Shifted Long + */ +LongPrototype.shr_u = LongPrototype.shiftRightUnsigned; + +/** + * Converts this Long to signed. + * @returns {!Long} Signed long + */ +LongPrototype.toSigned = function toSigned() { + if (!this.unsigned) + return this; + return fromBits(this.low, this.high, false); +}; + +/** + * Converts this Long to unsigned. + * @returns {!Long} Unsigned long + */ +LongPrototype.toUnsigned = function toUnsigned() { + if (this.unsigned) + return this; + return fromBits(this.low, this.high, true); +}; + +/** + * Converts this Long to its byte representation. + * @param {boolean=} le Whether little or big endian, defaults to big endian + * @returns {!Array.} Byte representation + */ +LongPrototype.toBytes = function toBytes(le) { + return le ? this.toBytesLE() : this.toBytesBE(); +}; + +/** + * Converts this Long to its little endian byte representation. + * @returns {!Array.} Little endian byte representation + */ +LongPrototype.toBytesLE = function toBytesLE() { + var hi = this.high, + lo = this.low; + return [ + lo & 0xff, + lo >>> 8 & 0xff, + lo >>> 16 & 0xff, + lo >>> 24, + hi & 0xff, + hi >>> 8 & 0xff, + hi >>> 16 & 0xff, + hi >>> 24 + ]; +}; + +/** + * Converts this Long to its big endian byte representation. + * @returns {!Array.} Big endian byte representation + */ +LongPrototype.toBytesBE = function toBytesBE() { + var hi = this.high, + lo = this.low; + return [ + hi >>> 24, + hi >>> 16 & 0xff, + hi >>> 8 & 0xff, + hi & 0xff, + lo >>> 24, + lo >>> 16 & 0xff, + lo >>> 8 & 0xff, + lo & 0xff + ]; +}; + +/** + * Creates a Long from its byte representation. + * @param {!Array.} bytes Byte representation + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @param {boolean=} le Whether little or big endian, defaults to big endian + * @returns {Long} The corresponding Long value + */ +Long.fromBytes = function fromBytes(bytes, unsigned, le) { + return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned); +}; + +/** + * Creates a Long from its little endian byte representation. + * @param {!Array.} bytes Little endian byte representation + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {Long} The corresponding Long value + */ +Long.fromBytesLE = function fromBytesLE(bytes, unsigned) { + return new Long( + bytes[0] | + bytes[1] << 8 | + bytes[2] << 16 | + bytes[3] << 24, + bytes[4] | + bytes[5] << 8 | + bytes[6] << 16 | + bytes[7] << 24, + unsigned + ); +}; + +/** + * Creates a Long from its big endian byte representation. + * @param {!Array.} bytes Big endian byte representation + * @param {boolean=} unsigned Whether unsigned or not, defaults to signed + * @returns {Long} The corresponding Long value + */ +Long.fromBytesBE = function fromBytesBE(bytes, unsigned) { + return new Long( + bytes[4] << 24 | + bytes[5] << 16 | + bytes[6] << 8 | + bytes[7], + bytes[0] << 24 | + bytes[1] << 16 | + bytes[2] << 8 | + bytes[3], + unsigned + ); +}; + +export default Long; \ No newline at end of file diff --git a/protocol/src/main/resources/es/buffer/longbits.mjs b/protocol/src/main/resources/es/buffer/longbits.mjs new file mode 100644 index 00000000..3d838fca --- /dev/null +++ b/protocol/src/main/resources/es/buffer/longbits.mjs @@ -0,0 +1,184 @@ +// from protobuf +import Long from './long'; + +/** + * Constructs new long bits. + * @classdesc Helper class for working with the low and high bits of a 64 bit value. + * @memberof util + * @constructor + * @param {number} lo Low 32 bits, unsigned + * @param {number} hi High 32 bits, unsigned + */ +function Longbits(lo, hi) { + // note that the casts below are theoretically unnecessary as of today, but older statically + // generated converter code might still call the ctor with signed 32bits. kept for compat. + + /** + * Low bits. + * @type {number} + */ + this.lo = lo >>> 0; + + /** + * High bits. + * @type {number} + */ + this.hi = hi >>> 0; +} + +/** + * Zig-zag encodes this long bits. + * @returns {util.Longbits} `this` + */ +Longbits.prototype.zzEncode = function zzEncode() { + const mask = this.hi >> 31; + this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0; + this.lo = (this.lo << 1 ^ mask) >>> 0; + return this; +}; + +/** + * Zig-zag decodes this long bits. + * @returns {util.Longbits} `this` + */ +Longbits.prototype.zzDecode = function zzDecode() { + const mask = -(this.lo & 1); + this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0; + this.hi = (this.hi >>> 1 ^ mask) >>> 0; + return this; +}; + +/** + * Converts this long bits to a long. + * @param {boolean} [unsigned=false] Whether unsigned or not + * @returns {Long} Long + */ +Longbits.prototype.toLong = function toLong(unsigned) { + return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned)); +}; + +/** + * Zero bits. + * @memberof util.LongBits + * @type {util.Longbits} + */ +const zero = Longbits.zero = new Longbits(0, 0); + +function from(value) { + if (typeof value === 'number') { + return fromNumber(value); + } + if (typeof value === 'string' || value instanceof String) { + value = Long.fromString(value); + } + return value.low || value.high ? new Longbits(value.low >>> 0, value.high >>> 0) : zero; +} + + +/** + * Constructs new long bits from the specified number. + * @param {number} value Value + * @returns {util.Longbits} Instance + */ +function fromNumber(value) { + if (value === 0) { + return zero; + } + const sign = value < 0; + if (sign) { + value = -value; + } + let lo = value >>> 0; + let hi = (value - lo) / 4294967296 >>> 0; + if (sign) { + hi = ~hi >>> 0; + lo = ~lo >>> 0; + if (++lo > 4294967295) { + lo = 0; + if (++hi > 4294967295) { + hi = 0; + } + } + } + return new Longbits(lo, hi); +} + +function writeVarint64(byteBuffer, value) { + let count = 0; + while (value.hi) { + byteBuffer.writeByte(value.lo & 127 | 128); + value.lo = (value.lo >>> 7 | value.hi << 25) >>> 0; + value.hi >>>= 7; + count = count + 7; + } + while (value.lo > 127) { + if (count >= 56) { + byteBuffer.writeByte(value.lo); + return; + } + byteBuffer.writeByte(value.lo & 127 | 128); + value.lo = value.lo >>> 7; + count = count + 7; + } + byteBuffer.writeByte(value.lo); +} + +function readLongVarint(buffer) { + // tends to deopt with local vars for octet etc. + const bits = new Longbits(0, 0); + let i = 0; + const len = buffer.length; + let pos = 0; + if (len - pos > 4) { // fast route (lo) + for (; i < 4; ++i) { + // 1st..4th + bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0; + if (buffer[pos++] < 128) { + return bits; + } + } + // 5th + bits.lo = (bits.lo | (buffer[pos] & 127) << 28) >>> 0; + bits.hi = (bits.hi | (buffer[pos] & 127) >> 4) >>> 0; + if (buffer[pos++] < 128) { + return bits; + } + i = 0; + } else { + for (; i < 3; ++i) { + // 1st..3th + bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0; + if (buffer[pos++] < 128) { + return bits; + } + } + // 4th + bits.lo = (bits.lo | (buffer[pos++] & 127) << i * 7) >>> 0; + return bits; + } + + // 6th..9th + for (; i < 4; ++i) { + // 最后一位直接写入 + if (pos === 8) { + bits.hi = (bits.hi | buffer[pos] << i * 7 + 3) >>> 0; + return bits; + } + bits.hi = (bits.hi | (buffer[pos] & 127) << i * 7 + 3) >>> 0; + if (buffer[pos++] < 128) { + return bits; + } + } + + return bits; +} + + +export function writeInt64(byteBuffer, value) { + const bits = from(value).zzEncode(); + writeVarint64(byteBuffer, bits); +} + +export function readInt64(buffer) { + return readLongVarint(buffer).zzDecode().toLong(false); +}