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))
def W_String_descr__new__(space, w_subtype, w_value): w_self = space.allocate_instance(W_String, w_subtype) 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") else: value = space.str_w(w_value) h_value = support.emjs_make_strn(value, len(value)) W_String.__init__(space.interp_w(W_String, w_self), h_value) return w_self
def dispatch_callback(dataptr, h_args): id = rffi.cast(lltype.Signed, dataptr) callback = global_callback_map.get(id) if callback is None: raise RuntimeError("invalid callback id; was it garbage-collected?") space = callback.space # Unpack h_args as an extension of the default args from the callback. # If the callback wants the value of 'this', pass it as first argument. args_w, kw_w = callback.__args__.unpack() if kw_w: raise RuntimeError("callback function kw args not implemented yet") h_args_len = support.emjs_length(h_args) all_args_len = len(args_w) + h_args_len if callback.wants_this: all_args_len += 1 all_args_w = [None] * all_args_len i = 0 if callback.wants_this: h_this = support.emjs_prop_get_str(h_args, "this") w_this = _wrap_handle(space, h_this) all_args_w[0] = w_this i += 1 for w_arg in args_w: all_args_w[i] = w_arg i += 1 for j in xrange(h_args_len): w_arg = _wrap_handle(space, support.emjs_prop_get_int(h_args, j)) all_args_w[i] = w_arg i += 1 # Do the call, propagating return value or error as appropriate. try: w_res = space.call(callback.w_callback, space.newtuple(all_args_w)) except OperationError, pyerr: # XXX TODO: tunnel the exception through JS and back to Python? # XXX TODO: allow the callback to raise js.Error and reflect it # properly through to the javascript side. # w_jserror = getstate(space).w_jserror # if isinstance(pyerr, w_jserror): # with _unwrap_handle(pyerror.args[0]) as h_err: # support.emjs_set_error(h_err) # else: # h_msg = make_error_string() # h_data = make_error_pickle() # h_err = new(h_pyerror, [h_msg, h_data]) # support.emjs_set_error(h_err) # support.emjs_free(h_err) errmsg = pyerr.errorstr(space) h_errmsg = support.emjs_make_strn(errmsg, len(errmsg)) support.emjs_set_error(h_errmsg) support.emjs_free(h_errmsg) return support.EMJS_ERROR
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))