Skip to content

Commit

Permalink
Other: Added codegen support for constructor functions, see #700
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 12, 2017
1 parent 4573f9a commit c0b7c9f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
29 changes: 13 additions & 16 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -150,7 +151,8 @@ var shortVars = {
"f": "impl",
"o": "options",
"d": "object",
"n": "long"
"n": "long",
"p": "properties"
};

function beautifyCode(code) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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]);
Expand All @@ -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("};");
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 23 additions & 2 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c0b7c9f

Please sign in to comment.