From 19cd94cab03604e2528cb444fe3e72003c6bbbeb Mon Sep 17 00:00:00 2001 From: ralphwetzel Date: Sat, 23 Jan 2021 19:53:45 +0100 Subject: [PATCH] Add 'subarray' and 'set' to TypedArray prototype --- js2py/prototypes/jstypedarray.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/js2py/prototypes/jstypedarray.py b/js2py/prototypes/jstypedarray.py index 43285aae..489de154 100644 --- a/js2py/prototypes/jstypedarray.py +++ b/js2py/prototypes/jstypedarray.py @@ -315,6 +315,25 @@ def reduceRight(callbackfn): k -= 1 return accumulator + def subarray(begin, end): + return slice(begin, end) + + def set(array, offset = 0): + offset = 0 if (offset.is_undefined() or offset.is_null()) else offset.to_int() + target = this.to_object() + target_len = target.get("length").to_uint32() + vals = to_arr(array) + vals_len = len(vals) + + if offset + vals_len > target_len: + raise this.MakeError( + 'RangeError', 'Trying to store values beyond target array length.') + + n = 0 + while n < vals_len: + this.put(str(n + offset), vals[n]) + n += 1 + def sort_compare(a, b, comp): if a is None: