terratenney / pyv8

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

Convert boxed object on translation from js to python types. #109

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is a feature enhancement.  It allows for boxed javascript primitives to 
mapped to the correct python type for strings, booleans, and numbers.

new String("abc") -> currently mapped to generic python JSObject, should be 
python string.

new Boolean(true) -> currently mapped to generic python JSObject, should be 
python boolean.

new Number(1.2) -> currently mapped to generic python JSObject, should be 
python float.

V8 3.4.13 (revision 8669) introduced a few API additions for v8::Value:

IsNumberObject()
IsStringObject()
IsBooleanObject()

In PyV8's CJavaScriptObject::Wrap(v8::Value ...) method, I implemented these 
additional checks:

    if (value->IsString() || value->IsStringObject()) {
    if (value->IsBoolean() || value->IsBooleanObject()) {
    if (value->IsNumber() || value->IsNumberObject()) {

I wrote this demo python code to test:

import PyV8

class Global(PyV8.JSClass):

    def __init__(self):
        pass

    def pytype(self, val):
        return str(type(val))

with PyV8.JSContext(Global()) as context:
    assert context.eval("""pytype(new String("ABC"));""") == "<type 'str'>"
    assert context.eval("""pytype(new Boolean(true));""") == "<type 'bool'>"
    assert context.eval("""pytype(new Number(1.5));""") == "<type 'float'>"

Original issue reported on code.google.com by ATM1...@gmail.com on 19 Oct 2011 at 7:36

GoogleCodeExporter commented 9 years ago

Original comment by flier...@gmail.com on 20 Oct 2011 at 5:55

GoogleCodeExporter commented 9 years ago
Please verify with PyV8 SVN trunk code after r419.

Thanks for your suggestion and patch :)

btw: we need use v8::Value::As to convert it to StringObject etc.

Original comment by flier...@gmail.com on 20 Oct 2011 at 2:34