Code Review: RubyScopes2


#1

tfpt review “/shelveset:RubyScopes2;REDMOND\tomat”

DLR

Deletes IDynamicObject and GlobalsDictionary types and Scope.GetKeys
method.
Adds a default implementation for ScriptCode.Run that creates a new
scope and calls Run(scope).
Adds assertions to ContractUtils.Invariant.

Ruby

RubyScope fixes and refactoring.

This change enables top-level hosted code to define instance methods
in DLR scope. It is no longer necessary to define them as singleton
methods.
Ruby engine executes the hosted code as if it was executed using
instance_eval on a proc. The behavior is the same except for control
flow - break or retry on top-level throws an exception as it does in
MRI.

This unit test illustrates the new behavior:

          // When executed without a scope top-level methods are 

defined on Object (as in MRI):
Engine.Execute(“def foo; 1; end”);
Assert(Context.ObjectClass.GetMethod(“foo”) != null);

          // When executed against a scope top-level methods are 

defined on main singleton (not on Object) and also stored in the scope:
var scope = Engine.CreateScope();
Engine.Execute(“def bar; 1; end”, scope);
Assert(Context.ObjectClass.GetMethod(“bar”) == null);
Assert(scope.GetVariable(“bar”) != null);

Fixes bug in top-level scope compilation - variables from the scope
were not bound properly:

          var compiled = 

Engine.CreateScriptSourceFromString(“some_variable”).Compile();
scope = Engine.CreateScope();
scope.SetVariable(“some_variable”, “foo”);
Assert(compiled.Execute(scope) == “foo”);

Tomas