This project is read-only.

Custom Scope Storage as default for ScriptEngine.CreateScope()

Sep 17, 2012 at 7:09 PM


I am currently in the process of polishing a DLR Lua implementation, fixing bugs and generally getting it to a usable state, and I have hit a bit of a wall. I am trying to implement a default Scope storage which will be used when another is not specified (i.e. through calls to ScriptEngine.ExecuteFile) however I cannot seem to find any way to override this default behaviour for the ExecuteFile(*) methods. [It is possible to provide a CreateScope override on ScriptCode which is then called when Execute(*) methods are called, thus fulfilling my requirements]

Possible Solutions

  1. Make ScriptEngine.CreateScope virtual and call _language.CreateScope from within ScriptEngine
    • Advantages
      • Also allows all scopes created by calls to ScriptEngine.CreateScope to make use of the default backing store, allowing languages finer control over this.
      • If not overridden by a language it will revert to the current behaviour, making this a non-breaking change.
    • Disadvantages
      • Requires changes to the DLR source code to implement
  2. Make ScriptEngine.ExecuteFile call _language.GetScope(file) instead of CreateScope() [Not feasible: would break the ability to call ExecuteFile multiple times and have each execution separately boxed]

I'll add more alternatives here as I think of them, but at the moment I am tending towards the first. If anyone has any better ideas, or knows of a way I can implement this using existing functionality (without requiring the user to create a scope each time) then I'd appreciate it.


//Initialize ScriptEngine instance
var engine = Lua.CreateEngine();

var scope = engine.CreateScope();
//Expected: scope.Storage should be of type: LuaTable
//Actual:   scope.Storage is of type: ScopeStorage

scope = engine.ExecuteFile(file);
//Expected: scope.Storage should be of type: LuaTable
//Actual:   scope.Storage is of type: ScopeStorage

Sep 17, 2012 at 9:49 PM
Edited Sep 18, 2012 at 8:53 AM

I have subsequently made the relevant changes to the DLR to implement option 1, these changes are contained within Pull Request 81.

Changes add the following methods to LanguageContext:


public virtual Scope CreateScope();
public virtual Scope CreateScope(IDictionary<string,object> dictionary);
public virtual Scope CreateScope(IDynamicMetaObjectProvider storage);

The changes also make calls to ScriptEngine.ExecuteFile(string file) and ScriptCode.CreateScope() both reference the LanguageContext's implementation of the CreateScope method.