shift-interpreter
Version:
Shift-interpreter is an experimental JavaScript meta-interpreter useful for reverse engineering and analysis. One notable difference from other projects is that shift-interpreter retains state over an entire script but can be fed expressions and statement
67 lines (66 loc) • 2.22 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../util");
const assert_1 = __importDefault(require("assert"));
describe('Try/Catch', () => {
it('should catch errors', () => {
util_1.assertResult(util_1.compare(`let msg = ''; try{ throw new Error('err') } catch(e) {msg = e.message} msg`, { Error }));
});
it('should allow rethrowing', () => {
util_1.assertResult(util_1.compare(`try{ throw new Error('err') } catch(e) {throw e}`, { Error }));
});
it('should return from try', () => {
util_1.assertResult(util_1.compare(`
function returns() {
try {
return 11;
} catch (thrown) {
return 'not this 1';
}
return 'not this 2';
};
returns();
`));
});
it('should be able to return errors from catch', () => {
const src = `
(function() {
try {
null[0];
} catch (t) {
return t;
}
})();`;
const result = util_1.compare(src);
assert_1.default.equal(result.interpreter.errorLocation, undefined);
util_1.assertResult(result);
});
it('should return from catch', () => {
util_1.assertResult(util_1.compare(`
let assert = {};
function assertThrows(expectedErrorConstructor, func) {
try {
func();
} catch (thrown) {
if (thrown.constructor !== expectedErrorConstructor) {
return 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
}
return 'OK';
}
return 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
};
assertThrows(Error, () => { throw new Error() })
`, { Error, console }));
});
});
describe('Try/Finally', () => {
it('should catch errors', () => {
util_1.assertResult(util_1.compare(`let msg = ''; try{ throw new Error('err') } catch(e) {msg = e.message} finally {msg+='finally'} msg`, {
Error,
}));
});
});
//# sourceMappingURL=try.statements.test.js.map