Skip to content

Commit e3f641f

Browse files
Fixed native property hiding on V8 (Issue ClearFoundry#98).
1 parent 0970b09 commit e3f641f

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

ClearScript/V8/ClearScriptV8/V8ContextImpl.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ void V8ContextImpl::GetHostObjectProperty(v8::Local<v8::Name> hKey, const v8::Pr
13051305
}
13061306

13071307
auto hHolder = info.Holder();
1308+
auto cacheCleared = false;
13081309

13091310
auto hAccessToken = hHolder->GetHiddenValue(pContextImpl->m_hAccessTokenName);
13101311
if (pContextImpl->m_hAccessToken != hAccessToken)
@@ -1318,19 +1319,29 @@ void V8ContextImpl::GetHostObjectProperty(v8::Local<v8::Name> hKey, const v8::Pr
13181319
}
13191320

13201321
hHolder->SetHiddenValue(pContextImpl->m_hAccessTokenName, pContextImpl->m_hAccessToken);
1322+
cacheCleared = true;
13211323

13221324
END_PULSE_VALUE_SCOPE
13231325
}
13241326

1325-
auto hResult = hHolder->GetRealNamedProperty(hName);
1326-
if (hResult.IsEmpty())
1327+
v8::Local<v8::Value> hResult;
1328+
if (!cacheCleared)
13271329
{
1328-
bool isCacheable;
1329-
hResult = pContextImpl->ImportValue(HostObjectHelpers::GetProperty(::GetHostObject(info.Holder()), StdString(hName), isCacheable));
1330-
if (isCacheable)
1331-
{
1332-
hHolder->ForceSet(hName, hResult);
1333-
}
1330+
BEGIN_PULSE_VALUE_SCOPE(&pContextImpl->m_DisableHostObjectInterception, true)
1331+
1332+
if (hHolder->HasOwnProperty(hName))
1333+
{
1334+
CALLBACK_RETURN(hResult);
1335+
}
1336+
1337+
END_PULSE_VALUE_SCOPE
1338+
}
1339+
1340+
bool isCacheable;
1341+
hResult = pContextImpl->ImportValue(HostObjectHelpers::GetProperty(::GetHostObject(info.Holder()), StdString(hName), isCacheable));
1342+
if (isCacheable)
1343+
{
1344+
hHolder->ForceSet(hName, hResult);
13341345
}
13351346

13361347
CALLBACK_RETURN(hResult);

ClearScriptTest/BugFixTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,22 @@ public void BugFix_V8HostMethodClobbering()
18111811
Assert.IsInstanceOfType(engine.Evaluate("host.newObj()"), typeof(PropertyBag));
18121812
}
18131813

1814+
[TestMethod, TestCategory("BugFix")]
1815+
public void BugFix_V8NativePropertyHiding()
1816+
{
1817+
var foo = new { toString = new Func<string>(() => "testValue") };
1818+
engine.Script.foo = foo;
1819+
Assert.AreEqual(foo.toString(), engine.Evaluate("foo.toString()"));
1820+
}
1821+
1822+
[TestMethod, TestCategory("BugFix")]
1823+
public void BugFix_V8NativePropertyHiding_Method()
1824+
{
1825+
var foo = new HideNativeJavaScriptMethod();
1826+
engine.Script.foo = foo;
1827+
Assert.AreEqual(foo.toString(), engine.Evaluate("foo.toString()"));
1828+
}
1829+
18141830
// ReSharper restore InconsistentNaming
18151831

18161832
#endregion
@@ -2029,6 +2045,18 @@ public class NumericArgConversionTest
20292045
public decimal? NullableDecimalProperty { get; set; }
20302046
}
20312047

2048+
public class HideNativeJavaScriptMethod
2049+
{
2050+
// ReSharper disable InconsistentNaming
2051+
2052+
public string toString()
2053+
{
2054+
return ToString();
2055+
}
2056+
2057+
// ReSharper restore InconsistentNaming
2058+
}
2059+
20322060
#endregion
20332061
}
20342062
}

0 commit comments

Comments
 (0)