<?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>Create Blog &#187; V8</title>
	<atom:link href="http://create.tpsitulsa.com/blog/category/v8/feed/" rel="self" type="application/rss+xml" />
	<link>http://create.tpsitulsa.com/blog</link>
	<description>the create framework blog</description>
	<lastBuildDate>Sat, 30 Jan 2010 20:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Making SproutCore Objects Show in Chromium&#8217;s Heap Profiler</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/15/sc-objects-heap-profiler/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/15/sc-objects-heap-profiler/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 16:38:08 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[V8]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[chromium]]></category>
		<category><![CDATA[google chrome]]></category>
		<category><![CDATA[memory leaks]]></category>
		<category><![CDATA[profiler]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=277</guid>
		<description><![CDATA[It&#8217;s a hack. If Chromium+v8 would just support displayName, it could all be nice and clean and part of SproutCore (please go tell the Chromium folks how helpful this would be). As it is, this hack could have some SEVERE side-effects, but chances are, if you are trying to debug a memory leak, you won&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a hack. If Chromium+v8 would <a href="http://code.google.com/p/chromium/issues/detail?id=17356&#038;q=area%3DDevTools&#038;colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner%20Mstone%20OS">just support displayName</a>, it could all be nice and clean and part of SproutCore (please go tell the Chromium folks how helpful this would be).</p>

<p>As it is, this hack could have some <em>SEVERE</em> side-effects, but chances are, if you are trying to debug a memory leak, you won&#8217;t care about these side-effects because everything else will be driving you MAD (and, you&#8217;ll probably be able to find these side-effects much more easily than the leak itself).</p>

<p>It is very simple; here is how to do it for ListItemView:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">SC.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>SC.<span style="color: #660066;">ListItemView</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> create<span style="color: #339933;">:</span> <span style="color: #003366; 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: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> SC._ListItemView<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
SC._ListItemView <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>base_type<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  base_type.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// for extra safety (should get around most potential side-effects):</span>
SC.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>SC._ListItemView<span style="color: #339933;">,</span> SC.<span style="color: #660066;">ListItemView</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
SC._ListItemView.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ListItemView</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span></pre></div></div>


<p>Now, ListItemViews will show up as SC._ListItemView in the heap profile. Hooray!</p>

<p>P.S. This revealed for me that the objects leaking were not SC.ListItemViews. I still have no idea what is leaking, and am giving up for now—but at least I&#8217;ll have better tools when I come back to it. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/15/sc-objects-heap-profiler/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Script &amp; the USPS Intelligent Mail Barcode</title>
		<link>http://create.tpsitulsa.com/blog/2009/03/16/script-the-usps-intelligent-mail-barcode/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/03/16/script-the-usps-intelligent-mail-barcode/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 04:08:28 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Barcode]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[USPS]]></category>
		<category><![CDATA[V8]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=105</guid>
		<description><![CDATA[We need a way to integrate the new USPS Intelligent Mail Barcode nicely into our existing workflow. The software we use for most of our composition seems to have some&#8230; disagreements (or something)&#8230; with it at times, even though it officially supports it. Other people using the same software recommend using the USPS&#8217;s library. That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>We need a way to integrate the new USPS Intelligent Mail Barcode nicely into our existing workflow. The software we use for most of our composition seems to have some&#8230; <em>disagreements (or something)</em>&#8230; with it at times, even though it officially supports it.</p>

<p>Other people using the same software recommend using the USPS&#8217;s library. That&#8217;s fine and all, but we don&#8217;t want to write a C program for each job &mdash; writing in C or C++ can be quite time consuming. And it requires compiling, creating projects, etc. In short, a lot of hassle.</p>

<p>While this may not be the course we end up following, I decided, tonight, that although I was half-asleep, I&#8217;d attempt to add scripting support for USPS&#8217;s library. At around 10:00, I downloaded the software from USPS. Now, roughly fifty minutes later, I have managed to write a script that uses the USPS barcode.</p>

<p><span id="more-105"></span></p>

<p>Here&#8217;s the script:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span>input<span style="color: #339933;">,</span> output<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> inputFile <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> InputStream<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> outputFile <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> OutputStream<span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>inputFile.<span style="color: #660066;">ok</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">//get line</span>
		<span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> inputFile.<span style="color: #660066;">getLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//split</span>
		<span style="color: #003366; font-weight: bold;">var</span> fields <span style="color: #339933;">=</span> line.<span style="color: #660066;">splitQuoted</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//if there aren't the required amount of fields, continue to next line (if any)</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fields.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			outputFile.<span style="color: #660066;">writeLine</span><span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//get needed fields</span>
		<span style="color: #003366; font-weight: bold;">var</span> track <span style="color: #339933;">=</span> fields<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> route <span style="color: #339933;">=</span> fields<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//set output</span>
		line <span style="color: #339933;">=</span> line <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> USPSBarcode.<span style="color: #660066;">encode</span><span style="color: #009900;">&#40;</span>track<span style="color: #339933;">,</span> route<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;">//write</span>
		outputFile.<span style="color: #660066;">writeLine</span><span style="color: #009900;">&#40;</span>line<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;!--</span> snipped<span style="color: #339933;">:</span> the splitQuoted string <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #339933;">--&gt;</span></pre></div></div>


<p>The Framework&#8217;s Script Engine made adding this to the Shell very easy (really, it should be moved into Spec, and Shell should get it from there, but for now&#8230;).</p>

<p>I will, over the course of the next couple of days, figure out a workflow for how I can get this code online without breaking the build for compiles without the library. Perhaps a command line switch? I will probably also move it to where it belongs: in Spec.</p>

<p>I will, however, go ahead and post the majority of the code here. I could have done it much quicker with a single function in global scope, but I prefer the cleanliness of using a JavaScript object (I could have still used an empty dummy object, but that wouldn&#8217;t have been proper).</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">#ifndef SHELL_WRAPPERS_USPSBARCODEWRAPPER_H_
#define SHELL_WRAPPERS_USPSBARCODEWRAPPER_H_
&nbsp;
#ifdef SHELL_WITH_USPS_BARCODE
&nbsp;
#include <span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span>
&nbsp;
extern <span style="color: #3366CC;">&quot;C&quot;</span> <span style="color: #009900;">&#123;</span>
	int USPS4CB<span style="color: #009900;">&#40;</span> char <span style="color: #339933;">*</span>TrackPtr<span style="color: #339933;">,</span> char <span style="color: #339933;">*</span>RoutePtr<span style="color: #339933;">,</span> char <span style="color: #339933;">*</span>BarPtr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">namespace</span> Shell
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">class</span> USPSBarcodeProvider
	<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">public</span><span style="color: #339933;">:</span>
		USPSBarcodeProvider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		std<span style="color: #339933;">::</span>string encode<span style="color: #009900;">&#40;</span>std<span style="color: #339933;">::</span>string tracker<span style="color: #339933;">,</span> std<span style="color: #339933;">::</span>string route<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tracker.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>
				route.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">11</span> <span style="color: #339933;">&amp;&amp;</span> 
				route.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">5</span> <span style="color: #339933;">&amp;&amp;</span> 
				route.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">9</span> <span style="color: #339933;">&amp;&amp;</span>
				route.<span style="color: #660066;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			char res<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">66</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			int result <span style="color: #339933;">=</span> USPS4CB<span style="color: #009900;">&#40;</span>
								 const_cast<span style="color: #339933;">&lt;</span>char <span style="color: #339933;">*&gt;</span> <span style="color: #009900;">&#40;</span>tracker.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
								 const_cast<span style="color: #339933;">&lt;</span>char <span style="color: #339933;">*&gt;</span> <span style="color: #009900;">&#40;</span>route.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
								 res
			<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>result <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
			res<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">65</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">return</span> res<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ScriptBaseNative<span style="color: #009900;">&#40;</span>Shell<span style="color: #339933;">::</span>USPSBarcodeProvider<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">namespace</span> Shell
<span style="color: #009900;">&#123;</span>
	SWrap<span style="color: #009900;">&#40;</span>USPSBarcodeWrapper<span style="color: #339933;">,</span> USPSBarcodeProvider<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">public</span><span style="color: #339933;">:</span>
		USPSBarcodeWrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			set<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;encode&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span>encode<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			className <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;USPSBarcodeProvider&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		SMember<span style="color: #009900;">&#40;</span>encode<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>extract<span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span>extract<span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>encode<span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span>stringValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span>stringValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
#endif
&nbsp;
#endif</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/03/16/script-the-usps-intelligent-mail-barcode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>v8, Application Exit, and Destructors</title>
		<link>http://create.tpsitulsa.com/blog/2009/02/20/v8-application-exit-and-destructors/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/02/20/v8-application-exit-and-destructors/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 00:22:12 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[V8]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=97</guid>
		<description><![CDATA[While working on a utility that may perhaps evolve into the next version of the Engine, written primarily in JavaScript, I ran into a small problem when I tried to wrap output files for use in JavaScript. v8, when it &#8220;goes away&#8221; at program end, does not collect its garbage. Not usually a problem, as [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a utility that may perhaps evolve into the next version of the Engine, written primarily in JavaScript, I ran into a small problem when I tried to wrap output files for use in JavaScript.</p>

<p>v8, when it &#8220;goes away&#8221; at program end, does not collect its garbage. Not usually a problem, as most of the time, the operating system will clean up after you. But I didn&#8217;t need the memory cleared. I needed a destructor called.</p>

<p>The output file wrapper was wrapping a class which wrapped ostream. The standard library stream usually uses a buffer, and only calling flush() or destroying the stream flushes it to the actual output. I needed this to happen automatically, so I wrote up a simple template class, <em>AutoDestroy</em>, along with a helper function, <em>autoDestroy</em>.</p>

<p><span id="more-97"></span></p>

<p>I call it roughly like this, but with smart pointers and other code mixed in:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//create a stream</span>
OutputStream <span style="color: #000040;">*</span>stream <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> OutputStream<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//set auto destroy</span>
autoDestroy<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//stream is deleted</span>
<span style="color: #0000dd;">delete</span> stream<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//cancel auto destroy</span>
cancelAutoDestroy<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>


<p>The code itself is pretty simple. There are probably some ways that are even simpler, more elegant, and take fewer lines of code, but this does seem to work:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/*
 * This template class is a very simple class which allows registration of items
 * needing destruction.
 *
 * Very simple: a set of items to destroy, and a map to keep track of how many
 * destruction references there are (allowing add and remove to be called
 * more than once).
 *
 * It is split into two parts (the AutoDestroy class and Destroyer) so that
 * the actual destruction call can be overridden (by specializing Destroyer).
 */</span>
&nbsp;
&nbsp;
<span style="color: #339900;">#include &lt;set&gt;</span>
<span style="color: #339900;">#include &lt;map&gt;</span>
&nbsp;
<span style="color: #0000ff;">namespace</span> Shell
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">class</span> Destroyer
	<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		Destroyer<span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ptr <span style="color: #000080;">=</span> what<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">void</span> destroy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">delete</span> ptr<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
		T <span style="color: #000040;">*</span>ptr<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">class</span> AutoDestroy
	<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
		<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Add<span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>add<span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Remove<span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #0000dd;">remove</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">virtual</span> ~AutoDestroy<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">typename</span> std<span style="color: #008080;">::</span><span style="color: #007788;">set</span><span style="color: #000080;">&lt;</span>T <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> i<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> autoDestroy.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i <span style="color: #000040;">!</span><span style="color: #000080;">=</span> autoDestroy.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				Destroyer<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span> destroyer<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				destroyer.<span style="color: #007788;">destroy</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">static</span> AutoDestroy <span style="color: #000040;">*</span>Instance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">static</span> AutoDestroy instance<span style="color: #008080;">;</span>
			<span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>instance<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
		std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>T <span style="color: #000040;">*</span>, <span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> references<span style="color: #008080;">;</span>
		std<span style="color: #008080;">::</span><span style="color: #007788;">set</span><span style="color: #000080;">&lt;</span>T <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> autoDestroy<span style="color: #008080;">;</span>
&nbsp;
		<span style="color: #0000ff;">void</span> add<span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #666666;">//if it doesn't exist yet, add it.</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>autoDestroy.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				<span style="color: #666666;">//insert into auto destroy list</span>
				autoDestroy.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #666666;">//set reference to 1</span>
				references<span style="color: #008000;">&#91;</span>what<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
				<span style="color: #666666;">//return because references is already 1</span>
				<span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #666666;">//increment reference</span>
			references<span style="color: #008000;">&#91;</span>what<span style="color: #008000;">&#93;</span><span style="color: #000040;">++</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0000ff;">void</span> <span style="color: #0000dd;">remove</span><span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #666666;">//if it doesn't exist, return</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>autoDestroy.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
&nbsp;
			<span style="color: #666666;">//decrement references, and if there aren't any, remove from list.</span>
			<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">--</span>references<span style="color: #008000;">&#91;</span>what<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				autoDestroy.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
				references.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #ff0000; font-style: italic;">/* Helper functions to make things dead simple */</span>
	<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">void</span> autoDestroy<span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		AutoDestroy<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Add</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">void</span> cancelAutoDestroy<span style="color: #008000;">&#40;</span>T <span style="color: #000040;">*</span>what<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		AutoDestroy<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Remove</span><span style="color: #008000;">&#40;</span>what<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/02/20/v8-application-exit-and-destructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On V8 and Multithreading</title>
		<link>http://create.tpsitulsa.com/blog/2009/02/02/on-v8-and-multithreading/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/02/02/on-v8-and-multithreading/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 04:02:22 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[V8]]></category>
		<category><![CDATA[multithreading]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=76</guid>
		<description><![CDATA[V8 is not thread-safe. Just look at the code for HandleScopes, located in handles.h: static v8::ImplementationUtilities::HandleScopeData current_; const v8::ImplementationUtilities::HandleScopeData previous_; The way HandleScopes work is simple: each handle is allocated in the current handle scope (HandleScope::current_). When a new HandleScope is created, it sets its previous_ to the previous one, and changes current_ to itself. [...]]]></description>
			<content:encoded><![CDATA[<p>V8 is not thread-safe. Just look at the code for HandleScopes, located in <a href="http://code.google.com/p/v8/source/browse/trunk/src/handles.h">handles.h</a>:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">static</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">ImplementationUtilities</span><span style="color: #008080;">::</span><span style="color: #007788;">HandleScopeData</span> current_<span style="color: #008080;">;</span>
<span style="color: #0000ff;">const</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">ImplementationUtilities</span><span style="color: #008080;">::</span><span style="color: #007788;">HandleScopeData</span> previous_<span style="color: #008080;">;</span></pre></div></div>


<p>The way HandleScopes work is simple: each handle is allocated in the current handle scope (<em>HandleScope::current_</em>). When a new HandleScope is created, it sets its <em>previous_</em> to the previous one, and changes <em>current_</em> to itself. The current handle scope is stored statically &mdash; in short, shared between all threads.</p>

<p>Logic dictates that this would cause problems in multithreading. Big time.</p>

<p>Consider the following overly simplified example:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//thread 1</span>
HandleScope handle_scope<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//thread 2</span>
HandleScope handle_scope<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//thread 1</span>
Local<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span> test <span style="color: #000080;">=</span> String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;test&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">//allocated in thread 2's context scope!</span>
&nbsp;
<span style="color: #666666;">//thread 2 closes, destroying its handle scope, and thus</span>
<span style="color: #666666;">//test, which was allocated inside of it</span>
<span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//thread 1 continues, and likely crashes, as it tries to use test, which was</span>
<span style="color: #666666;">//mistakenly allocated inside of thread 2's handle scope (and thread 2's</span>
<span style="color: #666666;">//handle scope has now ended).</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>test<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>IsString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>&#8220;That&#8217;s why that is no work.&#8221;</p>

<p><strong>Update: </strong> With enough locking, it <em>is</em> possible. v8 even has a <em>Locker</em> object to help. But it is still quite tricky, and appears to be prone to failure. I haven&#8217;t tested it much myself, as I currently don&#8217;t have a need for threading.</p>

<p>It would still be quite dangerous, especially if one unlocks while inside of a HandleScope.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/02/02/on-v8-and-multithreading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interesting Bug #1: The Case of the Sticky Object</title>
		<link>http://create.tpsitulsa.com/blog/2009/02/02/interesting-bug-1-the-case-of-the-sticky-object/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/02/02/interesting-bug-1-the-case-of-the-sticky-object/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 03:43:15 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[V8]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=73</guid>
		<description><![CDATA[In my post on persistent handles, I mentioned that it is good to keep a map between native C++ objects and their JavaScript wrappers, so that you can reuse the wrappers. I also mentioned that I clear V8&#8242;s reference to the native object when C++ &#8220;forces&#8221; a destroy. Everything worked, except one thing: when I [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="/blog/2009/01/31/persistent-handles/">post on persistent handles</a>, I mentioned that it is good to keep a map between native C++ objects and their JavaScript wrappers, so that you can reuse the wrappers.</p>

<p>I also mentioned that I clear V8&#8242;s reference to the native object when C++ &#8220;forces&#8221; a destroy.</p>

<p>Everything worked, except one thing: when I cleared the reference to the native object, I did not clear the reference from the map. Lo and behold, at some later point in time, when such an object is created <em>again</em>, the pointer to the original is still in my map. So, when I ask the map if a wrapper exists, it <em>does</em>. And its reference is NULL. So, I just passed a non-NULL object to JavaScript, and JavaScript got a NULL value.</p>

<p>All I needed to do to fix it was add a <em>std::set</em> of things to alert to the clearing of the reference, add the map-containing object to that set, and call a function which erases the entry in the map-containing object when the reference is cleared.</p>

<p>It is quite lucky for me that this bug occurred. It occurred due to an unintended consequence of normal object destruction. Accidentally, when the object was destroyed even through a process <em>initiated from JavaScript</em>, the &#8220;force destruction&#8221; message was sent out. Had this unintended side-effect to destruction not been there, I would not have noticed anything, and some wacky consequences of reusing wrappers for unrelated objects could have been extremely difficult to debug.</p>

<p>A bug a day keeps the doctor away?</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/02/02/interesting-bug-1-the-case-of-the-sticky-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persistent Handles</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/31/persistent-handles/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/01/31/persistent-handles/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 19:44:31 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[V8]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=33</guid>
		<description><![CDATA[You probably don&#8217;t like memory leaks. I don&#8217;t. But, if you have one object that needs to pass between two languages &#8212; C++ and JavaScript &#8212; each of which may keep that object indefinitely, it can become tricky to make sure that any given object is destroyed, and that it is destroyed at the right [...]]]></description>
			<content:encoded><![CDATA[<p>You probably don&#8217;t like memory leaks. I don&#8217;t. But, if you have one object that needs to pass between two languages &mdash; C++ and JavaScript &mdash; each of which may keep that object indefinitely, it can become tricky to make sure that any given object is destroyed, and that it is destroyed at the right time.</p>

<p>There are two things that can happen: an object can be deleted two soon, while it is still being used elsewhere &mdash; causing some particularly delicious crashes &mdash; or, an object can be deleted too late (or not at all), causing memory leaks.</p>

<p><span id="more-33"></span></p>

<p>Consider the point wrapping function from my <a href="http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/">previous post</a>:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Handle<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> wrapPoint<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>pointToWrap<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//enter a handle scope</span>
    HandleScope handle_scope<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//create a new point instance</span>
    Local<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> point_instance <span style="color: #000080;">=</span> point_templ<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NewInstance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//set that internal field</span>
    point_instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalField<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, External<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>pointToWrap<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//to prevent the point_instance from being destroyed when its</span>
    <span style="color: #666666;">//scope handle_scope is, use the Close() function</span>
    <span style="color: #0000ff;">return</span> handle_scope.<span style="color: #007788;">Close</span><span style="color: #008000;">&#40;</span>point_instance<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>What would happen if you passed a point wrapped using this function to JavaScript, and whatever JavaScript code you had decided to store it? It would then be a very bad idea to destroy it from C++: JavaScript would not know it was destroyed. If, for example, an accessor was called, it would call the C++ function corresponding to the accessor, and that C++ function would try to operate on the pointer stored in the internal field &mdash; the pointer to the point you destroyed.</p>

<p>There are a few possible solutions to the problem:</p>

<ul>
    <li><strong>Let JavaScript destroy it.</strong> For this, you need to know when JavaScript is no longer using the object. Thankfully, this is possible.</li>

    <li><strong>Have C++ alert JavaScript to the destruction.</strong> For this, you&#8217;d have to keep track of the JavaScript object, which means you&#8217;d have to keep an active reference to it. As normal local handles get destroyed when their context scope goes away, you need a way to make sure the object sticks around in JavaScript, or a way to know that if it is no longer referenced in JavaScript, or both.</li>
    
    <li><strong>Use smart pointers.</strong> In this case, you might store a pointer to a smart pointer to the object in the internal field. You&#8217;d still need to know when the object was no longer being used in JavaScript so you could get rid of that smart pointer, but in this case, the object will stick around so long as there is a reference to it from <em><strong>either</strong></em> C++ or JavaScript.</li>

    <li><strong>Hybrid.</strong> Perhaps you will need to have the ability to keep objects alive by referencing them from JavaScript, but there are some situations in which you simply want to get rid of them (and JavaScript should no longer be able to access them). In this case, you need to keep track of the objects <em>and</em> use a smart-pointer like methodology. I won&#8217;t go into this, as it is (at least the method I use) a bit too tedious to go over in detail.</li>
</ul>

<p><strong>Note: </strong> Keeping track of the wrapped C++ objects in JavaScript can be useful for reasons other than memory management. The technique (which <a href="http://src.chromium.org/viewvc/chrome/branches/chrome_official_branch/src/webkit/port/bindings/v8/dom_wrapper_map.h">is also used</a> in Google&#8217;s Chrome browser) also allows you to generate and reuse a single wrapper for any given object. In addition to being more memory efficient, this also allows JavaScript to add things to the object that will be remembered even if it jumps between languages a few times.</p>

<p>All of these techniques have one thing in common: they require the use of persistent handles.</p>

<h3>Persistent Handles</h3>

<p>A persistent handle is much like a C++ pointer:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//something like Value *value = new String(&quot;Hello, World&quot;);</span>
Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span> value <span style="color: #000080;">=</span> Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hello, World&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//something like delete value</span>
value.<span style="color: #007788;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//something like value = NULL;</span>
value.<span style="color: #007788;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>


<p>Persistent handles seem to be a bit like a handle pointing to a handle. The handle being pointed to will not be destroyed until it is specifically told to.</p>

<p>For instance, the following will cause memory leaks:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//something like Value *value = new String(&quot;Hello, World&quot;);</span>
Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span> value <span style="color: #000080;">=</span> Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hello, World&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//like setting value = new String(&quot;Hello, World 2&quot;); Where does the previous string go?</span>
<span style="color: #666666;">//answer: it floats.</span>
value <span style="color: #000080;">=</span> Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hello, World 2&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//and what about if we don't dispose of our persistent pointer</span>
<span style="color: #666666;">//if we don't do the equivalent of calling &quot;delete value;&quot;</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span></pre></div></div>


<p>You might be able to see how this could be used to keep a reference in a map between C++ objects and JavaScript objects, so that you could clear the JavaScript object&#8217;s internal field:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">map</span><span style="color: #000080;">&lt;</span>Point <span style="color: #000040;">*</span>, Persistent<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> jsPoints<span style="color: #008080;">;</span></pre></div></div>


<p>And then you could force the deletion of one something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> destroyJSPointReference<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>point<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//get the corresponding object</span>
    Persistent<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> object <span style="color: #000080;">=</span> jsPoints<span style="color: #008000;">&#91;</span>point<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//clear internal field</span>
    object<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalField<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//dispose and clear so that it will go away when nothing points to it</span>
    object.<span style="color: #007788;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    object.<span style="color: #007788;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<h3>Weak Handles</h3>

<p>But what if we wanted to know when JavaScript no longer uses an object? For that, we make the persistent handle <em>weak</em>. A weak persistent handle triggers a callback function when only weak handles point at an object. So, an adjusted wrapping function might look like this:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Handle<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> wrapPoint<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>pointToWrap<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//enter a handle scope</span>
    HandleScope handle_scope<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//store the wrappers in a map so that we don't double-delete. It's like double-dipping,</span>
    <span style="color: #666666;">//but seven times worse.</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>jsPoints.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>pointToWrap<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> jsPoints<span style="color: #008000;">&#91;</span>pointToWrap<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//create a new point instance and point to it with a persistent handle</span>
    Persistent<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> point_instance <span style="color: #000080;">=</span> 
    Persistent<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>point_templ<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NewInstance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//make the handle weak, with a callback</span>
    point_instance.<span style="color: #007788;">MakeWeak</span><span style="color: #008000;">&#40;</span>pointToWrap, weakPointCallback<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//add it to the map</span>
    jsPoints<span style="color: #008000;">&#91;</span>pointToWrap<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> point_instance<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
    <span style="color: #666666;">//set that internal field</span>
    point_instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalField<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, External<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>pointToWrap<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//to prevent the point_instance from being destroyed when its</span>
    <span style="color: #666666;">//scope handle_scope is, use the Close() function</span>
    <span style="color: #0000ff;">return</span> handle_scope.<span style="color: #007788;">Close</span><span style="color: #008000;">&#40;</span>point_instance<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> weakPointCallback<span style="color: #008000;">&#40;</span>Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span> object, <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>parameter<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Point <span style="color: #000040;">*</span>point <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>Point <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#40;</span>parameter<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//remove it from the map</span>
    jsPoints.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//javascript no longer uses the object</span>
    <span style="color: #666666;">//if we are certain C++ isn't using it, we could &quot;Let JavaScript destroy it&quot;:</span>
    <span style="color: #0000dd;">delete</span> point<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//clear the reference to it</span>
    object.<span style="color: #007788;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    object.<span style="color: #007788;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>Now, using smart pointers in the mix is relatively easy, but will depend on what kind of smart pointer methodology you use &mdash; my example may not apply to you. If you really really don&#8217;t want to use smart pointers, you can still achieve a bit of the same effect &mdash; although I don&#8217;t recommend it. Consider:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">set</span><span style="color: #000080;">&lt;</span>Point <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> usedInCPP<span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">set</span><span style="color: #000080;">&lt;</span>Point <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> usedInJS<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> JSDeletePoint<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>point<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    usedInJS.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>usedInCPP.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000dd;">delete</span> point<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> CPPDeletePoint<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>point<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    usedInCPP.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>usedInJS.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000dd;">delete</span> point<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>Something like this could allow you to keep track of references.</p>

<p>Using smart pointers is much easier. Note: my smart pointer type is named <em>R</em>:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
Handle<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> wrapPoint<span style="color: #008000;">&#40;</span>R<span style="color: #000080;">&lt;</span>Point<span style="color: #000080;">&gt;</span> pointToWrap<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//enter a handle scope</span>
    HandleScope handle_scope<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//store the wrappers in a map so that we have only one wrapper per object</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>jsPoints.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>pointToWrap<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> jsPoints<span style="color: #008000;">&#91;</span>pointToWrap<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//create a pointer to a smart pointer by making a new smart pointer</span>
    R<span style="color: #000080;">&lt;</span>Point<span style="color: #000080;">&gt;</span> <span style="color: #000040;">*</span>point <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> R<span style="color: #000080;">&lt;</span>Point<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#40;</span>pointToWrap<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//create a new point instance and point to it with a persistent handle</span>
    Persistent<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> point_instance <span style="color: #000080;">=</span> 
    Persistent<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>point_templ<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NewInstance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//make the handle weak, with a callback</span>
    point_instance.<span style="color: #007788;">MakeWeak</span><span style="color: #008000;">&#40;</span>point, weakPointCallback<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//add it to the map</span>
    jsPoints<span style="color: #008000;">&#91;</span>pointToWrap<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> point_instance<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
    <span style="color: #666666;">//set that internal field to the pointer to the smart pointer</span>
    point_instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalField<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, External<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//to prevent the point_instance from being destroyed when its</span>
    <span style="color: #666666;">//scope handle_scope is, use the Close() function</span>
    <span style="color: #0000ff;">return</span> handle_scope.<span style="color: #007788;">Close</span><span style="color: #008000;">&#40;</span>point_instance<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> weakPointCallback<span style="color: #008000;">&#40;</span>Persistent<span style="color: #000080;">&lt;</span>Value<span style="color: #000080;">&gt;</span> object, <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>parameter<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    R<span style="color: #000080;">&lt;</span>Point<span style="color: #000080;">&gt;</span> <span style="color: #000040;">*</span>point <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>R<span style="color: #000080;">&lt;</span>Point<span style="color: #000080;">&gt;</span> <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#40;</span>parameter<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//remove it from the map</span>
    jsPoints.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>point<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//delete the smart pointer</span>
    <span style="color: #0000dd;">delete</span> point<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//clear the reference to it</span>
    object.<span style="color: #007788;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    object.<span style="color: #007788;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>That&#8217;s roughly what I do in the script engine, but I also have the ability to force the deletion of an object entirely. I do this by not actually giving a pointer, or a pointer to a smart pointer, to JavaScript at all, but instead giving it a smart pointer to a class which wraps the object. This wrapper can then be alerted when the object is destroyed, and its reference to the object will then be cleared.</p>

<p>I hope some people will find this helpful &mdash; I would have liked to have it when I was starting out with V8 a couple of weeks ago.</p>

<p>Please feel free to correct me or ask questions if you see anything fishy or strange.</p>

<p><strong>Update: </strong> I realized I was forgetting to dispose of the persistent handle in the two examples above (lines object.Dispose and object.Clear). I have corrected this.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/01/31/persistent-handles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>V8 Objects</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 21:11:02 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[V8]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14</guid>
		<description><![CDATA[So, a problem arose. I wanted to account for two possibilities in the Script Engine: Passing existing objects from C++ to JavaScript, and creating new C++ objects from JavaScript. The act of passing a native object into scripting languages is usually referred to as wrapping (although, at one point inside the engine, I actually refer [...]]]></description>
			<content:encoded><![CDATA[<p>So, a problem arose.</p>

<p>I wanted to account for two possibilities in the Script Engine: Passing existing objects from C++ to JavaScript, and creating new C++ objects from JavaScript. The act of passing a native object into scripting languages is usually referred to as wrapping (although, at one point inside the engine, I actually refer to a part of the process as <em>unwrapping</em>).</p>

<p>The code I would like to be able to use is:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//create a point</span>
Point <span style="color: #000040;">*</span>point <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Point<span style="color: #008000;">&#40;</span><span style="color:#800080;">.42</span>, <span style="color: #0000dd;">42</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//pass it to some JavaScript function</span>
myJavaScriptFunction<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Call<span style="color: #008000;">&#40;</span>pointWrap<span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>



<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//create a Point</span>
<span style="color: #003366; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Point<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//set its X value</span>
point.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">4.2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//call some C++ function using it</span>
myCPPFunction<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>So how did I manage this in the engine? Well, as I am prone do, I overcomplicated it.</p>

<p><span id="more-14"></span>
I started by finding examples. V8 being as young as it is, there weren&#8217;t many. However, I did manage to find one in some older posts on the v8-users discussion group. Modeling my code off that, I came, unfortunately, to the wrong conclusion. Instead of covering the wrong conclusion first, which I feel will merely lead to confusion, I&#8217;ll cover the way I discovered was <em>correct</em>.</p>

<p>First, a summary of class and prototype paradigms.</p>

<h3>Paradigms</h3>

<p>If you think about a class, you&#8217;ll realize there are a few different elements:</p>

<ul>
    <li>Constructors</li>
    <li>Properties</li>
    <li>Functions</li>
    <li>(Maybe) Destructors</li>
</ul>

<p>To wrap an object in a V8-accessible form (a v8::Object) you will want to connect some or all of these to JavaScript.</p>

<p>In JavaScript, if an object has a constructor, it is in fact an instance of that constructor. Consider:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//make a constructor</span>
<span style="color: #003366; font-weight: bold;">var</span> MyClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>argument<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> argument<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//add functions all instances should have</span>
MyClass.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">say</span> <span style="color: #339933;">=</span> <span style="color: #003366; 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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//make an instance</span>
<span style="color: #003366; font-weight: bold;">var</span> testClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
testClass.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//alerts &quot;test&quot;</span></pre></div></div>


<p>This creates a new instance of the MyClass function. Creating an instance calls the function, setting <em>this.value</em> to <em>argument</em>, which, in this case, equals &#8220;test.&#8221;</p>

<p>Google does not give you C++ APIs that work in the JavaScript paradigm. Nor do they give APIs that work in the C++ paradigm. I think they were trying to make it so that you could pretend the model was a class model (like C++), but there is only so far you can go while still maintaining the full ability to manipulate the JavaScript model. This adds a bit of confusion.</p>

<h3>Wrapping</h3>

<p>So, how do you force the rigid C++ class structures into this JavaScript prototype-based paradigm? There are two parts of this problem. The first is how to create new objects (constructors). The next is how those objects should act once they are responding.</p>

<p>We shall start with the second, because things are easier if you do them in that order.</p>

<p>Objects themselves (not their constructors) must be wrapped in V8 templates. I use FunctionTemplates to wrap the objects. The process is simple, and an example is on V8&#8242;s website. I&#8217;ve modified it a bit. They use ObjectTemplates. I use FunctionTemplates because they are more proper: not only does JavaScript use them to initialize objects, but they (not ObjectTemplates) are the only ones able to inherit from other templates (though I do not actually use that feature).</p>

<p>So, to prepare a template for, say, a Point, you might do something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//create your function template</span>
Handle<span style="color: #000080;">&lt;</span>FunctionTemplate<span style="color: #000080;">&gt;</span> point_template <span style="color: #000080;">=</span> FunctionTemplate<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//get the point's instance template</span>
Handle<span style="color: #000080;">&lt;</span>ObjectTemplate<span style="color: #000080;">&gt;</span> point_instance_template <span style="color: #000080;">=</span> point_template<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>InstanceTemplate<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//set its internal field count to one (we'll put references to the C++ point here later)</span>
point_instance_template<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalFieldCount<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//add some properties (x and y)</span>
point_instance_template<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetAccessor<span style="color: #008000;">&#40;</span>String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;x&quot;</span><span style="color: #008000;">&#41;</span>, GetPointX, SetPointX<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
point_instance_template<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetAccessor<span style="color: #008000;">&#40;</span>String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;y&quot;</span><span style="color: #008000;">&#41;</span>, GetPointY, SetPointY<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>


<p>Tada! Now you have a template.</p>

<p>Now that you have a template, you will want to wrap it:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Handle<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> wrapPoint<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>pointToWrap<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">//enter a handle scope</span>
    HandleScope handle_scope<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//create a new point instance</span>
    Local<span style="color: #000080;">&lt;</span>Object<span style="color: #000080;">&gt;</span> point_instance <span style="color: #000080;">=</span> point_templ<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NewInstance<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//set that internal field</span>
    point_instance<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalField<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, External<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>pointToWrap<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//I'm skipping some Persistent MakeWeak goodness right here</span>
&nbsp;
    <span style="color: #666666;">//to prevent the point_instance from being destroyed when its</span>
    <span style="color: #666666;">//scope handle_scope is, use the Close() function</span>
    <span style="color: #0000ff;">return</span> handle_scope.<span style="color: #007788;">Close</span><span style="color: #008000;">&#40;</span>point_instance<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>That&#8217;s pretty simple. Now, what about constructors?</p>

<p>There are two ways to do this. The first is the way I ended up using.</p>

<p>This way is very simple. In it, you do <em><strong>not</strong></em> use the same FunctionTemplate for the constructor as you do for the template. That&#8217;s what I did at first, and I&#8217;ll explain why it didn&#8217;t work in a moment. While it may be a function, that does not mean it automatically works as a constructor.</p>

<p>Instead of reusing the same FunctionTemplate and simply giving it a C++ function to call, you can make a <em><strong>brand new</strong></em> FunctionTemplate:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//get your scope</span>
v8<span style="color: #008080;">::</span><span style="color: #007788;">HandleScope</span> handle_scope<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//Get your V8 Context</span>
Handle<span style="color: #000080;">&lt;</span>v8<span style="color: #008080;">::</span><span style="color: #007788;">Context</span><span style="color: #000080;">&gt;</span> context <span style="color: #000080;">=</span> myContext<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//Start a context scope</span>
v8<span style="color: #008080;">::</span><span style="color: #007788;">Context</span><span style="color: #008080;">::</span><span style="color: #007788;">Scope</span> context_scope<span style="color: #008000;">&#40;</span>context<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//Get the context's global scope (that's where we'll put the constructor)</span>
v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span><span style="color: #000080;">&lt;</span>v8<span style="color: #008080;">::</span><span style="color: #007788;">Object</span><span style="color: #000080;">&gt;</span> global <span style="color: #000080;">=</span> context<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Global<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//create function template for our constructor</span>
<span style="color: #666666;">//it will call the constructPoint function</span>
v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span><span style="color: #000080;">&lt;</span>v8<span style="color: #008080;">::</span><span style="color: #007788;">FunctionTemplate</span><span style="color: #000080;">&gt;</span> function <span style="color: #000080;">=</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">FunctionTemplate</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>constructPoint<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//set the function in the global scope -- that is, set &quot;Point&quot; to the constructor</span>
global<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Set<span style="color: #008000;">&#40;</span>v8<span style="color: #008080;">::</span><span style="color: #007788;">String</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Point&quot;</span><span style="color: #008000;">&#41;</span>, function<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetFunction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>


<p>And, of course, the function it calls:</p>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span><span style="color: #000080;">&lt;</span>v8<span style="color: #008080;">::</span><span style="color: #007788;">Value</span><span style="color: #000080;">&gt;</span>constructorCall<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Arguments <span style="color: #000040;">&amp;</span>args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// throw if called without `new'</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>args.<span style="color: #007788;">IsConstructCall</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> 
		<span style="color: #0000ff;">return</span> ThrowException<span style="color: #008000;">&#40;</span>String<span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Cannot call constructor as function&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">//start a handle scope</span>
	v8<span style="color: #008080;">::</span><span style="color: #007788;">HandleScope</span> handle_scope<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">//get an x and y</span>
	<span style="color: #0000ff;">double</span> x <span style="color: #000080;">=</span> args<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NumberValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> y <span style="color: #000080;">=</span> args<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NumberValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">//generate a new point</span>
	Point <span style="color: #000040;">*</span>point <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Point<span style="color: #008000;">&#40;</span>x, y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">//return the wrapped point</span>
	<span style="color: #0000ff;">return</span> wrapPoint<span style="color: #008000;">&#40;</span>point<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>


<p>Pretty simple. We simply create a point, and pass it to the point wrapper. Ironically, something akin to this is what I attempted first, although I did it wrong. I then looked for examples, and found one that did it incorrectly. Finally, I spent several hours debugging, until I finally figured out this seemingly proper, and very simple way (anyone, feel free to correct me). While my actual code is not quite as simple as this (as I do several memory management- and abstraction-related things in there as well), this covers the point.</p>

<p>So what did I do wrong?</p>

<h3>What I Did Wrong</h3>

<p>The mistake I made was to try to use only one FunctionTemplate. Instead of having two separate ones, I made one single one. It was both the constructor and the template. Both functions had their own wrapping code (I had tried calling the wrapping function from the constructor, but it caused a recursion error, for reasons that will become clear).</p>

<p>Constructing worked great, I couldn&#8217;t have been happier. But wrapping had problems. I noticed that even when I tried to wrap an existing object, the constructor function was called, creating a brand new one! After a bit of debugging, I traced it down to the function&#8217;s NewInstance function.</p>

<p>Remember that JavaScript, when it creates a new instance of a function, calls the function. Well, I was creating a new instance of the function, and so, since I had a function (the constructor) to be called, it was. This caused a new C++ object to be created whenever I wrapped an existing one. That object would then go float around in memory, as it was replaced by the wrapping function&#8217;s <em>SetInternalField</em> call.</p>

<p>Due to the way I was managing memory, this extra allocation of objects did not seem to cause a memory leak. Still, having an extra object floating around for an object&#8217;s lifespan did not sound good, so I had to find a way to fix it. And, finally, I did.</p>

<p>It did take a few hours, however, and, unfortunately, I was not on the clock at the time.</p>

<p><strong>Update: </strong> As Matthias Ernst pointed out, my example was adding properties directly to the FunctionTemplate instead of to the Function Template&#8217;s instance template.</p>

<h3>Update: The Second Way: Copy Constructor Method</h3>

<p>Thanks to Christian Plesner Hansen for giving another way around this problem!</p>

<p>Instead of having two FunctionTemplates, you can use one, and just pass the constructor an argument pointing to the existing object:</p>

<blockquote>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span> wrapPoint<span style="color: #008000;">&#40;</span>Point <span style="color: #000040;">*</span>value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  v8<span style="color: #008080;">::</span><span style="color: #007788;">HandleScope</span> scope<span style="color: #008080;">;</span>
  v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span> cons <span style="color: #000080;">=</span> pointConstructor<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetFunction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span> external <span style="color: #000080;">=</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">External</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span> result <span style="color: #000080;">=</span> cons<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>NewInstance<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #000040;">&amp;</span>external<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> scope.<span style="color: #007788;">Close</span><span style="color: #008000;">&#40;</span>result<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>




Within the constructor you check if the first argument is an external, and if it is you don&#8217;t construct a Point:


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span> constructorCall<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">Arguments</span><span style="color: #000040;">&amp;</span> args<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  v8<span style="color: #008080;">::</span><span style="color: #007788;">HandleScope</span> scope<span style="color: #008080;">;</span>
  v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span> external<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>IsExternal<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    external <span style="color: #000080;">=</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">Handle</span><span style="color: #008080;">::</span><span style="color: #007788;">Cast</span><span style="color: #008000;">&#40;</span>args<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> args<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Int32Value<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> y <span style="color: #000080;">=</span> args<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>Int32Value<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Point <span style="color: #000040;">*</span>p <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> Point<span style="color: #008000;">&#40;</span>x, y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    external <span style="color: #000080;">=</span> v8<span style="color: #008080;">::</span><span style="color: #007788;">External</span><span style="color: #008080;">::</span><span style="color: #007788;">New</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  args.<span style="color: #007788;">This</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetInternalField<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, wrapper<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> args.<span style="color: #007788;">This</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>



</blockquote>

<h3>Chrome&#8217;s Method</h3>

<p>In web pages, most elements are not created using constructors. Instead, they are created using the <em>document.createElement()</em> function. One notable exception is the <em>img</em> element, which may <em>also</em> be created using a constructor.</p>

<p>Chrome handles this, from what I can tell by reading their code generators, etc., by running a script similar to the following in V8:</p>


<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">function Image() {
                        return document.createElementNS(
                          'http://www.w3.org/1999/xhtml', 'img');
                      };
                      Image</pre></div></div>


<p>I think they do this so they can lazy-load the Image function, as many pages don&#8217;t use it at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
