[jsinterp] Handle NaN in bitwise operators

Closes #6131
This commit is contained in:
pukkandan 2023-05-20 02:57:59 +05:30
parent f7f7a877bf
commit 1d7656184c
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39
3 changed files with 20 additions and 1 deletions

View File

@ -445,6 +445,16 @@ class TestJSInterpreter(unittest.TestCase):
jsi = JSInterpreter('function x(){return 1236566549 << 5}')
self.assertEqual(jsi.call_function('x'), 915423904)
def test_bitwise_operators_typecast(self):
jsi = JSInterpreter('function x(){return null << 5}')
self.assertEqual(jsi.call_function('x'), 0)
jsi = JSInterpreter('function x(){return undefined >> 5}')
self.assertEqual(jsi.call_function('x'), 0)
jsi = JSInterpreter('function x(){return 42 << NaN}')
self.assertEqual(jsi.call_function('x'), 42)
def test_negative(self):
jsi = JSInterpreter("function f(){return 2 * -2.0;}")
self.assertEqual(jsi.call_function('f'), -4)

View File

@ -146,6 +146,10 @@ _NSIG_TESTS = [
'https://www.youtube.com/s/player/6f20102c/player_ias.vflset/en_US/base.js',
'lE8DhoDmKqnmJJ', 'pJTTX6XyJP2BYw',
),
(
'https://www.youtube.com/s/player/cfa9e7cb/player_ias.vflset/en_US/base.js',
'aCi3iElgd2kq0bxVbQ', 'QX1y8jGb2IbZ0w',
),
]

View File

@ -20,7 +20,12 @@ from .utils import (
def _js_bit_op(op):
def zeroise(x):
return 0 if x in (None, JS_Undefined) else x
if x in (None, JS_Undefined):
return 0
with contextlib.suppress(TypeError):
if math.isnan(x): # NB: NaN cannot be checked by membership
return 0
return x
def wrapped(a, b):
return op(zeroise(a), zeroise(b)) & 0xffffffff