10 ms·
php.js - Run PHP code in JavaScript
- yashg 14y agoOne question. Why?
- danbee 14y agoShits and giggles?
- danielwozniak 14y agoHa, 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?
- gubatron 14y agoto see how a shitty and slow language runs atop an even worst language, I can't believe this got to the homepage.
- Craiggybear 14y agoAny time now ... C and Assembler ... emitted as JavaScript. Sheesh.
- niklasvh 14y agoIt has no real practical use, but it was an amazing learning experience for me. So many puzzles to solve
- charliesome 14y agoYou're spot on. People asking 'why' or 'how is this useful?' are missing the point.
- nicholassmith 14y agoI 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 14y agoThis an very valid reason. The other one could be user-frontend editing of wordpress widgets or similar little nasty diy codebits.
- jqueryin 14y agoGreat use case, might I add.
- koglerjs 14y agoAs someone whose initial reaction was "God why?" I stand corrected--that's an excellent use for this project.
- nicholassmith 14y agoMust admit I had the same reaction and then it clicked and was much more impressive.
- tikhonj 14y agoAssuming 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/ 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!
- icoder 14y agoOne 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.
- ericclemmons 14y agoActually, 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.
- gubatron 14y agoyou 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.
- bmj1 14y agoActually 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.
- kaolinite 14y agoHm, this has the same name as another project (one which is very popular): http://phpjs.org/ http://phpjs.org/ This php.js is very useful as it implements a number of functions that JS is missing.
- dutchbrit 14y agoIndeed, 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 14y agoI 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/ http://phpjs.hertzen.com/ [2] http://phpjs.org/ http://phpjs.org/
- wilmoore 14y agoOne 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 :)
- conradfr 14y agoFunny, it works ! Not phpinfo() though :)
- atarian 14y agoI'd love to see a PHP Fiddle.
- clone1018 14y agohttp://ignite.io http://ignite.io have fun :)
- jamesmcn 14y agoGood use of unit tests. But I think I'm going to have nightmares now.
- kwamenum86 14y agoLike 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
- DanWaterworth 14y agoI 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 14y agoI 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 14y agoYou're technically right, but I was exaggerating for effect.
- mahmud 14y agoAlso, 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.
- seivan 14y agoThis 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. ;-)
- hkolek 14y agoWhich 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 14y agoThe 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 14y agoYes 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.
- gabordemooij 14y agoHm, 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.
- lukifer 14y agoI 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.
- twinturbo 14y agoInteresting challenge to work on. No real world use. Waste of effort to anyone other than the author(s).
- uwemaurer 14y agoGreat idea to merge two of the worst programming languages
- adrinavarro 14y agoI'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…
- sharjeel 14y agoCross compilation of two languages that most of the programmer really hate but can't get rid of because of their widespread use.
- schiptsov 14y agoIn node.js, I suppose?)
- chris_wot 14y agoI'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.
- niklasvh 14y agoI 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.
- phpnode 14y agodoing it in pure php would be impressive (or masochistic), but there's already http://php.net/manual/en/book.v8js.php http://php.net/manual/en/book.v8js.php
- lucassouza1 14y agoThere are great performance improvements using that. http://we-love-php.blogspot.de/2012/07/using-v8-javascript-engine-as-php.html http://we-love-php.blogspot.de/2012/07/using-v8-javascript-e...
- Digit-Al 14y agoPersonally, 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.
- gubatron 14y agowhy? 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 14y agoI can't wait for a world free of bigots like you, but hey, what you going to do.
- farmdawgnation 14y agoO.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.
- KaoruAoiShiho 14y agoHey guys I actually want to use this for something. I want to run sendy on it. http://news.ycombinator.com/item?id=4281964 http://news.ycombinator.com/item?id=4281964 I don't want to set up a php environment, is this going to work?
- aviraldg 14y agoWow, because running one "broken" language in another is just what we need.
- gddr 14y agoIn 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 14y agoYou should have a look at XUL :) If you're using Firefox, the future is now! https://developer.mozilla.org/en/XUL_Overlays https://developer.mozilla.org/en/XUL_Overlays
- vivab0rg 14y agoOh, the humanity! http://en.wikipedia.org/wiki/Herbert_Morrison_%28announcer%29#.22Oh.2C_the_humanity.22 http://en.wikipedia.org/wiki/Herbert_Morrison_%28announcer%2...
- zbuc 14y agoVery confusing, at first I thought this was about https://github.com/kvz/phpjs https://github.com/kvz/phpjs
- noiv 14y agoGiven 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 14y agoWe'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.
- toomuchcoffee 14y agoThe horror! The horror!
- no_more_death 14y agoSweet! Now I can run NodeJS on my server, and PHP in the browser!
- willybarro 14y agoGuys, 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.
- dbaupp 14y agoIs this particular different to what would be achieved by running Emscripten[1] on the normal C PHP interpreter? [1]: https://github.com/kripken/emscripten https://github.com/kripken/emscripten
- darthpanda 14y agoNO. You will break the universe. Do not attempt this.
- gubatron 14y ago"Let's take one of the worse languages and run it atop an even worse one."