<?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; javascript</title>
	<atom:link href="http://www.bombstrike.org/tag/javascript/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>Spidermonkey gets fixed and gets ready to move from &#8220;beta&#8221; to &#8220;stable&#8221;</title>
		<link>http://www.bombstrike.org/2010/03/spidermonkey-gets-fixed-and-gets-ready-to-move-from-beta-to-stable/</link>
		<comments>http://www.bombstrike.org/2010/03/spidermonkey-gets-fixed-and-gets-ready-to-move-from-beta-to-stable/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 22:07:31 +0000</pubDate>
		<dc:creator>Christophe Robin</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[pecl]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://www.bombstrike.org/?p=125</guid>
		<description><![CDATA[Hello, it&#8217;s been a while since I last wrote something on this blog. Today I come back to tell you that version 0.1.4 of spidermonkey has finally been released and provide fixes for 3 bugs that where posted on the PECL bug tracker, I&#8217;ll do a small explanation of each bug and how they were [...]]]></description>
			<content:encoded><![CDATA[<p>Hello, it&#8217;s been a while since I last wrote something on this blog. Today I come back to tell you that version 0.1.4 of spidermonkey has finally been released and provide fixes for 3 bugs that where posted on the PECL bug tracker, I&#8217;ll do a small explanation of each bug and how they were resolved bellow. I also took care of integrating some patches for spidermonkey 1.9, added the config.w32 from <em><a href="http://developer.cybozu.co.jp/oss/">hoshino at labs dot cybozu dot co dot jp</a></em> and changed a couple stuff there and there in the source code. I also added regression tests for the solved bugs and updated some of the previous ones to reflect the corresponding changes.</p>
<h1>Fixed bugs</h1>
<h2><strong>Bug <a href="http://pecl.php.net/bugs/bug.php?id=16866" target="_blank">16866</a>:</strong></h2>
<p>This bug was pretty basic, when an empty string was returned from Javascript, the current PHP thread would segfault. The reason was simple, when Spidermonkey hold a variable that contains an empty string, it doesn&#8217;t possess a <em>char* </em>that would point to this empty string to save memory, so when I tried to retrieve this non-existing value to create an empty string in PHP, it would just segfault. I just solved this by checking the variable length in JS before doing the conversion, quite trivial compared to some bugs to come. It also solved bug <a href="http://pecl.php.net/bugs/bug.php?id=16876" target="_blank">16876</a>.</p>
<h2>Bug <a href="http://pecl.php.net/bugs/bug.php?id=16890" target="_blank">16890</a>:</h2>
<p>That one was another beast, when an object created in javascript was converted to PHP, it would become NULL on the next call. Clearly there was something weird going on here, after some painful debug, I saw that the issue came from a small caching system I had made that had no sense at all and was actually useless. Here is what it was doing:</p>
<p>When the javascript class was converted, it created the object and store it in a Zend HashTable with the JSObject id as its index. It would then retrieve the object from this &#8220;<em>cache</em>&#8221; when trying to access the object again. The basic idea at the time was to optimize objects that where converted back and forth a lot of time by pulling them from the cache, and preventing cyclic references by providing the cached objects without having to traverse it ( otherwise it would result in an infinite loop ). While the goal was great, the execution had several flaw, the biggest one was that for an unknown reason all objects that where stored were returned as NULL by the HashTable. But even if it had worked it would have caused issue with objects that were converted, modified in Javascript afterward and converted again, as the stored object would have been returned and not the updated one.</p>
<p>To solve this cyclic reference issue, I totally changed the approach by first removing this HashTable idea and then creating a basic structure that looked like:</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _php_jsparent <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; JSObject &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">*</span>obj<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; zval &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933;">*</span>zobj<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">struct</span> _php_jsparent &nbsp; &nbsp;<span style="color: #339933;">*</span>parent<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> php_jsparent<span style="color: #339933;">;</span></div></div>
<p>When I run my js to zval routine, I pass a new parameter php_jsparent *parent that is NULL by default, if i&#8217;m converting an object, I create this jsparent object, store the current JSObject being converted, the stdClass zval that is being created and our parent if theres one and call the converting method recursively on each children. Now when *parent is defined, before converting an object I do a quick traversal of the parent links and if I find a JSObject *obj that is the same as the object I&#8217;m converting I just return the stored zobj, effectively solving the cyclic referencing issue.</p>
<p><i>Sample code:</i></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">a <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> b <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#125;</span><br />
a.<span style="color: #660066;">b</span> <span style="color: #339933;">=</span> a<br />
<span style="color: #006600; font-style: italic;">/* in this case, trying to convert the a object result as a cyclic reference<br />
&nbsp; when trying to convert the properties, the new code solves this when<br />
&nbsp; trying to convert b, it checks it's parent which is in this case a, and <br />
&nbsp; see that a == b, returning then the parent zobj and solving the issue */</span></div></div>
<h2><strong>Bug <a href="http://pecl.php.net/bugs/bug.php?id=16983" target="_blank">16983</a>:</strong></h2>
<p>Another tricky one. When the magic __get() method was defined on a class it became impossible to call any method on it.</p>
<p>The issue with this one was the JS_PropertyGetterPHP() function, which is a C getter function that is called on a Javascript object even if a property exists, I would then call the Zend getter function on the object and return the value if found. The main issue was that I wasn&#8217;t taking care of properties already defined on the JS side, and always trying to retrieve the data from the PHP side. This issue was solved pretty fast, I decided to reproduce the same functionality as PHP, that means that when this getter is called it first try to retrieve data from JS, in the case of methods, if would retrieve the property I had defined beforehand when creating the object and return it to our context. If it effectively doesn&#8217;t find anything on javascript, if then fallback on the PHP item, calling __get() if necessary.</p>
<h2>Feedback</h2>
<p>Most blocking bugs have been solved with this release, feel free to post on the bug tracker any new issue you would encounter and I&#8217;ll make sure to take a look. If no bugs are found, then I&#8217;ll make sure to finish a few things there and there and will release version 1.0.0 as stable to allow more peoples to use it safely.</p>
<h1>Future tasks</h1>
<p>There are several thing I&#8217;d like to do, like better debugging support using the tracing API that is being added in spidermonkey since 1.8+. But first would be support for closures in both directions, that means having access to JS function directly from PHP (that one is really hard, it will takes some time of tingling with the PHP internals to make it works) and assigning closures in JS (should be a lot easier). I already added the test on the SVN, and I&#8217;ll try to tackle this one in the next months.<br />
If you have any idea of feature that you&#8217;d like to see implemented, feel free to ask in the comments <img src='http://www.bombstrike.org/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bombstrike.org/2010/03/spidermonkey-gets-fixed-and-gets-ready-to-move-from-beta-to-stable/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Spidermonkey in PECL</title>
		<link>http://www.bombstrike.org/2009/02/spidermonkey-in-pecl/</link>
		<comments>http://www.bombstrike.org/2009/02/spidermonkey-in-pecl/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 17:59:04 +0000</pubDate>
		<dc:creator>Christophe Robin</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[pecl]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://www.bombstrike.org/?p=107</guid>
		<description><![CDATA[Today I finally released spidermonkey on PECL, you can find it here: http://pecl.php.net/package/spidermonkey/
The whole thing should work fine, I&#8217;m already working on several optimisations thanks to jorendorff from Mozilla on IRC. The next thing is to provide better error report, compilation/decompilation of scripts and more default functions for a bunch of things like Iterators and [...]]]></description>
			<content:encoded><![CDATA[<p>Today I finally released spidermonkey on PECL, you can find it here: <a href="http://pecl.php.net/package/spidermonkey/">http://pecl.php.net/package/spidermonkey/</a></p>
<p>The whole thing should work fine, I&#8217;m already working on several optimisations thanks to jorendorff from Mozilla on IRC. The next thing is to provide better error report, compilation/decompilation of scripts and more default functions for a bunch of things like Iterators and the like.</p>
<p>The last thing I added is a bunch of function applied to streams, here is an exemple:</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 context */</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><br />
<span style="color: #666666; font-style: italic;">/* provide fopen/fclose functions */</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;">'fopen'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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;">'fclose'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">/* for output */</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;">'printf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then in Javascript:</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;">/* open file */</span><br />
fd <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'somefile.txt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'r'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> fd.<span style="color: #660066;">getline</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&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: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><br />
fclose<span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span></div></div>
<p>Or</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;">/* open file */</span><br />
fd <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'somefile.txt'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'w'</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009966; font-style: italic;">/* here fd.SEEK_SET is the default value and is optional */</span><br />
fd.<span style="color: #660066;">seek</span><span style="color: #009900;">&#40;</span>1024<span style="color: #339933;">,</span> fd.<span style="color: #660066;">SEEK_SET</span><span style="color: #009900;">&#41;</span><br />
fd.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;some text here :D&quot;</span><span style="color: #009900;">&#41;</span><br />
fclose<span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.bombstrike.org/2009/02/spidermonkey-in-pecl/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Current status and API of spidermonkey</title>
		<link>http://www.bombstrike.org/2009/02/current-status-and-api-of-spidermonkey/</link>
		<comments>http://www.bombstrike.org/2009/02/current-status-and-api-of-spidermonkey/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 20:10:38 +0000</pubDate>
		<dc:creator>Christophe Robin</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[spidermonkey]]></category>

		<guid isPermaLink="false">http://www.bombstrike.org/?p=73</guid>
		<description><![CDATA[Spidermonkey had many features and changes done this week, so let&#8217;s look at what it can actually do, and what it can&#8217;t.
Features

Execute Javascript ( well, of course it does   )
Register PHP functions to use in Javascript
Assign PHP values in Javascript
Register Classes in Javascript, allowing the user to instanciate them.

Just with those functionalities, you [...]]]></description>
			<content:encoded><![CDATA[<p>Spidermonkey had many features and changes done this week, so let&#8217;s look at what it can actually do, and what it can&#8217;t.</p>
<p><strong>Features</strong></p>
<ol>
<li>Execute Javascript ( well, of course it does <img src='http://www.bombstrike.org/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  )</li>
<li>Register PHP functions to use in Javascript</li>
<li>Assign PHP values in Javascript</li>
<li>Register Classes in Javascript, allowing the user to instanciate them.</li>
</ol>
<p>Just with those functionalities, you can already do a great deal of things with JS, and extend it really easily.</p>
<p><strong>Conversion</strong></p>
<p>When working with each others, Javascript convert is types to Zval ( PHP native type ) and vice-versa. Most types are successfully converted, but there are some exceptions:</p>
<ul>
<li>Javascript arrays are converted to stdClass due to the fact that they are stored as Objects by Spidermonkey.</li>
<li>Javascript closures are not yet supported, but because PHP 5.3 support closures too, it should be done soon.</li>
<li>Javascript regexp are not supported either, because there are no equivalents on the PHP side. In the end they&#8217;ll be converted to string.</li>
</ul>
<p>Streams resources will soon have a prototype added by default with functions like read(), getline(), write(), etc&#8230;<br />
Objects extending the Iterator abstract will also have a forEach function defined.</p>
<p><strong>API</strong></p>
<p>Here is the API:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:560px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> JSContext <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* evaluate javascript source code<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param string $script &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A string containing the javascript source code<br />
&nbsp; &nbsp; &nbsp;* @return mixed &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The last value in the global scope is returned to PHP<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> mixed &nbsp;evaluateScript<span style="color: #009900;">&#40;</span>string <span style="color: #000088;">$script</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* register a function for use in javascript<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param callback $callback &nbsp; &nbsp; &nbsp; &nbsp; A valid callback that will be called<br />
&nbsp; &nbsp; &nbsp;* @param string &nbsp; $name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The name under which the function will appear in Javascript,<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mandatory for closures and recommanded for array($obj, 'function')<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> void &nbsp; registerFunction<span style="color: #009900;">&#40;</span>callback <span style="color: #000088;">$callback</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> string <span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* register a class for use in javascript so that it can be instancied using &quot;new&quot;<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param callback $class_name &nbsp; &nbsp; &nbsp; A valid class to export<br />
&nbsp; &nbsp; &nbsp;* @param string &nbsp; $exported_name &nbsp; &nbsp;The name under which the class will appear<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> void &nbsp; registerClass<span style="color: #009900;">&#40;</span>string <span style="color: #000088;">$class_name</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> string <span style="color: #000088;">$exported_name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* register a variable for use in javascript<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param string &nbsp; $name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The name of the variable in Javascript<br />
&nbsp; &nbsp; &nbsp;* @param mixed &nbsp; &nbsp;$value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Value for the variable, objects &nbsp;and resources<br />
&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; are passed by reference<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> void &nbsp; assign<span style="color: #009900;">&#40;</span>string <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> mixed <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* those function allow you to play with the version the engine is running<br />
&nbsp; &nbsp; &nbsp;* it's not totally compatible and most versions will not be loaded by<br />
&nbsp; &nbsp; &nbsp;* the engine, you can still have a try with the constants defined above */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> mixed &nbsp;setVersion<span style="color: #009900;">&#40;</span>long <span style="color: #000088;">$version</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> long &nbsp; getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> string getVersionString<span style="color: #009900;">&#40;</span>long <span style="color: #000088;">$version</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This is the current status at revision 38. Next revisions will be aimed at bug correction and error management. If you have any ideas for features, feel free to comments this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bombstrike.org/2009/02/current-status-and-api-of-spidermonkey/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New feature for spidermonkey: registerClass()</title>
		<link>http://www.bombstrike.org/2009/02/new-feature-for-spidermonkey-registerclass/</link>
		<comments>http://www.bombstrike.org/2009/02/new-feature-for-spidermonkey-registerclass/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 00:51:28 +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[spidermonkey]]></category>

		<guid isPermaLink="false">http://www.bombstrike.org/?p=67</guid>
		<description><![CDATA[As I explained in my previous blogpost, you can export objects to spidermonkey by using the assign() function on this object. While it&#8217;s enough for basic cases, It&#8217;s not enough for complex situations where Javascript need to create itself objects instances.
Solution
Tonight I worked on the solution named &#8220;registerClass()&#8220;. As examples are better than words, here [...]]]></description>
			<content:encoded><![CDATA[<p>As I explained in my previous <a href="http://www.bombstrike.org/2009/02/bringing-javascript-to-the-server/">blogpost</a>, you can export objects to spidermonkey by using the assign() function on this object. While it&#8217;s enough for basic cases, It&#8217;s not enough for complex situations where Javascript need to create itself objects instances.</p>
<p><strong>Solution</strong></p>
<p>Tonight I worked on the solution named &#8220;<em>registerClass()</em>&#8220;. As examples are better than words, here is how it works:</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;">/* first create context */</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><br />
<span style="color: #666666; font-style: italic;">/* register mysqli */</span><br />
<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;">'mysqli'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then in your javascript source:</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">db <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> mysqli<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'host'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'user'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'pass'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'db'</span><span style="color: #009900;">&#41;</span><br />
res <span style="color: #339933;">=</span> db.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SELECT * FROM t1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<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 />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something with line</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As you can see, this function allow you to instanciate objects yourself in Javascript, providing more power and flexibility. This function will be available tomorrow morning in the SVN.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bombstrike.org/2009/02/new-feature-for-spidermonkey-registerclass/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
