diff --git a/apps/src/gamelab/GameLab.js b/apps/src/gamelab/GameLab.js index bf35be24e9272..91a9060ebcc65 100644 --- a/apps/src/gamelab/GameLab.js +++ b/apps/src/gamelab/GameLab.js @@ -945,13 +945,15 @@ GameLab.prototype.initInterpreter = function(attachDebugger = true) { if (this.level.customHelperLibrary) { code += this.level.customHelperLibrary + '\n'; } + const userCodeStartOffset = code.length; code += this.studioApp_.getCode(); this.JSInterpreter.parse({ code, blocks: dropletConfig.blocks, blockFilter: this.level.executePaletteApisOnly && this.level.codeFunctions, enableEvents: true, - initGlobals: injectGamelabGlobals + initGlobals: injectGamelabGlobals, + userCodeStartOffset }); if (!this.JSInterpreter.initialized()) { return; diff --git a/apps/src/lib/tools/jsinterpreter/JSInterpreter.js b/apps/src/lib/tools/jsinterpreter/JSInterpreter.js index 50dbde7cc6ac5..0fdcc386d8284 100644 --- a/apps/src/lib/tools/jsinterpreter/JSInterpreter.js +++ b/apps/src/lib/tools/jsinterpreter/JSInterpreter.js @@ -130,9 +130,13 @@ export default class JSInterpreter { * @param {Function} [options.initGlobals] when supplied, this function will * be called during interpreter initialization so that additional globals * can be added with calls to createGlobalProperty() + * @param {number} [options.userCodeStartOffset] - offset in the code string where + * the user's created code begins. Allows other code to be injected before + * the user's program without disrupting line number calculations for + * debugging (default 0) */ parse(options) { - this.calculateCodeInfo(options.code); + this.calculateCodeInfo(options); if (!this.studioApp.hideSource && this.studioApp.editor) { const session = this.studioApp.editor.aceEditor.getSession(); @@ -239,14 +243,22 @@ export default class JSInterpreter { /** * Init `this.codeInfo` with cumulative length info (used to locate breakpoints). - * @param code + * @param {!Object} options + * @param {!string} options.code - Code to be executed by the interpreter. + * @param {number} [options.userCodeStartOffset] - offset in the code string where + * the user's created code begins. Allows other code to be injected before + * the user's program without disrupting line number calculations for + * debugging (default 0) */ - calculateCodeInfo(code) { + calculateCodeInfo(options) { + const {code, userCodeStartOffset = 0} = options; this.codeInfo = {}; this.codeInfo.code = code; - this.codeInfo.userCodeStartOffset = 0; - this.codeInfo.userCodeLength = code.length; - this.codeInfo.cumulativeLength = codegen.calculateCumulativeLength(code); + this.codeInfo.userCodeStartOffset = userCodeStartOffset; + this.codeInfo.userCodeLength = code.length - userCodeStartOffset; + this.codeInfo.cumulativeLength = codegen.calculateCumulativeLength( + code.slice(userCodeStartOffset) + ); } /** diff --git a/apps/test/unit/lib/tools/jsdebugger/DebugConsoleTest.js b/apps/test/unit/lib/tools/jsdebugger/DebugConsoleTest.js index dfaed13195c37..1a6a76e3b1853 100644 --- a/apps/test/unit/lib/tools/jsdebugger/DebugConsoleTest.js +++ b/apps/test/unit/lib/tools/jsdebugger/DebugConsoleTest.js @@ -140,7 +140,7 @@ describe('The DebugConsole component', () => { studioApp: {hideSource: true} }); const code = '0;\n1;\n2;\n3;\n4;\n5;\n6;\n7;'; - interpreter.calculateCodeInfo(code); + interpreter.calculateCodeInfo({code}); interpreter.parse({code}); interpreter.paused = true; interpreter.nextStep = JSInterpreter.StepType.IN; diff --git a/apps/test/unit/lib/tools/jsdebugger/reduxTest.js b/apps/test/unit/lib/tools/jsdebugger/reduxTest.js index 30247628012fa..2356532f61805 100644 --- a/apps/test/unit/lib/tools/jsdebugger/reduxTest.js +++ b/apps/test/unit/lib/tools/jsdebugger/reduxTest.js @@ -44,7 +44,7 @@ describe('The JSDebugger redux duck', () => { function runToBreakpoint() { const code = '0;\n1;\n2;\n3;\n4;\n5;\n6;\n7;'; - interpreter.calculateCodeInfo(code); + interpreter.calculateCodeInfo({code}); interpreter.parse({code}); interpreter.paused = true; interpreter.nextStep = JSInterpreter.StepType.IN; diff --git a/apps/test/unit/lib/tools/jsinterpreter/JSInterpreterTest.js b/apps/test/unit/lib/tools/jsinterpreter/JSInterpreterTest.js index 58f7431b819f4..87496d269c1cb 100644 --- a/apps/test/unit/lib/tools/jsinterpreter/JSInterpreterTest.js +++ b/apps/test/unit/lib/tools/jsinterpreter/JSInterpreterTest.js @@ -18,7 +18,7 @@ describe('The JSInterpreter class', function() { }); // Initialize a test program - jsInterpreter.calculateCodeInfo(code); + jsInterpreter.calculateCodeInfo({code}); jsInterpreter.parse({code: code}); assert(jsInterpreter.initialized());