1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2026-02-20 23:45:47 +00:00

[jsinterp] Fix bitwise operations (#15985)

Authored by: bashonly
This commit is contained in:
bashonly
2026-02-17 17:10:18 -06:00
committed by GitHub
parent abade83f8d
commit 62574f5763
3 changed files with 62 additions and 36 deletions

View File

@@ -9,7 +9,12 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import math
from yt_dlp.jsinterp import JS_Undefined, JSInterpreter, js_number_to_string
from yt_dlp.jsinterp import (
JS_Undefined,
JSInterpreter,
int_to_int32,
js_number_to_string,
)
class NaN:
@@ -101,8 +106,8 @@ class TestJSInterpreter(unittest.TestCase):
self._test('function f(){return 5 ^ 9;}', 12)
self._test('function f(){return 0.0 << NaN}', 0)
self._test('function f(){return null << undefined}', 0)
# TODO: Does not work due to number too large
# self._test('function f(){return 21 << 4294967297}', 42)
self._test('function f(){return -12616 ^ 5041}', -8951)
self._test('function f(){return 21 << 4294967297}', 42)
def test_array_access(self):
self._test('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}', [5, 2, 7])
@@ -447,6 +452,22 @@ class TestJSInterpreter(unittest.TestCase):
def test_splice(self):
self._test('function f(){var T = ["0", "1", "2"]; T["splice"](2, 1, "0")[0]; return T }', ['0', '1', '0'])
def test_int_to_int32(self):
for inp, exp in [
(0, 0),
(1, 1),
(-1, -1),
(-8951, -8951),
(2147483647, 2147483647),
(2147483648, -2147483648),
(2147483649, -2147483647),
(-2147483649, 2147483647),
(-2147483648, -2147483648),
(-16799986688, 379882496),
(39570129568, 915423904),
]:
assert int_to_int32(inp) == exp
def test_js_number_to_string(self):
for test, radix, expected in [
(0, None, '0'),