@@ -21,6 +21,12 @@ static_target.description = "Static code without reflection";
21
21
function static_target ( root , options , callback ) {
22
22
config = options ;
23
23
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 ( "" ) ;
24
30
if ( config . comments )
25
31
push ( "// Lazily resolved type references" ) ;
26
32
push ( "var $lazyTypes = [];" ) ;
@@ -36,7 +42,7 @@ function static_target(root, options, callback) {
36
42
push ( "" ) ;
37
43
if ( config . comments )
38
44
push ( "// Resolve lazy type names to actual types" ) ;
39
- push ( "$protobuf. util.lazyResolve($root, $lazyTypes);" ) ;
45
+ push ( "$util.lazyResolve($root, $lazyTypes);" ) ;
40
46
return callback ( null , out . join ( "\n" ) ) ;
41
47
} catch ( err ) {
42
48
return callback ( err ) ;
@@ -165,18 +171,22 @@ function beautify(code) {
165
171
function buildFunction ( type , functionName , gen , scope ) {
166
172
var code = gen . str ( functionName )
167
173
. replace ( / \( t h i s .c t o r \) / g, " $root" + type . fullName ) // types: construct directly instead of using reflected ctor
168
- . replace ( / ( t y p e s \[ \d + ] ) ( \. v a l u e s ) / g, "$1" ) ; // enums: use types[N] instead of reflected types[N].values
174
+ . replace ( / ( t y p e s \[ \d + ] ) ( \. v a l u e s ) / g, "$1" ) // enums: use types[N] instead of reflected types[N].values
175
+ . replace ( / \b W r i t e r \b / g, "$Writer" ) // use common aliases instead of binding through an iife
176
+ . replace ( / \b R e a d e r \b / g, "$Reader" )
177
+ . replace ( / \b u t i l \b / g, "$util" )
178
+ . replace ( / \b t y p e s \b / g, "$types" ) ;
169
179
170
180
if ( config . beautify )
171
181
code = beautify ( code ) ;
172
182
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 ;
178
184
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
+ } ) ;
180
190
181
191
// enclose all but the first and last line in an iife returning our properly scoped function
182
192
var lines = code . split ( / \n / g) ;
@@ -276,17 +286,17 @@ function buildType(ref, type) {
276
286
firstField = false ;
277
287
}
278
288
if ( field . repeated )
279
- push ( name ( type . name ) + ".prototype" + prop + " = $protobuf. util.emptyArray;" ) ;
289
+ push ( name ( type . name ) + ".prototype" + prop + " = $util.emptyArray;" ) ;
280
290
else if ( field . map )
281
- push ( name ( type . name ) + ".prototype" + prop + " = $protobuf. util.emptyObject;" ) ;
291
+ push ( name ( type . name ) + ".prototype" + prop + " = $util.emptyObject;" ) ;
282
292
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("
284
294
+ JSON . stringify ( field . typeDefault . low ) + ","
285
295
+ JSON . stringify ( field . typeDefault . high ) + ","
286
296
+ JSON . stringify ( field . typeDefault . unsigned )
287
297
+ ") : " + field . typeDefault . toNumber ( field . type . charAt ( 0 ) === "u" ) + ";" ) ;
288
298
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 ) ) + ");" ) ;
290
300
} else
291
301
push ( name ( type . name ) + ".prototype" + prop + " = " + JSON . stringify ( field . typeDefault ) + ";" ) ;
292
302
} ) ;
@@ -310,8 +320,8 @@ function buildType(ref, type) {
310
320
] ) ;
311
321
push ( "Object.defineProperty(" + name ( type . name ) + ".prototype, " + JSON . stringify ( oneof . name ) + ", {" ) ;
312
322
++ 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)" ) ;
315
325
-- indent ;
316
326
push ( "});" ) ;
317
327
} ) ;
@@ -350,16 +360,11 @@ function buildType(ref, type) {
350
360
push ( "" ) ;
351
361
pushComment ( [
352
362
"Encodes the specified " + type . name + " message." ,
353
- "@function" ,
354
363
"@param {" + fullName + "|Object} message " + type . name + " message or plain object to encode" ,
355
364
"@param {$protobuf.Writer} [writer] Writer to encode to" ,
356
365
"@returns {$protobuf.Writer} Writer"
357
366
] ) ;
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 ) ) ;
363
368
364
369
if ( config . delimited ) {
365
370
push ( "" ) ;
@@ -383,16 +388,11 @@ function buildType(ref, type) {
383
388
push ( "" ) ;
384
389
pushComment ( [
385
390
"Decodes " + aOrAn ( type . name ) + " message from the specified reader or buffer." ,
386
- "@function" ,
387
391
"@param {$protobuf.Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from" ,
388
392
"@param {number} [length] Message length if known beforehand" ,
389
393
"@returns {" + fullName + "} " + type . name
390
394
] ) ;
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 ) ) ;
396
396
397
397
if ( config . delimited ) {
398
398
push ( "" ) ;
@@ -403,7 +403,7 @@ function buildType(ref, type) {
403
403
] ) ;
404
404
push ( name ( type . name ) + ".decodeDelimited = function decodeDelimited(readerOrBuffer) {" ) ;
405
405
++ indent ;
406
- push ( "readerOrBuffer = readerOrBuffer instanceof $protobuf. Reader ? readerOrBuffer : $protobuf. Reader(readerOrBuffer);" ) ;
406
+ push ( "readerOrBuffer = readerOrBuffer instanceof $Reader ? readerOrBuffer : $Reader(readerOrBuffer);" ) ;
407
407
push ( "return this.decode(readerOrBuffer, readerOrBuffer.uint32());" ) ;
408
408
-- indent ;
409
409
push ( "};" ) ;
@@ -415,14 +415,10 @@ function buildType(ref, type) {
415
415
push ( "" ) ;
416
416
pushComment ( [
417
417
"Verifies " + aOrAn ( type . name ) + " message." ,
418
- "@function" ,
419
418
"@param {" + fullName + "|Object} message " + type . name + " message or plain object to verify" ,
420
419
"@returns {?string} `null` if valid, otherwise the reason why it is not"
421
420
] ) ;
422
- buildFunction ( type , "verify" , protobuf . verifier ( type ) , {
423
- util : "$protobuf.util" ,
424
- types : hasTypes ? "$types" : undefined
425
- } ) ;
421
+ buildFunction ( type , "verify" , protobuf . verifier ( type ) ) ;
426
422
427
423
}
428
424
@@ -433,16 +429,12 @@ function buildType(ref, type) {
433
429
"@param {Object.<string,*>} object Plain object" ,
434
430
"@returns {" + fullName + "} " + type . name
435
431
] ) ;
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 ) ) ;
440
433
441
434
push ( "" ) ;
442
435
pushComment ( [
443
436
"Creates " + aOrAn ( type . name ) + " message from a plain object. Also converts values to their respective internal types." ,
444
437
"This is an alias of {@link " + fullName + ".fromObject}." ,
445
- "@function" ,
446
438
"@param {Object.<string,*>} object Plain object" ,
447
439
"@returns {" + fullName + "} " + type . name
448
440
] ) ;
@@ -455,10 +447,7 @@ function buildType(ref, type) {
455
447
"@param {$protobuf.ConversionOptions} [options] Conversion options" ,
456
448
"@returns {Object.<string,*>} Plain object"
457
449
] ) ;
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 ) ) ;
462
451
463
452
push ( "" ) ;
464
453
pushComment ( [
0 commit comments