Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

String.join causes maximum recursion depth to be reached on FF and Chrome

Open RyliD opened this issue 9 years ago • 7 comments

Awesome job! This is a great project and I've really enjoyed working with it so far.

I did come across one minor issue. I apologize in advance if it's already been addressed or if it's an error on my part.

I noticed that my transpiled code contains the following block

String.prototype.join = function (strings) { return strings.join (this); };

This causes FireFox and Chrome to recurse infinitely.

Steps to reproduce:

  1. open the dev console of any page which includes transpiled javascript created with Transcrypt
  2. enter: 'foo'.join('bar')
  3. Maximum recursion depth reached

RyliD avatar Sep 12 '17 00:09 RyliD

I apologize. I realize that .join() expects an iterable as an argument, not a string. I'm not sure if this is really an issue as long as you don't accidentally pass in strings, which causes max recursion. Closing.

RyliD avatar Sep 12 '17 00:09 RyliD

Hmm, in Python string is a iterable, so as Transcrypt doesn't do it, I find it inconsistent, I'd recommend @JdeH take another look at it.

pmukherjeee avatar Sep 12 '17 00:09 pmukherjeee

It is is inconsistent with CPython indeed. I'll take a look at the amount of overhead (time and space) needed to adapt this. Code generated should remain fast and compact, sometimes at the expense of compatibility. I don't know yet at which side of the line this falls. By the way leaving this out was not a deliberate decision, but just an omission...

I'll reopen this as a reminder to take a good look at it.

JdeH avatar Sep 12 '17 08:09 JdeH

After some more debugging, I've discovered the reason why .join() was being a passed a string.

I'm using an external JavaScript library from a CDN, and that code contains the following logic in a loop:

a && a.join && (a = a.join(" "));

'a' apparently starts out as an Array, and is converted into String if it has the .join() method. Native JavaScript Arrays have a .join() method, Strings do not. It's relying on this check to determine if a JavaScript String contains the .join() method to decide whether it should call it again or not. Adding .join() to the prototype in the code Transcrypt causes this check to return true, which then in turn causes a string to be passed to the .join() method on string, causing the infinite recursion.

I'm solving this for now by doing the following 'hack' in my Transcrypt .py source at the beginning of my code before the external library is used:

String.prototype.py_join = String.prototype.join
del String.prototype.join

__pragma__ ('alias', 'join', 'py_join')

Perhaps a fix for this in the future is adding py_join as a predefined alias. This preserves the Python behavior of .join() available on strings in Transcrypt source, without altering the assumption that String does not contain .join() in external libraries. This of course does not address the fact that Strings are not iterable in Javascript, but are in CPython.... but at least a TypeError is raised rather than causing max recursion.

Examples from browser console running the fix:

'-'.py_join(['f','o','o'])
"f-o-o"

'-'.py_join('foo')
TypeError: strings.join is not a function

The caveat to this 'hack', is that JavaScript's native .join() method on Array can't be used from within Transcrypt, but Transcrypt enhances Arrays as lists and CPython doesn't contain a .join() method on lists, so this is OK for my purposes, as I don't intend to use the native JavaScript Array.join() from within Transcrypt code.

RyliD avatar Sep 12 '17 18:09 RyliD

Thanks for the careful analysis! Your alias solution may be a nice compromise, since it doesn't have any overhead and prevents to most adverse consequences of the current situation. Have to think about this a bit more.

JdeH avatar Sep 12 '17 19:09 JdeH

Just a word of caution regarding my aliasing 'hack' above. I think it breaks Transcrypt's print() method. I haven't dug into this too much as I don't really use print() much, but I think print() calls .join somewhere and the alias breaks something. console.log() still works of course, as it is a native JavaScript method.

RyliD avatar Sep 27 '17 15:09 RyliD

It's true that, as @pmukherjeee poses, treating string not as an iterable is inconsistent with CPython. However currently I cannot think of a lean and fast way to achieve that. So the alias solution @RyliD proposes would be a reasonable compromise, prevening at least this nasty infinite recursion. I hope that not too many current developers use <aPythonArray>.join, because that would have to be changed in <aPytonArray>.js_join. But since this join is a JS rather than a Python method I expect it only to be used in a js pragma, so in native js code, where it will continue to function, since aliases won't change anything there. After all in Python there's the itertools.chain, and, with operloading switched on, the + operator to glue arrays together. So I will add the alias to cover many cases in a reasonable way without losing performance or code compactness. And indeed, the print method will have to be adapted, but that looks easy at first sight.

JdeH avatar Sep 27 '17 17:09 JdeH