Skip to content

Commit c0b7c9f

Browse files
committed
Other: Added codegen support for constructor functions, see #700
1 parent 4573f9a commit c0b7c9f

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

cli/targets/static.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var Type = protobuf.Type,
1212
Service = protobuf.Service,
1313
Enum = protobuf.Enum,
1414
Namespace = protobuf.Namespace,
15+
Class = protobuf.Class,
1516
util = protobuf.util;
1617

1718
var out = [];
@@ -150,7 +151,8 @@ var shortVars = {
150151
"f": "impl",
151152
"o": "options",
152153
"d": "object",
153-
"n": "long"
154+
"n": "long",
155+
"p": "properties"
154156
};
155157

156158
function beautifyCode(code) {
@@ -214,7 +216,8 @@ function buildFunction(type, functionName, gen, scope) {
214216

215217
code = code.replace(/ {4}/g, "\t");
216218

217-
var hasScope = scope && Object.keys(scope).length;
219+
var hasScope = scope && Object.keys(scope).length,
220+
isCtor = functionName === type.name;
218221

219222
if (hasScope) // remove unused scope vars
220223
Object.keys(scope).forEach(function(key) {
@@ -223,7 +226,9 @@ function buildFunction(type, functionName, gen, scope) {
223226
});
224227

225228
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
227232
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
228233
else
229234
push(name(type.name) + "." + functionName + " = " + lines[0]);
@@ -235,7 +240,9 @@ function buildFunction(type, functionName, gen, scope) {
235240
push(line.trim());
236241
indent = prev;
237242
});
238-
if (hasScope)
243+
if (isCtor)
244+
push("}");
245+
else if (hasScope)
239246
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
240247
else
241248
push("};");
@@ -281,19 +288,9 @@ function buildType(ref, type) {
281288
type.comment ? "@classdesc " + type.comment : null,
282289
"@exports " + fullName,
283290
"@constructor",
284-
"@param {Object} [properties] Properties to set"
291+
"@param {Object} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
285292
]);
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));
297294

298295
// default values
299296
var firstField = true;

src/class.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function Class(type, ctor) {
2626
throw TypeError("ctor must be a function");
2727
} else
2828
// create named constructor functions (codegen is required anyway)
29-
ctor = util.codegen("p")("return c.call(this,p)").eof(type.name, {
30-
c: Message
29+
ctor = Class.generate(type).eof(type.name, {
30+
Message: Message
3131
});
3232

3333
// Let's pretend...
@@ -72,6 +72,27 @@ function Class(type, ctor) {
7272
return ctor.prototype;
7373
}
7474

75+
/**
76+
* Generates a constructor function for the specified type.
77+
* @param {Type} type Type to use
78+
* @returns {Codegen} Codegen instance
79+
*/
80+
Class.generate = function generate(type) {
81+
var gen = util.codegen("p");
82+
/*
83+
for (var i = 0, field; i < type.fieldsArray.length; ++i)
84+
if ((field = type._fieldsArray[i]).map) gen
85+
("this%s={}", util.safeProp(field.name));
86+
else if (field.repeated) gen
87+
("this%s=[]", util.safeProp(field.name));
88+
*/
89+
return gen
90+
("if(p){")
91+
("for(var ks=Object.keys(p),i=0;i<ks.length;++i)")
92+
("this[ks[i]]=p[ks[i]];")
93+
("}");
94+
};
95+
7596
/**
7697
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
7798
* @function

0 commit comments

Comments
 (0)