eval

Escape string in eval context with JSON.stringify

谁说我不能喝 提交于 2021-01-07 02:40:58
问题 First of all: I know that there are many questions related to escaping, but I did not found a generally working answer so far. Say I have this simple toy function for demonstration: function f(somePOJO) { var s = eval("'" + JSON.stringify(somePOJO) + "';"); // for demonstration only return JSON.parse(s); } const clone = f({a: 1, b: "c"}); Given an object literal such as {a: 1, b: "c"} (a POJO), f should return a "clone" of it. (Note that I do not really use this approach for cloning or

Evaluate/Execute Golang code/expressions like js' eval()

不想你离开。 提交于 2020-08-18 17:01:31
问题 Is there a eval() like method on golang? Evaluate/Execute JavaScript code/expressions: var x = 10; var y = 20; var a = eval("x * y") + "<br>"; var b = eval("2 + 2") + "<br>"; var c = eval("x + 17") + "<br>"; var res = a + b + c; The result of res will be: 200 4 27 Is this possible in golang? and why? 回答1: Is this possible in golang? and why? No, because golang is not that kind of language. It is intended to be compiled, not interpreted, so that the runtime does not contain any “string to code

Evaluate/Execute Golang code/expressions like js' eval()

僤鯓⒐⒋嵵緔 提交于 2020-08-18 16:56:11
问题 Is there a eval() like method on golang? Evaluate/Execute JavaScript code/expressions: var x = 10; var y = 20; var a = eval("x * y") + "<br>"; var b = eval("2 + 2") + "<br>"; var c = eval("x + 17") + "<br>"; var res = a + b + c; The result of res will be: 200 4 27 Is this possible in golang? and why? 回答1: Is this possible in golang? and why? No, because golang is not that kind of language. It is intended to be compiled, not interpreted, so that the runtime does not contain any “string to code

Evaluate/Execute Golang code/expressions like js' eval()

别等时光非礼了梦想. 提交于 2020-08-18 16:52:13
问题 Is there a eval() like method on golang? Evaluate/Execute JavaScript code/expressions: var x = 10; var y = 20; var a = eval("x * y") + "<br>"; var b = eval("2 + 2") + "<br>"; var c = eval("x + 17") + "<br>"; var res = a + b + c; The result of res will be: 200 4 27 Is this possible in golang? and why? 回答1: Is this possible in golang? and why? No, because golang is not that kind of language. It is intended to be compiled, not interpreted, so that the runtime does not contain any “string to code

Why does ast.literal_eval() seem to ignore declared variables?

≡放荡痞女 提交于 2020-06-29 04:14:16
问题 I have heard that ast.literal_eval is much safer than eval(), but while changing my code, I am getting 'malformed string/node' errors. For example: bar = False incorrect = {"foo":bar} correct = {"foo":"bar"} ast.literal_eval(incorrect) returns the error but ast.literal_eval(correct) returns the expected {"foo":"bar"} Why doesn't the first evaluation return {"foo":False} 回答1: Because it is not meant to do that. From the documentation: Safely evaluate an expression node or a Unicode or Latin-1

how to eval regular expression with embedded perl code

喜欢而已 提交于 2020-06-27 15:47:05
问题 So I tested out a regular expression that utilizes the experimental embedded code features. My tests worked, so I expounded upon it to do a more sophisticated script, but ran into errors. I traced the errors to a simple use of a variable in the regular expression not in the embedded code. I tried doing the regex in the suggested eval, but discovered that that wouldn't work because I could not access special variables after the eval'ed regular expression. I eventually re-wrote the code to not