Skip to content

Commit c58b2e6

Browse files
author
hartsantler
committed
core: made jsify recursive
1 parent e5a668f commit c58b2e6

3 files changed

Lines changed: 77 additions & 4 deletions

File tree

pythonjs.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// PythonJS Runtime - regenerated on: Sun Dec 15 19:06:10 2013
1+
// PythonJS Runtime - regenerated on: Mon Dec 16 20:15:19 2013
22
__NULL_OBJECT__ = Object.create(null);
33
if (( "window" ) in this && ( "document" ) in this) {
44
__NODEJS__ = false;
@@ -1007,6 +1007,26 @@ _setup_str_prototype.types_signature = { };
10071007
_setup_str_prototype.pythonscript_function = true;
10081008
_setup_str_prototype();
10091009
_setup_array_prototype = function(args, kwargs) {
1010+
var func = function() {
1011+
var i, item;
1012+
i = 0;
1013+
while(( i ) < this.length) {
1014+
item = this[i];
1015+
if (( typeof(item) ) == "object") {
1016+
if (item.jsify) {
1017+
this[i] = item.jsify();
1018+
}
1019+
}
1020+
i += 1;
1021+
}
1022+
return this;
1023+
}
1024+
1025+
func.NAME = "func";
1026+
func.args_signature = [];
1027+
func.kwargs_signature = { };
1028+
func.types_signature = { };
1029+
Array.prototype.jsify = func;
10101030
var func = function(a) {
10111031
if (( this.indexOf(a) ) == -1) {
10121032
return false;
@@ -2414,7 +2434,6 @@ var dict, __dict_attrs, __dict_parents;
24142434
__dict_attrs = Object();
24152435
__dict_parents = [];
24162436
__dict_properties = Object();
2417-
__dict_parents.push(jsifyable);
24182437
__dict_UID = 0;
24192438
__dict_attrs["UID"] = __dict_UID;
24202439
__dict___init__ = function(args, kwargs) {
@@ -2463,6 +2482,41 @@ __dict___init__.kwargs_signature = { js_object:undefined };
24632482
__dict___init__.types_signature = { js_object:"None" };
24642483
__dict___init__.pythonscript_function = true;
24652484
__dict_attrs["__init__"] = __dict___init__;
2485+
__dict_jsify = function(args, kwargs) {
2486+
var keys, value;
2487+
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {
2488+
/*pass*/
2489+
} else {
2490+
args = Array.prototype.slice.call(arguments);
2491+
kwargs = Object();
2492+
}
2493+
var signature, arguments;
2494+
signature = {"kwargs": Object(), "args": __create_array__("self")};
2495+
arguments = get_arguments(signature, args, kwargs);
2496+
var self = arguments['self'];
2497+
keys = __get__(__get__(Object, "keys"), "__call__")([self["$wrapped"]], __NULL_OBJECT__);
2498+
var __iterator__, key;
2499+
__iterator__ = __get__(__get__(keys, "__iter__"), "__call__")([], Object());
2500+
var __next__;
2501+
__next__ = __get__(__iterator__, "next_fast");
2502+
while(( __iterator__.index ) < __iterator__.length) {
2503+
key = __next__();
2504+
value = __get__(self["$wrapped"], "__getitem__")([key], Object());
2505+
if (( typeof(value) ) == "object") {
2506+
if (__get__(value, "jsify")) {
2507+
__get__(__get__(self["$wrapped"], "__setitem__"), "__call__")([key, __get__(__get__(value, "jsify"), "__call__")()], Object());
2508+
}
2509+
}
2510+
}
2511+
return self["$wrapped"];
2512+
}
2513+
2514+
__dict_jsify.NAME = "__dict_jsify";
2515+
__dict_jsify.args_signature = ["self"];
2516+
__dict_jsify.kwargs_signature = { };
2517+
__dict_jsify.types_signature = { };
2518+
__dict_jsify.pythonscript_function = true;
2519+
__dict_attrs["jsify"] = __dict_jsify;
24662520
__dict_get = function(args, kwargs) {
24672521
var __dict;
24682522
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {

runtime/builtins.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ def func(encoding):
329329
def _setup_array_prototype():
330330

331331
with javascript:
332+
@Array.prototype.jsify
333+
def func():
334+
i = 0
335+
while i < this.length:
336+
item = this[ i ]
337+
if typeof(item) == 'object':
338+
if item.jsify:
339+
this[ i ] = item.jsify()
340+
i += 1
341+
return this
332342

333343
@Array.prototype.__contains__
334344
def func(a):
@@ -731,7 +741,7 @@ def __contains__(self, value):
731741
class jsifyable:
732742
def jsify(self): return self[...]
733743

734-
class dict( jsifyable ):
744+
class dict:
735745
# http://stackoverflow.com/questions/10892322/javascript-hashtable-use-object-key
736746
# using a function as a key is allowed, but would waste memory because it gets converted to a string
737747
# http://stackoverflow.com/questions/10858632/are-functions-valid-keys-for-javascript-object-properties
@@ -758,6 +768,15 @@ def __init__(self, js_object=None):
758768
else: ## TODO - deprecate
759769
self[...] = js_object
760770

771+
def jsify(self):
772+
keys = Object.keys( self[...] )
773+
for key in keys:
774+
value = self[...][key]
775+
if typeof(value) == 'object':
776+
if value.jsify:
777+
self[...][key] = value.jsify()
778+
return self[...]
779+
761780
def get(self, key, _default=None):
762781
__dict = self[...]
763782
if JS("typeof(key) === 'object'"):

tests/test_calling_javascript_keyword_args.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
def test():
2020
global c
21-
c = ['hello', 'world'] ## automatically converted to Array when passed to js_function
21+
c = ['hello', 'world', {'xxx':99}, {'yyy':[1.1, {'sub':'AAA'}]}]
2222
print js_function( 'A','B',c, x=1, y=2, z=3 )
2323

2424
</script>

0 commit comments

Comments
 (0)