|
@@ -2,7 +2,7 @@ const fs = require('fs');
|
|
const util = require('util');
|
|
const util = require('util');
|
|
const {crc32} = require('crc');
|
|
const {crc32} = require('crc');
|
|
const SourceBuilder = require('../sourcebuilder');
|
|
const SourceBuilder = require('../sourcebuilder');
|
|
-const {snakeToCamelCase} = require("../utils");
|
|
|
|
|
|
+const {snakeToCamelCase, variableSnakeToCamelCase} = require("../utils");
|
|
|
|
|
|
const AUTO_GEN_NOTICE =
|
|
const AUTO_GEN_NOTICE =
|
|
"/*! File generated by TLObjects' generator. All changes will be ERASED !*/";
|
|
"/*! File generated by TLObjects' generator. All changes will be ERASED !*/";
|
|
@@ -92,10 +92,12 @@ const writeModules = (
|
|
|
|
|
|
// Import struct for the .__bytes__(self) serialization
|
|
// Import struct for the .__bytes__(self) serialization
|
|
builder.writeln("const struct = require('python-struct');");
|
|
builder.writeln("const struct = require('python-struct');");
|
|
|
|
+ builder.writeln(`const Helpers = require('../../utils/Helpers');`);
|
|
|
|
+ builder.writeln(`const BigIntBuffer = require("bigint-buffer");`);
|
|
|
|
|
|
const typeNames = new Set();
|
|
const typeNames = new Set();
|
|
const typeDefs = [];
|
|
const typeDefs = [];
|
|
-
|
|
|
|
|
|
+ /*
|
|
// Find all the types in this file and generate type definitions
|
|
// Find all the types in this file and generate type definitions
|
|
// based on the types. The type definitions are written to the
|
|
// based on the types. The type definitions are written to the
|
|
// file at the end.
|
|
// file at the end.
|
|
@@ -128,7 +130,7 @@ const writeModules = (
|
|
);
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ }*/
|
|
|
|
|
|
const imports = {};
|
|
const imports = {};
|
|
const primitives = new Set([
|
|
const primitives = new Set([
|
|
@@ -318,7 +320,7 @@ const writeClassConstructor = (tlobject, kind, typeConstructors, builder) => {
|
|
// Set the arguments
|
|
// Set the arguments
|
|
for (const arg of tlobject.realArgs) {
|
|
for (const arg of tlobject.realArgs) {
|
|
if (!arg.canBeInferred) {
|
|
if (!arg.canBeInferred) {
|
|
- builder.writeln(`this.${arg.name} = args.${arg.name};`);
|
|
|
|
|
|
+ builder.writeln(`this.${variableSnakeToCamelCase(arg.name)} = args.${variableSnakeToCamelCase(arg.name)};`);
|
|
}
|
|
}
|
|
|
|
|
|
// Currently the only argument that can be
|
|
// Currently the only argument that can be
|
|
@@ -452,8 +454,7 @@ const writeToBytes = (tlobject, builder) => {
|
|
// Some objects require more than one flag parameter to be set
|
|
// Some objects require more than one flag parameter to be set
|
|
// at the same time. In this case, add an assertion.
|
|
// at the same time. In this case, add an assertion.
|
|
const repeatedArgs = {};
|
|
const repeatedArgs = {};
|
|
-
|
|
|
|
- for (const arg of tlobject.args) {
|
|
|
|
|
|
+ for (let arg of tlobject.args) {
|
|
if (arg.isFlag) {
|
|
if (arg.isFlag) {
|
|
if (!repeatedArgs[arg.flagIndex]) {
|
|
if (!repeatedArgs[arg.flagIndex]) {
|
|
repeatedArgs[arg.flagIndex] = [];
|
|
repeatedArgs[arg.flagIndex] = [];
|
|
@@ -461,59 +462,50 @@ const writeToBytes = (tlobject, builder) => {
|
|
repeatedArgs[arg.flagIndex].push(arg);
|
|
repeatedArgs[arg.flagIndex].push(arg);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- for (const ra of Object.values(repeatedArgs)) {
|
|
|
|
|
|
+ for (let ra of Object.values(repeatedArgs)) {
|
|
if (ra.length > 1) {
|
|
if (ra.length > 1) {
|
|
- const cnd1 = ra.map(
|
|
|
|
- a => `(this.${a.name} || this.${a.name} !== null)`
|
|
|
|
- );
|
|
|
|
- const cnd2 = ra.map(
|
|
|
|
- a => `(this.${a.name} === null || this.${a.name} === false)`
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- builder.writeln(
|
|
|
|
- 'if (!(%s || %s)) {',
|
|
|
|
- cnd1.join(' && '),
|
|
|
|
- cnd2.join(' && ')
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- builder.writeln(
|
|
|
|
- "throw new Error('%s parameters must all be false-y (like null) or all me true-y');",
|
|
|
|
- ra.map(a => a.name).join(', ')
|
|
|
|
- );
|
|
|
|
- builder.endBlock();
|
|
|
|
|
|
+ let cnd1 = [];
|
|
|
|
+ let cnd2 = [];
|
|
|
|
+ let names = [];
|
|
|
|
+
|
|
|
|
+ for (let a of ra) {
|
|
|
|
+ cnd1.push(`this.${a.name} || this.${a.name}!==null`);
|
|
|
|
+ cnd2.push(`this.${a.name}===null || this.${a.name}===false`);
|
|
|
|
+ names.push(a.name);
|
|
|
|
+ }
|
|
|
|
+ builder.writeln("if (!((%s) && (%s)))\n\t throw new Error('%s paramaters must all"
|
|
|
|
+ + " be false-y or all true')", cnd1.join(" && "), cnd2.join(" && "), names.join(", "));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- const bytes = Buffer.from(
|
|
|
|
- parseInt(tlobject.id)
|
|
|
|
- .toString(16)
|
|
|
|
- .padStart(8, `0`),
|
|
|
|
- `hex`
|
|
|
|
- )
|
|
|
|
- .readUInt32LE(0)
|
|
|
|
- .toString(16)
|
|
|
|
- .padStart(8, `0`);
|
|
|
|
-
|
|
|
|
- builder.writeln('return Buffer.concat([');
|
|
|
|
|
|
+ builder.writeln("return Buffer.concat([");
|
|
builder.currentIndent++;
|
|
builder.currentIndent++;
|
|
-
|
|
|
|
- builder.writeln("'%s',", bytes);
|
|
|
|
-
|
|
|
|
- for (const arg of tlobject.args) {
|
|
|
|
|
|
+ let b = Buffer.alloc(4);
|
|
|
|
+ b.writeUInt32LE(tlobject.id, 0);
|
|
|
|
+ // First constructor code, we already know its bytes
|
|
|
|
+ builder.writeln('Buffer.from("%s","hex"),', b.toString("hex"));
|
|
|
|
+ for (let arg of tlobject.args) {
|
|
|
|
+ console.log("ok");
|
|
if (writeArgToBytes(builder, arg, tlobject.args)) {
|
|
if (writeArgToBytes(builder, arg, tlobject.args)) {
|
|
builder.writeln(',');
|
|
builder.writeln(',');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
builder.currentIndent--;
|
|
builder.currentIndent--;
|
|
- builder.writeln(']);');
|
|
|
|
|
|
+ builder.writeln("])");
|
|
builder.endBlock();
|
|
builder.endBlock();
|
|
|
|
+
|
|
};
|
|
};
|
|
|
|
|
|
// writeFromReader
|
|
// writeFromReader
|
|
const writeFromReader = (tlobject, builder) => {
|
|
const writeFromReader = (tlobject, builder) => {
|
|
|
|
+
|
|
builder.writeln("static fromReader(reader) {");
|
|
builder.writeln("static fromReader(reader) {");
|
|
|
|
+ for (const arg of tlobject.args) {
|
|
|
|
+ if (arg.name !== "flag")
|
|
|
|
+ builder.writeln("let %s", "_" + arg.name + ";");
|
|
|
|
+ }
|
|
|
|
+ builder.writeln("let _x;");
|
|
|
|
+
|
|
|
|
+
|
|
for (const arg of tlobject.args) {
|
|
for (const arg of tlobject.args) {
|
|
writeArgReadCode(builder, arg, tlobject.args, "_" + arg.name);
|
|
writeArgReadCode(builder, arg, tlobject.args, "_" + arg.name);
|
|
}
|
|
}
|
|
@@ -544,7 +536,7 @@ const writeArgToBytes = (builder, arg, args, name = null) => {
|
|
if (name === null) {
|
|
if (name === null) {
|
|
name = `this.${arg.name}`;
|
|
name = `this.${arg.name}`;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ name = variableSnakeToCamelCase(name);
|
|
// The argument may be a flag, only write if it's not None AND
|
|
// The argument may be a flag, only write if it's not None AND
|
|
// if it's not a True type.
|
|
// if it's not a True type.
|
|
// True types are not actually sent, but instead only used to
|
|
// True types are not actually sent, but instead only used to
|
|
@@ -643,7 +635,7 @@ const writeArgToBytes = (builder, arg, args, name = null) => {
|
|
builder.write(']');
|
|
builder.write(']');
|
|
|
|
|
|
if (arg.isVector) {
|
|
if (arg.isVector) {
|
|
- builder.write(']');
|
|
|
|
|
|
+ builder.write(')');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -672,7 +664,7 @@ const writeArgReadCode = (builder, arg, args, name) => {
|
|
// Treat 'true' flags as a special case, since they're true if
|
|
// Treat 'true' flags as a special case, since they're true if
|
|
// they're set, and nothing else needs to actually be read.
|
|
// they're set, and nothing else needs to actually be read.
|
|
if (arg.type === "true") {
|
|
if (arg.type === "true") {
|
|
- builder.writeln("let %s = Boolean(flags & %s)", name, 1 << arg.flagIndex);
|
|
|
|
|
|
+ builder.writeln("%s = Boolean(flags & %s)", name, 1 << arg.flagIndex);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -686,9 +678,9 @@ const writeArgReadCode = (builder, arg, args, name) => {
|
|
if (arg.isVector) {
|
|
if (arg.isVector) {
|
|
if (arg.useVectorId) {
|
|
if (arg.useVectorId) {
|
|
// We have to read the vector's constructor ID
|
|
// We have to read the vector's constructor ID
|
|
- builder.writeln("reader.readInt()");
|
|
|
|
|
|
+ builder.writeln("reader.readInt();");
|
|
}
|
|
}
|
|
- builder.writeln("let %s = []", name);
|
|
|
|
|
|
+ builder.writeln("%s = []", name);
|
|
builder.writeln("for (let i=0;i<reader.readInt();i++){");
|
|
builder.writeln("for (let i=0;i<reader.readInt();i++){");
|
|
// Temporary disable .is_vector, not to enter this if again
|
|
// Temporary disable .is_vector, not to enter this if again
|
|
arg.isVector = false;
|
|
arg.isVector = false;
|
|
@@ -701,29 +693,29 @@ const writeArgReadCode = (builder, arg, args, name) => {
|
|
builder.writeln("let flags = reader.readInt()");
|
|
builder.writeln("let flags = reader.readInt()");
|
|
builder.writeln();
|
|
builder.writeln();
|
|
} else if (arg.type === "int") {
|
|
} else if (arg.type === "int") {
|
|
- builder.writeln("let %s = reader.readInt()", name)
|
|
|
|
|
|
+ builder.writeln("%s = reader.readInt();", name)
|
|
} else if (arg.type === "long") {
|
|
} else if (arg.type === "long") {
|
|
- builder.writeln("let %s = reader.readInt()", name);
|
|
|
|
|
|
+ builder.writeln("%s = reader.readInt();", name);
|
|
} else if (arg.type === "int128") {
|
|
} else if (arg.type === "int128") {
|
|
- builder.writeln('let %s = reader.readLargeInt(bits=128)', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.readLargeInt(128);', name);
|
|
} else if (arg.type === "int256") {
|
|
} else if (arg.type === "int256") {
|
|
- builder.writeln('let %s = reader.readLargeInt(bits=256)', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.readLargeInt(256);', name);
|
|
} else if (arg.type === "double") {
|
|
} else if (arg.type === "double") {
|
|
- builder.writeln('let %s = reader.readDouble()', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.readDouble();', name);
|
|
} else if (arg.type === "string") {
|
|
} else if (arg.type === "string") {
|
|
- builder.writeln('let %s = reader.tgReadString()', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.tgReadString();', name);
|
|
} else if (arg.type === "Bool") {
|
|
} else if (arg.type === "Bool") {
|
|
- builder.writeln('let %s = reader.tgReadBool()', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.tgReadBool();', name);
|
|
} else if (arg.type === "true") {
|
|
} else if (arg.type === "true") {
|
|
- builder.writeln('let %s = true', name);
|
|
|
|
|
|
+ builder.writeln('%s = true;', name);
|
|
} else if (arg.type === "bytes") {
|
|
} else if (arg.type === "bytes") {
|
|
- builder.writeln('let %s = reader.tgReadBytes()', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.tgReadBytes();', name);
|
|
} else if (arg.type === "date") {
|
|
} else if (arg.type === "date") {
|
|
- builder.writeln('let %s = reader.tgReadDate()', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.tgReadDate();', name);
|
|
} else {
|
|
} else {
|
|
// Else it may be a custom type
|
|
// Else it may be a custom type
|
|
if (!arg.skipConstructorId) {
|
|
if (!arg.skipConstructorId) {
|
|
- builder.writeln('let %s = reader.tgReadObject()', name);
|
|
|
|
|
|
+ builder.writeln('%s = reader.tgReadObject();', name);
|
|
} else {
|
|
} else {
|
|
// Import the correct type inline to avoid cyclic imports.
|
|
// Import the correct type inline to avoid cyclic imports.
|
|
// There may be better solutions so that we can just access
|
|
// There may be better solutions so that we can just access
|
|
@@ -744,8 +736,8 @@ const writeArgReadCode = (builder, arg, args, name) => {
|
|
// file with the same namespace, but since it does no harm
|
|
// file with the same namespace, but since it does no harm
|
|
// and we don't have information about such thing in the
|
|
// and we don't have information about such thing in the
|
|
// method we just ignore that case.
|
|
// method we just ignore that case.
|
|
- builder.writeln('const %s = require("%S")', className, ns);
|
|
|
|
- builder.writeln("let %s = %s.fromReader(reader)", name, className);
|
|
|
|
|
|
+ builder.writeln('const %s = require("%S");', className, ns);
|
|
|
|
+ builder.writeln("%s = %s.fromReader(reader);", name, className);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -753,11 +745,12 @@ const writeArgReadCode = (builder, arg, args, name) => {
|
|
if (arg.isVector) {
|
|
if (arg.isVector) {
|
|
builder.endBlock();
|
|
builder.endBlock();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ builder.currentIndent -= 1;
|
|
if (wasFlag) {
|
|
if (wasFlag) {
|
|
builder.currentIndent -= 1;
|
|
builder.currentIndent -= 1;
|
|
builder.writeln("else");
|
|
builder.writeln("else");
|
|
- builder.writeln("let %s = null", name);
|
|
|
|
|
|
+ builder.currentIndent -= 1;
|
|
|
|
+ builder.writeln("%s = null", name);
|
|
builder.currentIndent -= 1;
|
|
builder.currentIndent -= 1;
|
|
// Restore .isFlag;
|
|
// Restore .isFlag;
|
|
arg.isFlag = true;
|
|
arg.isFlag = true;
|
|
@@ -776,6 +769,9 @@ const writePatched = (outDir, namespaceTlobjects) => {
|
|
builder.writeln(AUTO_GEN_NOTICE);
|
|
builder.writeln(AUTO_GEN_NOTICE);
|
|
builder.writeln("const struct = require('python-struct');");
|
|
builder.writeln("const struct = require('python-struct');");
|
|
builder.writeln(`const { TLObject, types, custom } = require('..');`);
|
|
builder.writeln(`const { TLObject, types, custom } = require('..');`);
|
|
|
|
+ builder.writeln(`const Helpers = require('../../utils/Helpers');`);
|
|
|
|
+ builder.writeln(`const BigIntBuffer = require("bigint-buffer");`);
|
|
|
|
+
|
|
builder.writeln();
|
|
builder.writeln();
|
|
|
|
|
|
for (const t of tlobjects) {
|
|
for (const t of tlobjects) {
|