[v8-users] a simple question about "args.This()->GetPointerFromInternalField"

16 views
Skip to first unread message

Seiji Sam Lee

unread,
May 11, 2010, 2:34:39 PM5/11/10
to v8-u...@googlegroups.com

That is a silly question:

 

I have done a classical implementation of File: a constructor, open, close, eof, isOpen, gets y puts.

But following code only print “Hi world!!”

 

$out=new File(“@out”);//really FILE*stdout

$out.puts("Hi world!!"); (1)

 

var echo=$out.puts;

echo("It must work"); (2)

 

I catch “FILE*” pointer with “args.This()->GetPointerFromInternalField(2)”.

In (1) “args.This()” is “$out” and in (2) “args.This()” is “echo” (I suppouse) that return NULL calling “GetPointerFromInternalField”.

 

Are there any way to allow that code like that works fine? That is, “echo” call catch correct “FILE*” parameter of “$out”?

 

Thanks in advance.

 

--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users

Anton Muhin

unread,
May 11, 2010, 2:47:15 PM5/11/10
to v8-u...@googlegroups.com
From the right email.

On Tue, May 11, 2010 at 10:45 PM, Anton Muhin <ant...@google.com> wrote:
> There are several problems with your code:
>
> 1) echo the way you use it is a bad idea, you need to do something
> like echo.call($out, "It must work").  That's the way JS works (hint:
> in the second case this is a global object);
>
> 2) overall you need args.Holder(), not args.This()---Holder() will
> give an object which implements File and thus has internal fields.
>
> yours,
> anton.

Stephan Beal

unread,
May 11, 2010, 2:54:16 PM5/11/10
to v8-u...@googlegroups.com
On Tue, May 11, 2010 at 8:34 PM, Seiji Sam Lee <seiji...@gmail.com> wrote:
> var echo=$out.puts;
...
> Are there any way to allow that code like that works fine? That is, “echo”
> call catch correct “FILE*” parameter of “$out”?

Something like:

var echo = function() {
$out.puts.apply( $out, Array.prototype.slice.apply( arguments,[0]) );
};


--
----- stephan beal
http://wanderinghorse.net/home/stephan/

Seiji Sam Lee

unread,
May 12, 2010, 9:28:56 AM5/12/10
to v8-u...@googlegroups.com
Thanks, Stephan and Anton, all yours solutions works fine, but not as I
expected!!

Yes, Holder() is the right way, and two functions are right too!

But I want to suggest a new property (like "Holder" or "This") called
"Owner", a reference to the object that own (in origin) the property.

It could allow things like that:

var echo=$out.puts
echo("Hello!!");

It simplify the creation of "delegates properties", in turn of put a
InternalField for each property. The scope of the delegated property is the
same of its owner.

Do you thing that this suggest is possible?


-----Mensaje original-----
De: v8-u...@googlegroups.com [mailto:v8-u...@googlegroups.com] En nombre
de Stephan Beal
Enviado el: martes, 11 de mayo de 2010 20:54
Para: v8-u...@googlegroups.com
Asunto: Re: [v8-users] a simple question about
"args.This()->GetPointerFromInternalField"

Anton Muhin

unread,
May 12, 2010, 9:41:27 AM5/12/10
to v8-u...@googlegroups.com
Again from right address, sorry.

On Wed, May 12, 2010 at 5:39 PM, Anton Muhin <ant...@google.com> wrote:
> That is what is usually named bind (for example,
> http://www.prototypejs.org/api/function/bind)
>
> So I don't think any JS engine (including V8) would have direct
> support for this.  But that's only my thoughts.
>
> yours,
> anton.

Seiji Sam Lee

unread,
May 12, 2010, 9:55:33 AM5/12/10
to v8-u...@googlegroups.com
Yes, named bind, ok, but ...

Do you think that Owner() technique would superimpose to named bind? Or that
two could coexist?


-----Mensaje original-----
De: v8-u...@googlegroups.com [mailto:v8-u...@googlegroups.com] En nombre
de Anton Muhin
Enviado el: miércoles, 12 de mayo de 2010 15:41

Anton Muhin

unread,
May 12, 2010, 9:58:30 AM5/12/10
to v8-u...@googlegroups.com
Owner, even if implemented which is not that easy as that would mean
that foo.func !== bar.func (unless some other tricks are employed)
would only benefit embedding API. bind, on the other hand, is 100% JS
solution which would work in any JS engine in the world (well, let's
hope it will).

So I personally don't see any benefits in Owner approach. YMMV of course.

yours,
anton.

Stephan Beal

unread,
May 12, 2010, 10:25:29 AM5/12/10
to v8-u...@googlegroups.com
On Wed, May 12, 2010 at 3:28 PM, Seiji Sam Lee <seiji...@gmail.com> wrote:
> But I want to suggest a new property (like "Holder" or "This") called
> "Owner", a reference to the object that own (in origin) the property.
>
> It could allow things like that:
>
> var echo=$out.puts
> echo("Hello!!");

No, it couldn't because when you copy $out.puts to echo you are
copying the function named puts and you are not copying any
information about the $out object. That function has no notion of a
parent/container/self object until it is called in the scope of a
given "this" object. That is, a function handle contains no metadata
which points back to its "original" owner (and that owner could change
over time). Consider:

$out = {x:1};
$in = {x:2};

function puts()
{
print(this.x);
}
$out.puts = puts;
$in.puts = puts;
// is equivalent to:
$out.puts = $in.puts = puts;

in that last statement, which object "should" be the Owner? It's not
answerable for the generic case.

Seiji Sam Lee

unread,
May 12, 2010, 12:31:33 PM5/12/10
to v8-u...@googlegroups.com
Ok, i become convinced!!

:-)

-----Mensaje original-----
De: v8-u...@googlegroups.com [mailto:v8-u...@googlegroups.com] En nombre
de Stephan Beal
Enviado el: miércoles, 12 de mayo de 2010 16:25
Para: v8-u...@googlegroups.com
Asunto: Re: [v8-users] a simple question about
"args.This()->GetPointerFromInternalField"

Reply all
Reply to author
Forward
0 new messages