TranscryptOrg / Transcrypt

Python 3.9 to JavaScript compiler - Lean, fast, open!
https://www.transcrypt.org
Apache License 2.0
2.82k stars 215 forks source link

.continue() method #826

Open faerot opened 1 year ago

faerot commented 1 year ago

When using IndexedDB, there is a continue() method in IDBCursor object. It is impossible to use in python due to parser error as "continue" is a reserved keyword. Trivial solution is to add 'js_continue' => 'continue' default alias into modules/org/transcrypt/compiler.py. Maybe it makes sense to add all python keywords this way. At least some other keywords like js_catch which is used in promises should be added.

mentalisttraceur commented 1 year ago

Does getattr(foo, 'continue') work instead of foo.continue?

faerot commented 1 year ago

I doubt it. Transcrypt does not produce bound methods for the built-in ones. I use foo['continue']() as a work around.

mentalisttraceur commented 1 year ago

Ohhh, okay, I think I see. (Thanks for adding that edit - before the edit my reaction was just "why do you doubt it? based on what?")

So as I understand it now:

foo.some_method normally works in Transcrypt because it does something to make sure that the generated JavaScript binds foo to some_method.

So even if getattr(foo, 'continue') can successfully get an attribute named continue, you're thinking it won't actually do the binding, and the binding won't be done prior.

Meanwhile, foo['continue']() works because it ends up falling through to basically identical JavaScript, and the JavaScript engine sets this to foo inside the function call the same whether you write foo['bar']() or foo.bar().

But I'm a little confused why Transcrypt can't implement getattr in a way that does all the same things as normal attribute access, or why Transcrypt can't bind all methods the same way even if they have names that match keywords....

faerot commented 1 year ago

The difference between python and javascript in terms of calling a method is basically python has call operator () and attribute operator . so calling a method is two independent operators, while in javascript .xxx() or ['xxx']() is sort of a single operator which sets "this" and calls method.

For user defined methods transcrypt generates properties that return callable closures so this works like bound methods in python, but for built-in methods this is impossible for obvious reasons. You can do getattr(foo, 'continue')() if foo is the class defined in transcrypt, but you cannot do it for builtins.