mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-12-02 19:36:45 +00:00
[jsinterp] Add charcodeAt
and bitwise overflow (#4706)
Authored by: elyse0
This commit is contained in:
parent
bfbecd1174
commit
f26af78a8a
|
@ -352,6 +352,22 @@ def test_regex(self):
|
||||||
''')
|
''')
|
||||||
self.assertEqual(jsi.call_function('x').flags & re.I, re.I)
|
self.assertEqual(jsi.call_function('x').flags & re.I, re.I)
|
||||||
|
|
||||||
|
def test_char_code_at(self):
|
||||||
|
jsi = JSInterpreter('function x(i){return "test".charCodeAt(i)}')
|
||||||
|
self.assertEqual(jsi.call_function('x', 0), 116)
|
||||||
|
self.assertEqual(jsi.call_function('x', 1), 101)
|
||||||
|
self.assertEqual(jsi.call_function('x', 2), 115)
|
||||||
|
self.assertEqual(jsi.call_function('x', 3), 116)
|
||||||
|
self.assertEqual(jsi.call_function('x', 4), None)
|
||||||
|
self.assertEqual(jsi.call_function('x', 'not_a_number'), 116)
|
||||||
|
|
||||||
|
def test_bitwise_operators_overflow(self):
|
||||||
|
jsi = JSInterpreter('function x(){return -524999584 << 5}')
|
||||||
|
self.assertEqual(jsi.call_function('x'), 379882496)
|
||||||
|
|
||||||
|
jsi = JSInterpreter('function x(){return 1236566549 << 5}')
|
||||||
|
self.assertEqual(jsi.call_function('x'), 915423904)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -18,10 +18,11 @@
|
||||||
|
|
||||||
|
|
||||||
def _js_bit_op(op):
|
def _js_bit_op(op):
|
||||||
|
def zeroise(x):
|
||||||
|
return 0 if x in (None, JS_Undefined) else x
|
||||||
|
|
||||||
def wrapped(a, b):
|
def wrapped(a, b):
|
||||||
def zeroise(x):
|
return op(zeroise(a), zeroise(b)) & 0xffffffff
|
||||||
return 0 if x in (None, JS_Undefined) else x
|
|
||||||
return op(zeroise(a), zeroise(b))
|
|
||||||
|
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
|
@ -692,6 +693,13 @@ def eval_method():
|
||||||
return obj.index(idx, start)
|
return obj.index(idx, start)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return -1
|
return -1
|
||||||
|
elif member == 'charCodeAt':
|
||||||
|
assertion(isinstance(obj, str), 'must be applied on a string')
|
||||||
|
assertion(len(argvals) == 1, 'takes exactly one argument')
|
||||||
|
idx = argvals[0] if isinstance(argvals[0], int) else 0
|
||||||
|
if idx >= len(obj):
|
||||||
|
return None
|
||||||
|
return ord(obj[idx])
|
||||||
|
|
||||||
idx = int(member) if isinstance(obj, list) else member
|
idx = int(member) if isinstance(obj, list) else member
|
||||||
return obj[idx](argvals, allow_recursion=allow_recursion)
|
return obj[idx](argvals, allow_recursion=allow_recursion)
|
||||||
|
|
Loading…
Reference in a new issue