Skip to content

Commit 3668cba

Browse files
author
hartsantler
committed
fixed dict.keys(), and fixed functional style list/tuple creation using backdoor list(js_object=Array)
1 parent 5022eb3 commit 3668cba

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

bindings/blockly.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def callback(self, jsfunc): ## decorator
162162
with javascript:
163163
arr = jsfunc.args_signature
164164
defs = jsfunc.kwargs_signature
165+
print 'jsfunc', jsfunc
166+
print 'arr', arr
165167
for i in range(arr.length):
166168
name = arr[i]
167169
if defs[name] is null: ## special case: null creates a non-dynamic "slot" input statement
@@ -420,12 +422,12 @@ def generator(block):
420422
## TODO what about pure javascript functions?
421423
if is_statement and block.parentBlock_: ## TODO request Blockly API change: "parentBlock_" to "parentBlock"
422424
print 'is_statement with parent block - OK'
423-
code += external_javascript_function + '( [' + ','.join(args) + '] )' ## calling from js a pyjs function
425+
code += external_javascript_function + '( [' + ','.join(args) + '], {} )' ## calling from js a pyjs function
424426
print code
425427

426428
elif block.parentBlock_: ## TODO request Blockly API change: "parentBlock_" to "parentBlock"
427429
print 'with parent block - OK'
428-
code += external_javascript_function + '( [' + ','.join(args) + '] )' ## calling from js a pyjs function
430+
code += external_javascript_function + '( [' + ','.join(args) + '], {} )' ## calling from js a pyjs function
429431
print code
430432

431433
else: ## TODO this should be a simple series of statements?

pythonjs/python_to_pythonjs.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,11 @@ def visit_Call(self, node):
952952
self.identifier += 1
953953

954954
if name in ('list', 'tuple'):
955-
writer.append( '%s = JS("%s.__dict__.js_object")' % (args_name, args))
955+
#writer.append( '%s = JS("%s.__dict__.js_object")' % (args_name, args))
956+
if args:
957+
writer.append( '%s = %s.__dict__.js_object' % (args_name, args)) ## test this
958+
else:
959+
writer.append( '%s = []' %args_name )
956960
else:
957961
writer.append('%s = JSArray(%s)' % (args_name, args))
958962

@@ -972,7 +976,10 @@ def visit_Call(self, node):
972976
if name == 'dict':
973977
return 'get_attribute(%s, "__call__")(%s, JSObject(js_object=%s))' % (name, args_name, kwargs_name)
974978
elif name in ('list', 'tuple'):
975-
return 'get_attribute(%s, "__call__")([], JSObject(js_object=%s))' % (name, args_name)
979+
if len(node.args):
980+
return 'get_attribute(%s, "__call__")([], JSObject(js_object=%s))' % (name, args_name)
981+
else:
982+
return 'get_attribute(%s, "__call__")([], %s)' % (name, kwargs_name)
976983
else:
977984
return 'get_attribute(%s, "__call__")(%s, %s)' % (name, args_name, kwargs_name)
978985

runtime/builtins.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,14 @@ def __setitem__(self, key, value):
462462
JS('__dict[key] = value')
463463

464464
def keys(self):
465-
__dict = self.js_object
466-
__keys = JS('Object.keys(__dict)') ## the problem with this is that keys are coerced into strings
467-
out = list()
468-
out.js_object = __keys
469-
return out
465+
#__dict = self.js_object
466+
#__keys = JS('Object.keys(__dict)') ## the problem with this is that keys are coerced into strings
467+
#out = list( js_object=__keys ) ## some bug in the translator prevents this
468+
#out.js_object = __keys ## this style is deprecated
469+
#return out
470+
with javascript:
471+
arr = Object.keys( self[...] )
472+
return list( js_object=arr )
470473

471474
def pop(self, key, d=None):
472475
v = self.get(key, None)

tests/blockly_custom_blocks.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,29 @@
138138
myblock1.set_external_function('func1')
139139
myblock1.add_input_value('a', type='Number')
140140
myblock1.add_input_value('b', type='String')
141-
141+
print( myblock1 )
142142
def func1(a,b):
143143
print 'a', a
144144
print 'b', b
145145

146-
myblock2 = ValueBlock('myblock2', output='Number')
146+
print 'testing myblock2'
147+
#myblock2 = ValueBlock('myblock2', output='Number')
148+
myblock2 = StatementBlock('myblock2')
149+
print 'myblock2', myblock2
147150
@myblock2.callback
148151
def my_extern_func(x,y,z, w=None):
149152
print 'hello extern func'
150153
return x+y+z
151154

155+
print 'OK'
152156
def show_code():
153157
with javascript:
154158
code = Blockly.Python.workspaceToCode()
155159
print( code )
156160

161+
print 'init blockly'
157162
initialize_blockly( on_changed_callback=show_code )
158-
163+
print 'DONE'
159164
</script>
160165

161166
</body>

0 commit comments

Comments
 (0)