fix(core): fix bare types and mt_message writing

This commit is contained in:
teidesu 2021-08-19 23:09:44 +03:00
parent 9b5ca0cb2a
commit 49eda8f0e3
5 changed files with 44 additions and 26 deletions

View file

@ -176,7 +176,7 @@ export class SerializationCounter implements ITlBinaryWriter {
vector(fn: TlBinaryWriterFunction, items: unknown[], bare?: boolean): void {
this.count += bare ? 4 : 8
items.forEach((it) => fn.call(this, it))
items.forEach((it) => fn.call(this, it, bare))
}
}
@ -301,11 +301,11 @@ export class BinaryWriter implements ITlBinaryWriter {
this._objectMap[obj._].call(this, obj, bare)
}
vector(fn: TlBinaryWriterFunction, val: unknown[]): void {
this.uint32(0x1cb5c415)
vector(fn: TlBinaryWriterFunction, val: unknown[], bare?: boolean): void {
if (!bare) this.uint32(0x1cb5c415)
this.uint32(val.length)
val.forEach((it) => fn.call(this, it))
val.forEach((it) => fn.call(this, it, bare))
}
result(): Buffer {

View file

@ -50,7 +50,7 @@ export interface ITlBinaryWriter {
*
* @param fn Writer function
* @param items Items to be written
* @param bare Whether the vector is bare (i.e. vector ID should not be written)
* @param bare Whether the vector is bare (i.e. object ID should not be written)
*/
vector(fn: TlBinaryWriterFunction, items: unknown[], bare?: boolean): void
}

View file

@ -80,6 +80,9 @@ function writeNamespace(nsType) {
write(`${cls.id}: function () {`)
tab()
let is_mt_message =
nsType === 'mtproto' && cls.name === 'message'
if (cls.arguments?.length) {
write(`var ret = {};`)
write(`ret._ = '${prefix}${cls.name}';`)
@ -105,6 +108,9 @@ function writeNamespace(nsType) {
)}`
)
}
} else {
if (is_mt_message && arg.name === 'body') {
write('ret.body = this.raw(ret.bytes)')
} else {
write(
`ret.${
@ -116,6 +122,7 @@ function writeNamespace(nsType) {
)};`
)
}
}
})
write('return ret;')

View file

@ -120,6 +120,9 @@ function writeNamespace(nsType) {
} else {
write(`_assert_has(obj, '${arg.name}')`)
}
if (is_mt_message && arg.name === 'body') {
write('this.raw(obj.body)')
} else {
write(
`this.${getFunctionCallByTypeName(
baseTypePrefix,
@ -127,6 +130,7 @@ function writeNamespace(nsType) {
`obj.${arg.name}`
)}`
)
}
if (arg.optional) {
untab()
}

View file

@ -162,6 +162,9 @@ const writeSingleSchemaEntry = (type) => {
ts.tab()
ts.write(`readonly _: '${prefix}${cls.name}',`)
let is_mt_message =
type === 'mtproto' && cls.name === 'message'
if (cls.arguments && cls.arguments.length) {
cls.arguments.forEach((arg) => {
if (arg.type === '$FlagsBitField') return
@ -170,11 +173,15 @@ const writeSingleSchemaEntry = (type) => {
arg.type = 'boolean'
if (arg.description) ts.comment(arg.description)
if (is_mt_message && arg.name === 'body') {
ts.write('readonly body: Buffer;')
} else {
ts.write(
`readonly ${arg.name}${
arg.optional ? '?' : ''
}: ${fullTypeName(arg.type)};`
)
}
})
}