Reviewers: Mads Ager, Description: Fixing wrong reference to this in eval.
Please review this at http://codereview.chromium.org/11227 SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/ Affected files: M src/runtime.h M src/runtime.cc M src/v8natives.js M test/cctest/test-api.cc Index: test/cctest/test-api.cc =================================================================== --- test/cctest/test-api.cc (revision 782) +++ test/cctest/test-api.cc (working copy) @@ -4029,8 +4029,16 @@ " var foo = 2;" " return eval('foo');" "})();")); - Local<Value> foo = script->Run(); - CHECK_EQ(2, foo->Int32Value()); + Local<Value> result = script->Run(); + CHECK_EQ(2, result->Int32Value()); + + // Test that un-aliased eval has right this. + script = + Script::Compile(v8_str("function MyObject() { this.self = eval('this'); }" + "var o = new MyObject();" + "o === o.self")); + result = script->Run(); + CHECK(result->IsTrue()); } Index: src/v8natives.js =================================================================== --- src/v8natives.js (revision 782) +++ src/v8natives.js (working copy) @@ -113,7 +113,7 @@ var f = %CompileString(x, 0, true); if (!IS_FUNCTION(f)) return f; - return f.call(this); + return f.call(%EvalReceiver(this)); } Index: src/runtime.cc =================================================================== --- src/runtime.cc (revision 782) +++ src/runtime.cc (working copy) @@ -3961,6 +3961,26 @@ } +static Object* Runtime_EvalReceiver(Arguments args) { + ASSERT(args.length() == 1); + StackFrameLocator locator; + JavaScriptFrame* frame = locator.FindJavaScriptFrame(1); + // Fetch the caller context from the frame. + Context* caller = Context::cast(frame->context()); + + // Check for eval() invocations that cross environments. Use the + // top frames receiver if evaluating in current environment. + Context* global_context = Top::context()->global()->global_context(); + if (caller->global_context() == global_context) { + return frame->receiver(); + } + + // Otherwise use the given argument (the global object of the + // receiving context). + return args[0]; +} + + static Object* Runtime_GlobalReceiver(Arguments args) { ASSERT(args.length() == 1); Object* global = args[0]; Index: src/runtime.h =================================================================== --- src/runtime.h (revision 782) +++ src/runtime.h (working copy) @@ -195,6 +195,7 @@ F(GlobalPrint, 1) \ \ /* Eval */ \ + F(EvalReceiver, 1) \ F(GlobalReceiver, 1) \ \ F(SetProperty, -1 /* 3 or 4 */) \ --~--~---------~--~----~------------~-------~--~----~ v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev -~----------~----~----~----~------~----~------~--~---
