@@ -12,6 +12,7 @@ var Type = protobuf.Type,
12
12
Service = protobuf . Service ,
13
13
Enum = protobuf . Enum ,
14
14
Namespace = protobuf . Namespace ,
15
+ Class = protobuf . Class ,
15
16
util = protobuf . util ;
16
17
17
18
var out = [ ] ;
@@ -150,7 +151,8 @@ var shortVars = {
150
151
"f" : "impl" ,
151
152
"o" : "options" ,
152
153
"d" : "object" ,
153
- "n" : "long"
154
+ "n" : "long" ,
155
+ "p" : "properties"
154
156
} ;
155
157
156
158
function beautifyCode ( code ) {
@@ -214,7 +216,8 @@ function buildFunction(type, functionName, gen, scope) {
214
216
215
217
code = code . replace ( / { 4 } / g, "\t" ) ;
216
218
217
- var hasScope = scope && Object . keys ( scope ) . length ;
219
+ var hasScope = scope && Object . keys ( scope ) . length ,
220
+ isCtor = functionName === type . name ;
218
221
219
222
if ( hasScope ) // remove unused scope vars
220
223
Object . keys ( scope ) . forEach ( function ( key ) {
@@ -223,7 +226,9 @@ function buildFunction(type, functionName, gen, scope) {
223
226
} ) ;
224
227
225
228
var lines = code . split ( / \n / g) ;
226
- if ( hasScope ) // enclose in an iife
229
+ if ( isCtor ) // constructor
230
+ push ( lines [ 0 ] ) ;
231
+ else if ( hasScope ) // enclose in an iife
227
232
push ( name ( type . name ) + "." + functionName + " = (function(" + Object . keys ( scope ) . join ( ", " ) + ") { return " + lines [ 0 ] ) ;
228
233
else
229
234
push ( name ( type . name ) + "." + functionName + " = " + lines [ 0 ] ) ;
@@ -235,7 +240,9 @@ function buildFunction(type, functionName, gen, scope) {
235
240
push ( line . trim ( ) ) ;
236
241
indent = prev ;
237
242
} ) ;
238
- if ( hasScope )
243
+ if ( isCtor )
244
+ push ( "}" ) ;
245
+ else if ( hasScope )
239
246
push ( "};})(" + Object . keys ( scope ) . map ( function ( key ) { return scope [ key ] ; } ) . join ( ", " ) + ");" ) ;
240
247
else
241
248
push ( "};" ) ;
@@ -281,19 +288,9 @@ function buildType(ref, type) {
281
288
type . comment ? "@classdesc " + type . comment : null ,
282
289
"@exports " + fullName ,
283
290
"@constructor" ,
284
- "@param {Object} [properties] Properties to set"
291
+ "@param {Object} [" + ( config . beautify ? " properties" : "p" ) + " ] Properties to set"
285
292
] ) ;
286
- push ( "function " + name ( type . name ) + "(properties) {" ) ;
287
- ++ indent ;
288
- push ( "if (properties)" ) ;
289
- ++ indent ;
290
- push ( "for (" + ( config . es6 ? "let" : "var" ) + " keys = Object.keys(properties), i = 0; i < keys.length; ++i)" ) ;
291
- ++ indent ;
292
- push ( "this[keys[i]] = properties[keys[i]];" ) ;
293
- -- indent ;
294
- -- indent ;
295
- -- indent ;
296
- push ( "}" ) ;
293
+ buildFunction ( type , type . name , Class . generate ( type ) ) ;
297
294
298
295
// default values
299
296
var firstField = true ;
0 commit comments