Skip to content

Commit 7746e0c

Browse files
author
hartsantler
committed
tested JSArray and JSObject, improved direct calling of JavaScript functions and functions attached to JavaScript instances.
1 parent 03daebe commit 7746e0c

6 files changed

Lines changed: 207 additions & 11 deletions

File tree

pythonscript.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// PythonScript Runtime - regenerated on: Thu Oct 10 18:03:46 2013
1+
// PythonScript Runtime - regenerated on: Sat Oct 12 04:11:21 2013
22
var jsrange = function(num) {
33
"Emulates Python's range function";
44
var i, r;
@@ -69,6 +69,7 @@ return object;
6969
}
7070
window["__call__"] = __call__
7171

72+
__call__.pythonscript_function = true;
7273
klass.__call__ = __call__;
7374
return klass;
7475
}
@@ -146,9 +147,21 @@ return attr;
146147
}
147148

148149
if(attr) {
150+
if(typeof(attr) === 'function' && attr.pythonscript_function === undefined && attr.is_wrapper === undefined) {
151+
var wrapper = function(args, kwargs) {
152+
return attr.apply(object, args);
153+
}
154+
window["wrapper"] = wrapper
155+
156+
wrapper.is_wrapper = true;
157+
return wrapper;
158+
}
159+
else {
149160
return attr;
150161
}
151162

163+
}
164+
152165
var __class__, __dict__, __get__, bases;
153166
__class__ = object.__class__;
154167
if(__class__) {
@@ -289,6 +302,55 @@ i = backup;
289302

290303
}
291304

305+
if(object instanceof Array) {
306+
if(attribute == "__getitem__") {
307+
var wrapper = function(args, kwargs) {
308+
return object[args[0]];
309+
}
310+
window["wrapper"] = wrapper
311+
312+
wrapper.is_wrapper = true;
313+
return wrapper;
314+
}
315+
else {
316+
if(attribute == "__setitem__") {
317+
var wrapper = function(args, kwargs) {
318+
object[args[0]] = args[1];
319+
}
320+
window["wrapper"] = wrapper
321+
322+
wrapper.is_wrapper = true;
323+
return wrapper;
324+
}
325+
326+
}
327+
328+
}
329+
else {
330+
if(attribute == "__getitem__") {
331+
var wrapper = function(args, kwargs) {
332+
return object[args[0]];
333+
}
334+
window["wrapper"] = wrapper
335+
336+
wrapper.is_wrapper = true;
337+
return wrapper;
338+
}
339+
else {
340+
if(attribute == "__setitem__") {
341+
var wrapper = function(args, kwargs) {
342+
object[args[0]] = args[1];
343+
}
344+
window["wrapper"] = wrapper
345+
346+
wrapper.is_wrapper = true;
347+
return wrapper;
348+
}
349+
350+
}
351+
352+
}
353+
292354
return undefined;
293355
}
294356
window["get_attribute"] = get_attribute
@@ -343,6 +405,7 @@ object[attribute] = value;
343405
}
344406
window["set_attribute"] = set_attribute
345407

408+
set_attribute.pythonscript_function = true;
346409
var get_arguments = function(signature, args, kwargs) {
347410
"Based on ``signature`` and ``args``, ``kwargs`` parameters retrieve\n the actual parameters.\n\n This will set default keyword arguments and retrieve positional arguments\n in kwargs if their called as such";
348411
if(args === undefined) {
@@ -424,6 +487,7 @@ return out;
424487
}
425488
window["get_arguments"] = get_arguments
426489

490+
get_arguments.pythonscript_function = true;
427491
var type = function(args, kwargs) {
428492
var class_name, parents, attrs;
429493
class_name = args[0];
@@ -433,6 +497,7 @@ return create_class(class_name, parents, attrs);
433497
}
434498
window["type"] = type
435499

500+
type.pythonscript_function = true;
436501
var getattr = function(args, kwargs) {
437502
var object, attribute;
438503
object = args[0];

runtime/pythonpythonjs.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __call__():
5858
if init:
5959
init.apply(None, arguments)
6060
return object
61+
__call__.pythonscript_function = True
6162
klass.__call__ = __call__
6263
return klass
6364

@@ -89,7 +90,6 @@ def get_attribute(object, attribute):
8990
if cached:
9091
return cached
9192
else:
92-
#print 'generating new wrapper'
9393
def wrapper(args,kwargs): return object.apply(None, args)
9494
wrapper.is_wrapper = True
9595
object.cached_wrapper = wrapper
@@ -116,7 +116,16 @@ def wrapper(args,kwargs): return attr.apply(object, args)
116116
return attr
117117

118118
if attr: ## what about cases where attr is zero?
119-
return attr
119+
if JS("typeof(attr) === 'function' && attr.pythonscript_function === undefined && attr.is_wrapper === undefined"):
120+
## to avoid problems with other generated wrapper funcs not marked with:
121+
## F.pythonscript_function or F.is_wrapper, we could check if object has these props:
122+
## bases, __name__, __dict__, __call__
123+
#print 'wrapping something external', object, attribute
124+
def wrapper(args,kwargs): return attr.apply(object, args)
125+
wrapper.is_wrapper = True
126+
return wrapper
127+
else:
128+
return attr
120129

121130
var(__class__, __dict__, __get__, bases)
122131

@@ -199,6 +208,26 @@ def method():
199208
return method
200209
else:
201210
return attr
211+
212+
if JS('object instanceof Array'):
213+
if attribute == '__getitem__':
214+
def wrapper(args,kwargs): return object[ args[0] ]
215+
wrapper.is_wrapper = True
216+
return wrapper
217+
elif attribute == '__setitem__':
218+
def wrapper(args,kwargs): object[ args[0] ] = args[1]
219+
wrapper.is_wrapper = True
220+
return wrapper
221+
222+
elif attribute == '__getitem__': ## this should be a JSObject - or anything else - is this always safe?
223+
def wrapper(args,kwargs): return object[ args[0] ]
224+
wrapper.is_wrapper = True
225+
return wrapper
226+
elif attribute == '__setitem__':
227+
def wrapper(args,kwargs): object[ args[0] ] = args[1]
228+
wrapper.is_wrapper = True
229+
return wrapper
230+
202231
return None # XXX: raise AttributeError instead
203232

204233

@@ -230,6 +259,7 @@ def set_attribute(object, attribute, value):
230259
__dict__[attribute] = value
231260
else:
232261
object[attribute] = value
262+
set_attribute.pythonscript_function = True
233263

234264

235265
def get_arguments(signature, args, kwargs):
@@ -281,15 +311,15 @@ def get_arguments(signature, args, kwargs):
281311
if signature.varkwarg:
282312
out[signature.varkwarg] = kwargs
283313
return out
284-
314+
get_arguments.pythonscript_function = True
285315

286316
def type(args, kwargs):
287317
var(class_name, parents, attrs)
288318
class_name = args[0]
289319
parents = args[1]
290320
attrs = args[2]
291321
return create_class(class_name, parents, attrs)
292-
322+
type.pythonscript_function = True
293323

294324
def getattr(args, kwargs):
295325
var(object, attribute)

tests/test_JSArray.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<html>
2+
<head>
3+
<script src="pythonscript.js"></script>
4+
5+
<script type="text/python">
6+
print 'starting test...'
7+
a = 'hello'
8+
b = 'world'
9+
print a, b
10+
lst = [1,2,3, a, b]
11+
print 'list created', lst
12+
arr1 = lst.js_object
13+
print 'got pointer to lst array'
14+
arr2 = JSArray()
15+
print 'new JSArray'
16+
print 'arr1', arr1
17+
print 'arr2', arr2
18+
19+
jsob = JSObject()
20+
print 'jsob', jsob
21+
22+
i = 0
23+
while i < arr1.length:
24+
arr1[i] = i * 100
25+
arr2[i] = i * 200
26+
arr2.push( lst[i] )
27+
print arr2[i], arr2[ i+1 ]
28+
29+
jsob[ 'mykey'+i ] = True
30+
i += 1
31+
32+
print lst.js_object
33+
print arr1
34+
print arr2
35+
print 'checking JSObject'
36+
print jsob
37+
38+
</script>
39+
40+
</head>
41+
<body>
42+
<button id="mybutton" onclick="test()">click me</button>
43+
</body>
44+
</html>

tests/test_array.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@
4747
print value
4848

4949
print( '--testing float8--' )
50-
d = array('float8', [0.12345, 0.5554, 0.99998, 500.88283, -1001.88716])
51-
for value in d:
52-
print value
50+
d = array('float8', [-10.0, 3.141592653589793, 10.0])
51+
print 'error:', d[1] - 3.141592653589793
52+
53+
d = array('float8', [-50.0, 3.141592653589793, 50.0])
54+
print 'error:', d[1] - 3.141592653589793
55+
print d[1]
5356

5457
print( '--testing float16--' )
55-
e = array('float16', [0.12345, 0.5554, 0.99998, 500.88283, -1001.88716])
56-
for value in e:
57-
print value
58+
e = array('float16', [-10.0, 3.141592653589793, 10.0])
59+
print 'error:', e[1] - 3.141592653589793
5860

5961

6062
</script>

tests/test_json.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<html>
2+
<head>
3+
<script src="pythonscript.js"></script>
4+
5+
<script type="text/python">
6+
7+
a = 'hello'
8+
b = 'world'
9+
lst = [1,2,3]
10+
11+
print JSON.stringify( a )
12+
print JSON.stringify( b )
13+
print JSON.stringify( lst )
14+
print JSON.stringify( lst.js_object )
15+
16+
i = 0
17+
while i < lst.js_object.length:
18+
print lst.js_object[i]
19+
lst.js_object[i] = i * 100
20+
i += 1
21+
print lst.js_object
22+
23+
print 'test complete'
24+
25+
</script>
26+
27+
</head>
28+
<body>
29+
<button id="mybutton" onclick="test()">click me</button>
30+
</body>
31+
</html>

tests/test_min_max.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 = [1,2,100, 3,4]
11+
print 'min:', min(a)
12+
print 'max:', max(a)
13+
14+
b = (-100, 1, 2, 3, 6, 50, 100, 0, 9)
15+
print 'min:', min(b)
16+
print 'max:', max(b)
17+
18+
</script>
19+
</head>
20+
21+
<body>
22+
<button onclick="test()">click me</button>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)