Error in V8 JavaScript engine when beginning of line matches?

I have a pretty great tool, underscore-cli , which gets the weirdest behavior when printing help / usage information.

In the usage () function, I do this to indent text blocks (e.g. parameters):

str.replace(/^/, " "); 

This regular expression, in addition to being pretty obvious, comes straight from the TJ Hollowaychuk commander.js code. The correct regular expression.

However, I get bizzare spaces inserted in the middle of my usage text. eg:

  Commands:
 ...
      values ​​Retrieve all the values ​​of an object properties.
      extend & ltobject> Override properties in the input data.
      defaults & ltobject> Fill in missing properties in the input data.
      any & ltexp> Return 'true' if any of the values ​​in the input make the expression true.  Expression args: (value, key, list)
          all & ltexp> Return 'true' if all values ​​in the input make the expression true.  Expression args: (value, key, list)
      isObject Return 'true' if the input data is an object with named properties
      isArray Return 'true' if the input data is an array
      isString Return 'true' if the input data is a string
 ...

The probability is 99%, this error should be a mistake in V8.

Does anyone know why this is happening, or what could be easiest?

Yup, it turns out that this is IS error V8, 1748, to be exact. Here's the workaround I used in the tool :

 str.replace(/(^|\n), "$1 "); 
+6
source share
2 answers

This is a bug in V8 (bug 1748):

http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/regress/regress-1748.js?spec=svn9504&r=9504

Here is the error test:

 function assertEquals(a, b, msg) { if(a !== b) { console.log("'%s' != '%s' %s", a, b, msg); } } var str = Array(10000).join("X"); str.replace(/^|X/g, function(m, i, s) { if (i > 0) assertEquals("X", m, "at position 0x" + i.toString(16)); }); 

In my window, it prints:

  'X'! = ''.  at position 0x100
 'X'! = ''.  at position 0x200
 'X'! = ''.  at position 0x300
 'X'! = ''.  at position 0x400
 'X'! = ''.  at position 0x500
 'X'! = ''.  at position 0x600
 ...

It does not print anything in jsfiddle (the V8 version in the Chrome browser has no error):

http://jsfiddle.net/PqDHk/


Error History:

From V8 changelog, the bug was fixed in V8-3.6.5 (2011-10-05).

From Node.js changelog , Node -0.6.5 should use V8-3.6.6.11!? !!?. Node.js was upgraded from V8-3.6.4 to V8-3.7.0 (Node -0.5.10), and then downgraded to V8-3.6.6 for Node -0.6.0. Theoretically, this error should be fixed before Node V0.6.0. Why is it still playing on Node -0.6.5 ??? Odd

Can someone with the latter (Node -0.6.15) run the test fragment above and report if it generates errors? Or I will eventually get along.

Thanks to ZachB for confirming this error on Node -0.6.15. I registered a problem ( issue # 3168 ) against node, and the fix ( 5d69bbf ) should be included in Node -0.6.16. :) :) :)

Until then, the workaround is to replace:

 str.replace(/^/, indent); 

FROM

 str.replace(/(^|\n)/, "$1" + indent); 

UPDATE: just for a giggle, I checked this on the current version of Node v0.8.1 and confirmed that the error had indeed been fixed. I did not bother to go back and confirm whether the error in 0.6.16 was fixed or once between them and next to v0.8.X.

+4
source

Workaround: grab the first character and replace it with spaces and itself

 str.replace(/^./, " $1"); 

or to make sure the row is no longer pending

 str.replace(/^[^\s]/, " $1"); 
+1
source

Source: https://habr.com/ru/post/913176/


All Articles