Skip to content

Commit 1c820a7

Browse files
fix: Deep types and tests to lookup arguments BEFORE return types
fix: Error messages when on single line fix: Bump and build
1 parent 19b2fa4 commit 1c820a7

File tree

12 files changed

+386
-358
lines changed

12 files changed

+386
-358
lines changed

bin/gpu-browser-core.js

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.0.0-rc.16
8-
* @date Thu Jun 20 2019 10:09:00 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.0.0-rc.17
8+
* @date Mon Jun 24 2019 21:10:10 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -1677,12 +1677,16 @@ class FunctionBuilder {
16771677
dynamicOutput,
16781678
} = kernel;
16791679

1680-
const lookupReturnType = (functionName, ast, requestingNode) => {
1681-
return functionBuilder.lookupReturnType(functionName, ast, requestingNode);
1680+
const needsArgumentType = (functionName, index) => {
1681+
return functionBuilder.needsArgumentType(functionName, index);
16821682
};
16831683

1684-
const lookupArgumentType = (argumentName, requestingNode) => {
1685-
return functionBuilder.lookupArgumentType(argumentName, requestingNode);
1684+
const assignArgumentType = (functionName, index, type) => {
1685+
functionBuilder.assignArgumentType(functionName, index, type);
1686+
};
1687+
1688+
const lookupReturnType = (functionName, ast, requestingNode) => {
1689+
return functionBuilder.lookupReturnType(functionName, ast, requestingNode);
16861690
};
16871691

16881692
const lookupFunctionArgumentTypes = (functionName) => {
@@ -1724,10 +1728,11 @@ class FunctionBuilder {
17241728
name: ast.id.name,
17251729
argumentNames,
17261730
lookupReturnType,
1727-
lookupArgumentType,
17281731
lookupFunctionArgumentTypes,
17291732
lookupFunctionArgumentName,
17301733
lookupFunctionArgumentBitRatio,
1734+
needsArgumentType,
1735+
assignArgumentType,
17311736
triggerImplyArgumentType,
17321737
triggerTrackArgumentSynonym,
17331738
lookupArgumentSynonym,
@@ -1741,10 +1746,11 @@ class FunctionBuilder {
17411746
isRootKernel: false,
17421747
onNestedFunction,
17431748
lookupReturnType,
1744-
lookupArgumentType,
17451749
lookupFunctionArgumentTypes,
17461750
lookupFunctionArgumentName,
17471751
lookupFunctionArgumentBitRatio,
1752+
needsArgumentType,
1753+
assignArgumentType,
17481754
triggerImplyArgumentType,
17491755
triggerTrackArgumentSynonym,
17501756
lookupArgumentSynonym,
@@ -1791,10 +1797,11 @@ class FunctionBuilder {
17911797
optimizeFloatMemory,
17921798
precision,
17931799
lookupReturnType,
1794-
lookupArgumentType,
17951800
lookupFunctionArgumentTypes,
17961801
lookupFunctionArgumentName,
17971802
lookupFunctionArgumentBitRatio,
1803+
needsArgumentType,
1804+
assignArgumentType,
17981805
triggerImplyArgumentType,
17991806
triggerTrackArgumentSynonym,
18001807
lookupArgumentSynonym,
@@ -1974,37 +1981,6 @@ class FunctionBuilder {
19741981
return this.getStringFromFunctionNames(Object.keys(this.functionMap));
19751982
}
19761983

1977-
lookupArgumentType(argumentName, requestingNode) {
1978-
const index = requestingNode.argumentNames.indexOf(argumentName);
1979-
if (index === -1) {
1980-
return null;
1981-
}
1982-
if (this.lookupChain.length === 0) {
1983-
return null;
1984-
}
1985-
let link = this.lookupChain[this.lookupChain.length - 1 - this.argumentChain.length];
1986-
if (!link) {
1987-
return null;
1988-
}
1989-
const {
1990-
ast,
1991-
requestingNode: parentRequestingNode
1992-
} = link;
1993-
if (ast.arguments.length === 0) {
1994-
return null;
1995-
}
1996-
const usedArgument = ast.arguments[index];
1997-
if (!usedArgument) {
1998-
return null;
1999-
}
2000-
2001-
this.argumentChain.push(argumentName);
2002-
2003-
const type = parentRequestingNode.getType(usedArgument);
2004-
this.argumentChain.pop();
2005-
return type;
2006-
}
2007-
20081984
lookupReturnType(functionName, ast, requestingNode) {
20091985
if (ast.type !== 'CallExpression') {
20101986
throw new Error(`expected ast type of "CallExpression", but is ${ ast.type }`);
@@ -2047,6 +2023,7 @@ class FunctionBuilder {
20472023
}
20482024

20492025
return null;
2026+
20502027
}
20512028

20522029
_getFunction(functionName) {
@@ -2113,6 +2090,12 @@ class FunctionBuilder {
21132090
}
21142091
}
21152092

2093+
needsArgumentType(functionName, i) {
2094+
if (!this._isFunction(functionName)) return false;
2095+
const fnNode = this._getFunction(functionName);
2096+
return !fnNode.argumentTypes[i];
2097+
}
2098+
21162099
assignArgumentType(functionName, i, argumentType, requestingNode) {
21172100
if (!this._isFunction(functionName)) return;
21182101
const fnNode = this._getFunction(functionName);
@@ -2228,8 +2211,9 @@ class FunctionNode {
22282211
this.contexts = null;
22292212
this.functionCalls = null;
22302213
this.states = [];
2214+
this.needsArgumentType = null;
2215+
this.assignArgumentType = null;
22312216
this.lookupReturnType = null;
2232-
this.lookupArgumentType = null;
22332217
this.lookupFunctionArgumentTypes = null;
22342218
this.lookupFunctionArgumentBitRatio = null;
22352219
this.triggerImplyArgumentType = null;
@@ -2462,12 +2446,6 @@ class FunctionNode {
24622446
const argumentType = this.argumentTypes[argumentIndex];
24632447
if (argumentType) {
24642448
type = argumentType;
2465-
} else if (this.lookupArgumentType) {
2466-
const foundArgumentType = this.lookupArgumentType(ast.name, this);
2467-
if (!this.argumentTypes[argumentIndex]) {
2468-
this.argumentTypes[argumentIndex] = foundArgumentType;
2469-
}
2470-
type = foundArgumentType;
24712449
}
24722450
}
24732451
if (!type && this.strictTypingChecking) {
@@ -2561,12 +2539,16 @@ class FunctionNode {
25612539
}
25622540
if (!ast.callee || !ast.callee.name) {
25632541
if (ast.callee.type === 'SequenceExpression' && ast.callee.expressions[ast.callee.expressions.length - 1].property.name) {
2564-
return this.lookupReturnType(ast.callee.expressions[ast.callee.expressions.length - 1].property.name, ast, this);
2542+
const functionName = ast.callee.expressions[ast.callee.expressions.length - 1].property.name;
2543+
this.inferArgumentTypesIfNeeded(functionName, ast.arguments);
2544+
return this.lookupReturnType(functionName, ast, this);
25652545
}
25662546
throw this.astErrorOutput('Unknown call expression', ast);
25672547
}
25682548
if (ast.callee && ast.callee.name) {
2569-
return this.lookupReturnType(ast.callee.name, ast, this);
2549+
const functionName = ast.callee.name;
2550+
this.inferArgumentTypesIfNeeded(functionName, ast.arguments);
2551+
return this.lookupReturnType(functionName, ast, this);
25702552
}
25712553
throw this.astErrorOutput(`Unhandled getType Type "${ ast.type }"`, ast);
25722554
case 'BinaryExpression':
@@ -2717,6 +2699,17 @@ class FunctionNode {
27172699
}
27182700
}
27192701

2702+
inferArgumentTypesIfNeeded(functionName, args) {
2703+
for (let i = 0; i < args.length; i++) {
2704+
if (!this.needsArgumentType(functionName, i)) continue;
2705+
const type = this.getType(args[i]);
2706+
if (!type) {
2707+
throw this.astErrorOutput(`Unable to infer argument ${i}`, args[i]);
2708+
}
2709+
this.assignArgumentType(functionName, i, type);
2710+
}
2711+
}
2712+
27202713
isAstMathVariable(ast) {
27212714
const mathProperties = [
27222715
'E',
@@ -7334,7 +7327,7 @@ class WebGLFunctionNode extends FunctionNode {
73347327
}
73357328
break;
73367329
}
7337-
throw this.astErrorOutput(`Unhandled argument combination of ${ argumentType } and ${ targetType }`, ast);
7330+
throw this.astErrorOutput(`Unhandled argument combination of ${ argumentType } and ${ targetType } for argument named "${ argument.name }"`, ast);
73387331
}
73397332
}
73407333
retArr.push(')');
@@ -12018,11 +12011,15 @@ const utils = {
1201812011
const start = ast.loc.start;
1201912012
const end = ast.loc.end;
1202012013
const result = [];
12021-
result.push(lines[start.line - 1].slice(start.column));
12022-
for (let i = start.line; i < end.line - 1; i++) {
12023-
result.push(lines[i]);
12014+
if (start.line === end.line) {
12015+
result.push(lines[start.line - 1].substring(start.column, end.column));
12016+
} else {
12017+
result.push(lines[start.line - 1].slice(start.column));
12018+
for (let i = start.line; i < end.line; i++) {
12019+
result.push(lines[i]);
12020+
}
12021+
result.push(lines[end.line - 1].slice(0, end.column));
1202412022
}
12025-
result.push(lines[end.line - 1].slice(0, end.column));
1202612023
return result.join('\n');
1202712024
},
1202812025

0 commit comments

Comments
 (0)