Xiang,
You're right and I must retract my reply to Stephan. I am not sure what
was happening before, but I am unable to reproduce this behavior in a
small test script. Instead it seems that the function template is frozen
at the first call to GetFunction(), as you would expect;
I think I may have misinterpreted the behavior I was seeing before and
stumbled upon the correct solution for the wrong reason. This is likely
my mistake, but I have changed versions of v8 several times.
Best,
Alfred
#include <iostream>
#include <v8.h>
using namespace v8;
Handle<Value> Print(const Arguments & args) {
for(int i = 0; i < args.Length(); ++i) {
String::Utf8Value string(args[i]);
std::cout << *string;
}
std::cout << std::endl;
return Undefined();
}
int main(int argc, char *argv[]) {
HandleScope handle_scope;
Handle<ObjectTemplate> global = ObjectTemplate::New();
global->Set("print", FunctionTemplate::New(Print));
Persistent<Context> context = Context::New(NULL, global);
Context::Scope context_scope(context);
Handle<FunctionTemplate> ftemplate = FunctionTemplate::New();
ftemplate->Set(String::New("a"), Number::New(0));
ftemplate->PrototypeTemplate()->Set("b", Number::New(1));
context->Global()->Set(String::New("A"), ftemplate->GetFunction());
// the c's and d's here end up undefined
ftemplate->Set(String::New("c"), Number::New(2));
ftemplate->PrototypeTemplate()->Set("d", Number::New(3));
context->Global()->Set(String::New("B"), ftemplate->GetFunction());
const char source[] = "a1 = new A; b1 = new B; print(A.a);
print(A.c); print(B.a); print(B.c); print(a1.b); print(b1.b);
print(a1.d); print(b1.d);";
/* The output:
0
undefined
0
undefined
1
1
undefined
undefined
*/
Handle<String> sourcestring = String::New(source);
Handle<Script> script = Script::Compile(sourcestring);
script->Run();
context.Dispose();
return 0;
};
On Tue, 2009-10-27 at 15:31 +0800, Xiang Zhong wrote:
> Hi, Alfred and Stephan,
>
>
>
>
> So, Does that means that we can get multiple function instance from
> single template in a context?
>
>
> There seems has a contradiction with document from google,
>
>
> Please check the bold part of following text. Is the document from
> google wrong?
>
>
> From http://code.google.com/apis/v8/embed.html
> Templates
> A template is a blueprint for JavaScript functions and objects in a
> context. You can use a template to wrap C++ functions and data
> structures within JavaScript objects so that they can be manipulated
> by JavaScript scripts. For example, Google Chrome uses templates to
> wrap C++ DOM nodes as JavaScript objects and to install functions in
> the global namespace. You can create a set of templates and then use
> the same ones for every new context you make. You can have as many
> templates as you require. However you can only have one instance of
> any template in any given context.
>
>
>
>
>
>
>
>
>
>
>
>
>
> On Tue, Oct 27, 2009 at 11:07 AM, Stephan Beal <[email protected]>
> wrote:
> On Tue, Oct 27, 2009 at 2:44 AM, Alfred Rossi
> <[email protected]> wrote:
> It would seem that whenever you call GetFunction() you
> get a distinct
> function based on the current state of the template.
> That is, if you
> add something to the template and call it again you
> get a different
> function, distinct from the first, with additional
> members. I assume
> that this is the intended behavior.
>
>
> Aha. That sounds reasonable, i guess. That would explain the
> problem i was chasing before i learned (through trial an
> error) not to call GetFunction() on my ctor template "too
> early" in the class binding process.
>
>
> What's really important to my case, though: if i call it twice
> _without_ modifying the template, will i get the same function
> object or not?
>
>
> Thanks :).
>
> --
> ----- stephan beal
> http://wanderinghorse.net/home/stephan/
>
>
>
>
>
>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---