Skip to content

Commit e8202a6

Browse files
fix: Mixing var and let/const contexts
1 parent 4d2f27e commit e8202a6

File tree

7 files changed

+78
-17
lines changed

7 files changed

+78
-17
lines changed

dist/gpu-browser-core.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.9.3
8-
* @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.9.4
8+
* @date Sat May 02 2020 11:46:49 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -4004,8 +4004,21 @@ class FunctionTracer {
40044004
}
40054005

40064006
getDeclaration(name) {
4007-
const { currentContext, currentFunctionContext } = this;
4008-
return currentContext[name] || currentFunctionContext[name] || null;
4007+
const { currentContext, currentFunctionContext, runningContexts } = this;
4008+
const declaration = currentContext[name] || currentFunctionContext[name] || null;
4009+
4010+
if (
4011+
!declaration &&
4012+
currentContext === currentFunctionContext &&
4013+
runningContexts.length > 0
4014+
) {
4015+
const previousRunningContext = runningContexts[runningContexts.length - 2];
4016+
if (previousRunningContext[name]) {
4017+
return previousRunningContext[name];
4018+
}
4019+
}
4020+
4021+
return declaration;
40094022
}
40104023

40114024
scan(ast) {

dist/gpu-browser-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gpu-browser.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.9.3
8-
* @date Wed Apr 01 2020 07:53:27 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.9.4
8+
* @date Sat May 02 2020 11:46:49 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -8457,8 +8457,21 @@ class FunctionTracer {
84578457
}
84588458

84598459
getDeclaration(name) {
8460-
const { currentContext, currentFunctionContext } = this;
8461-
return currentContext[name] || currentFunctionContext[name] || null;
8460+
const { currentContext, currentFunctionContext, runningContexts } = this;
8461+
const declaration = currentContext[name] || currentFunctionContext[name] || null;
8462+
8463+
if (
8464+
!declaration &&
8465+
currentContext === currentFunctionContext &&
8466+
runningContexts.length > 0
8467+
) {
8468+
const previousRunningContext = runningContexts[runningContexts.length - 2];
8469+
if (previousRunningContext[name]) {
8470+
return previousRunningContext[name];
8471+
}
8472+
}
8473+
8474+
return declaration;
84628475
}
84638476

84648477
scan(ast) {

dist/gpu-browser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "2.9.3",
3+
"version": "2.9.4",
44
"description": "GPU Accelerated JavaScript",
55
"engines": {
66
"node": ">=8.0.0"

src/backend/function-tracer.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,21 @@ class FunctionTracer {
9999
* @returns {IDeclaration}
100100
*/
101101
getDeclaration(name) {
102-
const { currentContext, currentFunctionContext } = this;
103-
return currentContext[name] || currentFunctionContext[name] || null;
102+
const { currentContext, currentFunctionContext, runningContexts } = this;
103+
const declaration = currentContext[name] || currentFunctionContext[name] || null;
104+
105+
if (
106+
!declaration &&
107+
currentContext === currentFunctionContext &&
108+
runningContexts.length > 0
109+
) {
110+
const previousRunningContext = runningContexts[runningContexts.length - 2];
111+
if (previousRunningContext[name]) {
112+
return previousRunningContext[name];
113+
}
114+
}
115+
116+
return declaration;
104117
}
105118

106119
/**

test/internal/function-tracer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ test('works with let VariableDeclarator', () => {
154154
assert.equal(bob.context['@contextType'], 'function');
155155
});
156156

157+
test('works with var & let VariableDeclarator together', () => {
158+
const ast = acorn.parse(`var bob = 0;
159+
for (let i = 0; i < 1; i++) { let pop = 0; }`);
160+
const functionTracer = new FunctionTracer(ast);
161+
162+
assert.equal(functionTracer.contexts[0].bob.context['@contextType'], 'function');
163+
assert.equal(functionTracer.contexts[0].i, undefined);
164+
assert.equal(functionTracer.contexts[0].pop, undefined);
165+
166+
assert.equal(functionTracer.contexts[1].bob.context['@contextType'], 'function');
167+
assert.equal(functionTracer.contexts[1].i.context['@contextType'], 'function');
168+
assert.equal(functionTracer.contexts[1].pop, undefined);
169+
170+
assert.equal(functionTracer.contexts[2].bob.context['@contextType'], 'function');
171+
assert.equal(functionTracer.contexts[2].i.context['@contextType'], 'function');
172+
assert.equal(functionTracer.contexts[2].pop, undefined);
173+
174+
assert.equal(functionTracer.contexts[3].bob.context['@contextType'], 'function');
175+
assert.equal(functionTracer.contexts[3].i.context['@contextType'], 'function');
176+
assert.equal(functionTracer.contexts[3].pop.context['@contextType'], 'function');
177+
});
178+
157179
test('works with FunctionExpression when runningContexts.length = 0', () => {
158180
const mockBody = {};
159181
let called = false;

0 commit comments

Comments
 (0)