Skip to content

Commit

Permalink
fix: Mixing var and let/const contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
robertleeplummerjr committed May 2, 2020
1 parent 4d2f27e commit e8202a6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
21 changes: 17 additions & 4 deletions dist/gpu-browser-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.9.3
* @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
* @version 2.9.4
* @date Sat May 02 2020 11:46:49 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -4004,8 +4004,21 @@ class FunctionTracer {
}

getDeclaration(name) {
const { currentContext, currentFunctionContext } = this;
return currentContext[name] || currentFunctionContext[name] || null;
const { currentContext, currentFunctionContext, runningContexts } = this;
const declaration = currentContext[name] || currentFunctionContext[name] || null;

if (
!declaration &&
currentContext === currentFunctionContext &&
runningContexts.length > 0
) {
const previousRunningContext = runningContexts[runningContexts.length - 2];
if (previousRunningContext[name]) {
return previousRunningContext[name];
}
}

return declaration;
}

scan(ast) {
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser-core.min.js

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions dist/gpu-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 2.9.3
* @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
* @version 2.9.4
* @date Sat May 02 2020 11:46:49 GMT-0400 (Eastern Daylight Time)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -8457,8 +8457,21 @@ class FunctionTracer {
}

getDeclaration(name) {
const { currentContext, currentFunctionContext } = this;
return currentContext[name] || currentFunctionContext[name] || null;
const { currentContext, currentFunctionContext, runningContexts } = this;
const declaration = currentContext[name] || currentFunctionContext[name] || null;

if (
!declaration &&
currentContext === currentFunctionContext &&
runningContexts.length > 0
) {
const previousRunningContext = runningContexts[runningContexts.length - 2];
if (previousRunningContext[name]) {
return previousRunningContext[name];
}
}

return declaration;
}

scan(ast) {
Expand Down
6 changes: 3 additions & 3 deletions dist/gpu-browser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "2.9.3",
"version": "2.9.4",
"description": "GPU Accelerated JavaScript",
"engines": {
"node": ">=8.0.0"
Expand Down
17 changes: 15 additions & 2 deletions src/backend/function-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,21 @@ class FunctionTracer {
* @returns {IDeclaration}
*/
getDeclaration(name) {
const { currentContext, currentFunctionContext } = this;
return currentContext[name] || currentFunctionContext[name] || null;
const { currentContext, currentFunctionContext, runningContexts } = this;
const declaration = currentContext[name] || currentFunctionContext[name] || null;

if (
!declaration &&
currentContext === currentFunctionContext &&
runningContexts.length > 0
) {
const previousRunningContext = runningContexts[runningContexts.length - 2];
if (previousRunningContext[name]) {
return previousRunningContext[name];
}
}

return declaration;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/internal/function-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ test('works with let VariableDeclarator', () => {
assert.equal(bob.context['@contextType'], 'function');
});

test('works with var & let VariableDeclarator together', () => {
const ast = acorn.parse(`var bob = 0;
for (let i = 0; i < 1; i++) { let pop = 0; }`);
const functionTracer = new FunctionTracer(ast);

assert.equal(functionTracer.contexts[0].bob.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[0].i, undefined);
assert.equal(functionTracer.contexts[0].pop, undefined);

assert.equal(functionTracer.contexts[1].bob.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[1].i.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[1].pop, undefined);

assert.equal(functionTracer.contexts[2].bob.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[2].i.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[2].pop, undefined);

assert.equal(functionTracer.contexts[3].bob.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[3].i.context['@contextType'], 'function');
assert.equal(functionTracer.contexts[3].pop.context['@contextType'], 'function');
});

test('works with FunctionExpression when runningContexts.length = 0', () => {
const mockBody = {};
let called = false;
Expand Down

0 comments on commit e8202a6

Please sign in to comment.