1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
| package examples;
import java.util.*;
import java.util.function.*;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
/* Generates random strings that are syntactically valid JavaScript */
public class JavaScriptCodeGenerator extends Generator<String> {
public JavaScriptCodeGenerator() {
super(String.class); // Register type of generated object
}
private GenerationStatus status; // saved state object when generating
private static final int MAX_IDENTIFIERS = 100;
private static final int MAX_EXPRESSION_DEPTH = 10;
private static final int MAX_STATEMENT_DEPTH = 6;
private static Set<String> identifiers; // Stores generated IDs, to promote re-use
private int statementDepth; // Keeps track of how deep the AST is at any point
private int expressionDepth; // Keeps track of how nested an expression is at any point
private static final String[] UNARY_TOKENS = {
"!", "++", "--", "~",
"delete", "new", "typeof"
};
private static final String[] BINARY_TOKENS = {
"!=", "!==", "%", "%=", "&", "&&", "&=", "*", "*=", "+", "+=", ",",
"-", "-=", "/", "/=", "<", "<<", ">>=", "<=", "=", "==", "===",
">", ">=", ">>", ">>=", ">>>", ">>>=", "^", "^=", "|", "|=", "||",
"in", "instanceof"
};
/** Main entry point. Called once per test case. Returns a random JS program. */
@Override
public String generate(SourceOfRandomness random, GenerationStatus status) {
this.status = status; // we save this so that we can pass it on to other generators
this.identifiers = new HashSet<>();
this.statementDepth = 0;
this.expressionDepth = 0;
return generateStatement(random).toString();
}
/** Utility method for generating a random list of items (e.g. statements, arguments, attributes) */
private static List<String> generateItems(Function<SourceOfRandomness, String> genMethod, SourceOfRandomness random,
int mean) {
int len = random.nextInt(mean*2); // Generate random number in [0, mean*2)
List<String> items = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
items.add(genMethod.apply(random));
}
return items;
}
/** Generates a random JavaScript statement */
private String generateStatement(SourceOfRandomness random) {
statementDepth++;
String result;
// If depth is too high, then generate only simple statements to prevent infinite recursion
// If not, generate simple statements after the flip of a coin
if (statementDepth >= MAX_STATEMENT_DEPTH || random.nextBoolean()) {
// Choose a random private method from this class, and then call it with `random`
result = random.choose(Arrays.<Function<SourceOfRandomness, String>>asList(
this::generateExpressionStatement,
this::generateBreakNode,
this::generateContinueNode,
this::generateReturnNode,
this::generateThrowNode,
this::generateVarNode,
this::generateEmptyNode
)).apply(random);
} else {
// If depth is low and we won the flip, then generate compound statements
// (that is, statements that contain other statements)
result = random.choose(Arrays.<Function<SourceOfRandomness, String>>asList(
this::generateIfNode,
this::generateForNode,
this::generateWhileNode,
this::generateNamedFunctionNode,
this::generateSwitchNode,
this::generateTryNode,
this::generateBlock
)).apply(random);
}
statementDepth--; // Reset statement depth when going up the recursive tree
return result;
}
/** Generates a random JavaScript expression using recursive calls */
private String generateExpression(SourceOfRandomness random) {
expressionDepth++;
String result;
// Choose terminal if nesting depth is too high or based on a random flip of a coin
if (expressionDepth >= MAX_EXPRESSION_DEPTH || random.nextBoolean()) {
result = random.choose(Arrays.<Function<SourceOfRandomness, String>>asList(
this::generateLiteralNode,
this::generateIdentNode
)).apply(random);
} else {
// Otherwise, choose a non-terminal generating function
result = random.choose(Arrays.<Function<SourceOfRandomness, String>>asList(
this::generateBinaryNode,
this::generateUnaryNode,
this::generateTernaryNode,
this::generateCallNode,
this::generateFunctionNode,
this::generatePropertyNode,
this::generateIndexNode,
this::generateArrowFunctionNode
)).apply(random);
}
expressionDepth--;
return "(" + result + ")";
}
/** Generates a random binary expression (e.g. A op B) */
private String generateBinaryNode(SourceOfRandomness random) {
String token = random.choose(BINARY_TOKENS); // Choose a binary operator at random
String lhs = generateExpression(random);
String rhs = generateExpression(random);
return lhs + " " + token + " " + rhs;
}
/** Generates a block of statements delimited by ';' and enclosed by '{' '}' */
private String generateBlock(SourceOfRandomness random) {
return "{ " + String.join(";", generateItems(this::generateStatement, random, 4)) + " }";
}
private String generateBreakNode(SourceOfRandomness random) {
return "break";
}
private String generateCallNode(SourceOfRandomness random) {
String func = generateExpression(random);
String args = String.join(",", generateItems(this::generateExpression, random, 3));
String call = func + "(" + args + ")";
if (random.nextBoolean()) {
return call;
} else {
return "new " + call;
}
}
private String generateCaseNode(SourceOfRandomness random) {
return "case " + generateExpression(random) + ": " + generateBlock(random);
}
private String generateCatchNode(SourceOfRandomness random) {
return "catch (" + generateIdentNode(random) + ") " +
generateBlock(random);
}
private String generateContinueNode(SourceOfRandomness random) {
return "continue";
}
private String generateEmptyNode(SourceOfRandomness random) {
return "";
}
private String generateExpressionStatement(SourceOfRandomness random) {
return generateExpression(random);
}
private String generateForNode(SourceOfRandomness random) {
String s = "for(";
if (random.nextBoolean()) {
s += generateExpression(random);
}
s += ";";
if (random.nextBoolean()) {
s += generateExpression(random);
}
s += ";";
if (random.nextBoolean()) {
s += generateExpression(random);
}
s += ")";
s += generateBlock(random);
return s;
}
private String generateFunctionNode(SourceOfRandomness random) {
return "function(" + String.join(", ", generateItems(this::generateIdentNode, random, 5)) + ")" + generateBlock(random);
}
private String generateNamedFunctionNode(SourceOfRandomness random) {
return "function " + generateIdentNode(random) + "(" + String.join(", ", generateItems(this::generateIdentNode, random, 5)) + ")" + generateBlock(random);
}
private String generateArrowFunctionNode(SourceOfRandomness random) {
String params = "(" + String.join(", ", generateItems(this::generateIdentNode, random, 3)) + ")";
if (random.nextBoolean()) {
return params + " => " + generateBlock(random);
} else {
return params + " => " + generateExpression(random);
}
}
private String generateIdentNode(SourceOfRandomness random) {
// Either generate a new identifier or use an existing one
String identifier;
if (identifiers.isEmpty() || (identifiers.size() < MAX_IDENTIFIERS && random.nextBoolean())) {
identifier = random.nextChar('a', 'z') + "_" + identifiers.size();
identifiers.add(identifier);
} else {
identifier = random.choose(identifiers);
}
return identifier;
}
private String generateIfNode(SourceOfRandomness random) {
return "if (" +
generateExpression(random) + ") " +
generateBlock(random) +
(random.nextBoolean() ? generateBlock(random) : "");
}
private String generateIndexNode(SourceOfRandomness random) {
return generateExpression(random) + "[" + generateExpression(random) + "]";
}
private String generateObjectProperty(SourceOfRandomness random) {
return generateIdentNode(random) + ": " + generateExpression(random);
}
private String generateLiteralNode(SourceOfRandomness random) {
// If we are not too deeply nested, then it is okay to generate array/object literals
if (expressionDepth < MAX_EXPRESSION_DEPTH && random.nextBoolean()) {
if (random.nextBoolean()) {
// Array literal
return "[" + String.join(", ", generateItems(this::generateExpression, random, 3)) + "]";
} else {
// Object literal
return "{" + String.join(", ", generateItems(this::generateObjectProperty, random, 3)) + "}";
}
} else {
// Otherwise, generate primitive literals
return random.choose(Arrays.<Supplier<String>>asList(
() -> String.valueOf(random.nextInt(-10, 1000)), // int literal
() -> String.valueOf(random.nextBoolean()), // bool literal
() -> generateStringLiteral(random),
() -> "undefined",
() -> "null",
() -> "this"
)).get();
}
}
private String generateStringLiteral(SourceOfRandomness random) {
// Generate an arbitrary string using the default string generator, and quote it
return '"' + gen().type(String.class).generate(random, status) + '"';
}
private String generatePropertyNode(SourceOfRandomness random) {
return generateExpression(random) + "." + generateIdentNode(random);
}
private String generateReturnNode(SourceOfRandomness random) {
return random.nextBoolean() ? "return" : "return " + generateExpression(random);
}
private String generateSwitchNode(SourceOfRandomness random) {
return "switch(" + generateExpression(random) + ") {"
+ String.join(" ", generateItems(this::generateCaseNode, random, 2)) + "}";
}
private String generateTernaryNode(SourceOfRandomness random) {
return generateExpression(random) + " ? " + generateExpression(random) +
" : " + generateExpression(random);
}
private String generateThrowNode(SourceOfRandomness random) {
return "throw " + generateExpression(random);
}
private String generateTryNode(SourceOfRandomness random) {
return "try " + generateBlock(random) + generateCatchNode(random);
}
private String generateUnaryNode(SourceOfRandomness random) {
String token = random.choose(UNARY_TOKENS);
return token + " " + generateExpression(random);
}
private String generateVarNode(SourceOfRandomness random) {
return "var " + generateIdentNode(random);
}
private String generateWhileNode(SourceOfRandomness random) {
return "while (" + generateExpression(random) + ")" + generateBlock(random);
}
}
|