Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@protobufjs/codegen/index.js
1126 views
1
"use strict";
2
module.exports = codegen;
3
4
/**
5
* Begins generating a function.
6
* @memberof util
7
* @param {string[]} functionParams Function parameter names
8
* @param {string} [functionName] Function name if not anonymous
9
* @returns {Codegen} Appender that appends code to the function's body
10
*/
11
function codegen(functionParams, functionName) {
12
13
/* istanbul ignore if */
14
if (typeof functionParams === "string") {
15
functionName = functionParams;
16
functionParams = undefined;
17
}
18
19
var body = [];
20
21
/**
22
* Appends code to the function's body or finishes generation.
23
* @typedef Codegen
24
* @type {function}
25
* @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
26
* @param {...*} [formatParams] Format parameters
27
* @returns {Codegen|Function} Itself or the generated function if finished
28
* @throws {Error} If format parameter counts do not match
29
*/
30
31
function Codegen(formatStringOrScope) {
32
// note that explicit array handling below makes this ~50% faster
33
34
// finish the function
35
if (typeof formatStringOrScope !== "string") {
36
var source = toString();
37
if (codegen.verbose)
38
console.log("codegen: " + source); // eslint-disable-line no-console
39
source = "return " + source;
40
if (formatStringOrScope) {
41
var scopeKeys = Object.keys(formatStringOrScope),
42
scopeParams = new Array(scopeKeys.length + 1),
43
scopeValues = new Array(scopeKeys.length),
44
scopeOffset = 0;
45
while (scopeOffset < scopeKeys.length) {
46
scopeParams[scopeOffset] = scopeKeys[scopeOffset];
47
scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];
48
}
49
scopeParams[scopeOffset] = source;
50
return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func
51
}
52
return Function(source)(); // eslint-disable-line no-new-func
53
}
54
55
// otherwise append to body
56
var formatParams = new Array(arguments.length - 1),
57
formatOffset = 0;
58
while (formatOffset < formatParams.length)
59
formatParams[formatOffset] = arguments[++formatOffset];
60
formatOffset = 0;
61
formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
62
var value = formatParams[formatOffset++];
63
switch ($1) {
64
case "d": case "f": return String(Number(value));
65
case "i": return String(Math.floor(value));
66
case "j": return JSON.stringify(value);
67
case "s": return String(value);
68
}
69
return "%";
70
});
71
if (formatOffset !== formatParams.length)
72
throw Error("parameter count mismatch");
73
body.push(formatStringOrScope);
74
return Codegen;
75
}
76
77
function toString(functionNameOverride) {
78
return "function " + (functionNameOverride || functionName || "") + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";
79
}
80
81
Codegen.toString = toString;
82
return Codegen;
83
}
84
85
/**
86
* Begins generating a function.
87
* @memberof util
88
* @function codegen
89
* @param {string} [functionName] Function name if not anonymous
90
* @returns {Codegen} Appender that appends code to the function's body
91
* @variation 2
92
*/
93
94
/**
95
* When set to `true`, codegen will log generated code to console. Useful for debugging.
96
* @name util.codegen.verbose
97
* @type {boolean}
98
*/
99
codegen.verbose = false;
100
101