qmphan / pyv8

Automatically exported from code.google.com/p/pyv8
0 stars 0 forks source link

Constructor Function Issue #143

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

If I run this code:

        code = """
        var Test = function() {
            this.trySomething();
        };
        Test.prototype.trySomething = function() {
        };"""
        ctx.eval(code)

        test = ctx.locals.Test()

It fails with:

*** JSError: JSError: TypeError: Object #<an Object> has no method 
'trySomething' (  @ 3 : 17 )  ->             this.wtf();

What is the expected output? What do you see instead?

I would expect that the equivalent of "new Test()" would be executed in python. 
How can I differentiate between running a function or constructing a function 
using pyv8?

What version of the product are you using? On what operating system?

I'm using pyv8 0.8 under Mac OS X.

Original issue reported on code.google.com by heynem...@gmail.com on 6 Dec 2012 at 5:56

GoogleCodeExporter commented 9 years ago
the part that says this.wtf is not this.wtf... I just copy/pasted part of the 
log from different parts.

Original comment by heynem...@gmail.com on 6 Dec 2012 at 6:10

GoogleCodeExporter commented 9 years ago

Original comment by flier...@gmail.com on 26 Dec 2012 at 7:28

GoogleCodeExporter commented 9 years ago
The major problem is PyV8 doesn't support to create JS object with constructor, 
please verify the fix with SVN trunk code after r470.

    def testConstructor(self):
        with JSContext() as ctx:
            ctx.eval("""
                var Test = function() {
                    this.trySomething();
                };
                Test.prototype.trySomething = function() {
                    this.name = 'flier';
                };

                var Test2 = function(first_name, last_name) {
                    this.name = first_name + ' ' + last_name;
                };
                """)

            self.assert_(isinstance(ctx.locals.Test, _PyV8.JSFunction))

            test = JSObject.create(ctx.locals.Test)

            self.assert_(isinstance(ctx.locals.Test, _PyV8.JSObject))
            self.assertEquals("flier", test.name);

            test2 = JSObject.create(ctx.locals.Test2, ('Flier', 'Lu'))

            self.assertEquals("Flier Lu", test2.name);

            test3 = JSObject.create(ctx.locals.Test2, ('Flier', 'Lu'), { 'email': 'flier.lu@gmail.com' })

            self.assertEquals("flier.lu@gmail.com", test3.email);

Original comment by flier...@gmail.com on 15 Jan 2013 at 9:42