<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BombStrike's blog &#187; server-side</title>
	<atom:link href="http://www.bombstrike.org/tag/server-side/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bombstrike.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 07 Mar 2010 22:44:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bringing Javascript to the server using PHP * update *</title>
		<link>http://www.bombstrike.org/2009/02/bringing-javascript-to-the-server/</link>
		<comments>http://www.bombstrike.org/2009/02/bringing-javascript-to-the-server/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 14:38:09 +0000</pubDate>
		<dc:creator>Christophe Robin</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[server-side]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://www.bombstrike.org/?p=37</guid>
		<description><![CDATA[I&#8217;ve been working for the last month on my first PHP extension since 2 years ago, a nice way to learn the new stuff Zend had in store for PHP 5.3. This extension is named SpiderMonkey, and embed the original engine made by Mozilla and available on most Linux distributions.
SpiderMonkey
SpiderMonkey is the Javascript engine used [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working for the last month on my first PHP extension since 2 years ago, a nice way to learn the new stuff Zend had in store for PHP 5.3. This extension is named SpiderMonkey, and embed the original engine made by Mozilla and available on most Linux distributions.</p>
<p><strong>SpiderMonkey</strong></p>
<p>SpiderMonkey is the Javascript engine used by Mozilla&#8217;s main products. It provide a nice C API for executing and interacting with Javascript. I base myself on the 1.7.0 version, which is the latest stable version. 1.8.0 is supposed to come soon but is still under development.</p>
<p><strong>Usage</strong></p>
<p>The API is straight-forward, a Javascript Runtime is first created, it&#8217;s a container for all &#8220;global&#8221; variables and Javascript Contexts. Contexts is the global scope where your program runs, all scripts executed on the same contexts will share the same global variables.</p>
<p><strong>This part changed in rev. 38, see below</strong></p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/* Initialisation of extension */</span><br />
<span style="color: #000088;">$rt</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSRuntime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">/* Create a single context, most application will only use<br />
* one context while a server ( like the pinetd httpd server )<br />
* would create a context by request */</span><br />
<span style="color: #000088;">$ctx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p><strong>Since rev. 38, you don&#8217;t need to instanciate the JSRuntime anymore</strong></p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/* Create a single context, most application will only use<br />
* one context while a server ( like the pinetd httpd server )<br />
* would create a context by request */</span><br />
<span style="color: #000088;">$ctx</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Once the context created, you&#8217;ll want to do two things:</p>
<ol>
<li>Assign values from PHP to JS</li>
<li>Register functions from PHP to JS</li>
</ol>
<p>For now, variables assigned from PHP are converted to a new Javascript variable and changing them in JS will not affect the original PHP value, while it&#8217;s supposed to be allowed later ( using a different function that will assign the variable as a reference and not a copy ), it&#8217;s already enough to provide <em>$_GET, $_POST, $_FILES</em> and the likes to JS. The only case where this change is for objects and resources, which keeps pointers to themselves thus allowing you to export a <em>$dom</em> variable containing a DOMDocument and acting on it by the mean of the object methods ( the object properties are not available ).</p>
<p>Exported functions are declared in the global scope.</p>
<p>Here is an exemple with <strong>mysqli</strong>:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/* Create a new database object */</span><br />
<span style="color: #000088;">$mysqli</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;db&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">/* Allow Javascript to call sprintf under the name sprintf */</span><br />
<span style="color: #000088;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sprintf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sprintf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">/* Provide our db connexion to Javascript */</span><br />
<span style="color: #000088;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'db'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mysqli</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then in Javascript you&#8217;d do:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* i know the sprintf is not the best way to do it, but for readability<br />
* i'll stick with it in this sample */</span><br />
res <span style="color: #339933;">=</span> db.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span>sprintf<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SELECT * FROM my_table WHERE field = %s'</span><span style="color: #339933;">,</span> db.<span style="color: #660066;">escape_string</span><span style="color: #009900;">&#40;</span>dynamic_data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> res.<span style="color: #660066;">fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; printf<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> line.<span style="color: #660066;">field2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Because this is based on PHP 5.3, you could also use closures in the assign function, allowing things like:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Allowing you to create a function foo() that echo &#8220;bar&#8221; when called.</p>
<p><strong>Future</strong></p>
<p>A lot of new features are already being implemented, here is a non-exhaustive list:</p>
<ol>
<li>Allowing to pass all variables as reference, allowing the Javascript to modify the PHP value</li>
<li>Allow to register &#8220;classes&#8221; prototypes, like:
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$ctx</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMDocument'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then you could just do &#8220;<em>var a = DOMDocument();</em>&#8221; in Javascript to instanciate the object.</li>
<li>Autoregister a prototype on some types of resources. A Stream resource could then be assigned in JS, and you could call stream.write, stream.read, etc&#8230;</li>
</ol>
<p><strong>How-to test ?</strong></p>
<p>The whole source code is available on a public read-only SVN: <a href="https://ookoo.org/svn/pecl-spidermonkey">https://ookoo.org/svn/pecl-spidermonkey</a>. Be careful, you need PHP 5.3.0 beta 1 to compile it ( it will not even compile on PHP 5.2 ). There may be some issues when dealing with many objects so don&#8217;t use it for production purpose.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> https:<span style="color: #000000; font-weight: bold;">//</span>ookoo.org<span style="color: #000000; font-weight: bold;">/</span>svn<span style="color: #000000; font-weight: bold;">/</span>pecl-spidermonkey<br />
<span style="color: #7a0874; font-weight: bold;">cd</span> pecl-spidermonkey<br />
phpize<br />
.<span style="color: #000000; font-weight: bold;">/</span>configure<br />
<span style="color: #c20cb9; font-weight: bold;">make</span><br />
.<span style="color: #000000; font-weight: bold;">/</span>test.sh</div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.bombstrike.org/2009/02/bringing-javascript-to-the-server/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
