9 ms·
PH7 – An Embedded Implementation of PHP (C Library)
- Touche 13y ago> gcc -W -Wall -O6 -o ph7_test ph7_intro.c ph7.c Nice! I wish creating Lua executables were this easy. With Lua you have to create a wrapper C file, use squishy to combine multiple lua files into one, and finally a script to turn the lua script into a C byte array. I have a boilerplate project that I use so that I just have to run `make`, that avoids the need to ever touch C for the most part. But it would be nicer if luac and luajit did this automatically.
- unwind 13y agoOn the other hand, the single-source engine is so large, GitHub won't display it on the web. :/
- symisc_devel 13y agoSQLite style: The amalgamation is a single C code file, that contains all C code for the core. With one big unit, the compiler can perform several smart optimizations that cannot be done if the source code was split between different C files.
- 72deluxe 13y agoAlso makes it dead easy to download and embed into projects! Hurray for single source files.
- eksith 13y agoI was curious why they did that. Just looked at the source and came across this : This file is an amalgamation of many separate C source files from PH7 version 2.1. By combining all the individual C code files into this single large file, the entire code can be compiled as a single translation unit.This allows many compilers to do optimization's that would not be possible if the files were compiled separately.Performance improvements are commonly seen when PH7 is compiled as a single translation unit. The target does say for embedded so I guess that's from experience running this on very small resources.
- unwind 13y agoBut the glueing together destroys any modularization they had going on, making the code way harder for anyone else to understand and read. The "typical" solution to that very valid problem is to use the preprocessor, i.e. create a "ph7-master.c" file that simply includes all the real C files. The comparative overhead of should be negligble, but then the modularization survives.
- Touche 13y agoOr use cat to combine the files prior to compiling.
- aktau 13y agoYou mean so you can distribute a standalone binary that is actually just a bunch of lua? Do you do this so that the users have a harder time modifying the code (just curious)? I'm supposing that this is the case otherwise it would work pretty well with by using the lua or luajit interpreter and some wrapper bash/cmd.exe script to run your lua file(s).
- Touche 13y ago> You mean so you can distribute a standalone binary that is actually just a bunch of lua? No, I'm saying that you can't. You create a wrapper main.c file that loads and executes the lua. I use a script that tranlates the lua into a C byte array so that I can avoid loading the external lua scripts. > Do you do this so that the users have a harder time modifying the code (just curious)? I'm supposing that this is the case otherwise it would work pretty well with by using the lua or luajit interpreter and some wrapper bash/cmd.exe script to run your lua file(s). No, I do it to avoid having to worry about where the .lua scripts are located. Having a single binary is nicety, not required of course.
- coldtea 13y ago>No, I'm saying that you can't. You create a wrapper main.c file that loads and executes the lua. I use a script that tranlates the lua into a C byte array so that I can avoid loading the external lua scripts. I think that's what he meant -- that ignoring the small wrapper part --which is boilerplate--, you can deliver a binary that's basically lua.
- fsniper 13y agoI think what you are looking is called a packager. I left The Windows world and scene long time ago but t was very popular in scenes. With a little search I found out UPX and it seems to be working with many unix binary formats too.. http://upx.sourceforge.net/ http://upx.sourceforge.net/
- justincormack 13y agoLuaJIT can output bytecode as .o files that you can just link. It is not very well documented but its pretty easy once you get the hang of it.
- Touche 13y ago.o files are not executable. You still have to link it to something that executes, and that something isn't Lua.
- ygra 13y agoCouldn't you tell the linker to use one of the functions in the .o as entry point?
- Touche 13y agoCode example? edit: If so I've been doing it wrong for years.
- stephan10h 13y agoThis guy is doing it: http://williamaadams.wordpress.com/2013/03/13/is-that-cool-or-what-embedding-luajit-bytecode/ http://williamaadams.wordpress.com/2013/03/13/is-that-cool-o...
- ygra 13y agold has a -e argument [−4] (and MSVC's link has /ENTRY [37]). Otherwise, if there is a function with the expected name, calling convention and arguments in the object file I doubt the linker will complain. [−4] ftp://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_24.html [37] http://msdn.microsoft.com/library/f9t8842e.aspx http://msdn.microsoft.com/library/f9t8842e.aspx
- oscargrouch 13y agoyes.. 1 - create the object files (with luajit?) 2 - create a main() in c.. use "extern" to reference the external symbol in the .o files and call it from main. The symbols in the object files produced by luajit or whatever need to be exported/ visible.. (are they mangled?) 3 - create a object file for main.c 4 - link the .o created from luajit(or whatever, dart?) and main.o together If the linker cant find the external symbol it wont link to a binary (so you need to know how, in what names they are exported.. use objdump to lookup for symbols)
- kodablah 13y agoThat is one big C file. Curious if you've ran against any of the Zend tests in the original implementation [1][2]. I do not see anything on http://ph7.symisc.net/ http://ph7.symisc.net/. 1 - https://github.com/php/php-src/tree/master/tests https://github.com/php/php-src/tree/master/tests 2 - https://github.com/php/php-src/tree/master/Zend/tests https://github.com/php/php-src/tree/master/Zend/tests
- deleted 13y ago[deleted]
- FigBug 13y agoI've been looking for something like this. Originally I had planned to do it with Python, but Python only seems to allow one interpreter at a time. Does anybody know if it's easy to make new functions available to PHP that would call into the C main program? It says "Built with more 470 function" how does that compare with standard PHP? If I take some PHP I've previously written or a library, is it going to run with minor modifications? Or is this a subset of PHP?
- symisc_devel 13y agoYes, most of the built-in functions (over 470) are foreign C/C++ functions installed exactly using the same interface: ph7_create_function();
- stevekemp 13y agoYeah that was pretty nice to see. My first thought on seeing this project was wondering how hard it would be to hook up to nginx, or similar. I don't really "do" PHP, so I'd probably not attempt it myself, but I get the feeling from having written nginx modules in the past that this doesn't look too hard - the PH7 API seems decent enough.
- frik 13y agoInteresting, please write a blog post if you investigate this. It could be like OpenResty (Lua + Nginx): http://openresty.org http://openresty.org I am using HHVM (Facebook's HipHop PHP w/JIT) with Nginx. that works great... but it requires a 64-bit server. So for smaller servers or embedded systems Ph7 sounds interesting.
- zalthor 13y agoCurious, what do people who work with embedded systems usually use to get a web interface for their device? Routers, for as long as I can remember have had a web UI though I'm not really sure what sort of a back end runs it.
- nifoc 13y agoThe UI (and maybe other parts?) of AVM routers ("FritzBox") is written in Lua.
- z92 13y agoAlmost everyone use Lua.
- captainmuon 13y agoMy router from D-Link uses php (at least it has php in the URLs). I've seen some that use custom C programs with a webserver (cgi-bin) too.
- up-n-atom 13y agoA few that I've come around and evaluated are https://wiki.strongswan.org/projects/strongswan/wiki/Libfast https://wiki.strongswan.org/projects/strongswan/wiki/Libfast + http://www.clearsilver.net/ http://www.clearsilver.net/ http://appwebserver.org/products/appweb/server-side-javascript.html http://appwebserver.org/products/appweb/server-side-javascri... http://duda.io/ http://duda.io/ http://luci.subsignal.org/ http://luci.subsignal.org/
- edsiper2 13y agoi would be interested into your evaluation of Duda I/O, let me know your comments.
- weland 13y agoLua is pretty widely used. C is also favoured but it's pretty easy to misuse so there are teams that shy away from it. I've seen Perl, Python and PHP, too, as well as some obscure languages. I distinctly remember seeing Tcl, but I don't remember the context so I might be bullshitting here. It greatly depends on flash and RAM though. Past a certain point, Python or PHP are just too large.
- jacob019 13y agoNormally I wouldn't touch PHP due to the long history of security issues. Would this do anything to improve it? Love the idea of an embeddable interpreter.
- davidw 13y agoIf you want an embeddable interpreter, Tcl and Lua are other possibilities.
- sjf 13y agoIf insecurities are part of PHP, no, not unless they deviate from the standard implementation. And there are most likely a bunch of undiscovered stack overflow type insecurities in this new interpreter as well.
- andrewmwatson 13y agoyou're right, RoR is WAY more secure...
- BlackDeath3 13y agoWhat made you jump to RoR?
- q3k 13y agoIt might be less secure due to the fact that this is a new, untested and unproved implementation of a pretty quirky language.
- munificent 13y agoThis seems like madness to me. It's "PHP", so it inherits everything that's bad about that language. But it's also incompatible with PHP, so you don't even get the power of being able to reuse the world's existing PHP code and expertise. * PH7 is case-sensitive unlike the standard PHP engine * Unlike the zend engine which passes arrays by copy, PH7 pass arrays by reference exactly like PHP5 does with objects (class instances). I'm not saying the extensions and changes they've made aren't for the better, but they mean this language is effectively not PHP. And, if you're going to use a language that's not PHP, why not use one that's significantly cleaner and simpler?
- Kiro 13y agoIt's still great for embedded devices when your main language is PHP.
- camus2 13y ago> when your main language is PHP. There is your problem.If you cant learn anything else pretty sure you havent the skills to program any "embedded device".
- ANTSANTS 13y agoI'd normally agree, but "embedded device" in this situation means "network router" (which are reasonably powerful computers in their own rights), and the application is not mission critical with hard deadlines and strict memory requirements, but just a web interface that is rarely active and can take as long as it wants running in the background. It doesn't seem unreasonable to me to set aside some portion of memory for the web interface, give the PH7 process low priority, and hire some PHP web devs that actually know what they're doing to make the interface for them. More sense than letting the firmware developers hack something together in C anyway.
- geon 13y agoI'd say the better way would be to develop the entire GUI with static HTML + JS files talking to the embedded device through a REST-ish interface. That way you can get a clean separation of the front and back ends, and the web developers get to work with exactly the tech they are used to.
- deleted 13y ago[deleted]
- captainmuon 13y agoThis is pretty cool. First I was wondering what for, but its great for their use case (embedded devices). Why not use a language for the web interface the developer is familiar with. Unfortunately, they're a bit late. Nowadays it would probably be cooler to use JS, if language popularity is the focus. Or maybe python. (Aside: I learned web development in the good old LAMP times. My silly gut feeling is still that PHP is pretty fast and flexible compared to Python which seems slow (on the server), and JS which is way too slow and fragile. I know it's completely backwards nowadays, but funny how what you've learned sticks. When I hear "JS on a Arduino" I always think "yeah sure....", but it's actually a clever idea, just like this.)
- Kiro 13y agoIs it Python really slower than PHP? If you could notice the slowness it seems like the difference would be pretty large.
- camus2 13y ago> compared to Python which seems slow This sentence doesnt mean anything. What Python task? What library ? what framework ? on what server? with which version ? ect... I can say PHP is slow too, just look at any PHP framework, lowest scores according to techempower benchmarks : http://www.techempower.com/benchmarks/#section=data-r8&hw=ec2&test=plaintext http://www.techempower.com/benchmarks/#section=data-r8&hw=ec...
- RussianCow 13y agoIn my experience the difference in performance of the languages is negligible (assuming you have a solid PHP config). I run sites written in each, and I can't say one has caused me more performance problems than the other; it just depends on what you're doing with them.
- TheZenPsycho 13y ago"I know it's completely backwards nowadays, but funny how what you've learned sticks."
- 13y ago
- frik 13y agoThe developer have also two other interesting projects on their website: * jx9 - programming language based on JSON (like Lua with a JS syntax): http://jx9.symisc.net/ http://jx9.symisc.net/ * unQLite - NoSQL database engine (like SQLite but NoSQL): http://unqlite.org/ http://unqlite.org/
- edsiper2 13y agoUnqlite is very good, we use it as our main in-memory key/value store in Duda I/O [http://duda.io http://duda.io].
- ausjke 13y agoFor embedded device normally LUA is used, how does PH7 compare to LUA? as PHP/LUA is normally used to provide web interface, how does PH7 do CGI?
- angersock 13y agoErm...how would people feel about having this as an available module for Node.js? No reason. ;)
- jbeja 13y ago...
- elwell 13y agoPretty cool, but why's it all in one .c file?
- rlidwka 13y ago62022 lines of code in a single file... is that a joke? :)