aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2025-04-04 12:15:38 +0100
committerdirkf <fieldhouse@gmx.net>2025-04-08 01:59:00 +0100
commit75134137947d1cd58b73f4186b3693d032c5bb66 (patch)
tree583a5c3785cd307e09fbfe1b29218637db80c574
parent67dbfa65f220e358de713977bed8d818059f7f27 (diff)
[JSInterp] Reorganise some declarations to align better with yt-dlp
-rw-r--r--test/test_jsinterp.py39
-rw-r--r--youtube_dl/jsinterp.py22
2 files changed, 31 insertions, 30 deletions
diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py
index 3c9650ab6..b0ac3a704 100644
--- a/test/test_jsinterp.py
+++ b/test/test_jsinterp.py
@@ -7,6 +7,7 @@ from __future__ import unicode_literals
import os
import sys
import unittest
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import math
@@ -146,6 +147,25 @@ class TestJSInterpreter(unittest.TestCase):
# https://github.com/ytdl-org/youtube-dl/issues/32815
self._test('function f(){return 0 - 7 * - 6;}', 42)
+ def test_bitwise_operators_typecast(self):
+ # madness
+ self._test('function f(){return null << 5}', 0)
+ self._test('function f(){return undefined >> 5}', 0)
+ self._test('function f(){return 42 << NaN}', 42)
+ self._test('function f(){return 42 << Infinity}', 42)
+ self._test('function f(){return 0.0 << null}', 0)
+ self._test('function f(){return NaN << 42}', 0)
+ self._test('function f(){return "21.9" << 1}', 42)
+ self._test('function f(){return true << "5";}', 32)
+ self._test('function f(){return true << true;}', 2)
+ self._test('function f(){return "19" & "21.9";}', 17)
+ self._test('function f(){return "19" & false;}', 0)
+ self._test('function f(){return "11.0" >> "2.1";}', 2)
+ 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)
+ 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])
@@ -482,25 +502,6 @@ class TestJSInterpreter(unittest.TestCase):
self._test('function f(){return -524999584 << 5}', 379882496)
self._test('function f(){return 1236566549 << 5}', 915423904)
- def test_bitwise_operators_typecast(self):
- # madness
- self._test('function f(){return null << 5}', 0)
- self._test('function f(){return undefined >> 5}', 0)
- self._test('function f(){return 42 << NaN}', 42)
- self._test('function f(){return 42 << Infinity}', 42)
- self._test('function f(){return 0.0 << null}', 0)
- self._test('function f(){return NaN << 42}', 0)
- self._test('function f(){return "21.9" << 1}', 42)
- self._test('function f(){return 21 << 4294967297}', 42)
- self._test('function f(){return true << "5";}', 32)
- self._test('function f(){return true << true;}', 2)
- self._test('function f(){return "19" & "21.9";}', 17)
- self._test('function f(){return "19" & false;}', 0)
- self._test('function f(){return "11.0" >> "2.1";}', 2)
- 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)
-
def test_negative(self):
self._test('function f(){return 2 * -2.0 ;}', -4)
self._test('function f(){return 2 - - -2 ;}', 0)
diff --git a/youtube_dl/jsinterp.py b/youtube_dl/jsinterp.py
index 69c8f77ca..971387df2 100644
--- a/youtube_dl/jsinterp.py
+++ b/youtube_dl/jsinterp.py
@@ -283,17 +283,6 @@ _OPERATORS = (
('**', _js_exp),
)
-_COMP_OPERATORS = (
- ('===', _js_id_op(operator.is_)),
- ('!==', _js_id_op(operator.is_not)),
- ('==', _js_eq),
- ('!=', _js_neq),
- ('<=', _js_comp_op(operator.le)),
- ('>=', _js_comp_op(operator.ge)),
- ('<', _js_comp_op(operator.lt)),
- ('>', _js_comp_op(operator.gt)),
-)
-
_LOG_OPERATORS = (
('|', _js_bit_op(operator.or_)),
('^', _js_bit_op(operator.xor)),
@@ -314,6 +303,17 @@ _UNARY_OPERATORS_X = (
_OPERATOR_RE = '|'.join(map(lambda x: re.escape(x[0]), _OPERATORS + _LOG_OPERATORS))
+_COMP_OPERATORS = (
+ ('===', _js_id_op(operator.is_)),
+ ('!==', _js_id_op(operator.is_not)),
+ ('==', _js_eq),
+ ('!=', _js_neq),
+ ('<=', _js_comp_op(operator.le)),
+ ('>=', _js_comp_op(operator.ge)),
+ ('<', _js_comp_op(operator.lt)),
+ ('>', _js_comp_op(operator.gt)),
+)
+
_NAME_RE = r'[a-zA-Z_$][\w$]*'
_MATCHING_PARENS = dict(zip(*zip('()', '{}', '[]')))
_QUOTES = '\'"/'