69 comments

[ 2.9 ms ] story [ 128 ms ] thread
One question. Why?
Ha, yeah. Since we've seen a linux kernel run under a javascript vm its no surprise to see php. Are there any real world uses for it though?
to see how a shitty and slow language runs atop an even worst language, I can't believe this got to the homepage.
Any time now ... C and Assembler ... emitted as JavaScript. Sheesh.
It has no real practical use, but it was an amazing learning experience for me. So many puzzles to solve
You're spot on. People asking 'why' or 'how is this useful?' are missing the point.
I can think of a couple of practical uses, the biggest one that comes to mind is helping places like CodeSchool do in browser PHP tutorials with the code actually executing. So definite practical uses :)
This an very valid reason. The other one could be user-frontend editing of wordpress widgets or similar little nasty diy codebits.
Great use case, might I add.
As someone whose initial reaction was "God why?" I stand corrected--that's an excellent use for this project.
Must admit I had the same reaction and then it clicked and was much more impressive.
Assuming you have strong convictions about using php instead of JavaScript, it could be very useful.

I'm actually using a similar tool (js_of_ocaml [1]) at work. It's great: we can take our product and package it for node or let people use it in the browser directly. And we can also have a native compiled version if performance is needed.

[1]: http://ocsigen.org/js_of_ocaml/

So if you have an almost religiously php-oriented shop working on something that isn't a web app, I could certainly see this being useful. That's exactly what the company I'm at is, except with OCaml instead of php. Of course, I can't actually imagine any company like that in php :P. (How many people use php for something that isn't server-side code?)

But the core idea is useful and awesome. And it's certainly very cool for a personal side-project!

Out of curiosity and if you don't mind me asking, what is the OCaml-shop company you're at? Are you based out of France?
It's a really tiny startup in the Bay Area.
One could have a chunk of php code that needs to be run client side (for whatever reason, offline access on a chromebook being one).

PHP.js could come in handy (no need for porting the whole thing to js). Especially if this code will change over time.

Actually, I stumbled on an article recently noting that you can use the V8 pecl extension for running computationally-heavy code as JS from within PHP, rather than PHP code itself, to shave off some cycles.

A tool like this, with enough support, could allow you to easily convert large chunks of slower, PHP code (like object graph hydration) into JS, run in V8 for speed, and use the result in PHP without manually converting libraries.

you should waste your time solving more useful puzzles. Head into Machine Learning for example, or if you are into VMs, why not try creating a Java VM on top of Cocoa for Android developers to invade iOS, that'll have more impact and you will have to learn some serious compiler theory.
Actually I think it does have a practical use: if I want to try out a PHP technique, maybe I'm porting a small code snippet, or I want to help a friend debug their code, but don't have a PHP environment available - this is ideal.
In that scenario getting a PHP environment is ideal. Using this is less than ideal.
I'd be more a fan of:

- curl -s url-to-php -o ... - ./configure && make && make install

But to each his own :)

Hm, this has the same name as another project (one which is very popular): http://phpjs.org/

This php.js is very useful as it implements a number of functions that JS is missing.

Indeed, been using php.js for quite a while here now. Will give this other one a shot later on though, see how they compare to one another.
I don't think they are comparable. The one linked by toni[1] takes PHP code and compiles it for running in Javascript; the one linked by kaolinite[2] is a translation of some PHP methods into pure Javascript.

[1] http://phpjs.hertzen.com/ [2] http://phpjs.org/

One of the problems I have with that project is that none of the functions are namespaced or sandboxed (i.e. all global); but then again, it is emulating PHP, so there you go :)
Funny, it works !

Not phpinfo() though :)

Good use of unit tests.

But I think I'm going to have nightmares now.

Like a 3 clawed hammer, there simply is no reason for this :) j/k

It's really cool. And really impressive. And if you can make it perform well in the browser (big if I bet) I can think of several valid use cases. Nice work

I love the explanation:

It starts by tokenizing the PHP code into tokens, which it then uses to build an AST tree. Once the tree has been constructed, the script compiles it into JavaScript that can be interpreted by the VM and then executes it.

What you've done there is you've explained the frontend processing that every compiler does, (tokenizing, building an AST) and missed out the part that is entirely specific to this particular one.

I mostly agree with the spirit of your comment.

But to nitpick a bit:

While most compilers today build an AST, it's by no means true that all do. Some very influential compilers through history explicitly does not.

E.g. Niklaus Wirth's languages, such as Pascal and Oberon are explicitly designed so that they can easily be compiled with single pass compilers that call the code generator to emit code from the parser instead of going via an AST, because he as more concerned with small easily understandable compilers than complex optimizations.

It's not nearly as common as it was, but it was a fairly widespread practice in compilers for languages where it is possible back when memory and disk space was still in short supply.

It's also not true that all compilers tokenize by any meaningful interpretation. While e.g. Wirth's Pascal-P compiler arguably tokenize, compact recursive descent compilers for languages with as small grammar as Wirth's languages can and often do blur the lines significantly, or have no tokenization as a separate step from the parser at all.

You're technically right, but I was exaggerating for effect.
Also, the 'T' in "AST" stands for tree. Not all intermediate-represenatations are trees: some are linear, like stack and register operations, others are graphs, etc.

But you were right, that description is .. PHPish.

The AST is typically the first generated intermediate representation and by definition it relates to the textual form of the language. All textual forms can be viewed as trees.
Is a tree without branches still a tree?
This is a great engineering feat to be honest. Well done :) Must have been an amazing experience to build.

Just.... Like if JS wasn't bad enough, now you can run PHP on it for that extra kick in the face. ;-)

Which PHP version does this implement? Doesn't seem to have lambda support :(

  call_user_func(function($what) { echo "hello $what\n"; }, "world");

  Parse error: Object [object Object] has no method 'Node_Expr_Closure' in /console.htm on line undefined


  var_dump([1,2,3]); 
works fine though so that must be php 5.4, no? But lack of lambdas makes me sad.

Nice work though.

The website indicates that lamdbas are supported:

'Any additional unconverted code that gets executed within the VM, such as eval or lambda functions will go through the same process before being executed.'

Or did I miss anything?

Yes you're right, but the code I posted above still doesn't work. It works fine with PHP 5.4 though. This also fails with the same error message:

  $fn = function() { echo 'foo'; };
But that's also perfectly valid PHP 5.4 code. Maybe I'm doing something wrong, idk.
Hm, this opens the door to integrating server and client side code more closely. For instance now we can 'share' validation rules in Forms with client side PHP scripts (the argument some people use to recommend server side JavaScript). I would really like that.
I was expecting to see something about phpjs.org, which is a great way to get some of the domain-specific language benefits of PHP from within Javascript/node. This project is impressive and quite interesting, but I don't see many practical use cases.
Interesting challenge to work on. No real world use. Waste of effort to anyone other than the author(s).
Great idea to merge two of the worst programming languages
I'm a bit skeptic but after trying it I must say that I love it.

I'd like to be able to do server-side compilation (PHP source file to JS file) and include the phpjs min library to run that PHP-to-JS code. With additional libraries and integration we could write client-side code in PHP as well…

Cross compilation of two languages that most of the programmer really hate but can't get rid of because of their widespread use.
I'm waiting to see someone compile and execute JavaScript in a PHP VM. They could then use this to run PHP from JavaScript from PHP.

I'd go further, but from here it's really turtles all the way down.

I had that very same idea, but personally think it would be far harder to do a JavaScript VM written in pure PHP than it is to do it this way around.
Personally, I like JavaScript; but I realise that for some people this is like saying that you have solved the problem of dog poo by creating a machine that turns it into cat poo.
why? why? why? starts to bang head against desk

can't wait for a world free of PHP and JS, and you go and create a VM in JS of... PHP.

I can't wait for a world free of bigots like you, but hey, what you going to do.
O.O Brainexplosion.

Implementing script interpreters / miscelleneous applications in JavaScript is quickly becoming the next evolution of esoteric programming languages such as Brainfuck and lolcode: useless, unless you're in college dealing with a TA that likes to be a jerk while grading your code. Heheheh.

Wow, because running one "broken" language in another is just what we need.