/**
 *    _____ __
 *   / ___// /_  _____
 *   \__ \/ / / / / _ \
 *  ___/ / / /_/ /  __/
 * /____/_/\__, /\___/
 *       /____/
 *       Copyright 2017 Slye Development Team. All Rights Reserved.
 *       Licence: MIT License
 */

const vm    = require('vm');

/**
 * Compile a esy code
 * @param code
 */
function compile(code) {
	var esy     = require('../src/index');
	esy.configs.run('cache.active', false);
	var tree    = esy.tree(code);
	return esy.compile(tree);
}

/**
 * Evaluate a js code
 * returns error.toString on errors
 * @param code
 * @return {*}
 */
function safeEval(code) {
	var re;
	try {
		re = eval(code);
	}catch (e){
		re = e.toString();
	}
	return re;
}

/**
 * Compare result of Esy code and JS version
 */
function compare(EsyCode, js){
	js      = js || EsyCode;
	EsyCode = compile(EsyCode);

	var sandbox1    = {},   // For Esy
		sandbox2    = {};   // For JS

	const script1   = new vm.Script(EsyCode);
	script1.runInNewContext(sandbox1);

	const script2   = new vm.Script(js);
	script2.runInNewContext(sandbox2);

	return JSON.stringify(sandbox1) == JSON.stringify(sandbox2);
}

module.exports.compile  = compile;
module.exports.safeEval = safeEval;
module.exports.compare  = compare;
@@ -885,16 +885,24 @@
 For JS%0A%0A
+%09try %7B%0A%09
 %09const s
@@ -936,16 +936,17 @@
 yCode);%0A
+%09
 %09script1
@@ -973,17 +973,57 @@
 dbox1);%0A
-%0A
+%09%7Dcatch (e)%7B%0A%09%09return false;%0A%09%7D%0A%0A%09try %7B%0A%09
 %09const s
@@ -1052,16 +1052,17 @@
 pt(js);%0A
+%09
 %09script2
@@ -1088,16 +1088,48 @@
 ndbox2);
+%0A%09%7Dcatch (e)%7B%0A%09%09return false;%0A%09%7D
 %0A%0A%09retur

Handle errors