Re: eval()
- Posted by katsmeow Jul 22, 2021
- 1015 views
{} = eval(`puts(1,"Hello World\n")`)
In Phix you must always declare variables before use, and such code fragments are no exception.
This is a literal string being passed, and so this will not work?
string cmd = `puts(1,"Hello World\n")` x = eval(cmd)
It is not expensive to copy variables/data from the calling code into and out of the "eval context" (for want of a better term). {{{ The eval function accepts up to 4 parameters: code is the source code fragment to be interpreted, rset is a list of string names of (static) result variable identifiers iset is a list of string names of (static) variables to initialise ival is a list of values matching iset, alternatively ival can be omitted and iset instead provided as a list of {name,value} pairs.
So this feature can be used to explore how a procedure will act on various settings? I can loop thru dynamically incremented values, for instance?
One thing that is not yet clear is the lifetime/persistency of generated code.
You are saying the data passed to eval() is all saved in ram in a way that it's automatically reclaimed by Phix whenever? I must not understand, i assumed this would be the standard operating situation. Again assuming, if in the code i first posted, i can store the code in variable cmd and simply call eval(cmd) again as i need? Or will this clutter up ram and crash Phix?
A little something i wrote ~25 years ago, on win95, as a line in a database :
<exec> /msg %chan <mirccolor12> The date and time here is: <mirccolor2> $gettok($asctime, 1-3, 32) $gettok($asctime, 5, 32) <mirccolor12> $atime
Comments/thoughts?
Well, you certainly left OE in the dust! That said, on a C-64 circa mid-1980's a program could grab some spare ram, copy vars and code needed to include into that ram, edit some pointers, and that would be a new environment, run the isolated sandboxed code, until the code terminated and the program put the pointers back, dropping it back to the original environment.
I am unsure how to say how great this can become, without tripping over my words.
Kat