Esempio n. 1
0
    def toString(self, v):
        if v.isUndefined():
            return Value.object(self.strings['undefined'])
        if v.isNull():
            return Value.object(self.strings['null'])
        if v.isBoolean():
            return Value.object(self.strings['true'] if v.toBoolean() else self.strings['false'])
        if v.isInt():
            return Value.object(new_string(str(v.toInt())))

        assert v.isObject()
        obj = v.toObject()
        if obj.isPrimitive():
            if obj.isDouble():
                return Value.object(new_string(obj.toString()))
            else:
                return v

        raise NotImplementedError('js object to string')
Esempio n. 2
0
    def setupGlobal(self):
        self.scope = None

        global_shape = Shape(0xf, 'Global', None, "", -1, 0, 0xff, None)
        global_properties = (c_int * 0xff)()
        global_object = PlainObject(addressof(global_shape), global_properties, None)

        self.root = [global_shape, global_properties, global_object]

        self.this = Value.object(global_object)
        self.object = Object(addressof(global_object))
Esempio n. 3
0
    def add(self, left, right):
        left = self.toPrimitive(left)
        right = self.toPrimitive(right)

        ltype = self.type(left)
        rtype = self.type(right)
        if ltype == ecma_type['string'] or rtype == ecma_type['string']:
            left = self.toString(left)
            right = self.toString(right)

            lstr = left.toObject().toString()
            rstr = right.toObject().toString()

            return Value.object(new_string(lstr + rstr))

        left = self.toNumber(left)
        right = self.toNumber(right)

        number = self.numberConvert(left) + self.numberConvert(right)
        return Value(boxed_number(number).value)
Esempio n. 4
0
    def toString(self, v):
        if v.isUndefined():
            return Value.object(self.strings['undefined'])
        if v.isNull():
            return Value.object(self.strings['null'])
        if v.isBoolean():
            return Value.object(self.strings['true'] if v.toBoolean(
            ) else self.strings['false'])
        if v.isInt():
            return Value.object(new_string(str(v.toInt())))

        assert v.isObject()
        obj = v.toObject()
        if obj.isPrimitive():
            if obj.isDouble():
                return Value.object(new_string(obj.toString()))
            else:
                return v

        raise NotImplementedError('js object to string')
Esempio n. 5
0
    def setupGlobal(self):
        self.scope = None

        global_shape = Shape(0xf, 'Global', None, "", -1, 0, 0xff, None)
        global_properties = (c_int * 0xff)()
        global_object = PlainObject(addressof(global_shape), global_properties,
                                    None)

        self.root = [global_shape, global_properties, global_object]

        self.this = Value.object(global_object)
        self.object = Object(addressof(global_object))
Esempio n. 6
0
    def toBoolean(self, v):
        if v.isUndefined():
            return Value(Value.false)
        if v.isNull():
            return Value(Value.false)
        if v.isBoolean():
            return v
        if v.isInt():
            return Value.boolean(v.toInt())

        obj = v.toObject()
        if obj.isObject():
            return Value(Value.true)
        if obj.isString():
            return Value.boolean(obj.toString())

        assert obj.isDouble()
        double = obj.toDouble()
        if math.isnan(double):
            return Value(Value.false)
        return Value.boolean(double)
Esempio n. 7
0
    def toNumber(self, v):
        if v.isUndefined():
            return Value.object(self.doubles['NaN'])
        if v.isNull():
            return Value.int(0)
        if v.isBoolean():
            return Value.int(0 + v.toBoolean())
        if v.isInt():
            return v
        obj = v.toObject()
        if obj.isString():
            str = obj.toString()
            try:
                v = int(str)
                return Value.int(v)
            except:
                try:
                    v = float(str)
                    return Value.object(new_float(v))
                except:
                    return Value.object(self.doubles['NaN'])

        if obj.isDouble():
            return v

        primitive_value = self.toPrimitive(v, 'number')
        return self.toNumber(primitive_value)
Esempio n. 8
0
    def toNumber(self, v):
        if v.isUndefined():
            return Value.object(self.doubles['NaN'])
        if v.isNull():
            return Value.int(0)
        if v.isBoolean():
            return Value.int(0 + v.toBoolean())
        if v.isInt():
            return v
        obj = v.toObject()
        if obj.isString():
            str = obj.toString()
            try:
                v = int(str)
                return Value.int(v)
            except:
                try:
                    v = float(str)
                    return Value.object(new_float(v))
                except:
                    return Value.object(self.doubles['NaN'])

        if obj.isDouble():
            return v

        primitive_value = self.toPrimitive(v, 'number')
        return self.toNumber(primitive_value)
Esempio n. 9
0
    def add(self, left, right):
        left = self.toPrimitive(left)
        right = self.toPrimitive(right)

        ltype = self.type(left)
        rtype = self.type(right)
        if ltype == ecma_type['string'] or rtype == ecma_type['string']:
            left = self.toString(left)
            right = self.toString(right)

            lstr = left.toObject().toString()
            rstr = right.toObject().toString()

            return Value.object(new_string(lstr + rstr))


        left = self.toNumber(left)
        right = self.toNumber(right)

        number = self.numberConvert(left) + self.numberConvert(right)
        return Value(boxed_number(number).value)
Esempio n. 10
0
    def toBoolean(self, v):
        if v.isUndefined():
            return Value(Value.false)
        if v.isNull():
            return Value(Value.false)
        if v.isBoolean():
            return v
        if v.isInt():
            return Value.boolean(v.toInt())

        obj = v.toObject()
        if obj.isObject():
            return Value(Value.true)
        if obj.isString():
            return Value.boolean(obj.toString())

        assert obj.isDouble()
        double = obj.toDouble()
        if math.isnan(double):
            return Value(Value.false)
        return Value.boolean(double)
Esempio n. 11
0
 def declare_vars(self, vars):
     for var in vars:
         self.ctx.object.addProperty(var.name, Value(Value.undefined))