Chakracore: A question about RegExp.prototype.toString

Created on 9 Jan 2021  路  4Comments  路  Source: chakra-core/ChakraCore

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

Bug Duplicate

Most helpful comment

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.

All 4 comments

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?

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.

Thank you for the report!

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

crdumoul picture crdumoul  路  4Comments

d3x0r picture d3x0r  路  5Comments

basdl picture basdl  路  3Comments

Kureev picture Kureev  路  5Comments

gustavopinto picture gustavopinto  路  3Comments