rjbrown
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2012
05:31 PM
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'); }")
8 REPLIES 8

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2012
06:04 PM
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:
should output to the console:
Likewise, since BrightScript supports anonymous functions, the following should produce the same results:
Note, the colons are used strictly for brevity in the example. The string you're Eval'ing can have carriage returns.
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
GPF
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2012
06:29 PM
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

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2012
07:19 PM
Re: create or overwrite functions with Eval()
If you look at the result of the Eval, you'll see:
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.
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
rjbrown
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2012
05:02 PM
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
as opposed to:
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.
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.

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2012
05:33 PM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
rjbrown
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2012
06:08 PM
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.
Anyway thanks much for your help.
Mr__E
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2012
07:05 PM
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.
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.
rjbrown
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2012
07:12 PM
Re: create or overwrite functions with Eval()
I tried using the m variable, but no workie.