Skip to content

Commit 99ad9cc

Browse files
committed
Added .create to statically generated types and uppercase nested elements to reflection namespaces, see #576; Also added Namespace#getEnum for completeness, see #576; Made pbjs use loadSync for deterministic outputs, see #573
1 parent ef43acf commit 99ad9cc

33 files changed

+342
-99
lines changed

bench/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var protobuf = require(".."),
2020
// To experience the impact by yourself, increase string lengths within bench.json.
2121

2222
var root = protobuf.loadSync(require.resolve("./bench.proto"));
23-
var Test = root.lookup("Test");
23+
var Test = root.resolveAll().lookup("Test");
2424

2525
// protobuf.util.codegen.verbose = true;
2626

bench/prof.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (process.execArgv.indexOf("--prof") < 0) {
1919
fs.unlink(file);
2020
});
2121
console.log("generating profile (may take a while) ...");
22-
var child = child_process.execSync("node --prof --trace-deopt " + process.argv.slice(1).join(' '), {
22+
var child = child_process.execSync("node --prof --trace-deopt " + process.execArgv.join(" ") + " " + process.argv.slice(1).join(' '), {
2323
cwd: process.cwd(),
2424
stdio: 'inherit'
2525
});

cli/pbjs.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ exports.main = function(args, callback) {
2626
root : "r"
2727
},
2828
string: [ "target", "out", "path", "wrap", "root" ],
29-
boolean: [ "keep-case", "encode", "decode", "verify", "delimited" ],
29+
boolean: [ "keep-case", "create", "encode", "decode", "verify", "delimited" ],
3030
default: {
3131
target: "json",
32+
create: true,
3233
encode: true,
3334
decode: true,
3435
verify: true,
@@ -76,6 +77,7 @@ exports.main = function(args, callback) {
7677
"",
7778
" Static targets only:",
7879
"",
80+
" --no-create Does not generate create functions used for runtime compatibility.",
7981
" --no-encode Does not generate encode functions.",
8082
" --no-decode Does not generate decode functions.",
8183
" --no-verify Does not generate verify functions.",
@@ -119,28 +121,31 @@ exports.main = function(args, callback) {
119121
"keepCase": argv["keep-case"] || false
120122
};
121123

122-
root.load(files, parseOptions, function(err) {
124+
var root;
125+
try {
126+
root = root.loadSync(files, parseOptions);
127+
} catch (err) {
128+
if (callback) {
129+
callback(err);
130+
return;
131+
} else
132+
throw err;
133+
}
134+
135+
target(root, argv, function(err, output) {
123136
if (err) {
124137
if (callback)
125138
return callback(err);
126139
else
127140
throw err;
128141
}
129-
target(root, argv, function(err, output) {
130-
if (err) {
131-
if (callback)
132-
return callback(err);
133-
else
134-
throw err;
135-
}
136-
if (output !== "") {
137-
if (argv.out)
138-
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
139-
else
140-
process.stdout.write(output, "utf8");
141-
}
142-
if (callback)
143-
callback(null);
144-
});
142+
if (output !== "") {
143+
if (argv.out)
144+
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
145+
else
146+
process.stdout.write(output, "utf8");
147+
}
148+
if (callback)
149+
return callback(null);
145150
});
146151
};

cli/targets/json-module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ json_module.description = "JSON representation as a module"
1212
function json_module(root, options, callback) {
1313

1414
try {
15-
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ");";
15+
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ").resolveAll();";
1616
output = util.wrap(options.wrap || "default", output, options.root);
1717
process.nextTick(function() {
1818
callback(null, output);

cli/targets/static.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,23 @@ function buildType(ref, type) {
270270
--indent;
271271
push("});");
272272
});
273+
274+
if (config.create) {
275+
push("");
276+
pushComment([
277+
"Creates a new " + type.name + " instance using the specified properties.",
278+
"@param {Object} [properties] Properties to set",
279+
"@returns {" + fullName + "} " + type.name + " instance"
280+
]);
281+
push(name(type.name) + ".create = function create(properties) {");
282+
++indent;
283+
push("return new " + name(type.name) + "(properties);");
284+
--indent;
285+
push("};");
286+
}
287+
273288

274289
if (config.encode) {
275-
276-
// #encode
277290
push("");
278291
pushComment([
279292
"Encodes the specified " + type.name + ".",
@@ -288,8 +301,6 @@ function buildType(ref, type) {
288301
});
289302

290303
if (config.delimited) {
291-
292-
// #encodeDelimited
293304
push("");
294305
pushComment([
295306
"Encodes the specified " + type.name + ", length delimited.",
@@ -308,8 +319,6 @@ function buildType(ref, type) {
308319
}
309320

310321
if (config.decode) {
311-
312-
// #decode
313322
push("");
314323
pushComment([
315324
"Decodes a " + type.name + " from the specified reader or buffer.",
@@ -324,8 +333,6 @@ function buildType(ref, type) {
324333
});
325334

326335
if (config.delimited) {
327-
328-
// #decodeDelimited
329336
push("");
330337
pushComment([
331338
"Decodes a " + type.name + " from the specified reader or buffer, length delimited.",
@@ -343,8 +350,6 @@ function buildType(ref, type) {
343350
}
344351

345352
if (config.verify) {
346-
347-
// #verify
348353
push("");
349354
pushComment([
350355
"Verifies a " + type.name + ".",

0 commit comments

Comments
 (0)