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. *