Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am curious about how global variables are handled by script engines. I am looking for a script engine that does not preserve the state of global variables upon invocation. Are there such engines out there? We are looking for a scripting language we can use under the script engine API for Java.

When making multiple invocations of a script engine, top-level calls to eval() or evaluate() method preserves the state of global variables, meaning that consequent calls to eval() will use the global variables as they were left by the last invocation. Is there a script engine that does not preserve the state, or provides the ability to reset the state, so that global variables are at their initial state every time the script engine is invoked?

share|improve this question
Any scripting engine I've ever embedded or thought about embedding offered some way to do this (though sometimes not obvious). Which ones did you see that don't allow this? I'm asking so I can avoid them. – delnan Dec 10 '12 at 17:23
I have been most interested in rhino or lujava. I was under the impression that luajava has no global state, but based on some examples I've seen, this is not the case. I've also thought about the possibility of "resetting" the global state before invocations and whether that functionality exists in any engine. – user1886930 Dec 10 '12 at 18:11
I'm no Lua expert, but it appears to me that you can always create a new lua_State, which will then be a blank slate. – delnan Dec 10 '12 at 18:13
are you familiar with any javascript engine that either does not rely or provides a way to "reset" on a global environment, similar to a lua_newstate? – user1886930 Dec 10 '12 at 18:27
V8 has "contexts" which are isolated from one another. I don't know if there are V8 bindings. – delnan Dec 10 '12 at 18:29

Any API to a scripting engine that allows for creation / teardown of the virtual machine associated with engine provides what you're looking for. Script hosts built for embedding into other software do this job well.

Lua is an embeddable scripting host, and this is what @delnan is talking about -- the API allows for the arbitrary instantiation of virtual machines that each have their own global state.

There are ways to do this in other scripting hosts (or rather, virtual machines). A .Net Common Language Runtime (CLR) can be invoked and used in a localized way via Microsoft's COM interface.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.