gurobokum / pyv8

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

Can't use arbitrary callable for __defineGetter__/__defineSetter__ #191

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Run the following code:

import PyV8

class Foober(object):
    def __init__(self, x):
        self.x = x 
    def __call__(self):
        return self.x

class Foo(object):
    def __init__(self):
        pass

class Global(object):
    def Foober(self, x):
        return Foober(x)

ctxt = PyV8.JSContext(Global())
with ctxt:
    print ctxt.eval("""
var obj = {};
obj.__defineGetter__('zomg', Foober(100));
obj.zomg;
""") 

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

I would expect '100' to be printed. Instead, I get 

TypeError: TypeError: Object.prototype.__defineGetter__: Expecting function (  
@ 3 : 4 )  -> obj.__defineGetter__('zomg'
, Foober(100));

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

Python 2.6.6, PyV8 preview r443, Windows 7

Please provide any additional information below.

This is more a feature request than a bug. 

Original issue reported on code.google.com by csaft...@gmail.com on 19 Jul 2013 at 5:29

GoogleCodeExporter commented 9 years ago
In fact, v8 doesn't allow to use non-function in __defineGetter__

// v8native.js

// Extensions for providing property getters and setters.
function ObjectDefineGetter(name, fun) {
  var receiver = this;
  if (receiver == null && !IS_UNDETECTABLE(receiver)) {
    receiver = %GlobalReceiver(global);
  }
  if (!IS_SPEC_FUNCTION(fun)) {
    throw new $TypeError(
        'Object.prototype.__defineGetter__: Expecting function');
  }
  var desc = new PropertyDescriptor();
  desc.setGet(fun);
  desc.setEnumerable(true);
  desc.setConfigurable(true);
  DefineOwnProperty(ToObject(receiver), ToName(name), desc, false);
}

you could wrap your object with anonymous function

with ctxt:
    print ctxt.eval("""
var obj = {};
obj.__defineGetter__('zomg', function() { return Foober(100)(); });
obj.zomg;
""")

Original comment by flier...@gmail.com on 12 Aug 2013 at 8:30