def new(space, w_fn, args_w): with _unwrap_handle(space, w_fn) as h_fn: h_args = support.emjs_make_array(len(args_w)) _check_error(space, h_args) for i in xrange(len(args_w)): with _unwrap_handle(space, args_w[i]) as h_arg: res = support.emjs_prop_set_int(h_args, i, h_arg) _check_error(space, res) h_res = support.emjs_new(h_fn, h_args) return _wrap_handle(space, h_res)
def descr__call__(self, space, args_w): h_args = support.emjs_make_array(len(args_w)) _check_error(space, h_args) for i in xrange(len(args_w)): with _unwrap_handle(space, args_w[i]) as h_arg: support.emjs_prop_set_int(h_args, i, h_arg) w_ctx = self.w_context if w_ctx is None: w_ctx = undefined with _unwrap_handle(space, w_ctx) as h_ctx: h_res = support.emjs_apply(self.handle, h_ctx, h_args) return _wrap_handle(space, h_res)
def W_Array_descr__new__(space, w_subtype, w_items, w_size): # XXX TODO: default arguments, somehow... size = space.int_w(w_size) h_self = support.emjs_make_array(size) _check_error(space, h_self) if space.is_true(w_items): w_iterator = space.iter(w_items) idx = 0 # XXX TODO: baseobjspace uses a jit merge point in this loop...? while True: try: w_item = space.next(w_iterator) except OperationError, e: if not e.match(space, space.w_StopIteration): raise break else: with _unwrap_handle(space, w_item) as h_item: res = support.emjs_prop_set_int(h_self, idx, h_item) _check_error(space, res) idx += 1
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))