...
@cscott
$ echo 'function f(){var a=0;function g(){return a;}return g();}' | npx js2php /dev/stdin
function f() { $a = 0; function g() use ( &$a ) { return $a; }return g(); }
$ php -a
Interactive shell
php > function f() { $a = 0; function g() use ( &$a ) { return $a; }return g(); }
PHP Parse error: syntax error, unexpected 'use' (T_USE), expecting '{' in php shell code on line 1
$ echo 'function f(){var x=0;var g=(x)=>x;}' | npx js2php /dev/stdin
<?php
function f() { $x = 0; $g = function ( $x ) use ( &$x ) {return $x; }; }
@cscott
$ echo 'function f(){}var g=f;' | npx js2php
<?php
function f() {}$g = $f;
@cscott
Wow, lots of additions @zaoqi!
Do you mind reviewing it? @2pha @allain @cscott @Grynn I'm not using this project anymore, so if you're okay with these changes, I'll just going to accept it.
Cheers!
Very cool!
It's not quite clear to me, but I think @zaoqi is proposing to merge a bunch of changes from the https://github.com/cscott/js2php fork back into master? They are all appropriate, I guess, except mabe for b4bd92a35dfd10f07b7b2a45605f13da91b935e3 and da730e8bef61448d66efcb6291f71429b60e4a10 -- and even those probably don't hurt anything.
I should note though that the focus of my patches shifted from trying to exactly match JS execution (ie, execute JavaScript in PHP) from trying to provide a reasonable-quality "natural reading" source-level transformation to guide a port. We are porting https://www.mediawiki.org/wiki/Parsoid from JS into PHP ( https://www.mediawiki.org/wiki/Parsing/Notes/Moving_Parsoid_Into_Core/Porting ) and we've found js2php invaluable for doing a first pass conversion of our ~70k lines of code (see wikimedia/parsoid@83ed537a118b7767b1e5d8e22ecad1482ae3263d). When we actually "port" the code after that, we can easily concentrate on the differences between the js2php-generated skeleton and the working file -- the diff now shows "real" changes, not just syntax conversion.
All of which is to say I wasn't super concerned with executability of the result, as long as it "looked right" to a reader, and in a few places I added big /* REVIEW ME */ type comments to the output where the semantics of the JS and PHP were subtly different and a human really needed to think hard about the code to figure out what the right thing to do was. So my fork is subtly different in purpose from mainline. But it was extremely helpful to us, thank you thank you @endel!