Restricting eval() to a narrow scope

Short answer: No. If it’s in the global scope, it’s available to anything. Long answer: if you’re eval()ing untrusted code that really wants to read or mess with your execution environment, you’re screwed. But if you own and trust all code being executed, including that being eval()ed, you can fake it by overriding the execution … Read more

Accessing or creating nested JavaScript objects with string key without eval

You could split the path and reduce the path by walking the given object. If no Object exist, create a new property with the name, or an array. Later assign the value. function setValue(object, path, value) { var way = path.replace(/\[/g, ‘.’).replace(/\]/g, ”).split(‘.’), last = way.pop(); way.reduce(function (o, k, i, kk) { return o[k] = … Read more

Process mathematical equations in php

My standard answer to this question whenever it crops up: Don’t use eval (especially as you’re stating that this is user input) or reinvent the wheel by writing your own formula parser. Take a look at the evalMath class on PHPClasses. It should do everything that you’ve listed here. EDIT re: Unfortunately evalMath does not … Read more

Is there C/C++ equivalent of eval(“function(arg1, arg2)”)?

C++ doesn’t have reflection so you must hack it, i. e.: #include <iostream> #include <map> #include <string> #include <functional> void foo() { std::cout << “foo()”; } void boo() { std::cout << “boo()”; } void too() { std::cout << “too()”; } void goo() { std::cout << “goo()”; } int main() { std::map<std::string, std::function<void()>> functions; functions[“foo”] = … Read more

Understanding ASP.NET Eval() and Bind()

For read-only controls they are the same. For 2 way databinding, using a datasource in which you want to update, insert, etc with declarative databinding, you’ll need to use Bind. Imagine for example a GridView with a ItemTemplate and EditItemTemplate. If you use Bind or Eval in the ItemTemplate, there will be no difference. If … Read more

Assign to a bash array variable indirectly, by dynamically constructed variable name

arr$varEnvCol[$index]=”$(…)” doesn’t work the way you expect it to – you cannot assign to shell variables indirectly – via an expression that expands to the variable name – this way. Your attempted workaround with eval is also flawed – see below. tl;dr If you use bash 4.3 or above: declare -n targetArray=”arr$varEnvCol” targetArray[index]=$(echo $line | … Read more

How do I use variables to set properties in VBA (Excel)

Solution 1. Use CallByName. Option Explicit Private Type Callable o As Object p As String End Type Public Sub SetProperty(ByVal path As String, ByVal Value As Variant, Optional ByVal RootObject As Object = Nothing) With GetObjectFromPath(RootObject, path) If IsObject(Value) Then CallByName .o, .p, VbSet, Value Else CallByName .o, .p, VbLet, Value End If End With … Read more

Haskell: how to evaluate a String like “1+2”

Your question leaves a lot of room for interpretation. I’m taking a guess you aren’t accustom to building a whole pipeline of lexing, parsing, maybe type checking, and evaluating. The long answer would involve you defining what language you wish to evaluate (Just integers with ‘+’, perhaps all rationals with ‘+’, ‘-‘ ‘*’, “https://stackoverflow.com/”, or … Read more