Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Run Perl 5 in the Browser!

by LanX (Saint)
on Aug 16, 2018 at 14:06 UTC ( [id://1220439]=note: print w/replies, xml ) Need Help??


in reply to Run Perl 5 in the Browser!

Hauke that's awesome!!!

I personally have a (too) long running project to exploit the semantic similarities for this goal and already put a lot of theoretical work into it.

JS is for 98% a simplified Perl, and patching B::Deparse to emit native JS should be possible.

But I was confronted with some problems:

  • How to translate RegExes, since JS only supports the Perl4 (sub) standard °
  • How to handle eval , since compiling the op-tree still needs a Perl parser.
  • How to cover the remaining 2 %
  • (update) How to cover XS code

Your approach could at least solve the eval bottleneck, my approach could probably lead to faster code and less footprint.

If you're interested I'd love to contribute, even meeting in Berlin for discussion.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

°) not sure how you solve this, does asm.js also cover the regex engine? My idea was to use re qw/debug/ to emit an "regex-optree" and translate this.

Replies are listed 'Best First'.
Re^2: Run Perl 5 in the Browser!
by haukex (Archbishop) on Aug 19, 2018 at 11:06 UTC
    not sure how you solve this, does asm.js also cover the regex engine?

    I'm compiling a whole perl binary to WebAssembly, including the regex engine and everything. I haven't gotten very far on porting Perl's test suite yet, because it makes use of some of the stuff that isn't supported (e.g. qx and signals), but the tests I was able to run have looked good so far.

    patching B::Deparse to emit native JS should be possible

    That's an interesting idea, and perhaps something I could think about is how to optimize code like the example I showed on the website:

    js('document')->getElementById('my_button') ->addEventListener('click', sub { js('window')->alert("You clicked the button!"); } );

    Running that invokes the JS/Perl glue three times (five if the callback is called), which isn't super efficient at the moment because it does a lot with strings and eval. One possible way to optimize that might be if there was a way to convert at least the first few method calls to

    js('document.getElementById("my_button").addEventListener("click",func +tion(){...})');
      I'm open for discussions, you might also be interested in the work of Flavio Glock and his perlito.

      update

      A general approach for performance enhancements could be to run Perl your way and to replace time consuming inner functions with direct JS translations.(kind of JIT if you want).

      From the Perl perspective these functions would have been monkey patched with XS code.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        A general approach for performance enhancements could be to run Perl your way and to replace time consuming inner functions with direct JS translations.(kind of JIT if you want).

        Yep, although I think it would only speed things up where the Perl<->JavaScript interface is involved, I suspect everything that's pure Perl may even be as fast or even faster than regular JavaScript, since WebAssembly should be faster than JavaScript, and it's a WebAssembly perl binary. OTOH, at the moment I just want to use this for UI's, so as long as button clicks etc. don't cause 100% CPU or noticeable delays, I'm not going to worry too much about performance just yet.

        From the Perl perspective these functions would have been monkey patched with XS code.

        Yes, the interesting thing there is that "XS" code can include JavaScript code ;-) See WebPerl.xs, a mishmash of C, JS, and XS... (and my first real use of XS too!)

        time consuming inner functions

        BTW, just another thought: As I said, my original intention with this was to be able to build user interfaces, so there's always the approach of moving the heavy work into a different process, in this case that would be the web server - even if that's running on the same machine, it'd still give a speedup for the heavy lifting.

Re^2: Run Perl 5 in the Browser!
by Anonymous Monk on Aug 20, 2018 at 11:12 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2025-07-09 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.