Skip to content

Commit 61ed1e6

Browse files
committed
Spawn objects with null prototype
1 parent 4c9f0c3 commit 61ed1e6

File tree

5 files changed

+44
-46
lines changed

5 files changed

+44
-46
lines changed

serde-generate/src/typescript.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> {
9090

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

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

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

162162
self.out.unindent();
163-
writeln!(self.out, "}}")?; // object end
163+
writeln!(self.out, "}})")?; // object end
164164

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

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

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

255255
self.out.unindent();
256-
writeln!(self.out, "}}")?; // object end
256+
writeln!(self.out, "}})")?; // object end
257257

258258
Ok(())
259259
}

suite/typescript/ts/bincode/registry.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type TupleStruct = [$t.i32, $t.f64, $t.str]
2828

2929
export type UnitStruct = $t.unit
3030

31-
export const ComplexStruct = {
31+
export const ComplexStruct = Object.assign(Object.create(null), {
3232
encode(value: ComplexStruct, writer = new BincodeWriter()) {
3333
SimpleStruct.encode(value.inner, writer)
3434
writer.writeBool(value.flag)
@@ -43,7 +43,7 @@ export const ComplexStruct = {
4343
return writer.getBytes()
4444
},
4545
decode(input: Uint8Array, reader = new BincodeReader(input)) {
46-
const value = {} as ComplexStruct
46+
const value = Object.create(null) as ComplexStruct
4747
value.inner = SimpleStruct.decode(input, reader)
4848
value.flag = reader.readBool()
4949
value.items = reader.readList<MultiEnum>(() => MultiEnum.decode(input, reader))
@@ -53,9 +53,9 @@ export const ComplexStruct = {
5353
value.map = reader.readMap<$t.i32, $t.i64>(reader.readI32.bind(reader), reader.readI64.bind(reader))
5454
return value
5555
}
56-
}
56+
})
5757

58-
export const MultiEnum = {
58+
export const MultiEnum = Object.assign(Object.create(null), {
5959
encode(value: MultiEnum, writer = new BincodeWriter()) {
6060
switch (value.$) {
6161
case "VariantA": {
@@ -86,34 +86,34 @@ export const MultiEnum = {
8686
let value: MultiEnum
8787
switch (reader.readVariantIndex()) {
8888
case 0: {
89-
value = { $: "VariantA" } as $t.WrapperOfCase<MultiEnum, "VariantA">
89+
value = Object.assign(Object.create(null), { $: "VariantA" }) as $t.WrapperOfCase<MultiEnum, "VariantA">
9090
value.VariantA = reader.readI32()
9191
break
9292
}
9393
case 1: {
94-
value = { $: "VariantB" } as $t.WrapperOfCase<MultiEnum, "VariantB">
94+
value = Object.assign(Object.create(null), { $: "VariantB" }) as $t.WrapperOfCase<MultiEnum, "VariantB">
9595
value.VariantB = reader.readString()
9696
break
9797
}
9898
case 2: {
99-
value = { $: "VariantC" } as $t.WrapperOfCase<MultiEnum, "VariantC">
100-
value.VariantC = {} as $t.WrapperOfCase<MultiEnum, "VariantC">["VariantC"]
99+
value = Object.assign(Object.create(null), { $: "VariantC" }) as $t.WrapperOfCase<MultiEnum, "VariantC">
100+
value.VariantC = Object.create(null) as $t.WrapperOfCase<MultiEnum, "VariantC">["VariantC"]
101101
value.VariantC.x = reader.readU8()
102102
value.VariantC.y = reader.readF64()
103103
break
104104
}
105105
case 3: {
106-
value = { $: "UnitVariant" } as $t.WrapperOfCase<MultiEnum, "UnitVariant">
106+
value = Object.assign(Object.create(null), { $: "UnitVariant" }) as $t.WrapperOfCase<MultiEnum, "UnitVariant">
107107
value.UnitVariant = reader.readUnit()
108108
break
109109
}
110110
}
111111

112112
return value
113113
}
114-
}
114+
})
115115

116-
export const NewtypeStruct = {
116+
export const NewtypeStruct = Object.assign(Object.create(null), {
117117
encode(value: NewtypeStruct, writer = new BincodeWriter()) {
118118
writer.writeI32(value)
119119
return writer.getBytes()
@@ -122,23 +122,23 @@ export const NewtypeStruct = {
122122
const value: NewtypeStruct = reader.readI32()
123123
return value
124124
}
125-
}
125+
})
126126

127-
export const SimpleStruct = {
127+
export const SimpleStruct = Object.assign(Object.create(null), {
128128
encode(value: SimpleStruct, writer = new BincodeWriter()) {
129129
writer.writeU32(value.a)
130130
writer.writeString(value.b)
131131
return writer.getBytes()
132132
},
133133
decode(input: Uint8Array, reader = new BincodeReader(input)) {
134-
const value = {} as SimpleStruct
134+
const value = Object.create(null) as SimpleStruct
135135
value.a = reader.readU32()
136136
value.b = reader.readString()
137137
return value
138138
}
139-
}
139+
})
140140

141-
export const TupleStruct = {
141+
export const TupleStruct = Object.assign(Object.create(null), {
142142
encode(value: TupleStruct, writer = new BincodeWriter()) {
143143
writer.writeI32(value[0])
144144
writer.writeF64(value[1])
@@ -149,9 +149,9 @@ export const TupleStruct = {
149149
const value: TupleStruct = [reader.readI32(), reader.readF64(), reader.readString()]
150150
return value
151151
}
152-
}
152+
})
153153

154-
export const UnitStruct = {
154+
export const UnitStruct = Object.assign(Object.create(null), {
155155
encode(value: UnitStruct, writer = new BincodeWriter()) {
156156
writer.writeUnit(null)
157157
return writer.getBytes()
@@ -160,4 +160,4 @@ export const UnitStruct = {
160160
const value: $t.unit = reader.readUnit()
161161
return value
162162
}
163-
}
163+
})

suite/typescript/ts/data.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
import * as Registry from "./bincode/registry.ts"
22

3-
export const SimpleStruct_obj: Registry.SimpleStruct = { a: 42, b: "Hello" }
3+
declare global {
4+
interface Object {
5+
dict<const T>(): T
6+
}
7+
}
8+
Object.prototype.dict = function<const T>(this: T) {
9+
return Object.assign(Object.create(null), this) as T
10+
}
11+
12+
export const SimpleStruct_obj: Registry.SimpleStruct = { a: 42, b: "Hello" }.dict()
413
export const SimpleStruct_bin = Uint8Array.from([42, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111])
514

615
export const MultiEnum_VariantC_obj: Registry.MultiEnum = {
716
$: "VariantC",
8-
VariantC: { x: 5, y: 3.14 }
9-
}
17+
VariantC: { x: 5, y: 3.14 }.dict()
18+
}.dict()
19+
1020
export const MultiEnum_VariantC_bin = Uint8Array.from([2, 0, 0, 0, 5, 31, 133, 235, 81, 184, 30, 9, 64])
1121

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

1525
export const ComplexStruct_obj: Registry.ComplexStruct = {
16-
inner: { a: 42, b: "Hello" },
26+
inner: { a: 42, b: "Hello" }.dict(),
1727
flag: true,
1828
items: [
19-
{ $: "VariantA", VariantA: 10 },
20-
{ $: "VariantB", VariantB: "World" }
29+
{ $: "VariantA", VariantA: 10 }.dict(),
30+
{ $: "VariantB", VariantB: "World" }.dict()
2131
],
2232
unit: null,
2333
newtype: 99,
2434
tuple: [123, 45.67, "Test"],
2535
map: new Map().set(3, 7n)
26-
}
36+
}.dict()
2737
export const ComplexStruct_bin = Uint8Array.from([
2838
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
2939
])

suite/typescript/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"strict": false,
34
"noEmit": true,
45
"module": "ESNext",
56
"moduleResolution": "Bundler",

tsconfig.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)