Friday, 9 August 2013

Run inputted text as javascript in a javascript application?

Run inputted text as javascript in a javascript application?

My goal is to write a javascript application that will take text as an
input and compile/run that text as code.
For example, say the JS application has a light that can turn red or
green. The user inputted text could be lightRed(); to turn it red, and
lightGreen(); to turn it green. I think the standard way to solve this
kind of issue is to implement some kind of lexer/parser, like what Jison
does.
However, I'm fairly new to JS programming and that seems like a daunting
task - especially when I later plan to add more complex functionality to
it like if/else statements. So I was wondering if it was possible to have
the inputted text treated as javascript, essentially using the browser's
ability to process javascript. So the javascript application will have a
light, and it will have functions called lightRed() and lightGreen(). Text
inputted to the javascript will be treated as javascript, so writing
lightRed() as text will directly execute the lightRed() function in the
application. Is this possible? Would this be more complicated than just
using something like Jison? Thanks!

No comments:

Post a Comment