Skip to content

Commit 7a5fc23

Browse files
committed
More magic in sandbox code mangling to make using a fundecl before its definition work
Closes #64
1 parent f070449 commit 7a5fc23

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

html/js/sandbox.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@
255255
}
256256
}
257257
patches.push({from: tryPos, text: "try{"});
258+
for (var i = ast.body.length - 1, catchPos = ast.end; i >= 0; i--) {
259+
var stat = ast.body[i];
260+
if (stat.type != "FunctionDeclaration") {
261+
catchPos = stat.end;
262+
break;
263+
}
264+
}
265+
patches.push({from: catchPos, text: "}catch(e){__sandbox.error(e);}"});
258266
patches.sort(function(a, b) { return a.from - b.from; });
259267
var out = "";
260268
for (var i = 0, pos = 0; i < patches.length; ++i) {
@@ -263,7 +271,7 @@
263271
pos = patch.to || patch.from;
264272
}
265273
out += code.slice(pos, code.length);
266-
return (strict ? '"use strict";' : "") + out + "\n}catch(e){__sandbox.error(e);}";
274+
return (strict ? '"use strict";' : "") + out;
267275
}
268276

269277
var Output = SandBox.Output = function(div) {

0 commit comments

Comments
 (0)