Example #1
0
def js_apply(ctx):
    from js.object_space import isnull_or_undefined
    func = ctx.this_binding()
    args = ctx.argv()

    this_arg = get_arg(args, 0)
    arg_array = get_arg(args, 1)

    if isnull_or_undefined(arg_array):
        res = func.Call(args=[], this=this_arg, calling_context=ctx)
        compl = NormalCompletion(value=_w(res))
        return compl

    from js.jsobj import W_BasicObject
    if not isinstance(arg_array, W_BasicObject):
        raise JsTypeError(u'')

    length = arg_array.get(u'length')
    n = length.ToUInt32()
    arg_list = []
    index = 0
    while index < n:
        index_name = unicode(str(index))
        next_arg = arg_array.get(index_name)
        arg_list.append(next_arg)
        index += 1

    res = func.Call(args=arg_list, this=this_arg, calling_context=ctx)
    compl = NormalCompletion(value=_w(res))
    return compl
 def set_mutable_binding(self, identifier, value, strict):
     assert identifier is not None and isinstance(identifier, unicode)
     assert self.has_binding(identifier)
     if not self._is_mutable_binding(identifier):
         from js.exception import JsTypeError
         raise JsTypeError(u'immutable binding')
     self._set_binding(identifier, value)
Example #3
0
    def _idx_put(self, idx, v, throw):
        d = self._can_idx_put(idx)
        can_put = d.can_put
        own_desc = d.own
        inherited_desc = d.inherited
        prop = d.prop

        if not can_put:
            if throw:
                raise JsTypeError(u"can't put %s" % (str(idx), ))
            else:
                return

        if is_data_descriptor(own_desc):
            value_desc = PropertyDescriptor(value=v)
            self._define_own_idx_property(idx, value_desc, throw, own_desc, prop)
            return

        if own_desc is None:
            desc = inherited_desc
        else:
            desc = own_desc

        if is_accessor_descriptor(desc):
            setter = desc.setter
            assert setter is not None
            setter.Call(this=self, args=[v])
        else:
            new_desc = DataPropertyDescriptor(v, True, True, True)
            self._define_own_idx_property(idx, new_desc, throw)
Example #4
0
 def eval(self, ctx):
     rval = ctx.stack_pop()
     lval = ctx.stack_pop()
     from js.jsobj import W_BasicObject
     if not isinstance(rval, W_BasicObject):
         raise JsTypeError(u'TypeError')
     res = rval.has_instance(lval)
     ctx.stack_append(_w(res))
Example #5
0
 def operation(self, ctx, left, right):
     from js.jsobj import W_BasicObject
     from js.object_space import newbool
     if not isinstance(right, W_BasicObject):
         raise JsTypeError(u"TypeError: fffuuu!")  # + repr(right)
     name = left.to_string()
     has_name = right.has_property(name)
     return newbool(has_name)
Example #6
0
def value_of(this, args):
    if isinstance(this, W_Boolean):
        b = this
    elif isinstance(this, W_BooleanObject):
        b = this.PrimitiveValue()
    else:
        raise JsTypeError(u'')

    return b
Example #7
0
def to_string(this, args):
    if isinstance(this, W_String):
        s = this
    elif isinstance(this, W_StringObject):
        s = this.PrimitiveValue()
    else:
        raise JsTypeError(u'')

    return s.to_string()
Example #8
0
def commonnew(ctx, obj, args):
    if not obj.is_callable():
        msg = u'%s is not a constructor' % (obj.to_string())
        raise JsTypeError(msg)

    from js.jsobj import W_BasicFunction
    assert isinstance(obj, W_BasicFunction)
    res = obj.Construct(args=args)
    return res
Example #9
0
def value_of(this, args):
    if isinstance(this, W_Number):
        num = this
    elif isinstance(this, W_NumericObject):
        num = this.PrimitiveValue()
    else:
        raise JsTypeError(u'')

    return num.ToNumber()
Example #10
0
def value_of(this, args):
    if isinstance(this, W_String):
        s = this
    elif isinstance(this, W_StringObject):
        s = this.PrimitiveValue()
    else:
        raise JsTypeError(u'')

    assert isinstance(s, W_String)
    return s
Example #11
0
def to_string(this, args):
    if isinstance(this, W_Boolean):
        b = this
    elif isinstance(this, W_BooleanObject):
        b = this.PrimitiveValue()
    else:
        raise JsTypeError(u'')

    if b.to_boolean() is True:
        return u'true'
    else:
        return u'false'
Example #12
0
    def delete(self, p, throw=False):
        desc = self.get_own_property(p)
        if desc is None:
            return True
        if desc.configurable:
            self._del_prop(p)
            return True

        if throw is True:
            raise JsTypeError(u'')

        return False
Example #13
0
def js_call(ctx):
    func = ctx.this_binding()
    args = ctx.argv()

    if not func.is_callable():
        raise JsTypeError(u'')

    this_arg = get_arg(args, 0)
    arg_list = args[1:]

    res = func.Call(args=arg_list, this=this_arg, calling_context=ctx)
    compl = NormalCompletion(value=_w(res))
    return compl
Example #14
0
def common_call(ctx, funcobj, args, this, identifyer):
    if not funcobj.is_callable():
        err = u"%s is not a callable (%s)" % (funcobj.to_string(),
                                              identifyer.to_string())
        raise JsTypeError(err)

    from js.jsobj import W_List, W_BasicFunction
    assert isinstance(args, W_List)
    assert isinstance(funcobj, W_BasicFunction)

    argv = args.to_list()
    res = funcobj.Call(args=argv, this=this, calling_context=ctx)
    return res
Example #15
0
    def default_value(self, hint='Number'):
        if hint == 'String':
            res = self._default_value_string_()
            if res is None:
                res = self._default_value_number_()
        else:
            res = self._default_value_number_()
            if res is None:
                res = self._default_value_string_()

        if res is not None:
            return res

        raise JsTypeError(u'')
Example #16
0
def to_string(this, args):
    if len(args) > 0:
        radix = args[0].ToInteger()
        if radix < 2 or radix > 36:
            raise JsRangeError

    if isinstance(this, W_Number):
        num = this
    elif isinstance(this, W_NumericObject):
        num = this.PrimitiveValue()
    else:
        raise JsTypeError(u'')

    # TODO radix, see 15.7.4.2
    return num.to_string()
Example #17
0
    def emit(self, bytecode):
        w_object = self.w_object
        left_expr = self.left_expr
        body = self.body

        w_object.emit(bytecode)
        bytecode.emit('LOAD_ITERATOR')
        # load the "last" iterations result
        bytecode.emit('LOAD_UNDEFINED')
        precond = bytecode.emit_startloop_label()
        finish = bytecode.prealocate_endloop_label(True)

        bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish)

        # put next iterator value on stack
        bytecode.emit('NEXT_ITERATOR')

        # store iterrator value into approperiate place
        if isinstance(left_expr, This):
            raise JsException(u'Invalid left-hand side in for-in')
        if isinstance(left_expr, Identifier):
            name = left_expr.name
            index = left_expr.index
            bytecode.emit('STORE', index, name)
            bytecode.emit('POP')
        elif isinstance(left_expr, VariableDeclaration):
            name = left_expr.identifier
            index = left_expr.index
            bytecode.emit('STORE', index, name)
            bytecode.emit('POP')
        elif isinstance(left_expr, MemberDot):
            bytecode.emit('LOAD_STRINGCONSTANT', left_expr.name)
            left_expr.left.emit(bytecode)
            bytecode.emit('STORE_MEMBER')
            bytecode.emit('POP')

        elif isinstance(left_expr, Member):
            left_expr.expr.emit(bytecode)
            left_expr.left.emit(bytecode)
            bytecode.emit('STORE_MEMBER')
            bytecode.emit('POP')
        else:
            raise JsTypeError(u'unsupported')

        body.emit(bytecode)
        bytecode.emit('JUMP', precond)
        bytecode.emit_endloop_label(finish)
Example #18
0
    def has_instance(self, v):
        from js.object_space import isnull_or_undefined
        if not isinstance(v, W_BasicObject):
            return False

        o = self.get(u'prototype')

        if not isinstance(o, W_BasicObject):
            raise JsTypeError(u'has_instance')

        while True:
            assert isinstance(v, W_BasicObject)
            v = v.prototype()
            if isnull_or_undefined(v):
                return False
            if v == o:
                return True
Example #19
0
def sort_compare(obj, j, k, comparefn=newundefined()):
    from js.object_space import isundefined

    j_string = j
    k_string = k
    has_j = obj.has_property(j)
    has_k = obj.has_property(k)

    if has_j is False and has_k is False:
        return 0
    if has_j is False:
        return 1
    if has_k is False:
        return -1

    x = obj.get(j_string)
    y = obj.get(k_string)

    if isundefined(x) and isundefined(y):
        return 0
    if isundefined(x):
        return 1
    if isundefined(y):
        return -1

    if not isundefined(comparefn):
        if not comparefn.is_callable():
            from js.exception import JsTypeError
            raise JsTypeError(u'')

        from js.jsobj import W_BasicFunction
        assert isinstance(comparefn, W_BasicFunction)
        res = comparefn.Call(args=[x, y], this=newundefined())
        return res.ToInteger()

    x_string = x.to_string()
    y_string = y.to_string()
    if x_string < y_string:
        return -1
    if x_string > y_string:
        return 1
    return 0
Example #20
0
    def put(self, p, v, throw=False):
        assert p is not None and isinstance(p, unicode)

        if not self.can_put(p):
            if throw:
                raise JsTypeError(u"can't put %s" % (p, ))
            else:
                return

        own_desc = self.get_own_property(p)
        if is_data_descriptor(own_desc) is True:
            value_desc = PropertyDescriptor(value=v)
            self.define_own_property(p, value_desc, throw)
            return

        desc = self.get_property(p)
        if is_accessor_descriptor(desc) is True:
            setter = desc.setter
            assert setter is not None
            setter.Call(this=self, args=[v])
        else:
            new_desc = DataPropertyDescriptor(v, True, True, True)
            self.define_own_property(p, new_desc, throw)
Example #21
0
 def get(self, p):
     v = W_BasicObject.get(self, p)
     if p is u'caller' and isinstance(v, W__Function) and v.is_strict():
         raise JsTypeError(u'')
     return v
Example #22
0
 def ToObject(self):
     raise JsTypeError(u'W_Root.ToObject')
Example #23
0
 def has_instance(self, other):
     raise JsTypeError(u'has_instance')
Example #24
0
 def check_object_coercible(self):
     raise JsTypeError(u'W_Undefined.check_object_coercible')
Example #25
0
def _ireject(throw, idx):
    if throw:
        raise JsTypeError(unicode(str(idx)))
    return False
Example #26
0
def reject(throw, msg=u''):
    if throw:
        raise JsTypeError(msg)
    return False
Example #27
0
    def Call(self, args=[], this=None, calling_context=None):
        if not isinstance(this, W_BasicObject):
            raise JsTypeError(u'')

        proto = args[0]
        this._prototype_ = proto
Example #28
0
 def ToObject(self):
     raise JsTypeError(u'W_Null.ToObject')
Example #29
0
 def check_object_coercible(self):
     raise JsTypeError(u'W_Null.check_object_coercible')
Example #30
0
 def ToObject(self):
     raise JsTypeError(u'W_Undefined.ToObject')