Re: string_exec()
- Posted by petelomax Mar 04, 2015
- 2231 views
after
context = string_exec(context,"a = 1")
...error should be one of:
- line 1, variable a has not been defined
What if variable a was declared in the program already?
If you mean outside a string_exec, it would make no difference, whereas a previous
context = string_exec(context,"atom a")
would. If you think carefully about it, a scheme whereby string_exec()'d code has full access to the host environment is a support and security nightmare, and it makes a huge amount of sense to properly separate, safe-guard, and ring-fence things. Plus there are reasonable workarounds, see below. As I tried to explain under Migration the idea is to replace say
atom a context = string_exec(context,"a = 1")
with
context = string_exec(context,"atom a\n"& "a = 1")
Of course it may not just be a single variable, but a huge chunk of program logic that goes with. It may involve significant refactoring (possible workaround below) but at least there is nothing to stop you using include statements in the string_exec.
And what if
atom b = 9 b += 1would b now be accessable to the program that called the string_exec()?
Yes. After
atom c context = string_exec(context,"atom b = 9\n"& "b += 1") c = get_value(context,"b")
then c would be set to 10. Personally, I'm not convinced that the corresponding set_value has much merit, since string_exec(context,"a = value") would have the same effect, but I'm not staunchly opposed to it or anything. I suppose you could perhaps code something like:
atom a = 1 sequence context = string_exec(NULL,"atom a") set_value(context,"a",a) context = string_exec(context,"a += 1") a = get_value(context,"a")
which, horrible confusion aside, should leave a 2 in both the "inner" and "outer" a.
Pete