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
56 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("../util");
describe('Variables', () => {
it('should declare and init variables and be able to retrieve the value', () => {
util_1.assertResult(util_1.compare('let a = 2; a;'));
});
it('should update values', () => {
util_1.assertResult(util_1.compare('let a = 2; a = 3; a;'));
});
it('should update and retrieve in one statement', () => {
util_1.assertResult(util_1.compare('let a = 2; a = a + 2; a;'));
});
it('decltype-less assignments should assign a global', () => {
const context = {};
util_1.assertResult(util_1.compare('function a() { b = 2; } a(); b', context));
// @ts-ignore
delete global.b;
});
it('var statements should be hoisted', () => {
util_1.assertResult(util_1.compare('function a() { b = 2; var b; } a(); b'));
});
it('should assign to hoisted variables', () => {
util_1.assertResult(util_1.compare('function a() { return b; b = 3; var b = 2; } a()'));
util_1.assertResult(util_1.compare('function a() { b = 3; return b; var b = 2; } a()'));
util_1.assertResult(util_1.compare('function a() { b = 3; var b = 2; return b; } a()'));
});
it('should support const', () => {
util_1.assertResult(util_1.compare('const a = 2; a;'));
});
it('should not allow reassignment to constants', () => {
util_1.assertResult(util_1.compare('const a = 3; try { a = 4 } catch(e) {} a; '));
});
it('should allow array pattern assignments', () => {
util_1.assertResult(util_1.compare('let [a] = [2]; a;'));
});
it('should allow array pattern assignments with defaults', () => {
util_1.assertResult(util_1.compare('let [a = 22] = []; a === 22;'));
});
it('should allow object pattern assignments', () => {
util_1.assertResult(util_1.compare('let {a} = {a:22}; a;'));
});
it('should allow object pattern assignments with defaults', () => {
util_1.assertResult(util_1.compare('let {a = 33} = {}; a;'));
});
it('should allow object pattern assignments with binding property', () => {
util_1.assertResult(util_1.compare('let {a : b} = {a:22}; b;'));
});
it('should allow object pattern assignments with computed property names', () => {
util_1.assertResult(util_1.compare('let {["a"] : b} = {a:22}; b;'));
});
it('should allow nested pattern assignments with computed property names', () => {
util_1.assertResult(util_1.compare('let {["a"] : [b]} = {a:[22]}; b === 22;'));
});
});
//# sourceMappingURL=variables.test.js.map