Procházet zdrojové kódy

Inherit toString from Error.prototype (#6391)

Richard před 5 roky
rodič
revize
a125635b29

+ 1 - 2
lib/Runtime/Library/JavascriptLibrary.cpp

@@ -2442,14 +2442,13 @@ namespace Js
     } \
     bool JavascriptLibrary::Initialize##error##Prototype(DynamicObject* prototype, DeferredTypeHandlerBase* typeHandler, DeferredInitializeMode mode) \
     { \
-        typeHandler->Convert(prototype, mode, 4); \
+        typeHandler->Convert(prototype, mode, 3); \
         JavascriptLibrary* library = prototype->GetLibrary(); \
         library->AddMember(prototype, PropertyIds::constructor, library->Get##error##Constructor()); \
         bool hasNoEnumerableProperties = true; \
         PropertyAttributes prototypeNameMessageAttributes = PropertyConfigurable | PropertyWritable; \
         library->AddMember(prototype, PropertyIds::name, library->CreateStringFromCppLiteral(_u(#errorName)), prototypeNameMessageAttributes); \
         library->AddMember(prototype, PropertyIds::message, library->GetEmptyString(), prototypeNameMessageAttributes); \
-        library->AddFunctionToLibraryObject(prototype, PropertyIds::toString, &JavascriptError::EntryInfo::ToString, 0); \
         prototype->SetHasNoEnumerableProperties(hasNoEnumerableProperties); \
         return true; \
     }

+ 34 - 0
test/Error/errorPrototypetoString.js

@@ -0,0 +1,34 @@
+//-------------------------------------------------------------------------------------------------------
+// Copyright (C) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
+//-------------------------------------------------------------------------------------------------------
+
+// Test the fix for https://github.com/microsoft/ChakraCore/issues/6372
+
+
+function checkObject(object)
+{
+    if (object.prototype.hasOwnProperty('toString'))
+    {
+        throw new Error(`${object.name}.prototype should not have own property 'toString'`);
+    }
+    if(object.prototype.toString !== Error.prototype.toString)
+    {
+        throw new Error(`${object.name}.prototype.toString should === Error.prototype.toString`);
+    }
+}
+
+checkObject(EvalError);
+checkObject(RangeError);
+checkObject(ReferenceError);
+checkObject(SyntaxError);
+checkObject(URIError);
+
+if (typeof WebAssembly !== 'undefined')
+{
+  checkObject(WebAssembly.CompileError);
+  checkObject(WebAssembly.LinkError);
+  checkObject(WebAssembly.RuntimeError);
+}
+
+print('pass');

+ 5 - 0
test/Error/rlexe.xml

@@ -178,4 +178,9 @@
       <compile-flags>-ExtendedErrorStackForTestHost -force:DeferParse</compile-flags>
     </default>
   </test>
+  <test>
+    <default>
+      <files>errorPrototypetoString.js</files>
+    </default>
+  </test>
 </regress-exe>