blob: 7469bbf94c3707821cfac551fefc9f026292e4dd (
plain)
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
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
/*
* Return true if both of these return true:
* - LENIENT_PRED applied to CODE
* - STRICT_PRED applied to CODE with a use strict directive added to the front
*
* Run STRICT_PRED first, for testing code that affects the global environment
* in loose mode, but fails in strict mode.
*/
function testLenientAndStrict(code, lenient_pred, strict_pred) {
return (strict_pred("'use strict'; " + code) &&
lenient_pred(code));
}
/*
* completesNormally(CODE) returns true if evaluating CODE (as eval
* code) completes normally (rather than throwing an exception).
*/
function completesNormally(code) {
try {
eval(code);
return true;
} catch (exception) {
return false;
}
}
/*
* raisesException(EXCEPTION)(CODE) returns true if evaluating CODE (as eval
* code) throws an exception object whose prototype is
* EXCEPTION.prototype, and returns false if it throws any other error
* or evaluates successfully. For example: raises(TypeError)("0()") ==
* true.
*/
function raisesException(exception) {
return function (code) {
try {
eval(code);
return false;
} catch (actual) {
return exception.prototype.isPrototypeOf(actual);
}
};
};
/*
* parsesSuccessfully(CODE) returns true if CODE parses as function
* code without an error.
*/
function parsesSuccessfully(code) {
try {
Function(code);
return true;
} catch (exception) {
return false;
}
};
/*
* parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE
* as function code raises EXCEPTION.
*/
function parseRaisesException(exception) {
return function (code) {
try {
Function(code);
return false;
} catch (actual) {
return exception.prototype.isPrototypeOf(actual);
}
};
};
/*
* Return the result of applying uneval to VAL, and replacing all runs
* of whitespace with a single horizontal space (poor man's
* tokenization).
*/
function clean_uneval(val) {
return uneval(val).replace(/\s+/g, ' ');
}
|