eval function in deaf context
by Konstantin Cryman
JavaScript
var ExecScript = (function(){
function ExecScript( contextObject, scriptBody ){
this.contextObject = contextObject || {};
this.originalScriptBody = scriptBody || '';
this.methodsProviderBody = '';
this.resultBody = '';
this.resultScriptFunction = false;
this.init();
}
ExecScript.prototype.init = function(){
this.methodsProviderBody += '// methods provider \n';
for( let KEY in this.contextObject ){
this.methodsProviderBody += 'let ' + KEY + ' = this.' + KEY + ';\n';
}
this.resultBody += this.methodsProviderBody;
this.resultBody += '\n //original script body \n';
this.resultBody += this.originalScriptBody;
this.resultScriptFunction = new Function( this.resultBody );
}
ExecScript.prototype.run = function(){
this.resultScriptFunction.call( this.contextObject );
}
return ExecScript;
})();
const updatorScript: string = ;
eval(updatorScript);
let actionFunc = new Function( `
this.draft()["${action.schematicUID}"].data.gadgets[${gadgetIndex}].${update.target} = this.upadate().value;
`);
actionFunc.call({
draft: () => {
return draft;
},
update: () => {
return update;
}
});
var testSctipt = `
getTagValue();
getProperty();
setProperty();
runScript();
console.log( this );
`;
var ProviderNativeMethods = {
getTagValue: function(){
console.log( "getTagValue" );
},
getProperty: function(){
console.log( "getProperty" );
},
setProperty: function(){
console.log( "setProperty" );
},
runScript: function(){
console.log( "runScript" );
},
};
var exec1 = new ExecScript( ProviderNativeMethods, testSctipt );
console.log( exec1 );
exec1.run();