create or overwrite functions with Eval()

Is it possible to use Eval() to create a function? Say I want to make a way of live-patching an app, by bringing down some code via http and then running it with Eval(). Can I actually create a new function (or overwrite an existing one) using Eval(), as I could in say javascript as below?

eval("window.hello = function() { alert('hello world'); }")

Sure. Eval() runs in the context of the channel, so anything executed in your Eval’d string will apply to the variables in the running app.

For example:

testVar = 100
print "Before:"; testVar
Eval("testVar = 0")
print "After:"; testVar

should output to the console:

Before: 100
After: 0

Likewise, since BrightScript supports anonymous functions, the following should produce the same results:

testFunc = Function()
    Return 100
End Function

print "Before:"; testFunc()
Eval("testFunc = Function():Return 1:End Function")
print "After:"; testFunc()

Note, the colons are used strictly for brevity in the example. The string you’re Eval’ing can have carriage returns.

a$="goto Hello"

eval(a$)

goto nothello

Hello:
  print "hello"
  goto endhello
nothello:

  print "not hello"

endhello:
stop

This seems to always print “not hello” on the console. Am I not calling eval correctly? I do see in the in the listing that it is showing the “goto hello” as the last command before the stop.

Thanks,
Troy

If you look at the result of the Eval, you’ll see:

ERRSTR: "Label/Line Not Found."
FILESPEC: "$LIVECOMPILE"
ERRNO: 14
LINENO: 0

My guess would be that labels are interpreted during the compile, so they’re not available at runtime, when the Eval is being executed. You get the same error if you type “goto Hello” at the debug command prompt.

Apologies for taking a few months to reply and thank you. :slightly_smiling_face: I finally got back to this little project, and your response was very helpful.

Now a followup question, if you don’t mind:

Can I use this technique to replace a global (non-anonymous) function declared as

Function testFunc()
    Return 100
End Function

as opposed to:

testFunc = Function()
    Return 100
End Function

Again, this is for a development/debugging tool where I can live-patch code. Since most of this code is, unfortunately, not written by me and is not object oriented, most of the functions are declared as in the first example.

I just tried testing this, and it doesn’t appear to work. It doesn’t generate an error, but it also doesn’t replace the function. So, it looks like you can only replace functions assigned to variables.

That’s the impression I was getting but thought I might have had the syntax wrong.

Anyway thanks much for your help.

Not able to test this at the moment, but wouldn’t the m global variable contain the function no matter how it was defined?

Also, since you mention that this is a dev/debug tool you probably know this, but for anyone else: be extremely careful when doing something like this. For the most part, running outside code is a bad idea.

I tried using the m variable, but no workie.