flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
260 stars 25 forks source link

Handle the method "get" of objects #46

Closed Konubinix closed 4 years ago

Konubinix commented 4 years ago

I was trying to play with the Hammer javascript library. To enable pinch, the javascript document says to run hammertime.get("pinch").set({enable: true});. I then wrote the python equivalent hammertime.get("pinch").set({"enable": True}) and got the error flexx-core.js:3266 Uncaught TypeError: Cannot read property 'set' of null.

This makes sense as the get method get transpiled into

if (this.constructor !== Object) return this.KEY.apply(this, arguments);
if (this[key] !== undefined) {return this[key];}
else if (d !== undefined) {return d;}
else {return null;}

I worked around this issue by calling the javascript code inside RawJS, but I wonder if there was a more elegant way of doing it.

Can we imagine adding a condition like if(this.get != undefined) {return this.get(key);} in the get method, and more generally in all the overrloaded methods I guess? Does it make any sense?

almarklein commented 4 years ago

I think what is going on here, is that hammertime is an Object, with functions added to it. But when PScript encounters an Object, it considers it a dict, and the code you see there is the implementation of Python dict.get(). So it's an unfortunate name clash.

So you'd have to trick PScript not to do this overloading. E.g.:

hammertime.gat = hammertime.get
hammertime.gat("pinch").set({"enable": True})
# or 
hget = hammertime.get
hget("pinch").set({"enable": True})
Konubinix commented 4 years ago

Thank you for the hint :-).