Permalink
Browse files

Other: Added codegen support for constructor functions, see #700

  • Loading branch information...
dcodeIO committed Mar 12, 2017
1 parent 4573f9a commit c0b7c9fa6309d345c4ce8e06fd86f27528f4ea66
Showing with 36 additions and 18 deletions.
  1. +13 −16 cli/targets/static.js
  2. +23 −2 src/class.js
@@ -12,6 +12,7 @@ var Type = protobuf.Type,
Service = protobuf.Service,
Enum = protobuf.Enum,
Namespace = protobuf.Namespace,
Class = protobuf.Class,
util = protobuf.util;
var out = [];
@@ -150,7 +151,8 @@ var shortVars = {
"f": "impl",
"o": "options",
"d": "object",
"n": "long"
"n": "long",
"p": "properties"
};
function beautifyCode(code) {
@@ -214,7 +216,8 @@ function buildFunction(type, functionName, gen, scope) {
code = code.replace(/ {4}/g, "\t");
var hasScope = scope && Object.keys(scope).length;
var hasScope = scope && Object.keys(scope).length,
isCtor = functionName === type.name;
if (hasScope) // remove unused scope vars
Object.keys(scope).forEach(function(key) {
@@ -223,7 +226,9 @@ function buildFunction(type, functionName, gen, scope) {
});
var lines = code.split(/\n/g);
if (hasScope) // enclose in an iife
if (isCtor) // constructor
push(lines[0]);
else if (hasScope) // enclose in an iife
push(name(type.name) + "." + functionName + " = (function(" + Object.keys(scope).join(", ") + ") { return " + lines[0]);
else
push(name(type.name) + "." + functionName + " = " + lines[0]);
@@ -235,7 +240,9 @@ function buildFunction(type, functionName, gen, scope) {
push(line.trim());
indent = prev;
});
if (hasScope)
if (isCtor)
push("}");
else if (hasScope)
push("};})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + ");");
else
push("};");
@@ -281,19 +288,9 @@ function buildType(ref, type) {
type.comment ? "@classdesc " + type.comment : null,
"@exports " + fullName,
"@constructor",
"@param {Object} [properties] Properties to set"
"@param {Object} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
]);
push("function " + name(type.name) + "(properties) {");
++indent;
push("if (properties)");
++indent;
push("for (" + (config.es6 ? "let" : "var") + " keys = Object.keys(properties), i = 0; i < keys.length; ++i)");
++indent;
push("this[keys[i]] = properties[keys[i]];");
--indent;
--indent;
--indent;
push("}");
buildFunction(type, type.name, Class.generate(type));
// default values
var firstField = true;
@@ -26,8 +26,8 @@ function Class(type, ctor) {
throw TypeError("ctor must be a function");
} else
// create named constructor functions (codegen is required anyway)
ctor = util.codegen("p")("return c.call(this,p)").eof(type.name, {
c: Message
ctor = Class.generate(type).eof(type.name, {
Message: Message
});
// Let's pretend...
@@ -72,6 +72,27 @@ function Class(type, ctor) {
return ctor.prototype;
}
/**
* Generates a constructor function for the specified type.
* @param {Type} type Type to use
* @returns {Codegen} Codegen instance
*/
Class.generate = function generate(type) {
var gen = util.codegen("p");
/*
for (var i = 0, field; i < type.fieldsArray.length; ++i)
if ((field = type._fieldsArray[i]).map) gen
("this%s={}", util.safeProp(field.name));
else if (field.repeated) gen
("this%s=[]", util.safeProp(field.name));
*/
return gen
("if(p){")
("for(var ks=Object.keys(p),i=0;i<ks.length;++i)")
("this[ks[i]]=p[ks[i]];")
("}");
};
/**
* Constructs a new message prototype for the specified reflected type and sets up its constructor.
* @function

0 comments on commit c0b7c9f

Please sign in to comment.