Skip to content

Commit

Permalink
Spawn objects with null prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFoxPro committed Sep 16, 2024
1 parent 4c9f0c3 commit 61ed1e6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 46 deletions.
12 changes: 6 additions & 6 deletions serde-generate/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {

fn generate_container(&mut self, name: &str, container: &ContainerFormat) -> Result<()> {
// ENCODE
writeln!(self.out, "export const {name} = {{")?;
writeln!(self.out, "export const {name} = Object.assign(Object.create(null), {{")?;
self.out.indent();

writeln!(self.out, "encode(value: {name}, writer = new BincodeWriter()) {{")?;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
ContainerFormat::TupleStruct(inner_types) => {
writeln!(self.out, "const value: {name} = {}", self.quote_read_value(&Format::Tuple(inner_types.clone())))?;
}
_ => { writeln!(self.out, "const value = {{}} as {name}")?; }
_ => { writeln!(self.out, "const value = Object.create(null) as {name}")?; }
}

match container {
Expand All @@ -160,7 +160,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
writeln!(self.out, "}}")?; // decode end

self.out.unindent();
writeln!(self.out, "}}")?; // object end
writeln!(self.out, "}})")?; // object end

Ok(())
}
Expand Down Expand Up @@ -216,7 +216,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
writeln!(self.out, r#"case {index}: {{"#)?;
self.out.indent();

writeln!(self.out, r#"value = {{ $: "{}" }} as $t.WrapperOfCase<{}, "{}">"#, variant.name, name, variant.name);
writeln!(self.out, r#"value = Object.assign(Object.create(null), {{ $: "{}" }}) as $t.WrapperOfCase<{}, "{}">"#, variant.name, name, variant.name);

match &variant.value {
VariantFormat::Unit => {
Expand All @@ -230,7 +230,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
writeln!(self.out, "value.{} = {}", variant.name, self.quote_read_value(inner));
}
VariantFormat::Struct(fields) => {
writeln!(self.out, r#"value.{var} = {{}} as $t.WrapperOfCase<{name}, "{var}">["{var}"]"#, var = variant.name);
writeln!(self.out, r#"value.{var} = Object.create(null) as $t.WrapperOfCase<{name}, "{var}">["{var}"]"#, var = variant.name);
for field in fields {
writeln!(self.out, "value.{}.{} = {}", variant.name, field.name, self.quote_read_value(&field.value))?;
}
Expand All @@ -253,7 +253,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
writeln!(self.out, "}}")?; // decode end

self.out.unindent();
writeln!(self.out, "}}")?; // object end
writeln!(self.out, "}})")?; // object end

Ok(())
}
Expand Down
38 changes: 19 additions & 19 deletions suite/typescript/ts/bincode/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type TupleStruct = [$t.i32, $t.f64, $t.str]

export type UnitStruct = $t.unit

export const ComplexStruct = {
export const ComplexStruct = Object.assign(Object.create(null), {
encode(value: ComplexStruct, writer = new BincodeWriter()) {
SimpleStruct.encode(value.inner, writer)
writer.writeBool(value.flag)
Expand All @@ -43,7 +43,7 @@ export const ComplexStruct = {
return writer.getBytes()
},
decode(input: Uint8Array, reader = new BincodeReader(input)) {
const value = {} as ComplexStruct
const value = Object.create(null) as ComplexStruct
value.inner = SimpleStruct.decode(input, reader)
value.flag = reader.readBool()
value.items = reader.readList<MultiEnum>(() => MultiEnum.decode(input, reader))
Expand All @@ -53,9 +53,9 @@ export const ComplexStruct = {
value.map = reader.readMap<$t.i32, $t.i64>(reader.readI32.bind(reader), reader.readI64.bind(reader))
return value
}
}
})

export const MultiEnum = {
export const MultiEnum = Object.assign(Object.create(null), {
encode(value: MultiEnum, writer = new BincodeWriter()) {
switch (value.$) {
case "VariantA": {
Expand Down Expand Up @@ -86,34 +86,34 @@ export const MultiEnum = {
let value: MultiEnum
switch (reader.readVariantIndex()) {
case 0: {
value = { $: "VariantA" } as $t.WrapperOfCase<MultiEnum, "VariantA">
value = Object.assign(Object.create(null), { $: "VariantA" }) as $t.WrapperOfCase<MultiEnum, "VariantA">
value.VariantA = reader.readI32()
break
}
case 1: {
value = { $: "VariantB" } as $t.WrapperOfCase<MultiEnum, "VariantB">
value = Object.assign(Object.create(null), { $: "VariantB" }) as $t.WrapperOfCase<MultiEnum, "VariantB">
value.VariantB = reader.readString()
break
}
case 2: {
value = { $: "VariantC" } as $t.WrapperOfCase<MultiEnum, "VariantC">
value.VariantC = {} as $t.WrapperOfCase<MultiEnum, "VariantC">["VariantC"]
value = Object.assign(Object.create(null), { $: "VariantC" }) as $t.WrapperOfCase<MultiEnum, "VariantC">
value.VariantC = Object.create(null) as $t.WrapperOfCase<MultiEnum, "VariantC">["VariantC"]
value.VariantC.x = reader.readU8()
value.VariantC.y = reader.readF64()
break
}
case 3: {
value = { $: "UnitVariant" } as $t.WrapperOfCase<MultiEnum, "UnitVariant">
value = Object.assign(Object.create(null), { $: "UnitVariant" }) as $t.WrapperOfCase<MultiEnum, "UnitVariant">
value.UnitVariant = reader.readUnit()
break
}
}

return value
}
}
})

export const NewtypeStruct = {
export const NewtypeStruct = Object.assign(Object.create(null), {
encode(value: NewtypeStruct, writer = new BincodeWriter()) {
writer.writeI32(value)
return writer.getBytes()
Expand All @@ -122,23 +122,23 @@ export const NewtypeStruct = {
const value: NewtypeStruct = reader.readI32()
return value
}
}
})

export const SimpleStruct = {
export const SimpleStruct = Object.assign(Object.create(null), {
encode(value: SimpleStruct, writer = new BincodeWriter()) {
writer.writeU32(value.a)
writer.writeString(value.b)
return writer.getBytes()
},
decode(input: Uint8Array, reader = new BincodeReader(input)) {
const value = {} as SimpleStruct
const value = Object.create(null) as SimpleStruct
value.a = reader.readU32()
value.b = reader.readString()
return value
}
}
})

export const TupleStruct = {
export const TupleStruct = Object.assign(Object.create(null), {
encode(value: TupleStruct, writer = new BincodeWriter()) {
writer.writeI32(value[0])
writer.writeF64(value[1])
Expand All @@ -149,9 +149,9 @@ export const TupleStruct = {
const value: TupleStruct = [reader.readI32(), reader.readF64(), reader.readString()]
return value
}
}
})

export const UnitStruct = {
export const UnitStruct = Object.assign(Object.create(null), {
encode(value: UnitStruct, writer = new BincodeWriter()) {
writer.writeUnit(null)
return writer.getBytes()
Expand All @@ -160,4 +160,4 @@ export const UnitStruct = {
const value: $t.unit = reader.readUnit()
return value
}
}
})
26 changes: 18 additions & 8 deletions suite/typescript/ts/data.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import * as Registry from "./bincode/registry.ts"

export const SimpleStruct_obj: Registry.SimpleStruct = { a: 42, b: "Hello" }
declare global {
interface Object {
dict<const T>(): T
}
}
Object.prototype.dict = function<const T>(this: T) {
return Object.assign(Object.create(null), this) as T
}

export const SimpleStruct_obj: Registry.SimpleStruct = { a: 42, b: "Hello" }.dict()
export const SimpleStruct_bin = Uint8Array.from([42, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111])

export const MultiEnum_VariantC_obj: Registry.MultiEnum = {
$: "VariantC",
VariantC: { x: 5, y: 3.14 }
}
VariantC: { x: 5, y: 3.14 }.dict()
}.dict()

export const MultiEnum_VariantC_bin = Uint8Array.from([2, 0, 0, 0, 5, 31, 133, 235, 81, 184, 30, 9, 64])

export const MultiEnum_Unit_obj: Registry.MultiEnum = { $: "UnitVariant", UnitVariant: null }
export const MultiEnum_Unit_obj: Registry.MultiEnum = { $: "UnitVariant", UnitVariant: null }.dict()
export const MultiEnum_Unit_bin = Uint8Array.from([3, 0, 0, 0])

export const ComplexStruct_obj: Registry.ComplexStruct = {
inner: { a: 42, b: "Hello" },
inner: { a: 42, b: "Hello" }.dict(),
flag: true,
items: [
{ $: "VariantA", VariantA: 10 },
{ $: "VariantB", VariantB: "World" }
{ $: "VariantA", VariantA: 10 }.dict(),
{ $: "VariantB", VariantB: "World" }.dict()
],
unit: null,
newtype: 99,
tuple: [123, 45.67, "Test"],
map: new Map().set(3, 7n)
}
}.dict()
export const ComplexStruct_bin = Uint8Array.from([
42, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 87, 111, 114, 108, 100, 99, 0, 0, 0, 123, 0, 0, 0, 246, 40, 92, 143, 194, 213, 70, 64, 4, 0, 0, 0, 0, 0, 0, 0, 84, 101, 115, 116, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0
])
1 change: 1 addition & 0 deletions suite/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"strict": false,
"noEmit": true,
"module": "ESNext",
"moduleResolution": "Bundler",
Expand Down
13 changes: 0 additions & 13 deletions tsconfig.json

This file was deleted.

0 comments on commit 61ed1e6

Please sign in to comment.