Skip to content

Commit 8db5f00

Browse files
authored
feat(JavaScript): impl the xlang string (apache#3197)
## Why? 1. Implement the xlang string 2. replace `InternalSerializerType` by `TypeId` to simplify the concept. ## What does this PR do? ## Related issues apache#3133 ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 3fddc9c commit 8db5f00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1753
-813
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,11 @@ jobs:
458458
${{ runner.os }}-maven-
459459
- name: Run JavaScript Xlang Test
460460
env:
461-
FORY_JavaScript_JAVA_CI: "1"
461+
# FORY_JAVASCRIPT_JAVA_CI: "1"
462462
run: |
463-
cd java
463+
cd javascript
464+
npm install
465+
cd ../java
464466
mvn -T16 --no-transfer-progress clean install -DskipTests
465467
cd fory-core
466468
mvn -T16 --no-transfer-progress test -Dtest=org.apache.fory.xlang.JavaScriptXlangTest

javascript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The Cross-Language part of the protocol is not stable, so the output of this lib
77
## Usage
88

99
```Javascript
10-
import Fory, { Type, InternalSerializerType } from '@apache-fory/fory';
10+
import Fory, { Type } from '@apache-fory/fory';
1111

1212
/**
1313
* @apache-fory/hps use v8's fast-calls-api that can be called directly by jit, ensure that the version of Node is 20 or above.

javascript/packages/fory/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import {
2323
ArrayTypeInfo,
2424
Type,
2525
} from "./lib/typeInfo";
26-
import { Serializer, InternalSerializerType, Mode } from "./lib/type";
26+
import { Serializer, Mode } from "./lib/type";
2727
import Fory from "./lib/fory";
2828
import { BinaryReader } from "./lib/reader";
2929
import { BinaryWriter } from "./lib/writer";
3030

3131
export {
3232
Serializer,
33-
InternalSerializerType,
3433
TypeInfo,
3534
ArrayTypeInfo,
3635
StructTypeInfo,

javascript/packages/fory/lib/classResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export default class ClassResolver {
173173
}
174174

175175
getSerializerById(id: number) {
176-
if (id | 0xff) {
176+
if (id <= 0xff) {
177177
return this.internalSerializer[id]!;
178178
} else {
179179
return this.customSerializer.get(id)!;

javascript/packages/fory/lib/fory.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default class {
9090
} else {
9191
const typeInfo = constructor;
9292
serializer = new Gen(this, replace).generateSerializer(typeInfo);
93+
this.classResolver.registerSerializer(typeInfo, serializer);
9394
}
9495
return {
9596
serializer,
@@ -124,8 +125,6 @@ export default class {
124125
}
125126

126127
this.binaryReader.uint8(); // skip language
127-
this.binaryReader.int32(); // native object offset. should skip. javascript support cross mode only
128-
this.binaryReader.int32(); // native object size. should skip.
129128
return serializer.read();
130129
}
131130

@@ -146,14 +145,10 @@ export default class {
146145
bitmap |= ConfigFlags.isCrossLanguageFlag;
147146
this.binaryWriter.uint8(bitmap);
148147
this.binaryWriter.uint8(Language.JAVASCRIPT);
149-
const cursor = this.binaryWriter.getCursor();
150-
this.binaryWriter.skip(4); // preserve 4-byte for nativeObjects start offsets.
151-
this.binaryWriter.uint32(0); // nativeObjects length.
152148
// reserve fixed size
153149
this.binaryWriter.reserve(serializer.fixedSize);
154150
// start write
155151
serializer.write(data);
156-
this.binaryWriter.setUint32Position(cursor, this.binaryWriter.getCursor()); // nativeObjects start offsets;
157152
return this.binaryWriter;
158153
}
159154

javascript/packages/fory/lib/gen/any.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TypeInfo } from "../typeInfo";
2121
import { CodecBuilder } from "./builder";
2222
import { BaseSerializerGenerator } from "./serializer";
2323
import { CodegenRegistry } from "./router";
24-
import { InternalSerializerType, RefFlags, Serializer, TypeId } from "../type";
24+
import { RefFlags, Serializer, TypeId } from "../type";
2525
import { Scope } from "./scope";
2626
import Fory from "../fory";
2727

@@ -38,7 +38,7 @@ export class AnySerializer {
3838
}
3939

4040
detectSerializer() {
41-
const typeId = this.fory.binaryReader.int16();
41+
const typeId = this.fory.binaryReader.readVarUint32Small7();
4242
let serializer: Serializer | undefined;
4343
if (TypeId.IS_NAMED_TYPE(typeId)) {
4444
const ns = this.fory.metaStringResolver.readNamespace(this.fory.binaryReader);
@@ -170,5 +170,5 @@ class AnySerializerGenerator extends BaseSerializerGenerator {
170170
}
171171
}
172172

173-
CodegenRegistry.register(InternalSerializerType.ANY, AnySerializerGenerator);
173+
CodegenRegistry.register(TypeId.UNKNOWN, AnySerializerGenerator);
174174
CodegenRegistry.registerExternal(AnySerializer);

javascript/packages/fory/lib/gen/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import { ArrayTypeInfo, TypeInfo } from "../typeInfo";
2121
import { CodecBuilder } from "./builder";
2222
import { CodegenRegistry } from "./router";
23-
import { InternalSerializerType } from "../type";
23+
import { TypeId } from "../type";
2424
import { Scope } from "./scope";
2525
import { CollectionSerializerGenerator } from "./collection";
2626

@@ -57,4 +57,4 @@ class ArraySerializerGenerator extends CollectionSerializerGenerator {
5757
}
5858
}
5959

60-
CodegenRegistry.register(InternalSerializerType.ARRAY, ArraySerializerGenerator);
60+
CodegenRegistry.register(TypeId.ARRAY, ArraySerializerGenerator);

javascript/packages/fory/lib/gen/binary.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TypeInfo } from "../typeInfo";
2121
import { CodecBuilder } from "./builder";
2222
import { BaseSerializerGenerator, RefState } from "./serializer";
2323
import { CodegenRegistry } from "./router";
24-
import { InternalSerializerType } from "../type";
24+
import { TypeId } from "../type";
2525
import { Scope } from "./scope";
2626

2727
class BinarySerializerGenerator extends BaseSerializerGenerator {
@@ -59,4 +59,4 @@ class BinarySerializerGenerator extends BaseSerializerGenerator {
5959
}
6060
}
6161

62-
CodegenRegistry.register(InternalSerializerType.BINARY, BinarySerializerGenerator);
62+
CodegenRegistry.register(TypeId.BINARY, BinarySerializerGenerator);

javascript/packages/fory/lib/gen/bool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TypeInfo } from "../typeInfo";
2121
import { CodecBuilder } from "./builder";
2222
import { BaseSerializerGenerator } from "./serializer";
2323
import { CodegenRegistry } from "./router";
24-
import { InternalSerializerType } from "../type";
24+
import { TypeId } from "../type";
2525
import { Scope } from "./scope";
2626

2727
class BoolSerializerGenerator extends BaseSerializerGenerator {
@@ -49,4 +49,4 @@ class BoolSerializerGenerator extends BaseSerializerGenerator {
4949
}
5050
}
5151

52-
CodegenRegistry.register(InternalSerializerType.BOOL, BoolSerializerGenerator);
52+
CodegenRegistry.register(TypeId.BOOL, BoolSerializerGenerator);

javascript/packages/fory/lib/gen/builder.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export class BinaryReaderBuilder {
9191
return `${this.holder}.stringLatin1()`;
9292
}
9393

94-
stringOfVarUInt32() {
95-
return `${this.holder}.stringOfVarUInt32()`;
94+
stringWithHeader() {
95+
return `${this.holder}.stringWithHeader()`;
9696
}
9797

9898
float64() {
@@ -115,6 +115,10 @@ export class BinaryReaderBuilder {
115115
return `${this.holder}.int16()`;
116116
}
117117

118+
readVarUint32Small7() {
119+
return `${this.holder}.readVarUint32Small7()`;
120+
}
121+
118122
uint64() {
119123
return `${this.holder}.uint64()`;
120124
}
@@ -189,6 +193,10 @@ class BinaryWriterBuilder {
189193
return `${this.holder}.varInt32(${v})`;
190194
}
191195

196+
writeVarUint32Small7(v: number | string) {
197+
return `${this.holder}.writeVarUint32Small7(${v})`;
198+
}
199+
192200
varUInt32(v: number | string) {
193201
return `${this.holder}.varUInt32(${v})`;
194202
}
@@ -201,8 +209,8 @@ class BinaryWriterBuilder {
201209
return `${this.holder}.varInt64(${v})`;
202210
}
203211

204-
stringOfVarUInt32(str: string) {
205-
return `${this.holder}.stringOfVarUInt32(${str})`;
212+
stringWithHeader(str: string) {
213+
return `${this.holder}.stringWithHeader(${str})`;
206214
}
207215

208216
bufferWithoutMemCheck(v: string) {

0 commit comments

Comments
 (0)