Skip to content

Commit cda5c54

Browse files
committed
New: Removed scoping iifes from generated static code
1 parent def7b45 commit cda5c54

File tree

10 files changed

+1754
-2011
lines changed

10 files changed

+1754
-2011
lines changed

cli/targets/static.js

+30-41
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ static_target.description = "Static code without reflection";
2121
function static_target(root, options, callback) {
2222
config = options;
2323
try {
24+
if (config.comments)
25+
push("// Common aliases");
26+
push("var $Reader = $protobuf.Reader,");
27+
push(" $Writer = $protobuf.Writer,");
28+
push(" $util = $protobuf.util;");
29+
push("");
2430
if (config.comments)
2531
push("// Lazily resolved type references");
2632
push("var $lazyTypes = [];");
@@ -36,7 +42,7 @@ function static_target(root, options, callback) {
3642
push("");
3743
if (config.comments)
3844
push("// Resolve lazy type names to actual types");
39-
push("$protobuf.util.lazyResolve($root, $lazyTypes);");
45+
push("$util.lazyResolve($root, $lazyTypes);");
4046
return callback(null, out.join("\n"));
4147
} catch (err) {
4248
return callback(err);
@@ -165,18 +171,22 @@ function beautify(code) {
165171
function buildFunction(type, functionName, gen, scope) {
166172
var code = gen.str(functionName)
167173
.replace(/\(this.ctor\)/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
168-
.replace(/(types\[\d+])(\.values)/g, "$1"); // enums: use types[N] instead of reflected types[N].values
174+
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
175+
.replace(/\bWriter\b/g, "$Writer") // use common aliases instead of binding through an iife
176+
.replace(/\bReader\b/g, "$Reader")
177+
.replace(/\butil\b/g, "$util")
178+
.replace(/\btypes\b/g, "$types");
169179

170180
if (config.beautify)
171181
code = beautify(code);
172182

173-
// remove unused scope vars
174-
Object.keys(scope).forEach(function(key) {
175-
if (!new RegExp("\\b(" + key + ")\\b", "g").test(code))
176-
delete scope[key];
177-
});
183+
var hasScope = scope && Object.keys(scope).length;
178184

179-
var hasScope = Object.keys(scope).length;
185+
if (hasScope) // remove unused scope vars
186+
Object.keys(scope).forEach(function(key) {
187+
if (!new RegExp("\\b(" + key + ")\\b", "g").test(code))
188+
delete scope[key];
189+
});
180190

181191
// enclose all but the first and last line in an iife returning our properly scoped function
182192
var lines = code.split(/\n/g);
@@ -276,17 +286,17 @@ function buildType(ref, type) {
276286
firstField = false;
277287
}
278288
if (field.repeated)
279-
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyArray;");
289+
push(name(type.name) + ".prototype" + prop + " = $util.emptyArray;");
280290
else if (field.map)
281-
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.emptyObject;");
291+
push(name(type.name) + ".prototype" + prop + " = $util.emptyObject;");
282292
else if (field.long)
283-
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.Long ? $protobuf.util.Long.fromBits("
293+
push(name(type.name) + ".prototype" + prop + " = $util.Long ? $util.Long.fromBits("
284294
+ JSON.stringify(field.typeDefault.low) + ","
285295
+ JSON.stringify(field.typeDefault.high) + ","
286296
+ JSON.stringify(field.typeDefault.unsigned)
287297
+ ") : " + field.typeDefault.toNumber(field.type.charAt(0) === "u") + ";");
288298
else if (field.bytes) {
289-
push(name(type.name) + ".prototype" + prop + " = $protobuf.util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
299+
push(name(type.name) + ".prototype" + prop + " = $util.newBuffer(" + JSON.stringify(Array.prototype.slice.call(field.typeDefault)) + ");");
290300
} else
291301
push(name(type.name) + ".prototype" + prop + " = " + JSON.stringify(field.typeDefault) + ";");
292302
});
@@ -310,8 +320,8 @@ function buildType(ref, type) {
310320
]);
311321
push("Object.defineProperty(" + name(type.name) + ".prototype, " + JSON.stringify(oneof.name) +", {");
312322
++indent;
313-
push("get: $protobuf.util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
314-
push("set: $protobuf.util.oneOfSetter($oneOfFields)");
323+
push("get: $util.oneOfGetter($oneOfFields = [" + oneof.oneof.map(JSON.stringify).join(", ") + "]),");
324+
push("set: $util.oneOfSetter($oneOfFields)");
315325
--indent;
316326
push("});");
317327
});
@@ -350,16 +360,11 @@ function buildType(ref, type) {
350360
push("");
351361
pushComment([
352362
"Encodes the specified " + type.name + " message.",
353-
"@function",
354363
"@param {" + fullName + "|Object} message " + type.name + " message or plain object to encode",
355364
"@param {$protobuf.Writer} [writer] Writer to encode to",
356365
"@returns {$protobuf.Writer} Writer"
357366
]);
358-
buildFunction(type, "encode", protobuf.encoder(type), {
359-
Writer : "$protobuf.Writer",
360-
util : "$protobuf.util",
361-
types : hasTypes ? "$types" : undefined
362-
});
367+
buildFunction(type, "encode", protobuf.encoder(type));
363368

364369
if (config.delimited) {
365370
push("");
@@ -383,16 +388,11 @@ function buildType(ref, type) {
383388
push("");
384389
pushComment([
385390
"Decodes " + aOrAn(type.name) + " message from the specified reader or buffer.",
386-
"@function",
387391
"@param {$protobuf.Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from",
388392
"@param {number} [length] Message length if known beforehand",
389393
"@returns {" + fullName + "} " + type.name
390394
]);
391-
buildFunction(type, "decode", protobuf.decoder(type), {
392-
Reader : "$protobuf.Reader",
393-
util : "$protobuf.util",
394-
types : hasTypes ? "$types" : undefined
395-
});
395+
buildFunction(type, "decode", protobuf.decoder(type));
396396

397397
if (config.delimited) {
398398
push("");
@@ -403,7 +403,7 @@ function buildType(ref, type) {
403403
]);
404404
push(name(type.name) + ".decodeDelimited = function decodeDelimited(readerOrBuffer) {");
405405
++indent;
406-
push("readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);");
406+
push("readerOrBuffer = readerOrBuffer instanceof $Reader ? readerOrBuffer : $Reader(readerOrBuffer);");
407407
push("return this.decode(readerOrBuffer, readerOrBuffer.uint32());");
408408
--indent;
409409
push("};");
@@ -415,14 +415,10 @@ function buildType(ref, type) {
415415
push("");
416416
pushComment([
417417
"Verifies " + aOrAn(type.name) + " message.",
418-
"@function",
419418
"@param {" + fullName + "|Object} message " + type.name + " message or plain object to verify",
420419
"@returns {?string} `null` if valid, otherwise the reason why it is not"
421420
]);
422-
buildFunction(type, "verify", protobuf.verifier(type), {
423-
util : "$protobuf.util",
424-
types : hasTypes ? "$types" : undefined
425-
});
421+
buildFunction(type, "verify", protobuf.verifier(type));
426422

427423
}
428424

@@ -433,16 +429,12 @@ function buildType(ref, type) {
433429
"@param {Object.<string,*>} object Plain object",
434430
"@returns {" + fullName + "} " + type.name
435431
]);
436-
buildFunction(type, "fromObject", protobuf.converter.fromObject(type), {
437-
util : "$protobuf.util",
438-
types : hasTypes ? "$types" : undefined
439-
});
432+
buildFunction(type, "fromObject", protobuf.converter.fromObject(type));
440433

441434
push("");
442435
pushComment([
443436
"Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
444437
"This is an alias of {@link " + fullName + ".fromObject}.",
445-
"@function",
446438
"@param {Object.<string,*>} object Plain object",
447439
"@returns {" + fullName + "} " + type.name
448440
]);
@@ -455,10 +447,7 @@ function buildType(ref, type) {
455447
"@param {$protobuf.ConversionOptions} [options] Conversion options",
456448
"@returns {Object.<string,*>} Plain object"
457449
]);
458-
buildFunction(type, "toObject", protobuf.converter.toObject(type), {
459-
util : "$protobuf.util",
460-
types : hasTypes ? "$types" : undefined
461-
});
450+
buildFunction(type, "toObject", protobuf.converter.toObject(type));
462451

463452
push("");
464453
pushComment([

0 commit comments

Comments
 (0)