Skip to content

Commit a3f98ff

Browse files
author
hartsantler
committed
lists can now be passed to webworkers and they will be synced one-way from the worker to the parent.
threading.shared_list is deprecated.
1 parent 4b519de commit a3f98ff

6 files changed

Lines changed: 259 additions & 117 deletions

File tree

pythonjs/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Brett Hartshorn <goatman.py@gmail.com>",
33
"name": "python-js",
44
"description": "python multi-translator: javascript, dart, coffee, lua, vis.js",
5-
"version": "0.9.3",
5+
"version": "0.9.4",
66
"license": "BSD-3-Clause",
77
"repository": {
88
"type": "git",
@@ -12,7 +12,9 @@
1212
"pythonjs",
1313
"python"
1414
],
15-
"dependencies": {},
15+
"dependencies": {
16+
"workerjs": "*"
17+
},
1618
"engines": {
1719
"node": "*"
1820
},

pythonjs/python_to_pythonjs.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
131131
self._with_dart = dart
132132
self._with_js = False
133133
self._in_lambda = False
134+
self._in_while_test = False
134135
self._use_threading = False
135136
self._webworker_functions = dict()
136137

@@ -294,7 +295,9 @@ def visit_Import(self, node):
294295
for alias in node.names:
295296
if alias.name == 'threading':
296297
self._use_threading = True
298+
#writer.write( 'Worker = require("/usr/local/lib/node_modules/workerjs")')
297299
writer.write( 'Worker = require("workerjs")')
300+
298301
else:
299302
#writer.write( '## import: %s :: %s' %(alias.name, alias.asname) )
300303
raise NotImplementedError('import, line %s' % node.lineno)
@@ -1117,7 +1120,7 @@ def visit_BinOp(self, node):
11171120
elif op == '+' and not self._with_dart:
11181121
if '+' in self._direct_operators:
11191122
return '%s+%s'%(left, right)
1120-
elif self._with_lua or self._in_lambda:
1123+
elif self._with_lua or self._in_lambda or self._in_while_test:
11211124
## this is also required when in an inlined lambda like "(lambda a,b: a+b)(1,2)"
11221125
return '__add_op(%s, %s)'%(left, right)
11231126
else:
@@ -2391,7 +2394,7 @@ def visit_FunctionDef(self, node):
23912394
# ie. that this function is not a callback of javascript code
23922395

23932396

2394-
writer.write("""if instanceof(args,Array) and typeof(kwargs) == 'object' and arguments.length==2:""")
2397+
writer.write("""if instanceof(args,Array) and Object.prototype.toString.call(kwargs) == '[object Object]' and arguments.length==2:""")
23952398
writer.push()
23962399
writer.write('pass') # do nothing if it's not called from javascript
23972400
writer.pull()
@@ -2419,6 +2422,11 @@ def visit_FunctionDef(self, node):
24192422

24202423
################# function body #################
24212424

2425+
2426+
if threaded:
2427+
for i,arg in enumerate(node.args.args):
2428+
writer.write( '%s = __webworker_wrap(%s, %s)' %(arg.id, arg.id, i))
2429+
24222430
#if self._cached_property: ## DEPRECATED
24232431
# writer.write('if self["__dict__"]["%s"]: return self["__dict__"]["%s"]' %(self._cached_property, self._cached_property))
24242432

@@ -2529,7 +2537,9 @@ def visit_FunctionDef(self, node):
25292537
## this fixes external javascript that is using `for (var i in anArray)`
25302538
head, tail = dec.split('.prototype.')
25312539
a = (head, tail, node.name)
2532-
writer.write('Object.defineProperty(%s.prototype, "%s", {enumerable:False, value:%s, writeable:False, configurable:False})' %a)
2540+
## these props need to be writeable so that webworkers can redefine methods like: push, __setitem__
2541+
## note to overwrite one of these props Object.defineProperty needs to be called again (ob.xxx=yyy will not work)
2542+
writer.write('Object.defineProperty(%s.prototype, "%s", {enumerable:False, value:%s, writeable:True, configurable:True})' %a)
25332543

25342544
elif dec == 'javascript':
25352545
pass
@@ -2801,8 +2811,9 @@ def visit_While(self, node):
28012811
c.constant = True
28022812
self._call_ids += 1
28032813

2804-
2814+
self._in_while_test = True
28052815
writer.write('while %s:' % self.visit(node.test))
2816+
self._in_while_test = False
28062817
writer.push()
28072818
map(self.visit, node.body)
28082819
writer.pull()

0 commit comments

Comments
 (0)