Fix GlobalMembers writes in V8ScriptEngine.

This commit is contained in:
ClearScript 2013-02-19 21:03:27 -05:00
Родитель c9a9565e7c
Коммит e424210043
6 изменённых файлов: 70 добавлений и 2 удалений

Просмотреть файл

@ -207,7 +207,7 @@ V8ContextImpl::V8ContextImpl(LPCWSTR pName, bool enableDebugging, bool disableGl
{
auto hGlobalTemplate = ObjectTemplate::New();
hGlobalTemplate->SetInternalFieldCount(1);
hGlobalTemplate->SetNamedPropertyHandler(GetGlobalProperty);
hGlobalTemplate->SetNamedPropertyHandler(GetGlobalProperty, SetGlobalProperty);
m_hContext = Context::New(nullptr, hGlobalTemplate);
@ -639,6 +639,37 @@ Handle<Value> V8ContextImpl::GetGlobalProperty(Local<String> hName, const Access
//-----------------------------------------------------------------------------
Handle<Value> V8ContextImpl::SetGlobalProperty(Local<String> hName, Local<Value> value, const AccessorInfo& info)
{
auto hGlobal = info.Holder();
_ASSERTE(hGlobal->InternalFieldCount() > 0);
auto pContextImpl = reinterpret_cast<V8ContextImpl*>(hGlobal->GetAlignedPointerFromInternalField(0));
if (pContextImpl != nullptr)
{
const vector<Persistent<Object>>& stack = pContextImpl->m_GlobalMembersStack;
if (stack.size() > 0)
{
if (hName->Equals(pContextImpl->m_hHostObjectCookieName))
{
return Handle<Value>();
}
for (auto it = stack.rbegin(); it != stack.rend(); it++)
{
if ((*it)->HasOwnProperty(hName) && (*it)->Set(hName, value))
{
return value;
}
}
}
}
return Handle<Value>();
}
//-----------------------------------------------------------------------------
Handle<Value> V8ContextImpl::GetHostObjectProperty(Local<String> hName, const AccessorInfo& info)
{
try

Просмотреть файл

@ -118,6 +118,7 @@ private:
Handle<Value> Wrap();
static Handle<Value> GetGlobalProperty(Local<String> hName, const AccessorInfo& info);
static Handle<Value> SetGlobalProperty(Local<String> hName, Local<Value> value, const AccessorInfo& info);
static Handle<Value> GetHostObjectProperty(Local<String> hName, const AccessorInfo& info);
static Handle<Value> SetHostObjectProperty(Local<String> hName, Local<Value> hValue, const AccessorInfo& info);

Просмотреть файл

@ -1,6 +1,6 @@
Index: tools/gyp/v8.gyp
===================================================================
--- tools/gyp/v8.gyp (revision 13633)
--- tools/gyp/v8.gyp (revision 13696)
+++ tools/gyp/v8.gyp (working copy)
@@ -32,6 +32,7 @@
'targets': [

Просмотреть файл

@ -135,6 +135,10 @@ namespace Microsoft.ClearScript.Test
var host = new HostFunctions();
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
engine.Execute("TestProperty = newObj()");
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
}
[TestMethod, TestCategory("JScriptEngine")]
@ -165,6 +169,10 @@ namespace Microsoft.ClearScript.Test
{
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
engine.Execute("StaticTestProperty = NewGuid()");
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
}
[TestMethod, TestCategory("JScriptEngine")]
@ -591,6 +599,10 @@ namespace Microsoft.ClearScript.Test
Property changed: Name; new value: Shane (static event)
";
public object TestProperty { get; set; }
public static object StaticTestProperty { get; set; }
// ReSharper disable UnusedMember.Local
private void PrivateMethod()

Просмотреть файл

@ -138,6 +138,10 @@ namespace Microsoft.ClearScript.Test
var host = new HostFunctions();
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
engine.Execute("TestProperty = newObj()");
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
}
[TestMethod, TestCategory("V8ScriptEngine")]
@ -168,6 +172,10 @@ namespace Microsoft.ClearScript.Test
{
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
engine.Execute("StaticTestProperty = NewGuid()");
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
}
[TestMethod, TestCategory("V8ScriptEngine")]
@ -636,6 +644,10 @@ namespace Microsoft.ClearScript.Test
Property changed: Name; new value: Shane (static event)
";
public object TestProperty { get; set; }
public static object StaticTestProperty { get; set; }
// ReSharper disable UnusedMember.Local
private void PrivateMethod()

Просмотреть файл

@ -137,6 +137,10 @@ namespace Microsoft.ClearScript.Test
var host = new HostFunctions();
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
engine.Execute("TestProperty = newObj()");
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
}
[TestMethod, TestCategory("VBScriptEngine")]
@ -167,6 +171,10 @@ namespace Microsoft.ClearScript.Test
{
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
engine.Execute("StaticTestProperty = NewGuid()");
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
}
[TestMethod, TestCategory("VBScriptEngine")]
@ -572,6 +580,10 @@ namespace Microsoft.ClearScript.Test
Property changed: Name; new value: Shane (static event)
";
public object TestProperty { get; set; }
public static object StaticTestProperty { get; set; }
// ReSharper disable UnusedMember.Local
private void PrivateMethod()