예제 #1
0
파일: interp_js.py 프로젝트: bukzor/pypy
def _convert(space, w_value):
    """Convert a wrapped value into a wrapped W_Value."""
    if space.is_w(w_value, space.w_None):
        return null
    if space.isinstance_w(w_value, space.w_bool):
        # This works because W_Bool is a subclass of W_Int.
        return W_Boolean(support.emjs_make_bool(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_int):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_long):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_float):
        return W_Number(support.emjs_make_double(space.float_w(w_value)))
    if space.isinstance_w(w_value, space.w_str):
        value = space.str_w(w_value)
        return W_String(support.emjs_make_strn(value, len(value)))
    if space.isinstance_w(w_value, space.w_unicode):
        # XXX TODO: it would be awesome not to have to go via utf8.
        # Can we convert from raw in-memory repr direct to JS string?
        value = space.unicode_w(w_value).encode("utf-8")
        return W_String(support.emjs_make_strn(value, len(value)))
    # XXX TODO: is this typecheck safe and accurate?
    if isinstance(w_value, pypy.interpreter.function.Function):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    if isinstance(w_value, pypy.interpreter.function.Method):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    errmsg = "could not convert py type to js type"
    raise OperationError(space.w_TypeError, space.wrap(errmsg))
예제 #2
0
def _convert(space, w_value):
    """Convert a wrapped value into a wrapped W_Value."""
    if space.is_w(w_value, space.w_None):
        return null
    if space.isinstance_w(w_value, space.w_bool):
        # This works because W_Bool is a subclass of W_Int.
        return W_Boolean(support.emjs_make_bool(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_int):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_long):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_float):
        return W_Number(support.emjs_make_double(space.float_w(w_value)))
    if space.isinstance_w(w_value, space.w_str):
        value = space.str_w(w_value)
        return W_String(support.emjs_make_strn(value, len(value)))
    if space.isinstance_w(w_value, space.w_unicode):
        # XXX TODO: it would be awesome not to have to go via utf8.
        # Can we convert from raw in-memory repr direct to JS string?
        value = space.unicode_w(w_value).encode("utf-8")
        return W_String(support.emjs_make_strn(value, len(value)))
    # XXX TODO: is this typecheck safe and accurate?
    if isinstance(w_value, pypy.interpreter.function.Function):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    if isinstance(w_value, pypy.interpreter.function.Method):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    errmsg = "could not convert py type to js type"
    raise OperationError(space.w_TypeError, space.wrap(errmsg))
예제 #3
0
def _convert(space, w_value):
    """Convert a wrapped value into a wrapped W_Value."""
    if space.is_w(w_value, space.w_None):
        return null
    if space.isinstance_w(w_value, space.w_bool):
        # This works because W_Bool is a subclass of W_Int.
        return W_Boolean(support.emjs_make_bool(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_int):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_long):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_float):
        return W_Number(support.emjs_make_double(space.float_w(w_value)))
    if space.isinstance_w(w_value, space.w_str):
        value = space.str_w(w_value)
        return W_String(support.emjs_make_strn(value, len(value)))
    if space.isinstance_w(w_value, space.w_unicode):
        # XXX TODO: it would be awesome not to have to go via utf8.
        # Can we convert from raw in-memory repr direct to JS string?
        value = space.unicode_w(w_value).encode("utf-8")
        return W_String(support.emjs_make_strn(value, len(value)))
    if space.isinstance_w(w_value, space.w_dict):
        obj = W_Object(support.emjs_make_object())
        w_items = space.call_method(w_value, 'items')
        w_iter = space.iter(w_items)
        while True:
            try:
                w_tup = space.next(w_iter)
                w_key = space.getitem(w_tup, space.newint(0))
                w_value = space.getitem(w_tup, space.newint(1))
                space.setattr(obj, w_key, w_value)
            except OperationError as e:
                if not e.match(space, space.w_StopIteration):
                    raise
                else:
                    break
        return obj
    if space.isinstance_w(w_value, space.w_list):
        lst = W_Array(support.emjs_make_array(space.int_w(space.len(w_value))))
        w_iter = space.iter(w_value)
        i = 0
        while True:
            try:
                w_item = space.next(w_iter)
                space.setitem(lst, space.newint(i), w_item)
                i += 1
            except OperationError as e:
                if not e.match(space, space.w_StopIteration):
                    raise
                else:
                    break
        return lst
    # XXX TODO: is this typecheck safe and accurate?
    if isinstance(w_value, pypy.interpreter.function.Function):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    if isinstance(w_value, pypy.interpreter.function.Method):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    errmsg = "could not convert py type to js type"
    raise OperationError(space.w_TypeError, space.wrap(errmsg))
예제 #4
0
def W_Number_descr__new__(space, w_subtype, w_value):
    w_self = space.allocate_instance(W_Number, w_subtype)
    value = space.float_w(w_value)
    h_value = support.emjs_make_double(value)
    W_Number.__init__(space.interp_w(W_Number, w_self), h_value)
    return w_self
예제 #5
0
파일: interp_js.py 프로젝트: bukzor/pypy
def W_Number_descr__new__(space, w_subtype, w_value):
    w_self = space.allocate_instance(W_Number, w_subtype)
    value = space.float_w(w_value)
    h_value = support.emjs_make_double(value)
    W_Number.__init__(space.interp_w(W_Number, w_self), h_value)
    return w_self
예제 #6
0
파일: interp_js.py 프로젝트: Darriall/pypy
def _convert(space, w_value):
    """Convert a wrapped value into a wrapped W_Value."""
    if space.is_w(w_value, space.w_None):
        return null
    if space.isinstance_w(w_value, space.w_bool):
        # This works because W_Bool is a subclass of W_Int.
        return W_Boolean(support.emjs_make_bool(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_int):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_long):
        return W_Number(support.emjs_make_int32(space.int_w(w_value)))
    if space.isinstance_w(w_value, space.w_float):
        return W_Number(support.emjs_make_double(space.float_w(w_value)))
    if space.isinstance_w(w_value, space.w_str):
        value = space.str_w(w_value)
        return W_String(support.emjs_make_strn(value, len(value)))
    if space.isinstance_w(w_value, space.w_unicode):
        # XXX TODO: it would be awesome not to have to go via utf8.
        # Can we convert from raw in-memory repr direct to JS string?
        value = space.unicode_w(w_value).encode("utf-8")
        return W_String(support.emjs_make_strn(value, len(value)))
    if space.isinstance_w(w_value, space.w_dict):
        obj = W_Object(support.emjs_make_object())
        w_items = space.call_method(w_value, 'items')
        w_iter = space.iter(w_items)
        while True:
            try:
                w_tup = space.next(w_iter)
                w_key = space.getitem(w_tup, space.newint(0))
                w_value = space.getitem(w_tup, space.newint(1))
                space.setattr(obj, w_key, w_value)
            except OperationError as e:
                if not e.match(space, space.w_StopIteration):
                    raise
                else:
                    break
        return obj
    if space.isinstance_w(w_value, space.w_list):
        lst = W_Array(support.emjs_make_array(space.int_w(space.len(w_value))))
        w_iter = space.iter(w_value)
        i = 0
        while True:
            try:
                w_item = space.next(w_iter)
                space.setitem(lst, space.newint(i), w_item)
                i += 1
            except OperationError as e:
                if not e.match(space, space.w_StopIteration):
                    raise
                else:
                    break
        return lst
    # XXX TODO: is this typecheck safe and accurate?
    if isinstance(w_value, pypy.interpreter.function.Function):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    if isinstance(w_value, pypy.interpreter.function.Method):
        args = pypy.interpreter.function.Arguments(space, [])
        h_cb, cb = _make_callback(space, w_value, False, args)
        return W_Function(h_cb, None, cb)
    errmsg = "could not convert py type to js type"
    raise OperationError(space.w_TypeError, space.wrap(errmsg))