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

... #42

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open

... #42

wants to merge 52 commits into from

Conversation

zaoqi
Copy link

@zaoqi zaoqi commented May 4, 2019

No description provided.

Multiple initializers should be comma-separated.
This prevents matching 'hasOwnProperty' and other properties of object.
Property names are encoded with a leading dot, since method names are the
most common case.  This allows us to handle `Array.prototype.slice.call(...)`
and similar constructs in a subsequent patch.
@zaoqi
Copy link
Author

zaoqi commented May 4, 2019

@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

@zaoqi
Copy link
Author

zaoqi commented May 4, 2019

$ 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

@zaoqi
Copy link
Author

zaoqi commented May 4, 2019

$ echo 'function f(){}var g=f;' | npx js2php
<?php
function f() {}$g = $f;

@cscott

@endel
Copy link
Owner

endel commented May 5, 2019

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!

@yi-ge
Copy link

yi-ge commented Jul 29, 2019

Very cool!

@cscott
Copy link
Contributor

cscott commented Jul 30, 2019

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 b4bd92a and da730e8 -- 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/mediawiki-services-parsoid@83ed537). 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!

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

Successfully merging this pull request may close these issues.

None yet

4 participants