1# this is based on jsarray.py 2 3from ..base import * 4try: 5 import numpy 6except: 7 pass 8 9 10@Js 11def Int8Array(): 12 TypedArray = (PyJsInt8Array, PyJsUint8Array, PyJsUint8ClampedArray, 13 PyJsInt16Array, PyJsUint16Array, PyJsInt32Array, 14 PyJsUint32Array, PyJsFloat32Array, PyJsFloat64Array) 15 a = arguments[0] 16 if isinstance(a, PyJsNumber): # length 17 length = a.to_uint32() 18 if length != a.value: 19 raise MakeError('RangeError', 'Invalid array length') 20 temp = Js(numpy.full(length, 0, dtype=numpy.int8)) 21 temp.put('length', a) 22 return temp 23 elif isinstance(a, PyJsString): # object (string) 24 temp = Js(numpy.array(list(a.value), dtype=numpy.int8)) 25 temp.put('length', Js(len(list(a.value)))) 26 return temp 27 elif isinstance(a, PyJsArray) or isinstance(a, TypedArray) or isinstance( 28 a, PyJsArrayBuffer): # object (Array, TypedArray) 29 array = a.to_list() 30 array = [(int(item.value) if item.value != None else 0) 31 for item in array] 32 temp = Js(numpy.array(array, dtype=numpy.int8)) 33 temp.put('length', Js(len(array))) 34 return temp 35 elif isinstance(a, PyObjectWrapper): # object (ArrayBuffer, etc) 36 if len(arguments) > 1: 37 offset = int(arguments[1].value) 38 else: 39 offset = 0 40 if len(arguments) > 2: 41 length = int(arguments[2].value) 42 else: 43 length = int(len(a.obj) - offset) 44 array = numpy.frombuffer( 45 a.obj, dtype=numpy.int8, count=length, offset=offset) 46 temp = Js(array) 47 temp.put('length', Js(length)) 48 temp.buff = array 49 return temp 50 temp = Js(numpy.full(0, 0, dtype=numpy.int8)) 51 temp.put('length', Js(0)) 52 return temp 53 54 55Int8Array.create = Int8Array 56Int8Array.own['length']['value'] = Js(3) 57 58Int8Array.define_own_property( 59 'prototype', { 60 'value': Int8ArrayPrototype, 61 'enumerable': False, 62 'writable': False, 63 'configurable': False 64 }) 65 66Int8ArrayPrototype.define_own_property( 67 'constructor', { 68 'value': Int8Array, 69 'enumerable': False, 70 'writable': True, 71 'configurable': True 72 }) 73 74Int8ArrayPrototype.define_own_property('BYTES_PER_ELEMENT', { 75 'value': Js(1), 76 'enumerable': False, 77 'writable': False, 78 'configurable': False 79}) 80