
php.js - Run PHP code in JavaScript - toni
http://phpjs.hertzen.com/
======
DanWaterworth
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.

~~~
vidarh
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.

~~~
DanWaterworth
You're technically right, but I was exaggerating for effect.

~~~
mahmud
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.

~~~
DanWaterworth
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.

------
niklasvh
It has no real practical use, but it was an amazing learning experience for
me. So many puzzles to solve

~~~
nicholassmith
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 :)

~~~
andreasklinger
This an very valid reason. The other one could be user-frontend editing of
wordpress widgets or similar little nasty diy codebits.

~~~
jqueryin
Great use case, might I add.

------
kaolinite
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.

~~~
dutchbrit
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.

~~~
jlogsdon
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/>

------
gabordemooij
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.

------
chris_wot
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.

~~~
phpnode
doing it in pure php would be impressive (or masochistic), but there's already
<http://php.net/manual/en/book.v8js.php>

~~~
lucassouza1
There are great performance improvements using that. [http://we-love-
php.blogspot.de/2012/07/using-v8-javascript-e...](http://we-love-
php.blogspot.de/2012/07/using-v8-javascript-engine-as-php.html)

------
bmj1
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.

~~~
Kudos
In that scenario getting a PHP environment is ideal. Using this is less than
ideal.

------
uwemaurer
Great idea to merge two of the worst programming languages

------
Digit-Al
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.

------
kwamenum86
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

------
no_more_death
Sweet! Now I can run NodeJS on my server, and PHP in the browser!

------
noiv
Given all the compile to JavaScript projects and even video decoders running
in JS, I wonder why there is still no chip capable of executing JS on pure
Silicon. Since the standard is a very slow moving target, that should speed up
all Web applications a lot making it a considerable business case.

~~~
Xcelerate
We'll eventually see chips optimized for JS execution. It just takes a while.
It took a long time for H.264 (video) decoding/encoding to be built in to the
hardware.

JS is also a general purpose language, but there's still plenty of potential
for hardware optimization.

------
farmdawgnation
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.

------
yashg
One question. Why?

~~~
danielwozniak
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?

------
adrinavarro
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…

------
seivan
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. ;-)

------
gddr
In 15 years we are all gonna surf the web in a "virtual browser" that will be
coded in HTML + Javascript running inside another browser.

Ha ha, that's silly, I'm just joking. Or am I?

~~~
tonyarkles
You should have a look at XUL :) If you're using Firefox, the future is now!

<https://developer.mozilla.org/en/XUL_Overlays>

------
jamesmcn
Good use of unit tests.

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

------
hkolek
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.

~~~
LinaLauneBaer
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?

~~~
hkolek
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.

------
sharjeel
Cross compilation of two languages that most of the programmer really hate but
can't get rid of because of their widespread use.

------
lukifer
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.

------
atarian
I'd love to see a PHP Fiddle.

~~~
clone1018
<http://ignite.io> have fun :)

------
KaoruAoiShiho
Hey guys I actually want to use this for something.

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?

------
dbaupp
Is this particular different to what would be achieved by running
Emscripten[1] on the normal C PHP interpreter?

[1]: <https://github.com/kripken/emscripten>

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

------
conradfr
Funny, it works !

Not phpinfo() though :)

------
willybarro
Guys, this is really impressive! If you don't like PHP or JS, try to build a
VM running Python on Go, or something you find sexy.

------
toomuchcoffee
The horror! The horror!

------
gubatron
"Let's take one of the worse languages and run it atop an even worse one."

------
zbuc
Very confusing, at first I thought this was about
<https://github.com/kvz/phpjs>

------
gubatron
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.

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

------
vivab0rg
Oh, the humanity!

[http://en.wikipedia.org/wiki/Herbert_Morrison_%28announcer%2...](http://en.wikipedia.org/wiki/Herbert_Morrison_%28announcer%29#.22Oh.2C_the_humanity.22)

