chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.12k stars 1.2k forks source link

A question about RegExp.prototype.toString #6569

Closed YiWen-y closed 3 years ago

YiWen-y commented 3 years ago
Version

chakra-9e2f198

Test case
var a = RegExp(1);
RegExp.prototype.toString.call(a);
var b = Object(1);
RegExp.prototype.toString.call(b);
Execution steps
./ChakraCore/out/Release/ch testcase.js
Output
TypeError: RegExp.prototype.toString: 'this' is not a RegExp object
Expected behavior
Description

I checked the ES standard and found that the standard only stipulates that the this value of RegExp.prototype.toString must be an object, but chakra requires this value to be a regular object. May I know what is the reason?

According to ES2019 standard, the steps of RegExp.prototype.toString are as follows.

21.2.5.15RegExp.prototype.toString ( )

  1. Let R be the this value.
  2. If Type(R) is not Object, throw a TypeError exception.
  3. Let pattern be ? ToString(? Get(R, "source")).
  4. Let flags be ? ToString(? Get(R, "flags")).
  5. Let result be the string-concatenation of "/", pattern, "/", and flags.
  6. Return result.

The ECMAScript standard reference is as follow:

http://www.ecma-international.org/ecma-262/10.0/index.html#sec-regexp.prototype.tostring

ljharb commented 3 years ago

The source and flags getters both throw if the receiver is not a regex, which covers your a.

what happens with b if you comment out a?

YiWen-y commented 3 years ago

When executing the above test case, Chakra throws TypeError: RegExp.prototype.toString:'this' is not a RegExp object in line 4 (b). After commenting out a, executing this code still throws TypeError. I also executed this test cases on other engines (such as v8, spider Monkey, JavascriptCore, etc.), and they all executed normally without causing any errors.

Fly-Style commented 3 years ago

Thank you for the report!

rhuanjl commented 3 years ago

The correct implementation is behind the flag ES6RegExPrototypeProperties if you run the test with -ES6RegExPrototypeProperties to enable the flag it should have the correct result.

Enabling that flag is part of #6390 I'm afraid ChakraCore's regexp engine is a weak point of sorts.