On Wed, Jan 5, 2011 at 1:13 PM, Andrey <[email protected]> wrote: > If you call o.foo(bar) and o.foo is an >> property handler returning a v8::Function, then that function should >> be called with Arguments of length 1. > > Thank you. > Really, it works when I delete one string, > > Handle<Value> foo(const Arguments& args) > { > int argc = args.Length(); > return Undefined(); > } > > Handle<v8::Value> Getter( Local<v8::String> property, const > AccessorInfo& info ) > { > HandleScope handle_scope; > if (property->Equals(v8::String::New("foo"))) > { > Handle<FunctionTemplate> templ = FunctionTemplate::New(foo); > Handle<Function> func = templ->GetFunction(); > Local<Object> obj = func->NewInstance();//delete this string > return handle_scope.Close(obj); > } > return Undefined(); > } > > The error was in creating instance of the function.
Yip, both the function/instance model of JavaScript as well as the template model of V8 can mislead you. func->NewInstance() creates a foo and calls foo() as constructor with zero args. Not what you wanted. Please beware that FunctionTemplates and their function instance are not garbage collected until the context is GCed (http://code.google.com/p/v8/source/browse/trunk/include/v8.h?r=6179#1949, also search the archives of this group). It is generally not a good idea to dynamically create function templates per property access like you do - instead create only one or a bounded number of templates for foo and use JS functions and scoping to bind different dynamic values to it. Matthias -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
