Hi, all!

Are there any strong reasons *not* to allow a given class instance to
be created with this syntax:

var x = Foo();

as opposed to:

var x = new Foo():

?

When a v8-bound ctor is called we can check IsConstructorCall() to
determine if 'new' was used, and behave
differently in that case. My question is, however, is there a good
reason (in terms of class design) to differentiate between the two
forms of calls (for the generic case)?

On a related note: i came across the following trick today for
generically handling the "treated 'Foo()' as 'new Foo()'" case, and
thought it might be useful to someone out there:


        static Handle<Value> yourConstructorFunction( const Arguments &
argv )
        {
#if 1
            /**
               Allow construction without 'new' by forcing this
               function to be called in a ctor context...
            */
            if (!argv.IsConstructCall())
            {
                const int argc = argv.Length();
                Handle<Function> ctor( Function::Cast(*argv.Callee
()));
                std::vector< Handle<Value> > av(static_cast<size_t>
(argc),Undefined());
                for( int i = 0; i < argc; ++i ) av[i] = argv[i];
                return ctor->NewInstance( argc, &av[0] ); // calls
yourConstructorFunction() again
            }
#else // this was my code before ^^^^
            /**
               Why have this limitation? If we don't, v8 pukes when
               the ctor is called, with
               "v8::Object::SetInternalField() Writing internal field
               out of bounds".
            */
            if (!argv.IsConstructCall())
            {
                std::ostringstream os;
                os << "The "<< ClassOpsType::ClassName() << "
constructor cannot be called as function!";
                return ThrowException(String::New(os.str().c_str()));
            }
#endif
...

}

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to