diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-04-23 20:05:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-23 20:05:22 +0800 |
commit | 9bf72cf75822044ae314b4646db9aefb1bd38284 (patch) | |
tree | bf149011065d6c801217585f4d6bc813343fe0c7 /test/sandbox.js | |
parent | 64d74432f6e475df921d0ec49c4d15e5d2ae891d (diff) | |
download | tracifyjs-9bf72cf75822044ae314b4646db9aefb1bd38284.tar.gz tracifyjs-9bf72cf75822044ae314b4646db9aefb1bd38284.zip |
improve parser under "use strict" (#1836)
- `const` without value
- `delete` of expression
- redefining `arguments` or `eval`
extend `test/ufuzz.js`
- optionally generate "use strict"
- improve handling of test cases with syntax errors
- group IIFE generation
- generate bare anonymous functions
- workaround `console.log()` for `new function()`
- generate expressions with `this`
fixes #1810
Diffstat (limited to 'test/sandbox.js')
-rw-r--r-- | test/sandbox.js | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/test/sandbox.js b/test/sandbox.js index 894349fb..eb9f1f0f 100644 --- a/test/sandbox.js +++ b/test/sandbox.js @@ -1,15 +1,35 @@ var vm = require("vm"); +function safe_log(arg) { + if (arg) switch (typeof arg) { + case "function": + return arg.toString(); + case "object": + if (/Error$/.test(arg.name)) return arg.toString(); + arg.constructor.toString(); + for (var key in arg) { + arg[key] = safe_log(arg[key]); + } + } + return arg; +} + var FUNC_TOSTRING = [ "Function.prototype.toString = Function.prototype.valueOf = function() {", - " var ids = [];", + " var id = 0;", " return function() {", - " var i = ids.indexOf(this);", - " if (i < 0) {", - " i = ids.length;", - " ids.push(this);", + ' if (this === Array) return "[Function: Array]";', + ' if (this === Object) return "[Function: Object]";', + " var i = this.name;", + ' if (typeof i != "number") {', + " i = ++id;", + ' Object.defineProperty(this, "name", {', + " get: function() {", + " return i;", + " }", + " });", " }", - ' return "[Function: __func_" + i + "__]";', + ' return "[Function: " + i + "]";', " }", "}();", ].join("\n"); @@ -21,16 +41,14 @@ exports.run_code = function(code) { }; try { vm.runInNewContext([ - "!function() {", FUNC_TOSTRING, + "!function() {", code, "}();", ].join("\n"), { console: { log: function() { - return console.log.apply(console, [].map.call(arguments, function(arg) { - return typeof arg == "function" || arg && /Error$/.test(arg.name) ? arg.toString() : arg; - })); + return console.log.apply(console, [].map.call(arguments, safe_log)); } } }, { timeout: 5000 }); |