Skip to content
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

Forchapeatl type meta #1825

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions javascript/packages/fury/lib/fury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
import ClassResolver from "./classResolver";
import { BinaryWriter } from "./writer";
import { BinaryReader } from "./reader";
import { BinaryReaderBuilder } from "./gen/builder";
import { ReferenceResolver } from "./referenceResolver";
import { ConfigFlags, Serializer, Config, Language, MAGIC_NUMBER } from "./type";
import { OwnershipError } from "./error";
import { InputType, ResultType, TypeDescription } from "./description";
import { generateSerializer, AnySerializer } from "./gen";
import { TypeMeta } from './meta/TypeMeta';


export default class {
binaryReader: BinaryReader;
binaryWriter: BinaryWriter;
classResolver = new ClassResolver();
referenceResolver: ReferenceResolver;
anySerializer: AnySerializer;
typeMeta = TypeMeta;

constructor(public config: Config = {
refTracking: false,
Expand Down Expand Up @@ -131,3 +135,11 @@ export default class {
return this.serializeInternal(data, serializer).dumpAndOwn();
}
}

class TypeMetaBuilder{
constructor(private fury: string){
}
from_bytes(reader: string){
return `${this.fury}.typeMeta.from_bytes(${reader})`;
}
}
16 changes: 15 additions & 1 deletion javascript/packages/fury/lib/gen/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ import { Scope } from "./scope";
import { getMeta } from "../meta";
import { TypeDescription } from "../description";
import Fury from "../fury";
import { TypeMeta } from '../meta/TypeMeta';
import { BinaryReader } from "../reader";

class BinaryReaderBuilder {
class TypeMetaBuilder{
constructor(private fury: string){

}
from_bytes(reader: string){
return `${this.fury}.typeMeta.from_bytes(${reader})`;
}
}

export class BinaryReaderBuilder {
constructor(private holder: string) {

}
Expand Down Expand Up @@ -309,6 +320,7 @@ class ClassResolverBuilder {
export class CodecBuilder {
reader: BinaryReaderBuilder;
writer: BinaryWriterBuilder;
typeMeta: TypeMetaBuilder; // Use the TypeMetaWrapper
referenceResolver: ReferenceResolverBuilder;
classResolver: ClassResolverBuilder;

Expand All @@ -321,6 +333,8 @@ export class CodecBuilder {
this.writer = new BinaryWriterBuilder(bw);
this.classResolver = new ClassResolverBuilder(cr);
this.referenceResolver = new ReferenceResolverBuilder(rr);
this.typeMeta = new TypeMetaBuilder("fury"); // Initialize the TypeMetaWrapper

}

furyName() {
Expand Down
13 changes: 11 additions & 2 deletions javascript/packages/fury/lib/gen/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { CodegenRegistry } from "./router";
import { BaseSerializerGenerator, RefState } from "./serializer";
import SerializerResolver from "../classResolver";
import { MetaString } from "../meta/MetaString";
import { FieldInfo, TypeMeta } from '../meta/TypeMeta';


// Ensure MetaString methods are correctly implemented
const computeMetaInformation = (description: any) => {
Expand Down Expand Up @@ -81,10 +83,17 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
const options = this.description.options;
const expectHash = computeStructHash(this.description);
const metaInformation = Buffer.from(computeMetaInformation(this.description));
const fields = Object.entries(options.props).map(([key, value]) => {
return new FieldInfo(key, value.type);
});
const binary = TypeMeta.from_fields(256, fields).to_bytes();
const binaryLength = binary.length; // Capture the length of the binary data

// Encode binary data as a base64 string
const binaryBase64 = Buffer.from(binary).toString("base64");
return `
${this.builder.writer.int32(expectHash)};
${this.builder.writer.buffer(`Buffer.from("${metaInformation.toString("base64")}", "base64")`)};
${this.builder.writer.buffer(binaryBase64)};
Copy link
Member

@theweipeng theweipeng Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the base64 string is "AQAAAAAAAAACgATsEgsABQg="
the code you generated will be bw.buffer(AQAAAAAAAAACgATsEgsABQg=); it includes syntax error

${Object.entries(options.props).sort().map(([key, inner]) => {
const InnerGeneratorClass = CodegenRegistry.get(inner.type);
if (!InnerGeneratorClass) {
Expand Down Expand Up @@ -112,7 +121,7 @@ class ObjectSerializerGenerator extends BaseSerializerGenerator {
}).join(",\n")}
};
${this.maybeReference(result, refState)}
${this.builder.reader.buffer(encodedMetaInformation.byteLength)}
${this.builder.typeMeta.from_bytes(this.builder.reader.ownName())}
${Object.entries(options.props).sort().map(([key, inner]) => {
const InnerGeneratorClass = CodegenRegistry.get(inner.type);
if (!InnerGeneratorClass) {
Expand Down
167 changes: 167 additions & 0 deletions javascript/packages/fury/lib/meta/TypeMeta.ts
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() {
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();
}
}
Loading