Skip to content

Commit 115cdce

Browse files
author
hartsantler
committed
added array class that wraps ArrayBuffer and DataView,
added test_array.html
1 parent 7316b4d commit 115cdce

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

runtime/builtins.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def next(self):
4848
self.index = self.index + 1
4949
return item
5050

51+
52+
5153
class tuple:
5254
def __init__(self, js_object=None):
5355
self.js_object = JSArray()
@@ -288,3 +290,104 @@ def __init__(self, jsstring):
288290

289291
def __iter__(self):
290292
return Iterator(self.jsstring, 0)
293+
294+
295+
class array:
296+
## note that class-level dicts can only be used after the dict class has been defined above
297+
typecodes = {
298+
'c': 1, # char
299+
'b': 1, # signed char
300+
'B': 1, # unsigned char
301+
'u': 2, # unicode
302+
'h': 2, # signed short
303+
'H': 2, # unsigned short
304+
'i': 4, # signed int
305+
'I': 4, # unsigned int
306+
'l': 4, # signed long
307+
'L': 4, # unsigned long
308+
'f': 4, # float
309+
'd': 8, # double
310+
}
311+
typecode_names = {
312+
'c': 'Int8',
313+
'b': 'Int8',
314+
'B': 'Uint8',
315+
'u': 'Uint16',
316+
'h': 'Int16',
317+
'H': 'Uint16',
318+
'i': 'Int32',
319+
'I': 'Uint32',
320+
#'l': 'TODO',
321+
#'L': 'TODO',
322+
'f': 'Float32',
323+
'd': 'Float64'
324+
}
325+
def __init__(self, typecode, initializer=None, little_endian=False):
326+
self.typecode = typecode
327+
self.little_endian = little_endian
328+
size = 0
329+
if initializer:
330+
length = len(initializer)
331+
print 'array.initalizer length', length
332+
print 'array.typecode', typecode
333+
print 'array.type size', self.typecodes[ typecode ]
334+
size = length * self.typecodes[ typecode ]
335+
else: size = 0
336+
self.size = size
337+
print 'array.init bytes', size
338+
buff = JS('new ArrayBuffer(size)')
339+
self.dataview = JS('new DataView(buff)')
340+
self.buffer = buff
341+
self.fromlist( initializer )
342+
343+
def fromlist(self, lst):
344+
print 'array.fromlist->', lst
345+
length = len(lst)
346+
step = self.typecodes[ self.typecode ]
347+
size = length * step
348+
349+
dataview = self.dataview
350+
func_name = 'set'+self.typecode_names[ self.typecode ]
351+
print 'func name->', func_name
352+
func = JS('dataview[func_name].bind(dataview)')
353+
print 'func->', func
354+
if size <= self.size:
355+
i = 0; offset = 0
356+
while i < length:
357+
item = lst[i]
358+
print ' item->', item
359+
print ' index', i
360+
print ' offset', offset
361+
#JS('func.apply(dataview, [offset, item])')
362+
#JS('func.call(dataview, offset, item)')
363+
JS('func(offset,item)')
364+
offset += step
365+
i += 1
366+
else:
367+
raise TypeError
368+
369+
def __getitem__(self, index):
370+
step = self.typecodes[ self.typecode ]
371+
offset = step * index
372+
373+
dataview = self.dataview
374+
func_name = 'get'+self.typecode_names[ self.typecode ]
375+
func = JS('dataview[func_name].bind(dataview)')
376+
377+
if offset < self.size:
378+
return JS('func(offset)')
379+
else:
380+
raise IndexError
381+
382+
def __setitem__(self, index, value):
383+
step = self.typecodes[ self.typecode ]
384+
offset = step * index
385+
386+
dataview = self.dataview
387+
func_name = 'set'+self.typecode_names[ self.typecode ]
388+
func = JS('dataview[func_name].bind(dataview)')
389+
390+
if offset < self.size:
391+
JS('func(offset, value)')
392+
else:
393+
raise IndexError

tests/test_array.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<script src="pythonscript.js"></script>
4+
5+
<script type="text/python" closure="false">
6+
7+
8+
def test():
9+
global a
10+
a = array('i', [1,2,3,4] )
11+
print(a.typecode)
12+
print(a.buffer)
13+
14+
print a[0]
15+
print a[1]
16+
print a[2]
17+
print a[3]
18+
19+
a[0] = 1000
20+
print a[0]
21+
22+
</script>
23+
</head>
24+
25+
<body>
26+
<button onclick="test()">click me</button>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)