@@ -16,11 +16,6 @@ def compute_for_maker(Job):
1616 def compute_for (* args , ** kwargs ):
1717 args = list (args )
1818
19- for i , arg in enumerate (args ):
20- if isinstance (arg , FunctionType ):
21- args [i ] = dill .source .getsource (arg )
22-
23-
2419 # Hide values from PythonMonkey which aren't supported
2520 # TODO: This is bad for a number of reasons:
2621 ####################################################
@@ -32,34 +27,63 @@ def compute_for(*args, **kwargs):
3227 job_input_idx = None
3328 job_args_idx = None
3429
35- # compute.for(start, end, step, work, args)
36- if len (args ) == 5 :
37- job_args_idx = 4
38-
39- # compute.for(iterableObject, work, args)
40- elif len (args ) <= 3 :
41- job_input_idx = 0
42-
43- if len (args ) == 3 :
44- job_args_idx = 2
45-
46- # clean up job input for PythonMonkey
30+ for i , arg in enumerate (args ):
31+ if isinstance (arg , FunctionType ) or isinstance (arg , str ):
32+ # work function arg separates input from arguments, find indices to hide based on it
33+ if i == 1 : # compute.for(iterableObject, work, args), need to wrap iterable
34+ job_input_idx = 0
35+ if i < len (args ) - 1 : # work function isn't last argument, so last value is args in compute.for
36+ job_args_idx = len (args ) - 1
37+ if isinstance (arg , FunctionType ):
38+ args [i ] = dill .source .getsource (arg )
39+
40+ # Process for ensuring symbols aren't mutated in the python -> js layer:
41+ # 1. Check if symbol is coming from a dcp module/class. If so, set it as the js_ref. Skip next steps.
42+ # 2. Determine if input array can be mutated, or create new array for input set
43+ # 3. For each input element, dereference js_ref if from dcp-client, add a guard if pythonmonkey will mutate it, else as it as-is.
4744 if job_input_idx != None :
48- if js .utils .instanceof (getattr (args [job_input_idx ], "js_ref" , None ), pm .eval ("globalThis.dcp.compute.RemoteDataSet" )):
49- args [job_input_idx ] = args [job_input_idx ].js_ref
50- elif hasattr (args [job_input_idx ], '__setitem__' ):
51- for i , val in enumerate (args [job_input_idx ]): #TODO don't enumerate each time... perhaps wrap in iterator
52- if js .utils .throws_or_coerced_in_pm (val ):
53- args [job_input_idx ][i ] = { '__pythonmonkey_guard' : val }
54-
55- # clean up job args for PythonMonkey
45+ if hasattr (args [job_input_idx ], 'js_ref' ) and dry .class_manager .reg .find_from_js_instance (args [job_input_idx ].js_ref ):
46+ args [job_input_idx ] = args [job_input_idx ]
47+ else :
48+ try :
49+ tmp = args [job_input_idx ][0 ]
50+ args [job_input_idx ][0 ] = { 'arbitrary-input-test' : True }
51+ args [job_input_idx ][0 ] = tmp
52+
53+ newArr = args [job_input_idx ]
54+ except (ValueError , TypeError , IndexError ):
55+ newArr = [ 'placeholder' for i in range (len (args [job_input_idx ]))]
56+
57+ for i , val in enumerate (args [job_input_idx ]):
58+ if hasattr (val , 'js_ref' ) and dry .class_manager .reg .find_from_js_instance (val .js_ref ):
59+ newArr [i ] = val
60+ elif js .utils .throws_or_coerced_in_pm (val ):
61+ newArr [i ] = { '__pythonmonkey_guard' : val }
62+ else :
63+ newArr [i ] = val
64+ args [job_input_idx ] = newArr
65+
5666 if job_args_idx != None :
57- if js .utils .instanceof (getattr (args [job_args_idx ], "js_ref" , None ), pm .eval ("globalThis.dcp.compute.RemoteDataSet" )):
58- args [job_args_idx ] = args [job_args_idx ].js_ref
59- elif hasattr (args [job_args_idx ], '__setitem__' ):
67+ if hasattr (args [job_args_idx ], 'js_ref' ) and dry .class_manager .reg .find_from_js_instance (args [job_args_idx ].js_ref ):
68+ args [job_args_idx ] = args [job_args_idx ]
69+ else :
70+ try :
71+ tmp = args [job_args_idx ][0 ]
72+ args [job_args_idx ][0 ] = { 'arbitrary-input-test' : True }
73+ args [job_args_idx ][0 ] = tmp
74+
75+ newArr = args [job_args_idx ]
76+ except ValueError as e :
77+ newArr = [ 'placeholder' for i in range (len (args [job_args_idx ]))]
78+
6079 for i , val in enumerate (args [job_args_idx ]):
61- if js .utils .throws_or_coerced_in_pm (val ):
62- args [job_args_idx ][i ] = { '__pythonmonkey_guard' : val }
80+ if hasattr (val , 'js_ref' ) and dry .class_manager .reg .find_from_js_instance (val .js_ref ):
81+ newArr [i ] = val
82+ elif js .utils .throws_or_coerced_in_pm (val ):
83+ newArr [i ] = { '__pythonmonkey_guard' : val }
84+ else :
85+ newArr [i ] = val
86+ args [job_args_idx ] = newArr
6387
6488 ####################################################
6589
@@ -82,10 +106,6 @@ def compute_for(*args, **kwargs):
82106 })
83107 """ )
84108
85- if len (args ) <= 3 and not js .utils .instanceof (args [job_input_idx ], pm .eval ("globalThis.dcp.compute.RemoteDataSet" )):
86- if isinstance (args [0 ], Iterable ):
87- args [0 ] = pm .new (JSIterator )(iter (args [0 ]))#(IterableWrapper(args[0]))
88-
89109 compute_for_js = pm .eval ("globalThis.dcp.compute.for" )
90110 job_js = dry .aio .blockify (compute_for_js )(* args , ** kwargs )
91111 return Job (job_js )
0 commit comments