Skip to content

Commit 6a54671

Browse files
committed
Added while loop test case
1 parent 9c0f8af commit 6a54671

2 files changed

Lines changed: 92 additions & 37 deletions

File tree

src/backend/functionNode.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
/// writeVariables - {[String,...]} List of variables write operations occur
2121
///
2222
var functionNode = (function() {
23-
23+
2424
//
2525
// Constructor
2626
//----------------------------------------------------------------------------------------------------
27-
27+
2828
///
2929
/// Function: functionNode
3030
///
@@ -38,24 +38,24 @@ var functionNode = (function() {
3838
/// returnType - {String} The return type, assumes "float" if null
3939
///
4040
function functionNode( gpu, functionName, jsFunction, paramTypeArray, returnType ) {
41-
41+
4242
this.gpu = gpu;
43-
43+
4444
//
4545
// Internal vars setup
4646
//
4747
this.calledFunctions = [];
4848
this.initVariables = [];
4949
this.readVariables = [];
5050
this.writeVariables = [];
51-
51+
5252
//
5353
// Missing jsFunction object exception
5454
//
5555
if( jsFunction == null ) {
5656
throw "jsFunction, parameter is null";
5757
}
58-
58+
5959
//
6060
// Setup jsFunction and its string property + validate them
6161
//
@@ -64,22 +64,22 @@ var functionNode = (function() {
6464
console.error("jsFunction, to string conversion check falied: not a function?", this.jsFunctionString);
6565
throw "jsFunction, to string conversion check falied: not a function?";
6666
}
67-
67+
6868
if( !isFunction(jsFunction) ) {
6969
//throw "jsFunction, is not a valid JS Function";
7070
this.jsFunction = null;
7171
} else {
7272
this.jsFunction = jsFunction;
7373
}
74-
74+
7575
//
7676
// Setup the function name property
7777
//
7878
this.functionName = functionName || (jsFunction && jsFunction.name) || FUNCTION_NAME.exec(this.jsFunctionString)[1];
7979
if( !(this.functionName) ) {
8080
throw "jsFunction, missing name argument or value";
8181
}
82-
82+
8383
//
8484
// Extract parameter name, and its argument types
8585
//
@@ -98,23 +98,23 @@ var functionNode = (function() {
9898
this.paramType.push("float");
9999
}
100100
}
101-
101+
102102
//
103103
// Return type handling
104104
//
105105
this.returnType = returnType || "float";
106106
}
107-
107+
108108
//
109109
// Utility functions
110110
//----------------------------------------------------------------------------------------------------
111-
111+
112112
///
113113
/// Function: isFunction
114114
///
115115
/// [static] Return TRUE, on a JS function
116116
///
117-
/// This is 'static' function, not a class function (functionNode.prototype)
117+
/// This is 'static' function, not a class function functionNode.isFunction(...)
118118
///
119119
/// Parameters:
120120
/// funcObj - {JS Function} Object to validate if its a function
@@ -125,13 +125,13 @@ var functionNode = (function() {
125125
function isFunction( funcObj ) {
126126
return typeof(funcObj) === 'function';
127127
}
128-
128+
129129
///
130130
/// Function: validateStringIsFunction
131131
///
132132
/// [static] Return TRUE, on a valid JS function string
133133
///
134-
/// This is 'static' function, not a class function (functionNode.prototype)
134+
/// This is 'static' function, not a class function functionNode.validateStringIsFunction(...)
135135
///
136136
/// Parameters:
137137
/// funcStr - {String} String of JS function to validate
@@ -145,17 +145,17 @@ var functionNode = (function() {
145145
}
146146
return false;
147147
}
148-
148+
149149
var FUNCTION_NAME = /function ([^(]*)/;
150150
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
151151
var ARGUMENT_NAMES = /([^\s,]+)/g;
152-
152+
153153
///
154154
/// Function: getParamNames
155155
///
156156
/// [static] Return list of parameter names extracted from the JS function string
157157
///
158-
/// This is 'static' function, not a class function (functionNode.prototype)
158+
/// This is 'static' function, not a class function: functionNode.getParamNames(...)
159159
///
160160
/// Parameters:
161161
/// funcStr - {String} String of JS function to validate
@@ -176,34 +176,34 @@ var functionNode = (function() {
176176
functionNode.isFunction = isFunction;
177177
functionNode.validateStringIsFunction = validateStringIsFunction;
178178
functionNode.getParamNames = getParamNames;
179-
179+
180180
//
181181
// Core function
182182
//----------------------------------------------------------------------------------------------------
183-
183+
184184
///
185185
/// Function: getJSFunction
186186
///
187187
/// Gets and return the stored JS Function.
188188
/// Note: that this internally eval the function, if only the string was provided on construction
189189
///
190190
/// Returns:
191-
/// {JS Function} The function object
191+
/// {JS Function} The function object
192192
///
193193
function getJSFunction() {
194194
if( this.jsFunction ) {
195195
return this.jsFunction;
196196
}
197-
197+
198198
if( this.jsFunctionString ) {
199199
this.jsFunction = eval( this.jsFunctionString );
200200
return this.jsFunction;
201201
}
202-
202+
203203
throw "Missin jsFunction, and jsFunctionString parameter";
204204
}
205205
functionNode.prototype.getJSFunction = getJSFunction;
206-
206+
207207
///
208208
/// Function: getJS_AST
209209
///
@@ -221,25 +221,25 @@ var functionNode = (function() {
221221
if( this.jsFunctionAST ) {
222222
return this.jsFunctionAST;
223223
}
224-
224+
225225
inParser = inParser || parser;
226226
if( inParser == null ) {
227227
throw "Missing JS to AST parser";
228228
}
229-
229+
230230
var prasedObj = parser.parse( "var "+this.functionName+" = "+this.jsFunctionString+";" );
231231
if( prasedObj === null ) {
232232
throw "Failed to parse JS code via JISON";
233233
}
234-
234+
235235
// take out the function object, outside the var declarations
236236
var funcAST = prasedObj.body[0].declarations[0].init;
237237
this.jsFunctionAST = funcAST;
238-
238+
239239
return funcAST;
240240
}
241241
functionNode.prototype.getJS_AST = getJS_AST;
242-
242+
243243
///
244244
/// Function: getWebglString
245245
///
@@ -252,11 +252,11 @@ var functionNode = (function() {
252252
if( this.webglFunctionString ) {
253253
return this.webglFunctionString;
254254
}
255-
255+
256256
return this.webglFunctionString = functionNode_webgl(this);
257257
}
258258
functionNode.prototype.getWebglFunctionString = getWebglFunctionString;
259-
259+
260260
///
261261
/// Function: setWebglString
262262
///
@@ -269,6 +269,6 @@ var functionNode = (function() {
269269
this.webglFunctionString = shaderCode;
270270
}
271271
functionNode.prototype.setWebglFunctionString = setWebglFunctionString;
272-
272+
273273
return functionNode;
274274
})();

test/src/features/for_loop.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ function for_loop_test( assert, mode ) {
55
for(var i = 0.0; i<10.0; i++) {
66
x = x + 1.0;
77
}
8-
8+
99
return (a[this.thread.x] + b[this.thread.x] + x);
1010
}, {
1111
dimensions : [6],
1212
mode : mode
1313
});
14-
14+
1515
assert.ok( f !== null, "function generated test");
16-
16+
1717
var a = [1, 2, 3, 5, 6, 7];
1818
var b = [4, 5, 6, 1, 2, 3];
19-
19+
2020
var res = f(a,b);
2121
var exp = [15, 17, 19, 16, 18, 20];
22-
22+
2323
for(var i = 0; i < exp.length; ++i) {
2424
QUnit.close(exp[i], res[i], 0.1, "Result arr idx: "+i);
2525
}
@@ -36,3 +36,58 @@ QUnit.test( "for_loop (GPU)", function( assert ) {
3636
QUnit.test( "for_loop (CPU)", function( assert ) {
3737
for_loop_test(assert, "cpu");
3838
});
39+
40+
// Prevent test function leak
41+
(function() {
42+
43+
function evil_while_kernalFunction(a, b) {
44+
var x = 0.0;
45+
var i = 0;
46+
while(i<100) {
47+
x = x + 1.0;
48+
++i;
49+
}
50+
51+
return (a[this.thread.x] + b[this.thread.x] + x);
52+
}
53+
54+
var evil_while_a = [1, 2, 3, 5, 6, 7];
55+
var evil_while_b = [4, 5, 6, 1, 2, 3];
56+
var evil_while_cpuRef = new GPU();
57+
var evil_while_cpuRef_f = evil_while_cpuRef.createKernel(evil_while_kernalFunction, {
58+
dimensions : [6],
59+
mode : "cpu"
60+
});
61+
62+
var evil_while_exp = evil_while_cpuRef_f(evil_while_a,evil_while_b);
63+
64+
function evil_while_loop_test( assert, mode ) {
65+
var gpu = new GPU();
66+
67+
var f = gpu.createKernel(evil_while_kernalFunction, {
68+
dimensions : [6],
69+
mode : mode
70+
});
71+
72+
assert.ok( f !== null, "function generated test");
73+
74+
var res = f(evil_while_a,evil_while_b);
75+
76+
for(var i = 0; i < evil_while_exp.length; ++i) {
77+
QUnit.close(evil_while_exp[i], res[i], 0.1, "Result arr idx: "+i);
78+
}
79+
}
80+
81+
QUnit.test( "evil_while_loop (auto)", function( assert ) {
82+
evil_while_loop_test(assert, null);
83+
});
84+
85+
QUnit.test( "evil_while_loop (GPU)", function( assert ) {
86+
evil_while_loop_test(assert, "gpu");
87+
});
88+
89+
QUnit.test( "evil_while_loop (CPU)", function( assert ) {
90+
evil_while_loop_test(assert, "cpu");
91+
});
92+
93+
})();

0 commit comments

Comments
 (0)