<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: V8 Objects</title>
	<atom:link href="http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/</link>
	<description>the create framework blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 22:25:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: chrwal</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-363</link>
		<dc:creator>chrwal</dc:creator>
		<pubDate>Fri, 09 Jul 2010 22:25:05 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-363</guid>
		<description>&lt;p&gt;Ok Alex,&lt;/p&gt;

&lt;p&gt;I finally figured it out.  I had to cobble my solution from piecing together other samples.  In the final snippet you hang the data on the This() pointer but in the Google example they use the Holder().  I was trying to get the data from the Holder which has no slot set up for the data and thereby throws and exception.&lt;/p&gt;

&lt;p&gt;The problem I&#039;ve been having has been inconsistency among the various samples.  You example is helpful but contributes to the inconsistency especially for newbies.  I recalled you or your colleagues are working on a V8 Cookbook.  That would be very helpful and help advance adoption of the V8 engine.&lt;/p&gt;

&lt;p&gt;thx&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Ok Alex,</p>

<p>I finally figured it out.  I had to cobble my solution from piecing together other samples.  In the final snippet you hang the data on the This() pointer but in the Google example they use the Holder().  I was trying to get the data from the Holder which has no slot set up for the data and thereby throws and exception.</p>

<p>The problem I&#8217;ve been having has been inconsistency among the various samples.  You example is helpful but contributes to the inconsistency especially for newbies.  I recalled you or your colleagues are working on a V8 Cookbook.  That would be very helpful and help advance adoption of the V8 engine.</p>

<p>thx</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Iskander</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-362</link>
		<dc:creator>Alex Iskander</dc:creator>
		<pubDate>Fri, 09 Jul 2010 18:49:32 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-362</guid>
		<description>&lt;p&gt;If I had time or still worked with V8, I would. Unfortunately, neither are true.&lt;/p&gt;

&lt;p&gt;In my defense, this article was meant to be used for illustration purposes; the reader is supposed to be familiar with v8 and how it works; also, at the time, it was based on the definition of Point that was in the embedder&#039;s guide; this definition may have since changed, I really don&#039;t know. In any case, this was simply meant to expand on that in a rather abstract way. Still, it does look like I was a bit too lazy.&lt;/p&gt;

&lt;p&gt;As for v8::Handle::Cast... I seem to remember it existing. It may be a mistake (like the constructPoint appears to be), but perhaps, if indeed it is not in V8, perhaps it used to be and was removed?&lt;/p&gt;

&lt;p&gt;I&#039;m sorry for the trouble you&#039;ve had. Keep in mind that this was my first blog post, and is over a year and a half old.&lt;/p&gt;

&lt;p&gt;Alex&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>If I had time or still worked with V8, I would. Unfortunately, neither are true.</p>

<p>In my defense, this article was meant to be used for illustration purposes; the reader is supposed to be familiar with v8 and how it works; also, at the time, it was based on the definition of Point that was in the embedder&#8217;s guide; this definition may have since changed, I really don&#8217;t know. In any case, this was simply meant to expand on that in a rather abstract way. Still, it does look like I was a bit too lazy.</p>

<p>As for v8::Handle::Cast&#8230; I seem to remember it existing. It may be a mistake (like the constructPoint appears to be), but perhaps, if indeed it is not in V8, perhaps it used to be and was removed?</p>

<p>I&#8217;m sorry for the trouble you&#8217;ve had. Keep in mind that this was my first blog post, and is over a year and a half old.</p>

<p>Alex</p>]]></content:encoded>
	</item>
	<item>
		<title>By: chrwal</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-361</link>
		<dc:creator>chrwal</dc:creator>
		<pubDate>Fri, 09 Jul 2010 10:23:41 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-361</guid>
		<description>&lt;p&gt;Your example is extremely confusing.  There are variable introduced in your code snippets with no definitions.  For example in ConstructorCall where did wrapper come from?  There is no &quot;Handle&quot; type in V8 there is a Handle.  However Handle has no definition for Cast().  Also in wrapPoint where did point_templ come from.  How did wrapPoint obtain point_templ?  point_templ is not defined as a global.  You also altered the name of the point constructor function which didn&#039;t match.  You use name &quot;constructPoint&quot; in FunctionTemplate::New then followed the constructor function named &quot;ConstructorCall&quot;.&lt;/p&gt;

&lt;p&gt;It is very frustrating reading your examples because you didn&#039;t do a good job of explaining the introduction of variables.  It also would be helpful to post the entire working code.  Had you did that the snippets would  have been correct and compilable.  Can you please address the aforementioned issued and correct the snippets.  That would be a big help to me to be able to get a working sample.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Your example is extremely confusing.  There are variable introduced in your code snippets with no definitions.  For example in ConstructorCall where did wrapper come from?  There is no &#8220;Handle&#8221; type in V8 there is a Handle.  However Handle has no definition for Cast().  Also in wrapPoint where did point_templ come from.  How did wrapPoint obtain point_templ?  point_templ is not defined as a global.  You also altered the name of the point constructor function which didn&#8217;t match.  You use name &#8220;constructPoint&#8221; in FunctionTemplate::New then followed the constructor function named &#8220;ConstructorCall&#8221;.</p>

<p>It is very frustrating reading your examples because you didn&#8217;t do a good job of explaining the introduction of variables.  It also would be helpful to post the entire working code.  Had you did that the snippets would  have been correct and compilable.  Can you please address the aforementioned issued and correct the snippets.  That would be a big help to me to be able to get a working sample.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Iskander</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-11</link>
		<dc:creator>Alex Iskander</dc:creator>
		<pubDate>Tue, 03 Feb 2009 13:15:14 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-11</guid>
		<description>&lt;p&gt;Thanks. I thought about passing an argument, but never thought about passing an external as an argument. And I suppose that&#039;s exactly like C++ copy constructors too. I&#039;ll probably update the article soon.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks. I thought about passing an argument, but never thought about passing an external as an argument. And I suppose that&#8217;s exactly like C++ copy constructors too. I&#8217;ll probably update the article soon.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Plesner Hansen</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-10</link>
		<dc:creator>Christian Plesner Hansen</dc:creator>
		<pubDate>Tue, 03 Feb 2009 09:41:39 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-10</guid>
		<description>&lt;p&gt;Very nice overview!&lt;/p&gt;

&lt;p&gt;The reason why it isn&#039;t straightforward to use the same function as constructor and for creating wrappers is probably that you don&#039;t need it in a browser -- there you do &#039;document.createElement(&quot;div&quot;)&#039; rather than &#039;new DivElement()&#039;.  so there&#039;s never been a need for it in v8.  However, by using a small hack it is possible.&lt;/p&gt;

&lt;p&gt;As you say, the problem when using the constructor for making wrappers for existing points is that it creates a new Point instance.  One way to work around this for &#039;wrapPoint&#039; to pass an argument to the constructor that tells it that there already is an instance: the External value containing the point.&lt;/p&gt;

&lt;p&gt;v8::Handle wrapPoint(Point *value) {
  v8::HandleScope scope;
  v8::Handle cons = pointConstructor-&gt;GetFunction();
  v8::Handle external = v8::External::New(value);
  v8::Handle result = cons-&gt;NewInstance(1, &amp;external);
  return scope.Close(result);
}&lt;/p&gt;

&lt;p&gt;Within the constructor you check if the first argument is an external, and if it is you don&#039;t construct a Point:&lt;/p&gt;

&lt;p&gt;v8::Handle constructorCall(const v8::Arguments&amp; args) {
  v8::HandleScope scope;
  v8::Handle external;
  if (args[0]-&gt;IsExternal()) {
    external = v8::Handle::Cast(args[0]);
  } else {
    int x = args[0]-&gt;Int32Value();
    int y = args[1]-&gt;Int32Value();
    Point *p = new Point(x, y);
    external = v8::External::New(p);
  }
  args.This()-&gt;SetInternalField(0, wrapper);
  return args.This();
}&lt;/p&gt;

&lt;p&gt;Note that now you can use the instance created by the constructor.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Very nice overview!</p>

<p>The reason why it isn&#8217;t straightforward to use the same function as constructor and for creating wrappers is probably that you don&#8217;t need it in a browser &#8212; there you do &#8216;document.createElement(&#8220;div&#8221;)&#8217; rather than &#8216;new DivElement()&#8217;.  so there&#8217;s never been a need for it in v8.  However, by using a small hack it is possible.</p>

<p>As you say, the problem when using the constructor for making wrappers for existing points is that it creates a new Point instance.  One way to work around this for &#8216;wrapPoint&#8217; to pass an argument to the constructor that tells it that there already is an instance: the External value containing the point.</p>

<p>v8::Handle wrapPoint(Point *value) {
  v8::HandleScope scope;
  v8::Handle cons = pointConstructor-&gt;GetFunction();
  v8::Handle external = v8::External::New(value);
  v8::Handle result = cons-&gt;NewInstance(1, &amp;external);
  return scope.Close(result);
}</p>

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

<p>v8::Handle constructorCall(const v8::Arguments&amp; args) {
  v8::HandleScope scope;
  v8::Handle external;
  if (args[0]-&gt;IsExternal()) {
    external = v8::Handle::Cast(args[0]);
  } else {
    int x = args[0]-&gt;Int32Value();
    int y = args[1]-&gt;Int32Value();
    Point *p = new Point(x, y);
    external = v8::External::New(p);
  }
  args.This()-&gt;SetInternalField(0, wrapper);
  return args.This();
}</p>

<p>Note that now you can use the instance created by the constructor.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: alex</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-8</link>
		<dc:creator>alex</dc:creator>
		<pubDate>Sat, 31 Jan 2009 14:28:00 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-8</guid>
		<description>&lt;p&gt;That&#039;s probably part of what confused me, actually. I wanted to use the function to initialize &lt;em&gt;this&lt;/em&gt;, I think by using &lt;em&gt;args.Holder()&lt;/em&gt;. Doing it that way, however, would require that I still use two separate FunctionTemplates, identical except that one actually has a C++ callback, whereas the other does not; otherwise, NewInstance() calls the constructor even when I&#039;m trying to wrap an existing object (if there is another way around this, I&#039;d like to hear it).&lt;/p&gt;

&lt;p&gt;Note that I actually prevent it from being called without new:
&lt;pre lang=&quot;cpp&quot;&gt;
// throw if called without `new&#039;
if (!args.IsConstructCall()) 
    return ThrowException(String::New(&quot;Cannot call constructor as function&quot;));
&lt;/pre&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>That&#8217;s probably part of what confused me, actually. I wanted to use the function to initialize <em>this</em>, I think by using <em>args.Holder()</em>. Doing it that way, however, would require that I still use two separate FunctionTemplates, identical except that one actually has a C++ callback, whereas the other does not; otherwise, NewInstance() calls the constructor even when I&#8217;m trying to wrap an existing object (if there is another way around this, I&#8217;d like to hear it).</p>

<p>Note that I actually prevent it from being called without new:

</p>
<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><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></pre></div></div>


]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-7</link>
		<dc:creator>Matthias</dc:creator>
		<pubDate>Sat, 31 Jan 2009 10:34:42 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-7</guid>
		<description>&lt;p&gt;There is still one funny aspect to this: the &quot;Point&quot; function is actually a plain function (it constructs and returns a new object when it is actually supposed to &lt;em&gt;initialize&lt;/em&gt; &quot;this&quot;) but you make it look like a constructor.&lt;/p&gt;

&lt;p&gt;You could actually use it like so: &quot;var point = Point(3, 4)&quot;; whereas &quot;new Point(3, 4)&quot; will 
- create a new object
- invoke Point(3, 4) on it
- which will create and return a totally different object via wrapObject, becoming the result of the &quot;new&quot; expression
- the object in step 1) is garbage collected&lt;/p&gt;

&lt;p&gt;The fact that you can return something else than &quot;this&quot; from a JS constructor is surprising and I&#039;m glad I finally understood what was itching me about your example.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>There is still one funny aspect to this: the &#8220;Point&#8221; function is actually a plain function (it constructs and returns a new object when it is actually supposed to <em>initialize</em> &#8220;this&#8221;) but you make it look like a constructor.</p>

<p>You could actually use it like so: &#8220;var point = Point(3, 4)&#8221;; whereas &#8220;new Point(3, 4)&#8221; will 
- create a new object
- invoke Point(3, 4) on it
- which will create and return a totally different object via wrapObject, becoming the result of the &#8220;new&#8221; expression
- the object in step 1) is garbage collected</p>

<p>The fact that you can return something else than &#8220;this&#8221; from a JS constructor is surprising and I&#8217;m glad I finally understood what was itching me about your example.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: alex</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-6</link>
		<dc:creator>alex</dc:creator>
		<pubDate>Sat, 31 Jan 2009 00:56:46 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-6</guid>
		<description>&lt;p&gt;Thanks. I do hope others will find it helpful.&lt;/p&gt;

&lt;p&gt;I&#039;m thinking of making one about persistent pointers, covering both the general overview (including the difference between Clear and Dispose that was cleared up for us) and the MakeWeak functionality. What do you think?&lt;/p&gt;

&lt;p&gt;Alex&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks. I do hope others will find it helpful.</p>

<p>I&#8217;m thinking of making one about persistent pointers, covering both the general overview (including the difference between Clear and Dispose that was cleared up for us) and the MakeWeak functionality. What do you think?</p>

<p>Alex</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Vladimir Florentino</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-5</link>
		<dc:creator>Vladimir Florentino</dc:creator>
		<pubDate>Sat, 31 Jan 2009 00:28:51 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-5</guid>
		<description>&lt;p&gt;Nice overview, Alex.&lt;/p&gt;

&lt;p&gt;This is an issue that caused me many ours of debugging and hair pulling. I&#039;m sure it will be very helpful to others.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Nice overview, Alex.</p>

<p>This is an issue that caused me many ours of debugging and hair pulling. I&#8217;m sure it will be very helpful to others.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: alex</title>
		<link>http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/comment-page-1/#comment-4</link>
		<dc:creator>alex</dc:creator>
		<pubDate>Fri, 30 Jan 2009 18:11:35 +0000</pubDate>
		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=14#comment-4</guid>
		<description>&lt;p&gt;Thanks for pointing that out. I was copying, pasting, and modifying Google&#039;s example (in their embedding API page) and they were using ObjectTemplates. I was, it appears, a bit careless while doing so, and forgot to change that.&lt;/p&gt;

&lt;p&gt;Thanks for the correction!
Alex&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks for pointing that out. I was copying, pasting, and modifying Google&#8217;s example (in their embedding API page) and they were using ObjectTemplates. I was, it appears, a bit careless while doing so, and forgot to change that.</p>

<p>Thanks for the correction!
Alex</p>]]></content:encoded>
	</item>
</channel>
</rss>

