That's a weekend project and it turned out better than I expected. Thanks to nikic's php-parser, astexplorer.net and Babel infrastructure handling the gnarly bits (e.g. source maps just worked).
I started it because I hoped it would speed up rewrites of projects from PHP to JS, but now that I've actually seen the results, I realised that bad PHP code makes only worse JS (and there's no point rewriting good PHP code).
Still, I think it could be useful for sharing some logic between client and the server, without replacing server-side with Node. This isn't a VM, so it has zero overhead at the cost of being loose with semantic edge cases.
And I find it funny that it now takes 1 line of config to make JS tools work with PHP syntax instead.
You did a great job with this, I started something really similar about 6 months ago because I wanted to port SabreDAV to js (I didn't quite get to the stage of self hosting PHP parser) but eventually came to the same conclusions, even with a good codebase idiomatic PHP does not map cleanly to idiomatic JS
We ran across @kornelski yesterday asking about some old babel code (1.) and were wondering what the angle is. Kornel's an accomplished coder: he did pngquant and imageoptim.
The author himself calls it the "stupidest project I've ever done". But it's probably a nice way to get 90% of the way there when converting PHP fns (such as validation et al) into the browser. Also, and perhaps most importantly, it's a great exercise in writing Babel plugins.
I really enjoyed this. Thanks for writing it, @kornelski.
This isnt running PHP in the browser. It's running a quasi-JS runtime with PHP syntax.
To run "PHP in the browser" you'd need tests passing on all the major php frameworks, as per, Hack's testsuit. Otherwise you're forcing people to learn some language not quite either JS or PHP.
Indeed. For this I prioritized readable and JS-like output (and no runtime) over handling things exactly.
There's a different project doing it via a VM in JS: https://phpjs.hertzen.com/ and it handles most of the PHP test suite.
On the language level most stuff works well enough. However, the biggest blocker is impedance mismatch between PHP and JS environments, e.g. request-per-process vs shared server, `echo`, modules vs autoload & namespaces, etc. so even 100% accurate PHP execution is going to be doing odd things in the JS world.
I've generally found the behavior of builtin PHP methods to be less predictable and more involved than in JS, so I wonder if the best approach to writing a transpiler in this case would be to go to the C source and negotiate from there.
Having done PHP for years and years before the language itself found adequate elegance, I wouldn't want the burden of replicating all its weird behaviors for something like this, at least without seeing the source to understand what's happening.
If you want to use an existing toolchain, you could use HPHPc (the predecessor to HHVM, which transpiled PHP to C code) and emscripten to translate from C to Javascript. It would probably require some mucking around at the emscripten layer though to get all the packages linking properly, because HPHPc has a non-trivial number of (non-optional) runtime deps for supporting all of the various php modules. You should just be able to compile them separately with emscripten into a prefix (e.g. don't use emscripten ports, which I've had a terrible time with) but YMMV.
I dockerized a working version of HPHPc (which is quite difficult to set up due to bitrot) at https://github.com/allanlw/hphpc-docker if you want to play around with the compiler. I might try to take a shot at passing it through emscripten later.
I think the difference is that PHP's built-ins were originally created as a "toolset" and thus often do more than one thing in fairly specific combinations. I believe PHP has improved on that but it can still get hairy because of the sheer number of functions living in the global namespace (and horribly outdated community-provided examples in the documentation).
Heh, I did it the other direction back in the day as a toy [0]. Back in TypeScript's earlier days I figured it could be a neat frontend for the otherwise ugly PHP...not really.
I could see many ways how this can go wrong and be popular with guys who have PHP code and want to run it on the browser because they 1. either don't master javascript or 2. want to save time.
I wonder how easy it is to translate between dynamic languages since they all support a quite similar core set (classes, closures, sets, dicts, iterables ...)
The semantics can still be quite different. You start hitting a lot of corner cases very quickly, unless one of the languages was designed to have semantics close to the other's, or the two languages at least share heavy influence from a common ancestor.
Many dynamically typed languages treat null/nill/None, empty string, and empty list as "non-truthy" in a Boolean context, but that's not universal.
JavaScript's notion of a class is very different from Python's. Ruby's take on who can call private methods is different from most languages. Python is in a minority for supporting multiple inheritance.
In Common Lisp, functions and variables have separate namespaces, whereas in Scheme, they share a single namespace.
PHP just has bizarre object conversion and comparison rules that are often inconsistent and therefore incompatible with most other languages (ignoring the Turing tarpit).
Getting good enough lexical analysis of the source language to efficiently implement lexical closures in a target language without them can be a pain.
On the other hand, if you're cross-compiling buggy webapps written by developers who don't properly understand the semantics of the language they're written in, maybe you can just ignore the semantic mismatches and get a different set of bugs coming out the other side while accidentally fixing other bugs.
I feel you, brother. There are too many people using crappy languages. If only everybody saw the light and used the only good language out there, Oberon-2.
I feel your sarcasm. Lets be real though, V8 is a fast implementation but javascript as a language itself separate from implementation is a POS. ES6 and ES7 is proof that ES5 was just plain bad.
Wrong. Javascript was designed in a week. The creator admits it's bad. This is universally known by professional programmers, why are people defending this?
HN has two very divergent opinions on JS but the people who hate it really hate it,and the people who like JS may outnumber the ones who hate it but you can reliably expect people to complain about Javascript on every HN thread about it. JS is third in dislikes on there (Java is actually the second-most hated language, who knew?), so I wasn't technically correct but I was in the ballpark.
So you can tell me why it isn't, then? I don't see WPF being as clean as React is. It uses tons of magic strings in XAML files. JSX properties are expressions and thus can be statically typed. QML is almost the same, just a little different syntax, but I don't see any difference that would make it cleaner than React.
Fine but its dangerous. Javascript/Ecmascript, is a poor designed language, the scope of the variables is broken. Instead, PHP is well defined in this aspect.
But I'd like to point out that "the scope of the variables is broken" doesn't make any sense: prior to ES6 JavaScript only had function scope -- which is not broken but also not what most people are used to. ES6 added let/const which are block scoped (and thus more familiar). Neither of the two is broken, nor did the addition break anything.
The only oddity I can think of that would provoke such criticism would be a misunderstanding of non-strict-mode JS's behaviour with regard to implicit globals, e.g. what happens outside strict-mode when variables are used without being declared or what the "with" statement does (which is heavily discouraged). But that's like saying PHP is stupid because its array literals look weird.
Clue 1: it praises PHP. Even PHP apologists usually restrict themselves to simply defending PHP rather than praising it.
Clue 2: it calls JS "dangerous" and "poor designed" (sic). This is vague but provocative enough to invite emotional responses rather than factual discussion.
Clue 3: it explicitly calls out "the scope of variables" as "broken". Again this is vague enough to be ambiguous but strongly worded enough to seem insulting. This is especially confusing if contrasted with PHP, which isn't exactly known for being intuitive and simple in that regard: http://php.net/manual/en/language.variables.variable.php
But you're right, which is why I said it seems like an obvious troll comment rather than insisting that it can only be that. It might just be a poorly worded expression of frustration stemming from an incomplete understanding. Hence my attempt at a factual response.
I don't really think anyome ought to be starting flame wars over whether PHP or JS is a poorly designed language. It's like watching Hitler fight Stalin.
I started it because I hoped it would speed up rewrites of projects from PHP to JS, but now that I've actually seen the results, I realised that bad PHP code makes only worse JS (and there's no point rewriting good PHP code).
Still, I think it could be useful for sharing some logic between client and the server, without replacing server-side with Node. This isn't a VM, so it has zero overhead at the cost of being loose with semantic edge cases.
And I find it funny that it now takes 1 line of config to make JS tools work with PHP syntax instead.