Skip to content

Commit e424210

Browse files
Fix GlobalMembers writes in V8ScriptEngine.
1 parent c9a9565 commit e424210

6 files changed

Lines changed: 70 additions & 2 deletions

File tree

ClearScript/V8/ClearScriptV8/V8ContextImpl.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ V8ContextImpl::V8ContextImpl(LPCWSTR pName, bool enableDebugging, bool disableGl
207207
{
208208
auto hGlobalTemplate = ObjectTemplate::New();
209209
hGlobalTemplate->SetInternalFieldCount(1);
210-
hGlobalTemplate->SetNamedPropertyHandler(GetGlobalProperty);
210+
hGlobalTemplate->SetNamedPropertyHandler(GetGlobalProperty, SetGlobalProperty);
211211

212212
m_hContext = Context::New(nullptr, hGlobalTemplate);
213213

@@ -639,6 +639,37 @@ Handle<Value> V8ContextImpl::GetGlobalProperty(Local<String> hName, const Access
639639

640640
//-----------------------------------------------------------------------------
641641

642+
Handle<Value> V8ContextImpl::SetGlobalProperty(Local<String> hName, Local<Value> value, const AccessorInfo& info)
643+
{
644+
auto hGlobal = info.Holder();
645+
_ASSERTE(hGlobal->InternalFieldCount() > 0);
646+
647+
auto pContextImpl = reinterpret_cast<V8ContextImpl*>(hGlobal->GetAlignedPointerFromInternalField(0));
648+
if (pContextImpl != nullptr)
649+
{
650+
const vector<Persistent<Object>>& stack = pContextImpl->m_GlobalMembersStack;
651+
if (stack.size() > 0)
652+
{
653+
if (hName->Equals(pContextImpl->m_hHostObjectCookieName))
654+
{
655+
return Handle<Value>();
656+
}
657+
658+
for (auto it = stack.rbegin(); it != stack.rend(); it++)
659+
{
660+
if ((*it)->HasOwnProperty(hName) && (*it)->Set(hName, value))
661+
{
662+
return value;
663+
}
664+
}
665+
}
666+
}
667+
668+
return Handle<Value>();
669+
}
670+
671+
//-----------------------------------------------------------------------------
672+
642673
Handle<Value> V8ContextImpl::GetHostObjectProperty(Local<String> hName, const AccessorInfo& info)
643674
{
644675
try

ClearScript/V8/ClearScriptV8/V8ContextImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class V8ContextImpl: public V8Context
118118
Handle<Value> Wrap();
119119

120120
static Handle<Value> GetGlobalProperty(Local<String> hName, const AccessorInfo& info);
121+
static Handle<Value> SetGlobalProperty(Local<String> hName, Local<Value> value, const AccessorInfo& info);
121122

122123
static Handle<Value> GetHostObjectProperty(Local<String> hName, const AccessorInfo& info);
123124
static Handle<Value> SetHostObjectProperty(Local<String> hName, Local<Value> hValue, const AccessorInfo& info);

ClearScript/V8/V8/V8Patch.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Index: tools/gyp/v8.gyp
22
===================================================================
3-
--- tools/gyp/v8.gyp (revision 13633)
3+
--- tools/gyp/v8.gyp (revision 13696)
44
+++ tools/gyp/v8.gyp (working copy)
55
@@ -32,6 +32,7 @@
66
'targets': [

ClearScriptTest/JScriptEngineTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ public void JScriptEngine_AddHostObject_GlobalMembers()
135135
var host = new HostFunctions();
136136
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
137137
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
138+
139+
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
140+
engine.Execute("TestProperty = newObj()");
141+
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
138142
}
139143

140144
[TestMethod, TestCategory("JScriptEngine")]
@@ -165,6 +169,10 @@ public void JScriptEngine_AddHostType_GlobalMembers()
165169
{
166170
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
167171
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
172+
173+
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
174+
engine.Execute("StaticTestProperty = NewGuid()");
175+
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
168176
}
169177

170178
[TestMethod, TestCategory("JScriptEngine")]
@@ -591,6 +599,10 @@ function onStaticChange(s, e) {
591599
Property changed: Name; new value: Shane (static event)
592600
";
593601

602+
public object TestProperty { get; set; }
603+
604+
public static object StaticTestProperty { get; set; }
605+
594606
// ReSharper disable UnusedMember.Local
595607

596608
private void PrivateMethod()

ClearScriptTest/V8ScriptEngineTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public void V8ScriptEngine_AddHostObject_GlobalMembers()
138138
var host = new HostFunctions();
139139
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
140140
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
141+
142+
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
143+
engine.Execute("TestProperty = newObj()");
144+
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
141145
}
142146

143147
[TestMethod, TestCategory("V8ScriptEngine")]
@@ -168,6 +172,10 @@ public void V8ScriptEngine_AddHostType_GlobalMembers()
168172
{
169173
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
170174
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
175+
176+
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
177+
engine.Execute("StaticTestProperty = NewGuid()");
178+
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
171179
}
172180

173181
[TestMethod, TestCategory("V8ScriptEngine")]
@@ -636,6 +644,10 @@ function onStaticChange(s, e) {
636644
Property changed: Name; new value: Shane (static event)
637645
";
638646

647+
public object TestProperty { get; set; }
648+
649+
public static object StaticTestProperty { get; set; }
650+
639651
// ReSharper disable UnusedMember.Local
640652

641653
private void PrivateMethod()

ClearScriptTest/VBScriptEngineTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ public void VBScriptEngine_AddHostObject_GlobalMembers()
137137
var host = new HostFunctions();
138138
engine.AddHostObject("host", HostItemFlags.GlobalMembers, host);
139139
Assert.IsInstanceOfType(engine.Evaluate("newObj()"), typeof(PropertyBag));
140+
141+
engine.AddHostObject("test", HostItemFlags.GlobalMembers, this);
142+
engine.Execute("TestProperty = newObj()");
143+
Assert.IsInstanceOfType(TestProperty, typeof(PropertyBag));
140144
}
141145

142146
[TestMethod, TestCategory("VBScriptEngine")]
@@ -167,6 +171,10 @@ public void VBScriptEngine_AddHostType_GlobalMembers()
167171
{
168172
engine.AddHostType("Guid", HostItemFlags.GlobalMembers, typeof(Guid));
169173
Assert.IsInstanceOfType(engine.Evaluate("NewGuid()"), typeof(Guid));
174+
175+
engine.AddHostType("Test", HostItemFlags.GlobalMembers, GetType());
176+
engine.Execute("StaticTestProperty = NewGuid()");
177+
Assert.IsInstanceOfType(StaticTestProperty, typeof(Guid));
170178
}
171179

172180
[TestMethod, TestCategory("VBScriptEngine")]
@@ -572,6 +580,10 @@ call staticEventCookie.disconnect()
572580
Property changed: Name; new value: Shane (static event)
573581
";
574582

583+
public object TestProperty { get; set; }
584+
585+
public static object StaticTestProperty { get; set; }
586+
575587
// ReSharper disable UnusedMember.Local
576588

577589
private void PrivateMethod()

0 commit comments

Comments
 (0)