From 6a1ec7631fe1c66b51b65b26dad714f1128de41b Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 21 Oct 2012 12:29:38 +0200 Subject: [PATCH] Do not modify |options| passed to |PEG.buildParser| Modifying |options| can lead to subtle bugs. --- src/compiler/passes/generate-code.js | 12 +++++------- src/utils.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/compiler/passes/generate-code.js b/src/compiler/passes/generate-code.js index 3fab741..16c1067 100644 --- a/src/compiler/passes/generate-code.js +++ b/src/compiler/passes/generate-code.js @@ -1,12 +1,10 @@ /* Generates the parser code. */ PEG.compiler.passes.generateCode = function(ast, options) { - options = options || {}; - if (options.cache === undefined) { - options.cache = false; - } - if (options.trackLineAndColumn === undefined) { - options.trackLineAndColumn = false; - } + options = clone(options) || {}; + defaults(options, { + cache: false, + trackLineAndColumn: false + }); /* * Codie 1.1.0 diff --git a/src/utils.js b/src/utils.js index e90faa9..24e2739 100644 --- a/src/utils.js +++ b/src/utils.js @@ -71,6 +71,22 @@ function values(object) { return result; } +function clone(object) { + var result = {}; + for (var key in object) { + result[key] = object[key]; + } + return result; +} + +function defaults(object, defaults) { + for (var key in defaults) { + if (object[key] === undefined) { + object[key] = defaults[key]; + } + } +} + /* * Returns a string padded on the left to a desired length with a character. *