forked from alainyog/JavaScript.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (54 loc) · 1.63 KB
/
Copy pathindex.js
File metadata and controls
63 lines (54 loc) · 1.63 KB
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
var assert = require('chai').assert,
Sandbox = require('javascript-sandbox'),
jshint = require('jshint').JSHINT;
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function verify(code) {
if (!jshint('/* jshint asi:true */' + code)) {
var error = jshint.errors[0];
if (!error.message) {
error.message = 'Looks like there is a syntax error in your code: ' + error.reason || error.raw;
}
if (error.message.match(/Missing semicolon/)) return;
if (error.message.match(/Expected an assignment or function call and instead saw an expression/)) return;
it('f_jshint_error', function() {
assert(false, error.message);
});
}
}
var state = {};
// getOwnPropertyNames doesn't work in IE 8 and below.
if (Object.getOwnPropertyNames(state).length === 0) {
var courseState = readCookie('course_state'),
tryName = readCookie('try_name');
if (courseState) {
state = JSON.parse(decodeURIComponent(courseState));
} else if (tryName) {
state.username = tryName;
}
}
module.exports = {
assert: assert,
verify: verify,
Sandbox: Sandbox,
activeSandbox: new Sandbox(),
state: state,
evaluate: function(code) {
return this.activeSandbox.evaluate(code);
},
restoreName: function() {
try {
this.evaluate('firstName;');
} catch(e) {
this.evaluate('var firstName = "' + this.state.username + '";');
}
}
};