Babel-preset-php: PHP7 to ES7 syntax translator(gitlab.com) |
Babel-preset-php: PHP7 to ES7 syntax translator(gitlab.com) |
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.
atm you have to add `<?php` at the beginning.
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 project is silly and exists only to expand Atwood's Law."
So maybe take it with a grain of salt, and just enjoy a neat project for what it is.
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.
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.
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.
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.
Someone did take roughly the approach you're describing. A VM in JavaScript that groks php bytecode: https://github.com/niklasvh/php.js
It wouldn't be a perfect comparison since the generated JS code might be less optimized. But it could give one a ballpark estimate.
Amazing that the code actually reads better than the original. (at least for me)
I wonder how many of the php extensions can be easily ported to JS code as well ?
0 - http://cretz.github.io/pratphall/ - https://github.com/cretz/pratphall
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.
Other than that you're right
Python 2093, C 1630, Go 766, SQL 706, Haskell 691, JavaScript 683, C# 666
I know it doesn't mean anything but I think it's funny, at least.
Full list: https://pastebin.com/3HevKLzz
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.
Honestly, how can you tell? I feel like that's something someone could say if they are under the misunderstanding you (very aptly) describe.
Luckily ES2015 introduced blocked scoped variables (const and let), so this is no longer an issue.
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.
Right? Because otherwise you’d be making no sense, and you’d not do that, would you?
Should I not consider myself a professional developer because I don't find JavaScript bad? In fact, there are parts of it I quite like.
>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?
That is an opinionated message about a language. I imagine people are taking offense to my opinion, in which case I urge everyone to take a step back and look at the difference between the insulting sarcasm and an extreme opinion. Sometimes circumstance and bias blinds you into taking offense, especially if you're one of those people who only knows javascript.
>Should I not consider myself a professional developer because I don't find JavaScript bad? In fact, there are parts of it I quite like.
This message illustrates exactly what I'm talking about. When did I say any of this? Did I say you're not a professional developer because you don't find javascript bad? No. I said Professional developers find javascript to be really shitty, and it's quite universally known. My mistake, I should have wrote "Many" professional developers, as you seem to mistook it to be "All" professional developers.
Look, there is no denying this fact. A great number of really good developers intensely hate javascript, there's a reason why. I will say this: You are a horrible javascript developer if you do not have the ability to comprehend the bad parts of javascript and why "many" developers find the language so shitty.
No. It's a rude, factually incorrect, and just plain obnoxious comment with nothing to recommend it. Are you shocked it was met with ridicule and hostility?
> you seem to mistook it to be "All" professional developers.
That's because you're a bad writer who speaks in (incorrect) absolutes.
> Look, there is no denying this fact.
He said while lots of people deny it.
> A great number of really good developers intensely hate javascript, there's a reason why.
I've never found a really good developer to hate any language. Good developers tend to appreciate languages for their strengths - even if they prefer not to use them or don't like their idioms.
It's often a sign of a very poor developer to get into the kind of crazy language war stuff you're talking about.
I'm never shocked it when things are met with hostility. It's natural for people to meet constructive criticism with hostility, that is human nature and apparently yours as well. What I said was not rude at all, I directly re-quoted what I said so you can point out exactly which part of my message was rude. You couldn't. You are a liar, which is a thousand times worse than rude. Also it's not factually incorrect. It's factually correct. Stop lying.
>> you seem to mistook it to be "All" professional developers. >That's because you're a bad writer who speaks in (incorrect) absolutes.
I am a bad writer or are you a bad reader? Where exactly did I use the term "All" developers? Nowhere. If I used that term nowhere than logically there is no way I could have spoken in absolute terms. You are again lying. You are a liar. The worst of it's kind. I wonder if you are just not reading the text carefully or if you are deliberately just an ass.
>I've never found a really good developer to hate any language. Good developers tend to appreciate languages for their strengths - even if they prefer not to use them or don't like their idioms. >It's often a sign of a very poor developer to get into the kind of crazy language war stuff you're talking about.
You haven't met enough developers. Good developers tend to be highly, highly opinionated. They have extreme opinions about many things and languages they hate. Again you are speaking from inexperience or lies. Lets put it this way: It is not probable that all languages are created equal... with strengths balancing weaknesses, this is not the way the world works. Things come out better or worse, rarely are things created totally yin yang balanced... Languages are the same... some are better some are worse.
>> Look, there is no denying this fact. >He said while lots of people deny it.
You gotta be kidding me. There are a good amount of developers who hate javascript. Unless you don't work at a node shop than you've probably been drinking that koolaid since forever. Many, Many developers don't like javascript, this is obvious af.