Beispiel #1
0
def _get_from_desc(desc, this):
    from js.object_space import newundefined
    if desc is None:
        return newundefined()

    if is_data_descriptor(desc):
        return desc.value

    if desc.has_set_getter() is False:
        return newundefined()

    getter = desc.getter
    res = getter.Call(this=this)
    return res
Beispiel #2
0
    def __init__(self, code, formal_parameters=[], argv=[], this=newundefined(), strict=False, scope=None, w_func=None):
        from js.jsobj import W_BasicObject
        from js.object_space import object_space, isnull_or_undefined

        stack_size = code.estimated_stack_size()
        env_size = code.env_size() + 1  # neet do add one for the arguments object

        ExecutionContext.__init__(self, stack_size, env_size)

        self._code_ = code
        self._argument_values_ = argv
        self._strict_ = strict
        self._scope_ = scope
        self._w_func_ = w_func
        self._calling_context_ = None

        from js.lexical_environment import DeclarativeEnvironment
        localEnv = DeclarativeEnvironment(scope, env_size, False)
        self._lexical_environment_ = localEnv
        self._variable_environment_ = localEnv

        if strict:
            self._this_binding_ = this
        else:
            if this is None or isnull_or_undefined(this):
                self._this_binding_ = object_space.global_object
            else:
                assert isinstance(this, W_BasicObject)

                if this.klass() is not 'Object':
                    self._this_binding_ = this.ToObject()
                else:
                    self._this_binding_ = this

        self.declaration_binding_initialization()
 def create_mutuable_binding(self, identifier, deletable):
     from js.object_space import newundefined
     assert identifier is not None and isinstance(identifier, unicode)
     assert not self.has_binding(identifier)
     self._add_binding(identifier, newundefined())
     self._set_mutable_binding(identifier)
     if deletable:
         self._set_deletable_binding(identifier)
 def create_mutuable_binding(self, identifier, deletable):
     from js.object_space import newundefined
     assert identifier is not None and isinstance(identifier, unicode)
     assert not self.has_binding(identifier)
     self._add_binding(identifier, newundefined())
     self._set_mutable_binding(identifier)
     if deletable:
         self._set_deletable_binding(identifier)
 def get_binding_value(self, identifier, strict=False):
     from js.object_space import newundefined
     assert identifier is not None and isinstance(identifier, unicode)
     if not self.has_binding(identifier):
         if strict:
             from js.exception import JsReferenceError
             raise JsReferenceError(identifier)
         else:
             return newundefined()
     return self._get_binding(identifier)
 def get_binding_value(self, identifier, strict=False):
     from js.object_space import newundefined
     assert identifier is not None and isinstance(identifier, unicode)
     if not self.has_binding(identifier):
         if strict:
             from js.exception import JsReferenceError
             raise JsReferenceError(identifier)
         else:
             return newundefined()
     return self._get_binding(identifier)
Beispiel #7
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
Beispiel #8
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
Beispiel #9
0
def for_each(this, args):
    obj = this
    length = this.get(u'length').ToUInt32()

    callback = get_arg(args, 0)
    from js.jsobj import W_BasicFunction
    assert isinstance(callback, W_BasicFunction)

    for i in xrange(length):
        x = obj.get(unicode(str(i)))
        callback.Call(args=[x], this=newundefined())
Beispiel #10
0
def for_each(this, args):
    obj = this
    length = this.get(u'length').ToUInt32()

    callback = get_arg(args, 0)
    from js.jsobj import W_BasicFunction
    assert isinstance(callback, W_BasicFunction)

    for i in xrange(length):
        x = obj.get(unicode(str(i)))
        callback.Call(args=[x], this=newundefined())
    def create_mutuable_binding(self, n, d):
        assert n is not None and isinstance(n, unicode)
        bindings = self.binding_object
        assert bindings.has_property(n) is False
        if d is True:
            config_value = False
        else:
            config_value = True

        from js.jsobj import PropertyDescriptor
        from js.object_space import newundefined
        desc = PropertyDescriptor(value=newundefined(), writable=True, enumerable=True, configurable=config_value)
        bindings.define_own_property(n, desc, True)
    def get_binding_value(self, n, s=False):
        from js.object_space import newundefined
        assert n is not None and isinstance(n, unicode)
        bindings = self.binding_object
        value = bindings.has_property(n)
        if value is False:
            if s is False:
                return newundefined()
            else:
                from js.exception import JsReferenceError
                raise JsReferenceError(self.__class__)

        return bindings.get(n)
    def get_binding_value(self, n, s=False):
        from js.object_space import newundefined
        assert n is not None and isinstance(n, unicode)
        bindings = self.binding_object
        value = bindings.has_property(n)
        if value is False:
            if s is False:
                return newundefined()
            else:
                from js.exception import JsReferenceError
                raise JsReferenceError(self.__class__)

        return bindings.get(n)
Beispiel #14
0
def pop(this, args):
    o = this.ToObject()
    lenVal = o.get(u'length')
    l = lenVal.ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        indx = l - 1
        indxs = unicode(str(indx))
        element = o.get(indxs)
        o.delete(indxs, True)
        o.put(u'length', _w(indx))
        return element
Beispiel #15
0
def pop(this, args):
    o = this.ToObject()
    lenVal = o.get(u'length')
    l = lenVal.ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        indx = l - 1
        indxs = unicode(str(indx))
        element = o.get(indxs)
        o.delete(indxs, True)
        o.put(u'length', _w(indx))
        return element
    def create_mutuable_binding(self, n, d):
        assert n is not None and isinstance(n, unicode)
        bindings = self.binding_object
        assert bindings.has_property(n) is False
        if d is True:
            config_value = False
        else:
            config_value = True

        from js.jsobj import PropertyDescriptor
        from js.object_space import newundefined
        desc = PropertyDescriptor(value=newundefined(),
                                  writable=True,
                                  enumerable=True,
                                  configurable=config_value)
        bindings.define_own_property(n, desc, True)
Beispiel #17
0
def shift(this, args):
    o = this.ToObject()
    l = o.get(u'length').ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        new_length = l - 1
        element = o.get(u"0")
        for i in xrange(0, new_length):
            indx = unicode(str(i))
            next_indx = unicode(str(i + 1))
            o.put(indx, o.get(next_indx))
        o.put(u'length', _w(new_length))
        return element
Beispiel #18
0
def shift(this, args):
    o = this.ToObject()
    l = o.get(u'length').ToUInt32()

    if l == 0:
        o.put(u'length', _w(0))
        return newundefined()
    else:
        new_length = l - 1
        element = o.get(u"0")
        for i in xrange(0, new_length):
            indx = unicode(str(i))
            next_indx = unicode(str(i + 1))
            o.put(indx, o.get(next_indx))
        o.put(u'length', _w(new_length))
        return element
Beispiel #19
0
def setup(global_object):
    from rpython.rlib.objectmodel import we_are_translated
    from js.builtins import put_intimate_function, put_native_function, put_property
    from js.builtins.number import w_NAN
    from js.builtins.number import w_POSITIVE_INFINITY
    from js.object_space import newundefined

    # 15.1.1.1
    put_property(global_object, u'NaN', w_NAN, writable=False, enumerable=False, configurable=False)

    # 15.1.1.2
    put_property(global_object, u'Infinity', w_POSITIVE_INFINITY, writable=False, enumerable=False, configurable=False)

    # 15.1.1.3
    put_property(global_object, u'undefined', newundefined(), writable=False, enumerable=False, configurable=False)

    # 15.1.2.1
    put_intimate_function(global_object, u'eval', js_eval, params=[u'x'])

    # 15.1.2.2
    put_native_function(global_object, u'parseInt', parse_int, params=[u'string', u'radix'])

    # 15.1.2.3
    # TODO
    put_native_function(global_object, u'parseFloat', parse_float, params=[u'string'])

    # 15.1.2.4
    put_native_function(global_object, u'isNaN', is_nan, params=[u'number'])

    # 15.1.2.5
    put_native_function(global_object, u'isFinite', is_finite, params=[u'number'])

    put_native_function(global_object, u'alert', alert)

    put_native_function(global_object, u'print', printjs)

    put_native_function(global_object, u'escape', escape, params=[u'string'])

    put_native_function(global_object, u'unescape', unescape, params=[u'string'])

    put_native_function(global_object, u'version', version)

    ## debugging
    if not we_are_translated():
        put_native_function(global_object, u'pypy_repr', pypy_repr)
        put_native_function(global_object, u'inspect', inspect)
 def implicit_this_value(self):
     from js.object_space import newundefined
     if self.provide_this is True:
         return self.binding_object
     return newundefined()
 def implicit_this_value(self):
     from js.object_space import newundefined
     return newundefined()
Beispiel #22
0
    def declaration_binding_initialization(self):
        from js.object_space import newundefined

        env = self._variable_environment_.environment_record
        strict = self._strict_
        code = jit.promote(self._code_)

        if code.is_eval_code():
            configurable_bindings = True
        else:
            configurable_bindings = False

        # 4.
        if code.is_function_code():
            names = code.params()
            n = 0
            args = self._argument_values_

            arg_count = len(args)
            for arg_name in names:
                n += 1
                if n > arg_count:
                    v = newundefined()
                else:
                    v = args[n - 1]
                arg_already_declared = env.has_binding(arg_name)
                if arg_already_declared is False:
                    env.create_mutuable_binding(arg_name, configurable_bindings)
                env.set_mutable_binding(arg_name, v, False)

        # 5.
        func_declarations = code.functions()
        for fn in func_declarations:
            fo = None
            func_already_declared = env.has_binding(fn)
            if func_already_declared is False:
                env.create_mutuable_binding(fn, configurable_bindings)
            else:
                pass  # see 10.5 5.e
            env.set_mutable_binding(fn, fo, False)

        arguments_already_declared = env.has_binding(u'arguments')
        # 7.
        if code.is_function_code() and arguments_already_declared is False:
            from js.jsobj import W_Arguments
            # TODO get calling W_Function
            func = self._w_func_
            arguments = self._argument_values_
            names = code.params()
            args_obj = W_Arguments(func, names, arguments, env, strict)

            if strict is True:
                env.create_immutable_bining(u'arguments')
                env.initialize_immutable_binding(u'arguments', args_obj)
            else:
                env.create_mutuable_binding(u'arguments', False)  # TODO not sure if mutable binding is deletable
                env.set_mutable_binding(u'arguments', args_obj, False)

        # 8.
        var_declarations = code.variables()
        for dn in var_declarations:
            var_already_declared = env.has_binding(dn)
            if var_already_declared is False:
                env.create_mutuable_binding(dn, configurable_bindings)
                env.set_mutable_binding(dn, newundefined(), False)
Beispiel #23
0
def empty(this, args):
    from js.object_space import newundefined
    return newundefined()
 def implicit_this_value(self):
     from js.object_space import newundefined
     if self.provide_this is True:
         return self.binding_object
     return newundefined()
Beispiel #25
0
def setup(global_object):
    from rpython.rlib.objectmodel import we_are_translated
    from js.builtins import put_intimate_function, put_native_function, put_property
    from js.builtins.number import w_NAN
    from js.builtins.number import w_POSITIVE_INFINITY
    from js.object_space import newundefined

    # 15.1.1.1
    put_property(global_object,
                 u'NaN',
                 w_NAN,
                 writable=False,
                 enumerable=False,
                 configurable=False)

    # 15.1.1.2
    put_property(global_object,
                 u'Infinity',
                 w_POSITIVE_INFINITY,
                 writable=False,
                 enumerable=False,
                 configurable=False)

    # 15.1.1.3
    put_property(global_object,
                 u'undefined',
                 newundefined(),
                 writable=False,
                 enumerable=False,
                 configurable=False)

    # 15.1.2.1
    put_intimate_function(global_object, u'eval', js_eval, params=[u'x'])

    # 15.1.2.2
    put_native_function(global_object,
                        u'parseInt',
                        parse_int,
                        params=[u'string', u'radix'])

    # 15.1.2.3
    # TODO
    put_native_function(global_object,
                        u'parseFloat',
                        parse_float,
                        params=[u'string'])

    # 15.1.2.4
    put_native_function(global_object, u'isNaN', is_nan, params=[u'number'])

    # 15.1.2.5
    put_native_function(global_object,
                        u'isFinite',
                        is_finite,
                        params=[u'number'])

    put_native_function(global_object, u'alert', alert)

    put_native_function(global_object, u'print', printjs)

    put_native_function(global_object, u'escape', escape, params=[u'string'])

    put_native_function(global_object,
                        u'unescape',
                        unescape,
                        params=[u'string'])

    put_native_function(global_object, u'version', version)

    ## debugging
    if not we_are_translated():
        put_native_function(global_object, u'pypy_repr', pypy_repr)
        put_native_function(global_object, u'inspect', inspect)
 def implicit_this_value(self):
     from js.object_space import newundefined
     return newundefined()
Beispiel #27
0
 def eval(self, ctx):
     from js.object_space import newundefined
     ctx.stack_append(newundefined())
Beispiel #28
0
def get_arg(args, index, default=newundefined()):
    if len(args) > index:
        return args[index]
    return default
Beispiel #29
0
 def eval(self, ctx):
     from js.object_space import newundefined
     ctx.stack_append(newundefined())