Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
rjbrown
Visitor

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'); }")
0 Kudos
8 REPLIES 8
TheEndless
Channel Surfer

Re: create or overwrite functions with Eval()

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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
GPF
Visitor

Re: create or overwrite functions with Eval()

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
0 Kudos
TheEndless
Channel Surfer

Re: create or overwrite functions with Eval()

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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
rjbrown
Visitor

Re: create or overwrite functions with Eval()

Apologies for taking a few months to reply and thank you. 🙂 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.
0 Kudos
TheEndless
Channel Surfer

Re: create or overwrite functions with Eval()

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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
rjbrown
Visitor

Re: create or overwrite functions with Eval()

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

Anyway thanks much for your help.
0 Kudos
Mr__E
Channel Surfer

Re: create or overwrite functions with Eval()

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.
0 Kudos
rjbrown
Visitor

Re: create or overwrite functions with Eval()

I tried using the m variable, but no workie.
0 Kudos