Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying substr expressions evaluate incorrectly #69

Open
johannes-riecken opened this issue Dec 3, 2022 · 1 comment
Open

Modifying substr expressions evaluate incorrectly #69

johannes-riecken opened this issue Dec 3, 2022 · 1 comment

Comments

@johannes-riecken
Copy link

I'm very fascinated by all the work that went into this project and I have written quite a bit of Perl code with few dependencies that I could use Perlito on.
One issue that I think might be easy to fix with much benefit, is that I often like using modifying substr expressions as it's often faster than regexes, but in the generated JS code, the string doesn't get modified.
Here are three ways to modify substrings.

my $s = 'hello';
# 0
substr $s, 0, 1, 'H';
# 1
substr($s, 0, 1) = 'G';
# 2
my $ref = \substr $s, 0, 1;
$$ref = 'F';

Running any of these followed by say $s; should result in a modified string. Running with Perlito5 gives a compiler error in case[1] and results in output of unmodified hello in the other cases.

@fglock
Copy link
Owner

fglock commented Dec 4, 2022

Note that in JavaScript strings are constants - unlike in Perl, the function returns a new string and the original string doesn't get modified.

Can it be done? In some cases yes, but it takes some work.

Some of these cases can be implemented with a Macro - a rule that specifies how a certain code pattern should be mapped to a replacement output.

This will not fix the case for $$ref above, or when substr() is used as a subroutine parameter. It is also slow, because it involves executing more code.

For reference, the current implementation of substr for JavaScript is located at:

-- compile-time (this code executes during translation to JS)
https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/Apply.pm#L851

-- run-time (this becomes a JS function)
https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/CORE.pm#L364

How macros would work:

substr $s, 0, 1, 'H' can be transformed into do { $s = 'H' + substr($s, 2) }

Which code to modify:

Another example of using a macro to implement mutators in JS is the ++ operator - https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/Apply.pm#L442
(note the emit_wrap_javascript2() that works like a "do BLOCK").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants