-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(javascript): Added serialization and serialization for Type Meta Layer #1825
Merged
+191
−27
Merged
Changes from 11 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
d9ee8cd
Added wrapper on typemeta
Forchapeatl b0d73b9
Add files via upload
Forchapeatl 03f6227
Update object.ts
Forchapeatl f639b3c
using BinaryBuilderReader
Forchapeatl 5d30608
Removed MetaString and Using TypeMeta
Forchapeatl b88b616
Update builder.ts
Forchapeatl 3a4e9f4
reoved TypeMeta
Forchapeatl a7d7bf1
added ownName to binary.reader
Forchapeatl b81255a
moved from TypeMetaWrapper to TypeMetaBuilder
Forchapeatl d259a2d
Added TypeMeta
Forchapeatl 2780033
Added TypeMeta to constructor
Forchapeatl 81b274f
removed TypeMeta from constructor
Forchapeatl b67f509
FIxed syntax error
Forchapeatl 753869e
fixed lint errors
Forchapeatl ef92c34
Fixed Meta Layer Buffer
Forchapeatl 615a473
fixed byte length over flow
Forchapeatl 42845e6
removed BinaryReader Builder dependency
Forchapeatl 1434328
fixed lint errors
Forchapeatl 418f4a5
Fixed Meta Layer Buffer
Forchapeatl 7b666c2
fixed lint errors
Forchapeatl 4a3b3f5
fixed lint errors
Forchapeatl aedf2aa
fixed lint errors
Forchapeatl f3b87da
Merge branch 'main' into Forchapeatl-TypeMeta
Forchapeatl 9c48706
switched to camel casing
Forchapeatl b9c3cc9
switched to camel casing
Forchapeatl 636fb05
switched to camel casing
Forchapeatl 5418532
Added Compatible mode
Forchapeatl 7cd91cd
Added mode to fury config
Forchapeatl 2bb55ad
exported Mode
Forchapeatl 1555617
used Mode from type.ts
Forchapeatl f107fc1
SchemaConsistent for default mode
Forchapeatl 37e88d8
Reading TypeMeta only in Compatible mode
Forchapeatl 91fc901
Imported Fury Mode
Forchapeatl 1941170
fixed lint errors
Forchapeatl 83bb434
Merge branch 'main' into Forchapeatl-TypeMeta
theweipeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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. | ||
*/ | ||
|
||
import { BinaryWriter } from "../writer"; | ||
import { BinaryReader } from "../reader"; | ||
import { ObjectTypeDescription, Type } from "../description"; | ||
import { MetaString } from "./MetaString"; | ||
import { BinaryReaderBuilder } from "../gen/builder"; | ||
|
||
|
||
enum Encoding { | ||
Utf8, | ||
AllToLowerSpecial, | ||
LowerUpperDigitSpecial, | ||
} | ||
|
||
export class FieldInfo { | ||
constructor(private field_name: string, private field_id: number) { | ||
} | ||
|
||
static u8_to_encoding(value: number) { | ||
switch (value) { | ||
case 0x00: | ||
return Encoding.Utf8; | ||
case 0x01: | ||
return Encoding.AllToLowerSpecial; | ||
case 0x02: | ||
return Encoding.LowerUpperDigitSpecial; | ||
} | ||
} | ||
|
||
static uint8ArrayToBinary(array: Uint8Array): string { | ||
return Array.from(array) | ||
.map(byte => byte.toString(2).padStart(8, "0")) | ||
.join(""); | ||
} | ||
|
||
static from_bytes(reader: BinaryReaderBuilder) { | ||
const header = parseInt((reader.uint8())); | ||
const encoding = this.u8_to_encoding((header & 0b11000) >> 3); | ||
let size = (header & 0b11100000) >> 5; | ||
size = (size === 0b111) ? parseInt(reader.varInt32()) + 7 : size; | ||
const type_id = parseInt(reader.int16()); | ||
const base64String = reader.buffer(size); | ||
|
||
// Decode the base64 string | ||
const binaryString = atob(base64String); | ||
|
||
// Convert the binary string to an array of numbers | ||
const numberArray = Array.from(binaryString, (char) => char.charCodeAt(0)); | ||
|
||
// Create a new Uint8Array from the number array | ||
const uint8Array = new Uint8Array(numberArray); | ||
|
||
const field_name = MetaString.decode(uint8Array); | ||
return new FieldInfo(field_name, type_id); | ||
} | ||
|
||
to_bytes() { | ||
const writer = new BinaryWriter({}); | ||
const meta_string = MetaString.encode(this.field_name); | ||
let header = 1 << 2; | ||
const encoded = FieldInfo.uint8ArrayToBinary(meta_string); | ||
const size = encoded.length; | ||
header |= MetaString.inferBitsPerChar(this.field_name) << 3; | ||
const big_size = size >= 7; | ||
if (big_size) { | ||
header |= 0b11100000; | ||
writer.uint8(header); | ||
writer.varInt32(size - 7); | ||
} else { | ||
header |= size << 5; | ||
writer.uint8(header); | ||
} | ||
writer.int16(this.field_id); | ||
writer.buffer(meta_string); | ||
return writer.dump(); | ||
} | ||
} | ||
|
||
// Using classes to emulate struct methods in Rust | ||
class TypeMetaLayer { | ||
constructor(private type_id: number, private field_info: FieldInfo[]) { | ||
} | ||
|
||
get_type_id() { | ||
return this.type_id; | ||
} | ||
|
||
get_field_info() { | ||
return this.field_info; | ||
} | ||
|
||
to_bytes() { | ||
const writer = new BinaryWriter({}); | ||
writer.varInt32(this.field_info.length); | ||
writer.varInt32(this.type_id); | ||
for (const field of this.field_info) { | ||
writer.buffer(field.to_bytes()); | ||
} | ||
return writer.dump(); | ||
} | ||
|
||
static from_bytes(reader: BinaryReaderBuilder) { | ||
const field_num = BigInt(reader.varInt32()); | ||
const type_id = parseInt(reader.varInt32()); | ||
const field_info = []; | ||
for (let i = 0; i < field_num; i++) { | ||
field_info.push(FieldInfo.from_bytes(reader)); | ||
} | ||
return new TypeMetaLayer(type_id, field_info); | ||
} | ||
} | ||
|
||
export class TypeMeta { | ||
constructor(private hash: bigint, private layers: TypeMetaLayer[]) { | ||
} | ||
|
||
get_field_info() { | ||
theweipeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this.layers[0].get_field_info(); | ||
} | ||
|
||
get_type_id() { | ||
return this.layers[0].get_type_id(); | ||
} | ||
|
||
static from_fields(type_id: number, field_info: FieldInfo[]) { | ||
return new TypeMeta(BigInt(0), [new TypeMetaLayer(type_id, field_info)]); | ||
} | ||
|
||
static from_bytes(reader: BinaryReaderBuilder) { | ||
let header = parseInt(reader.uint64()); | ||
const hash = !Number.isNaN(header) ? BigInt(header) >> BigInt(8) : BigInt(0); | ||
//const hash = BigInt(header) >> BigInt(8); | ||
const layer_count = !Number.isNaN(header) ? BigInt(header) & BigInt(0b1111): BigInt(0); | ||
const layers = []; | ||
for (let i = 0; i < layer_count; i++) { | ||
layers.push(TypeMetaLayer.from_bytes(reader)); | ||
} | ||
return new TypeMeta(hash, layers); | ||
} | ||
|
||
to_bytes() { | ||
const writer = new BinaryWriter({}); | ||
writer.uint64(BigInt((this.hash << BigInt(8)) | BigInt((this.layers.length & 0b1111)))); | ||
for (const layer of this.layers) { | ||
writer.buffer(layer.to_bytes()); | ||
} | ||
return writer.dump(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from_bytes
in TypeMeta is a static method, if you want to call it, you should replace bythis.typeMeta = TypeMeta