Compare commits

..

2 Commits

Author SHA1 Message Date
dirkf
ee37ce225b x 2025-03-08 23:30:46 +00:00
dirkf
e8a0514f4f [JSInterp] Improve tests
* from yt-dlp/yt-dlp#12313
* also fix d7c2708
2025-03-08 23:16:11 +00:00
2 changed files with 7 additions and 5 deletions

View File

@ -212,9 +212,10 @@ class TestJSInterpreter(unittest.TestCase):
# undefined # undefined
self._test(jsi, NaN, args=[JS_Undefined]) self._test(jsi, NaN, args=[JS_Undefined])
# y,m,d, ... - may fail with older dates lacking DST data # y,m,d, ... - may fail with older dates lacking DST data
jsi = JSInterpreter('function f() { return new Date(%s); }' self.assertAlmostEqual(JSInterpreter(
% ('2024, 5, 29, 2, 52, 12, 42',)) 'function f() { return new Date(%s); }'
self._test(jsi, 1719625932042) % ('2024, 5, 29, 2, 52, 12, 42',)).call_function('f'),
1719625932042 + time.timezone * 1000, places=0)
# no arg # no arg
self.assertAlmostEqual(JSInterpreter( self.assertAlmostEqual(JSInterpreter(
'function f() { return new Date() - 0; }').call_function('f'), 'function f() { return new Date() - 0; }').call_function('f'),

View File

@ -154,6 +154,7 @@ def _js_to_primitive(v):
) )
# more exact: yt-dlp/yt-dlp#12110
def _js_toString(v): def _js_toString(v):
return ( return (
'undefined' if v is JS_Undefined 'undefined' if v is JS_Undefined
@ -162,7 +163,7 @@ def _js_toString(v):
else 'null' if v is None else 'null' if v is None
# bool <= int: do this first # bool <= int: do this first
else ('false', 'true')[v] if isinstance(v, bool) else ('false', 'true')[v] if isinstance(v, bool)
else re.sub(r'\.?0*$', '', '{0:.7f}'.format(v)) if isinstance(v, compat_numeric_types) else re.sub(r'(?<=\d)\.?0*$', '', '{0:.7f}'.format(v)) if isinstance(v, compat_numeric_types)
else _js_to_primitive(v)) else _js_to_primitive(v))
@ -546,7 +547,7 @@ class JSInterpreter(object):
return "Invalid Date" return "Invalid Date"
def valueOf(self): def valueOf(self):
return _NaN if self._t is None else int(self._t) return _NaN if self._t is None else self._t
@classmethod @classmethod
def __op_chars(cls): def __op_chars(cls):