Пример #1
0
    def __init__(self):
        allon = DE | DD | RO
        global_instance = W_Object(Class="global")

        ctx = global_context(global_instance)

        w_ObjPrototype = W_Object(Prototype=None, Class='Object')

        w_Function = W_Function(ctx, Class='Function', Prototype=w_ObjPrototype)
        w_FncPrototype = W_Function(ctx, Class='Function', Prototype=w_ObjPrototype)

        w_Function.Put(ctx, 'prototype', w_FncPrototype, flags = allon)
        w_Function.Put(ctx, 'length', W_IntNumber(1), flags = allon)
        w_Function.Put(ctx, 'constructor', w_Function, flags=allon)

        global_instance.Put(ctx, 'Function', w_Function)
        global_instance.Prototype = w_ObjPrototype

        w_Object = W_ObjectObject('Object', w_FncPrototype)
        w_Object.Put(ctx, 'prototype', w_ObjPrototype, flags = allon)
        w_Object.Put(ctx, 'length', W_IntNumber(1), flags = allon)

        global_instance.Put(ctx, 'Object', w_Object)

        toString = W_ToString(ctx)

        put_values(ctx, w_ObjPrototype, {
            'constructor': w_Object,
            '__proto__': w_Null,
            'toString': toString,
            'toLocaleString': toString,
            'valueOf': W_ValueOf(ctx),
            'hasOwnProperty': W_HasOwnProperty(ctx),
            'isPrototypeOf': W_IsPrototypeOf(ctx),
            'propertyIsEnumerable': W_PropertyIsEnumerable(ctx),
        })

        #properties of the function prototype
        put_values(ctx, w_FncPrototype, {
            'constructor': w_Function,
            '__proto__': w_FncPrototype,
            'toString': W_FToString(ctx),
            'apply': W_Apply(ctx),
            'call': W_Call(ctx),
            'arguments': w_Null,
            'valueOf': W_ValueOf(ctx),
        })

        w_Boolean = W_BooleanObject('Boolean', w_FncPrototype)
        w_Boolean.Put(ctx, 'constructor', w_FncPrototype, flags = allon)
        w_Boolean.Put(ctx, 'length', W_IntNumber(1), flags = allon)

        w_BoolPrototype = create_object(ctx, 'Object', Value=newbool(False))
        w_BoolPrototype.Class = 'Boolean'

        put_values(ctx, w_BoolPrototype, {
            'constructor': w_FncPrototype,
            '__proto__': w_ObjPrototype,
            'toString': W_BooleanValueToString(ctx),
            'valueOf': get_value_of('Boolean')(ctx),
        })

        w_Boolean.Put(ctx, 'prototype', w_BoolPrototype, flags = allon)
        global_instance.Put(ctx, 'Boolean', w_Boolean)

        #Number
        w_Number = W_NumberObject('Number', w_FncPrototype)

        w_empty_fun = w_Function.Call(ctx, args=[W_String('')])

        w_NumPrototype = create_object(ctx, 'Object', Value=W_FloatNumber(0.0))
        w_NumPrototype.Class = 'Number'
        put_values(ctx, w_NumPrototype, {
            'constructor': w_Number,
            '__proto__': w_empty_fun,
            'toString': W_NumberValueToString(ctx),
            'valueOf': get_value_of('Number')(ctx),
        })

        put_values(ctx, w_Number, {
            'constructor': w_FncPrototype,
            'prototype': w_NumPrototype,
            '__proto__': w_FncPrototype,
            'length'   : W_IntNumber(1),
        })
        w_Number.propdict['prototype'].flags |= RO
        w_Number.Put(ctx, 'MAX_VALUE', W_FloatNumber(1.7976931348623157e308), flags = RO|DD)
        w_Number.Put(ctx, 'MIN_VALUE', W_FloatNumber(0), flags = RO|DD)
        w_Number.Put(ctx, 'NaN', W_FloatNumber(NAN), flags = RO|DD)
        # ^^^ this is exactly in test case suite
        w_Number.Put(ctx, 'POSITIVE_INFINITY', W_FloatNumber(INFINITY), flags = RO|DD)
        w_Number.Put(ctx, 'NEGATIVE_INFINITY', W_FloatNumber(-INFINITY), flags = RO|DD)


        global_instance.Put(ctx, 'Number', w_Number)


        #String
        w_String = W_StringObject('String', w_FncPrototype)

        w_StrPrototype = create_object(ctx, 'Object', Value=W_String(''))
        w_StrPrototype.Class = 'String'
        w_StrPrototype.Put(ctx, 'length', W_IntNumber(0))

        put_values(ctx, w_StrPrototype, {
            'constructor': w_String,
            '__proto__': w_StrPrototype,
            'toString': W_StringValueToString(ctx),
            'valueOf': get_value_of('String')(ctx),
            'charAt': W_CharAt(ctx),
            'charCodeAt': W_CharCodeAt(ctx),
            'concat': W_Concat(ctx),
            'indexOf': W_IndexOf(ctx),
            'lastIndexOf': W_LastIndexOf(ctx),
            'substring': W_Substring(ctx),
            'split': W_Split(ctx),
            'toLowerCase': W_ToLowerCase(ctx),
            'toUpperCase': W_ToUpperCase(ctx)
        })

        w_String.Put(ctx, 'prototype', w_StrPrototype, flags=allon)
        w_String.Put(ctx, 'fromCharCode', W_FromCharCode(ctx))
        global_instance.Put(ctx, 'String', w_String)

        w_Array = W_ArrayObject('Array', w_FncPrototype)

        w_ArrPrototype = W_Array(Prototype=w_ObjPrototype)

        put_values(ctx, w_ArrPrototype, {
            'constructor': w_FncPrototype,
            '__proto__': w_ArrPrototype,
            'toString': W_ArrayToString(ctx),
            'join': W_ArrayJoin(ctx),
            'reverse': W_ArrayReverse(ctx),
            'sort': W_ArraySort(ctx),
            'push': W_ArrayPush(ctx),
            'pop': W_ArrayPop(ctx),
        })

        w_Array.Put(ctx, 'prototype', w_ArrPrototype, flags = allon)
        w_Array.Put(ctx, '__proto__', w_FncPrototype, flags = allon)
        w_Array.Put(ctx, 'length', W_IntNumber(1), flags = allon)
        global_instance.Put(ctx, 'Array', w_Array)


        #Math
        w_math = W_Object(Class='Math')
        global_instance.Put(ctx, 'Math', w_math)
        w_math.Put(ctx, '__proto__',  w_ObjPrototype)
        w_math.Put(ctx, 'prototype', w_ObjPrototype, flags = allon)
        w_math.Put(ctx, 'abs', W_Builtin(absjs, Class='function'))
        w_math.Put(ctx, 'floor', W_Builtin(floorjs, Class='function'))
        w_math.Put(ctx, 'round', W_Builtin(roundjs, Class='function'))
        w_math.Put(ctx, 'pow', W_Builtin(powjs, Class='function'))
        w_math.Put(ctx, 'sqrt', W_Builtin(sqrtjs, Class='function'))
        w_math.Put(ctx, 'log', W_Builtin(logjs, Class='function'))
        w_math.Put(ctx, 'E', W_FloatNumber(math.e), flags=allon)
        w_math.Put(ctx, 'LN2', W_FloatNumber(math.log(2)), flags=allon)
        w_math.Put(ctx, 'LN10', W_FloatNumber(math.log(10)), flags=allon)
        log2e = math.log(math.e) / math.log(2) # rpython supports log with one argument only
        w_math.Put(ctx, 'LOG2E', W_FloatNumber(log2e), flags=allon)
        w_math.Put(ctx, 'LOG10E', W_FloatNumber(math.log10(math.e)), flags=allon)
        w_math.Put(ctx, 'PI', W_FloatNumber(math.pi), flags=allon)
        w_math.Put(ctx, 'SQRT1_2', W_FloatNumber(math.sqrt(0.5)), flags=allon)
        w_math.Put(ctx, 'SQRT2', W_FloatNumber(math.sqrt(2)), flags=allon)
        w_math.Put(ctx, 'random', W_Builtin(randomjs, Class='function'))
        w_math.Put(ctx, 'min', W_Builtin(minjs, Class='function'))
        w_math.Put(ctx, 'max', W_Builtin(maxjs, Class='function'))
        global_instance.Put(ctx, 'version', W_Builtin(versionjs), flags=allon)

        #Date
        w_Date = W_DateObject('Date', w_FncPrototype)

        w_DatePrototype = create_object(ctx, 'Object', Value=W_String(''))
        w_DatePrototype.Class = 'Date'

        put_values(ctx, w_DatePrototype, {
            '__proto__': w_DatePrototype,
            'valueOf': get_value_of('Date')(ctx),
            'getTime': get_value_of('Date')(ctx)
        })

        w_Date.Put(ctx, 'prototype', w_DatePrototype, flags=allon)
        global_instance.Put(ctx, 'Date', w_Date)

        global_instance.Put(ctx, 'NaN', W_FloatNumber(NAN), flags = DE|DD)
        global_instance.Put(ctx, 'Infinity', W_FloatNumber(INFINITY), flags = DE|DD)
        global_instance.Put(ctx, 'undefined', w_Undefined, flags = DE|DD)

        put_values(ctx, global_instance, {
            'this': global_instance,
            'eval': W_Eval(ctx),
            'parseInt': W_ParseInt(ctx),
            'parseFloat': W_ParseFloat(ctx),
            'isNaN': W_Builtin(isnanjs),
            'isFinite': W_Builtin(isfinitejs),
            'print': W_Builtin(printjs),
            'alert': W_Builtin(noop),
            'unescape': W_Builtin(unescapejs),
            'load': W_Builtin(make_loadjs(self))
        })

        # debugging
        if not we_are_translated():
            global_instance.Put(ctx, 'pypy_repr', W_Builtin(pypy_repr))


        self.global_context = ctx
        self.w_Global = global_instance
        self.w_Object = w_Object
Пример #2
0
 def __init__(self, Class, Prototype, ctx=None,
              Value=w_Undefined, callfunc=None):
     W_Object.__init__(self, ctx, Prototype,
                       Class, Value, callfunc)