From 21816ed8ef57710c889757773677b1d7bbb5bcbf Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Tue, 7 Mar 2017 15:08:23 -0800 Subject: [PATCH] Fix duplicate logs from scripts when ext is used --- lib/sandbox.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/sandbox.js b/lib/sandbox.js index 5dbc0ce..b74117e 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -193,6 +193,7 @@ function commit() { function ext() { + ++ state.extCounter; let extCall = Array.from(arguments).map(String); let key = `${state.revision}: ${JSON.stringify(extCall)}`; @@ -205,7 +206,7 @@ function ext() { function log(msg, meta) { - if (state.revision === state.maxRevision) { + if (state.revision === state.maxRevision && state.extCounter >= 0) { const details = Object.assign({}, meta, { sessionContext: state.sessionContext, message: `Script: ${msg}` @@ -255,7 +256,7 @@ function errorToFault(err) { } -function run(script, globals, sessionContext, startRevision, maxRevision, callback) { +function run(script, globals, sessionContext, startRevision, maxRevision, callback, extCounter = 0) { state = { sessionContext: sessionContext, revision: startRevision, @@ -264,7 +265,8 @@ function run(script, globals, sessionContext, startRevision, maxRevision, callba declarations: [], extensions: {}, clear: [], - rng: null + rng: null, + extCounter: extCounter }; for (let n of Object.keys(context)) @@ -288,7 +290,12 @@ function run(script, globals, sessionContext, startRevision, maxRevision, callba } let _state = state; - let args = arguments; + let args = Array.from(arguments); + + // Need to maintain a counter of ext() calls to avoid calling certain + // functions (e.g. log()) multiple times as calling ext() results in + // re-execution of script without revision being incremented. + args[6] = 0 - (state.extCounter - extCounter); let counter = 3; for (let key of Object.keys(_state.extensions)) {