Skip to content

Commit 4b519de

Browse files
author
hartsantler
committed
pass args from main to webworker function.
1 parent cb515b4 commit 4b519de

3 files changed

Lines changed: 41 additions & 5 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,8 @@ def visit_FunctionDef(self, node):
21602160
writer.write('def onmessage(e):')
21612161
writer.push()
21622162
writer.write( 'print("got message from parent")' )
2163-
writer.write( 'if e.data.type=="execute": %s.call(self, e.data.args, {}); self.postMessage({"type":"terminate"})' %node.name )
2163+
#writer.write( 'if e.data.type=="execute": %s.call(self, e.data.args, {}); self.postMessage({"type":"terminate"})' %node.name )
2164+
writer.write( 'if e.data.type=="execute": %s.apply(self, e.data.args); self.postMessage({"type":"terminate"})' %node.name )
21642165
writer.write( 'elif e.data.type=="append": __shared_list.push(e.data.value)' )
21652166
writer.write( 'elif e.data.type=="setitem": __shared_list.insert(e.data.index, e.data.value)' )
21662167
writer.pull()
@@ -2444,7 +2445,7 @@ def visit_FunctionDef(self, node):
24442445
for i in range(timeouts):
24452446
writer.pull()
24462447
## workerjs for nodejs requires at least 100ms to initalize onmessage/postMessage
2447-
writer.write('setTimeout(__callback%s, 500)' %i)
2448+
writer.write('setTimeout(__callback%s, 100)' %i)
24482449

24492450
if self._return_type: ## check if a return type was caught
24502451
if return_type:

regtests/run.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,14 @@ def translate_js(filename, javascript=False, dart=False, coffee=False, lua=False
297297
if multioutput:
298298
d = json.loads( stdout )
299299
stdout = d.pop('main')
300-
300+
builtins = read(os.path.join("../pythonjs", "pythonjs.js"))
301301
for jsfile in d:
302302
if not jsfile.startswith('/'):
303303
stdout = stdout.replace('"%s"' %jsfile, '"/tmp/%s"' %jsfile)
304-
write( os.path.join('/tmp', jsfile), d[jsfile] )
304+
write(
305+
os.path.join('/tmp', jsfile),
306+
'\n'.join( [builtins, d[jsfile]] )
307+
)
305308

306309
if dart:
307310

@@ -518,7 +521,7 @@ def display(function):
518521
display(run_pythonjs_test_on_node)
519522

520523
if '--no-javascript-mode' not in sys.argv:
521-
js = translate_js(filename, javascript=True)
524+
js = translate_js(filename, javascript=True, multioutput=filename.startswith('./threads/'))
522525
if rhino_runnable:
523526
display(run_pythonjsjs_test_on)
524527
if node_runnable:

regtests/threads/args.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""simple thread"""
2+
3+
import threading
4+
5+
def webworker(a):
6+
## In CPython this decorator does nothing.
7+
## In PythonJS this decorator name is special,
8+
## and triggers special code injected into the
9+
## header of the function, and saves the worker
10+
## function in a file "xxx.js" and in the main
11+
## script the function is changed to a string
12+
return lambda f : f
13+
14+
@webworker( 'xxx.js' )
15+
def mythread(a,b):
16+
print(a)
17+
print(b)
18+
threading.shared_list.append( a )
19+
threading.shared_list.append( b )
20+
21+
def main():
22+
if PYTHON != "PYTHONJS":
23+
threading.start_new_thread = threading._start_new_thread
24+
threading.shared_list = list()
25+
26+
t = threading.start_new_thread( mythread, ('hello', 'worker') )
27+
ticks = 0
28+
while len(threading.shared_list) < 2:
29+
ticks += 1
30+
31+
TestError( threading.shared_list[0] == 'hello' )
32+
TestError( threading.shared_list[1] == 'worker' )

0 commit comments

Comments
 (0)