Closed GoogleCodeExporter closed 9 years ago
In fact, PyV8 doesn't make the decision, v8 only allow function for toString
and valueOf
// runtime.js
// ECMA-262, section 8.6.2.6, page 28.
function DefaultString(x) {
var toString = x.toString;
if (IS_SPEC_FUNCTION(toString)) {
var s = %_CallFunction(x, toString);
if (%IsPrimitive(s)) return s;
}
var valueOf = x.valueOf;
if (IS_SPEC_FUNCTION(valueOf)) {
var v = %_CallFunction(x, valueOf);
if (%IsPrimitive(v)) return v;
}
throw %MakeTypeError('cannot_convert_to_primitive', []);
}
// macros.py
# Macro for ECMAScript 5 queries of the type:
# "IsCallable(O)"
# We assume here that this is the same as being either a function or a function
# proxy. That ignores host objects with [[Call]] methods, but in most situations
# we cannot handle those anyway.
macro IS_SPEC_FUNCTION(arg) = (%_ClassOf(arg) === 'Function');
You could just wrap your object with lambda
class Foo(object):
def __init__(self):
self.toString = lambda: Foober("Hi")()
Original comment by flier...@gmail.com
on 12 Aug 2013 at 8:26
Hmm, interesting... do you think it would be worth it to make any callable()
python object map to a Function with properties? In Python a 'function' is just
an object with a __call__ property, after all... why should functions, unbound
instance methods, bound instance methods, and classes all be usable but not
classes with a __call__ method?
Original comment by csaft...@gmail.com
on 12 Aug 2013 at 7:10
As you know, every Python object will be wrapped as a JS object to V8 side, the
type can't be both [object] and [function], so I think use a lambda in Python
side will be acceptable.
Original comment by flier...@gmail.com
on 14 Aug 2013 at 2:58
But of course! It seems to me it would be better to have Python objects that
have a __call__ method be [function], and other non-number etc. types to be
[object] - this would also resolve issue #191 - but it's ultimately up to you!
I admit it is not such a big deal. This only really came up because I was
trying to work around the memory issues, but without those issues, there's no
reason these would matter in particular.
Original comment by csaft...@gmail.com
on 14 Aug 2013 at 4:40
Original issue reported on code.google.com by
csaft...@gmail.com
on 17 Jul 2013 at 10:31