php.js - Run PHP code in JavaScript
phpjs.hertzen.com
phpjs.hertzen.com
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.
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.
But you were right, that description is .. PHPish.
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!
PHP.js could come in handy (no need for porting the whole thing to js). Especially if this code will change over time.
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.
This php.js is very useful as it implements a number of functions that JS is missing.
I'd go further, but from here it's really turtles all the way down.
- curl -s url-to-php -o ... - ./configure && make && make install
But to each his own :)
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
JS is also a general purpose language, but there's still plenty of potential for hardware optimization.
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.
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…
Just.... Like if JS wasn't bad enough, now you can run PHP on it for that extra kick in the face. ;-)
Ha ha, that's silly, I'm just joking. Or am I?
But I think I'm going to have nightmares now.
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.
'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?
$fn = function() { echo 'foo'; };
But that's also perfectly valid PHP 5.4 code. Maybe I'm doing something wrong, idk.I want to run sendy on it.
http://news.ycombinator.com/item?id=4281964
I don't want to set up a php environment, is this going to work?
Not phpinfo() though :)
can't wait for a world free of PHP and JS, and you go and create a VM in JS of... PHP.
http://en.wikipedia.org/wiki/Herbert_Morrison_%28announcer%2...