[pypy-svn] r55285 - in pypy/branch/js-refactoring/pypy/lang/js: . test test/ecma

santagada at codespeak.net santagada at codespeak.net
Tue May 27 02:46:30 CEST 2008


Author: santagada
Date: Tue May 27 02:46:27 2008
New Revision: 55285

Modified:
   pypy/branch/js-refactoring/pypy/lang/js/baseop.py
   pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
   pypy/branch/js-refactoring/pypy/lang/js/js_interactive.py
   pypy/branch/js-refactoring/pypy/lang/js/jscode.py
   pypy/branch/js-refactoring/pypy/lang/js/jsobj.py
   pypy/branch/js-refactoring/pypy/lang/js/test/ecma/conftest.py
   pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py
Log:
new array.Put method, some more To... receive the context, new reporting, and a huge bug about primitive objects not having propdicts. probably they also need the funcions from their object conterparts

Modified: pypy/branch/js-refactoring/pypy/lang/js/baseop.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/baseop.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/baseop.py	Tue May 27 02:46:27 2008
@@ -34,8 +34,8 @@
 
 def sub(ctx, nleft, nright):
     if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
-        ileft = nleft.ToInt32()
-        iright = nright.ToInt32()
+        ileft = nleft.ToInt32(ctx)
+        iright = nright.ToInt32(ctx)
         try:
             return W_IntNumber(ovfcheck(ileft - iright))
         except OverflowError:
@@ -46,8 +46,8 @@
 
 def mult(ctx, nleft, nright):
     if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
-        ileft = nleft.ToInt32()
-        iright = nright.ToInt32()
+        ileft = nleft.ToInt32(ctx)
+        iright = nright.ToInt32(ctx)
         try:
             return W_IntNumber(ovfcheck(ileft * iright))
         except OverflowError:
@@ -57,9 +57,9 @@
     return W_FloatNumber(fleft * fright)
 
 def mod(ctx, nleft, nright): # XXX this one is really not following spec
-    ileft = nleft.ToInt32()
-    iright = nright.ToInt32()
-    return W_IntNumber(ileft % iright)
+    fleft = nleft.ToNumber(ctx)
+    fright = nright.ToNumber(ctx)
+    return W_FloatNumber(fleft % fright)
 
 def division(ctx, nleft, nright):
     # XXX optimise for ints and floats

Modified: pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/interpreter.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/interpreter.py	Tue May 27 02:46:27 2008
@@ -94,10 +94,10 @@
 
 class W_ArrayObject(W_NativeObject):
     def Call(self, ctx, args=[], this=None):
-        proto = ctx.get_global().Get('Array').Get('prototype')
+        proto = ctx.get_global().Get(ctx, 'Array').Get(ctx, 'prototype')
         array = W_Array(ctx, Prototype=proto, Class = proto.Class)
         for i in range(len(args)):
-            array.Put(str(i), args[i])
+            array.Put(ctx, str(i), args[i])
         return array
 
     def Construct(self, ctx, args=[]):
@@ -127,7 +127,7 @@
         return W_FloatNumber(NAN)
     s = args[0].ToString(ctx).strip(" ")
     if len(args) > 1:
-        radix = args[1].ToInt32()
+        radix = args[1].ToInt32(ctx)
     else:
         radix = 10
     if len(s) >= 2 and (s.startswith('0x') or s.startswith('0X')) :
@@ -324,7 +324,7 @@
     def Call(self, ctx, args=[], this=None):
         string = this.ToString(ctx)
         if len(args)>=1:
-            pos = args[0].ToInt32()
+            pos = args[0].ToInt32(ctx)
             if (not pos >=0) or (pos > len(string) - 1):
                 return W_String('')
         else:
@@ -349,7 +349,7 @@
         if len(args) < 2:
             pos = 0
         else:
-            pos = args[1].ToInt32()
+            pos = args[1].ToInt32(ctx)
         pos = min(max(pos, 0), size)
         return W_IntNumber(string.find(substr, pos))
 
@@ -360,11 +360,11 @@
         if len(args) < 1:
             start = 0
         else:
-            start = args[0].ToInt32()
+            start = args[0].ToInt32(ctx)
         if len(args) < 2:
             end = size
         else:
-            end = args[1].ToInt32()
+            end = args[1].ToInt32(ctx)
         tmp1 = min(max(start, 0), size)
         tmp2 = min(max(end, 0), size)
         start = min(tmp1, tmp2)
@@ -373,9 +373,9 @@
 
 class W_ArrayToString(W_NewBuiltin):
     def Call(self, ctx, args=[], this=None):
-        length = this.Get('length').ToUInt32()
+        length = this.Get(ctx, 'length').ToUInt32(ctx)
         sep = ','
-        return W_String(sep.join([this.Get(str(index)).ToString(ctx) 
+        return W_String(sep.join([this.Get(ctx, str(index)).ToString(ctx) 
                             for index in range(length)]))
 
 class W_DateFake(W_NewBuiltin): # XXX This is temporary
@@ -393,7 +393,7 @@
     def __init__(self):
         def put_values(obj, dictvalues):
             for key,value in dictvalues.iteritems():
-                obj.Put(key, value)
+                obj.Put(ctx, key, value)
             
         w_Global = W_Object(Class="global")
 
@@ -404,17 +404,17 @@
         w_Function = W_Function(ctx, Class='Function', 
                               Prototype=w_ObjPrototype)
         
-        w_Global.Put('Function', w_Function)
+        w_Global.Put(ctx, 'Function', w_Function)
         
         w_Object = W_ObjectObject('Object', w_Function)
-        w_Object.Put('prototype', w_ObjPrototype, dd=True, de=True, ro=True)
+        w_Object.Put(ctx, 'prototype', w_ObjPrototype, dd=True, de=True, ro=True)
         
-        w_Global.Put('Object', w_Object)
+        w_Global.Put(ctx, 'Object', w_Object)
         w_FncPrototype = w_Function.Call(ctx, this=w_Function)
-        w_Function.Put('prototype', w_FncPrototype, dd=True, de=True, ro=True)
-        w_Function.Put('constructor', w_Function)
+        w_Function.Put(ctx, 'prototype', w_FncPrototype, dd=True, de=True, ro=True)
+        w_Function.Put(ctx, 'constructor', w_Function)
         
-        w_Object.Put('length', W_IntNumber(1), ro=True, dd=True)
+        w_Object.Put(ctx, 'length', W_IntNumber(1), ro=True, dd=True)
         
         toString = W_ToString(ctx)
         
@@ -439,8 +439,8 @@
         })
         
         w_Boolean = W_BooleanObject('Boolean', w_FncPrototype)
-        w_Boolean.Put('constructor', w_FncPrototype, dd=True, ro=True, de=True)
-        w_Boolean.Put('length', W_IntNumber(1), dd=True, ro=True, de=True)
+        w_Boolean.Put(ctx, 'constructor', w_FncPrototype, dd=True, ro=True, de=True)
+        w_Boolean.Put(ctx, 'length', W_IntNumber(1), dd=True, ro=True, de=True)
         
         w_BoolPrototype = create_object(ctx, 'Object', Value=W_Boolean(False))
         w_BoolPrototype.Class = 'Boolean'
@@ -452,9 +452,9 @@
             'valueOf': get_value_of('Boolean', ctx),
         })
 
-        w_Boolean.Put('prototype', w_BoolPrototype, dd=True, ro=True, de=True)
+        w_Boolean.Put(ctx, 'prototype', w_BoolPrototype, dd=True, ro=True, de=True)
 
-        w_Global.Put('Boolean', w_Boolean)
+        w_Global.Put(ctx, 'Boolean', w_Boolean)
 
         #Number
         w_Number = W_NumberObject('Number', w_FncPrototype)
@@ -477,18 +477,18 @@
             'length'   : W_IntNumber(1),
         })
         w_Number.propdict['prototype'].ro = True
-        w_Number.Put('MAX_VALUE', W_FloatNumber(1.7976931348623157e308),
+        w_Number.Put(ctx, 'MAX_VALUE', W_FloatNumber(1.7976931348623157e308),
                      ro=True, dd=True)
-        w_Number.Put('MIN_VALUE', W_FloatNumber(0), ro=True, dd=True)
-        w_Number.Put('NaN', W_FloatNumber(NAN), ro=True, dd=True)
+        w_Number.Put(ctx, 'MIN_VALUE', W_FloatNumber(0), ro=True, dd=True)
+        w_Number.Put(ctx, 'NaN', W_FloatNumber(NAN), ro=True, dd=True)
         # ^^^ this is exactly in test case suite
-        w_Number.Put('POSITIVE_INFINITY', W_FloatNumber(INFINITY),
+        w_Number.Put(ctx, 'POSITIVE_INFINITY', W_FloatNumber(INFINITY),
                      ro=True, dd=True)
-        w_Number.Put('NEGATIVE_INFINITY', W_FloatNumber(-INFINITY),
+        w_Number.Put(ctx, 'NEGATIVE_INFINITY', W_FloatNumber(-INFINITY),
                      ro=True, dd=True)
         
 
-        w_Global.Put('Number', w_Number)
+        w_Global.Put(ctx, 'Number', w_Number)
         
                 
         #String
@@ -508,8 +508,8 @@
             'substring': W_Substring(ctx),
         })
         
-        w_String.Put('prototype', w_StrPrototype)
-        w_Global.Put('String', w_String)
+        w_String.Put(ctx, 'prototype', w_StrPrototype)
+        w_Global.Put(ctx, 'String', w_String)
 
         w_Array = W_ArrayObject('Array', w_FncPrototype)
 
@@ -522,43 +522,43 @@
             'toString': W_ArrayToString(ctx),
         })
         
-        w_Array.Put('prototype', w_ArrPrototype)
-        w_Global.Put('Array', w_Array)
+        w_Array.Put(ctx, 'prototype', w_ArrPrototype)
+        w_Global.Put(ctx, 'Array', w_Array)
         
         
         #Math
         w_math = W_Object(Class='Math')
-        w_Global.Put('Math', w_math)
-        w_math.Put('__proto__',  w_ObjPrototype)
-        w_math.Put('prototype', w_ObjPrototype, dd=True, de=True, ro=True)
-        w_math.Put('abs', W_Builtin(absjs, Class='function'))
-        w_math.Put('floor', W_Builtin(floorjs, Class='function'))
-        w_math.Put('pow', W_Builtin(powjs, Class='function'))
-        w_math.Put('sqrt', W_Builtin(sqrtjs, Class='function'))
-        w_math.Put('E', W_FloatNumber(math.e))
-        w_math.Put('PI', W_FloatNumber(math.pi))
+        w_Global.Put(ctx, 'Math', w_math)
+        w_math.Put(ctx, '__proto__',  w_ObjPrototype)
+        w_math.Put(ctx, 'prototype', w_ObjPrototype, dd=True, de=True, ro=True)
+        w_math.Put(ctx, 'abs', W_Builtin(absjs, Class='function'))
+        w_math.Put(ctx, 'floor', W_Builtin(floorjs, 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, 'E', W_FloatNumber(math.e))
+        w_math.Put(ctx, 'PI', W_FloatNumber(math.pi))
         
-        w_Global.Put('version', W_Builtin(versionjs))
+        w_Global.Put(ctx, 'version', W_Builtin(versionjs))
         
         #Date
         w_Date = W_DateFake(ctx, Class='Date')
-        w_Global.Put('Date', w_Date)
+        w_Global.Put(ctx, 'Date', w_Date)
         
-        w_Global.Put('NaN', W_FloatNumber(NAN))
-        w_Global.Put('Infinity', W_FloatNumber(INFINITY))
-        w_Global.Put('undefined', w_Undefined)
-        w_Global.Put('eval', W_Builtin(evaljs))
-        w_Global.Put('parseInt', W_Builtin(parseIntjs))
-        w_Global.Put('parseFloat', W_Builtin(parseFloatjs))
-        w_Global.Put('isNaN', W_Builtin(isnanjs))
-        w_Global.Put('isFinite', W_Builtin(isfinitejs))            
+        w_Global.Put(ctx, 'NaN', W_FloatNumber(NAN))
+        w_Global.Put(ctx, 'Infinity', W_FloatNumber(INFINITY))
+        w_Global.Put(ctx, 'undefined', w_Undefined)
+        w_Global.Put(ctx, 'eval', W_Builtin(evaljs))
+        w_Global.Put(ctx, 'parseInt', W_Builtin(parseIntjs))
+        w_Global.Put(ctx, 'parseFloat', W_Builtin(parseFloatjs))
+        w_Global.Put(ctx, 'isNaN', W_Builtin(isnanjs))
+        w_Global.Put(ctx, 'isFinite', W_Builtin(isfinitejs))            
 
-        w_Global.Put('print', W_Builtin(printjs))
-        w_Global.Put('this', w_Global)
+        w_Global.Put(ctx, 'print', W_Builtin(printjs))
+        w_Global.Put(ctx, 'this', w_Global)
 
         # DEBUGGING
         if 0:
-            w_Global.Put('pypy_repr', W_Builtin(pypy_repr))
+            w_Global.Put(ctx, 'pypy_repr', W_Builtin(pypy_repr))
         
         self.global_context = ctx
         self.w_Global = w_Global

Modified: pypy/branch/js-refactoring/pypy/lang/js/js_interactive.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/js_interactive.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/js_interactive.py	Tue May 27 02:46:27 2008
@@ -49,9 +49,10 @@
     def __init__(self, locals=None, filename="<console>"):
         code.InteractiveConsole.__init__(self, locals, filename)
         self.interpreter = Interpreter()
-        self.interpreter.w_Global.Put('quit', W_Builtin(quitjs))
-        self.interpreter.w_Global.Put('load', W_Builtin(loadjs))
-        self.interpreter.w_Global.Put('trace', W_Builtin(tracejs))
+        ctx = self.interpreter.global_context
+        self.interpreter.w_Global.Put(ctx, 'quit', W_Builtin(quitjs))
+        self.interpreter.w_Global.Put(ctx, 'load', W_Builtin(loadjs))
+        self.interpreter.w_Global.Put(ctx, 'trace', W_Builtin(tracejs))
 
 
     def runcodefromfile(self, filename):

Modified: pypy/branch/js-refactoring/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/jscode.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/jscode.py	Tue May 27 02:46:27 2008
@@ -201,8 +201,8 @@
 
 class BaseBinaryBitwiseOp(Opcode):
     def eval(self, ctx, stack):
-        s5 = stack.pop().ToInt32()
-        s6 = stack.pop().ToInt32()
+        s5 = stack.pop().ToInt32(ctx)
+        s6 = stack.pop().ToInt32(ctx)
         stack.append(self.operation(ctx, s5, s6))
     
     def operation(self, ctx, op1, op2):
@@ -287,7 +287,7 @@
     def eval(self, ctx, stack):
         xxx
         scope = ctx.scope[self.depth]
-        stack.append(scope.Get(self.identifier))
+        stack.append(scope.Get(ctx, self.identifier))
         #stack.append(W_Reference(self.identifier, scope))
 
     def __repr__(self):
@@ -298,10 +298,10 @@
         self.counter = counter
 
     def eval(self, ctx, stack):
-        proto = ctx.get_global().Get('Array').Get('prototype')
+        proto = ctx.get_global().Get(ctx, 'Array').Get(ctx, 'prototype')
         array = W_Array(ctx, Prototype=proto, Class = proto.Class)
         for i in range(self.counter):
-            array.Put(str(self.counter - i - 1), stack.pop())
+            array.Put(ctx, str(self.counter - i - 1), stack.pop())
         stack.append(array)
 
     def __repr__(self):
@@ -325,13 +325,13 @@
         self.funcobj = funcobj
 
     def eval(self, ctx, stack):
-        proto = ctx.get_global().Get('Function').Get('prototype')
+        proto = ctx.get_global().Get(ctx, 'Function').Get(ctx, 'prototype')
         w_func = W_Object(ctx=ctx, Prototype=proto, Class='Function',
                           callfunc=self.funcobj)
-        w_func.Put('length', W_IntNumber(len(self.funcobj.params)))
+        w_func.Put(ctx, 'length', W_IntNumber(len(self.funcobj.params)))
         w_obj = create_object(ctx, 'Object')
-        w_obj.Put('constructor', w_func, de=True)
-        w_func.Put('prototype', w_obj)
+        w_obj.Put(ctx, 'constructor', w_func, de=True)
+        w_func.Put(ctx, 'prototype', w_obj)
         stack.append(w_func)
 
     def __repr__(self):
@@ -344,7 +344,7 @@
 
 #     def eval(self, ctx, stack):
 #         value = stack[-1]
-#         ctx.scope[self.depth].Put(self.name, value)
+#         ctx.scope[self.depth].Put(ctx, self.name, value)
 
 #     def __repr__(self):
 #         return 'STORE_VAR "%s"' % self.name
@@ -358,7 +358,7 @@
         for _ in range(self.counter):
             name = stack.pop().ToString(ctx)
             w_elem = stack.pop()
-            w_obj.Put(name, w_elem)
+            w_obj.Put(ctx, name, w_elem)
         stack.append(w_obj)
 
     def __repr__(self):
@@ -370,7 +370,7 @@
 
     def eval(self, ctx, stack):
         w_obj = stack.pop().ToObject(ctx)
-        stack.append(w_obj.Get(self.name))
+        stack.append(w_obj.Get(ctx, self.name))
         #stack.append(W_Reference(self.name, w_obj))
 
     def __repr__(self):
@@ -380,7 +380,7 @@
     def eval(self, ctx, stack):
         name = stack.pop().ToString(ctx)
         w_obj = stack.pop().ToObject(ctx)
-        stack.append(w_obj.Get(name))
+        stack.append(w_obj.Get(ctx, name))
         #stack.append(W_Reference(name, w_obj))
 
 class COMMA(BaseUnaryOperation):
@@ -449,20 +449,20 @@
 
 class URSH(BaseBinaryBitwiseOp):
     def eval(self, ctx, stack):
-        op2 = stack.pop().ToUInt32()
-        op1 = stack.pop().ToUInt32()
+        op2 = stack.pop().ToUInt32(ctx)
+        op1 = stack.pop().ToUInt32(ctx)
         stack.append(W_IntNumber(op1 >> (op2 & 0x1F)))
 
 class RSH(BaseBinaryBitwiseOp):
     def eval(self, ctx, stack):
-        op2 = stack.pop().ToUInt32()
-        op1 = stack.pop().ToInt32()
+        op2 = stack.pop().ToUInt32(ctx)
+        op1 = stack.pop().ToInt32(ctx)
         stack.append(W_IntNumber(op1 >> intmask(op2 & 0x1F)))
 
 class LSH(BaseBinaryBitwiseOp):
     def eval(self, ctx, stack):
-        op2 = stack.pop().ToUInt32()
-        op1 = stack.pop().ToInt32()
+        op2 = stack.pop().ToUInt32(ctx)
+        op1 = stack.pop().ToInt32(ctx)
         stack.append(W_IntNumber(op1 << intmask(op2 & 0x1F)))
 
 class MUL(BaseBinaryOperation):
@@ -542,7 +542,7 @@
         value = stack.pop()
         name = elem.ToString()
         value = self.operation(ctx, left, name, value)
-        left.Put(name, value)
+        left.Put(ctx, name, value)
         stack.append(value)
 
 class STORE_MEMBER(BaseStoreMember):
@@ -551,7 +551,7 @@
 
 class BaseStoreMemberAssign(BaseStoreMember):
     def operation(self, ctx, left, name, value):
-        prev = left.Get(name)
+        prev = left.Get(ctx, name)
         return self.decision(ctx, value, prev)
 
 class STORE_MEMBER_ADD(BaseStoreMemberAssign):
@@ -594,8 +594,8 @@
 
 class BaseAssignBitOper(BaseStore):
     def process(self, ctx, name, stack):
-        right = stack.pop().ToInt32()
-        left = ctx.resolve_identifier(name).ToInt32()
+        right = stack.pop().ToInt32(ctx)
+        left = ctx.resolve_identifier(name).ToInt32(ctx)
         result = self.operation(ctx, left, right)
         stack.append(result)
         return result
@@ -705,14 +705,14 @@
 
     def eval(self, ctx, stack):
         # function declaration actyally don't run anything
-        proto = ctx.get_global().Get('Function').Get('prototype')
+        proto = ctx.get_global().Get(ctx, 'Function').Get(ctx, 'prototype')
         w_func = W_Object(ctx=ctx, Prototype=proto, Class='Function', callfunc=self.funcobj)
-        w_func.Put('length', W_IntNumber(len(self.funcobj.params)))
+        w_func.Put(ctx, 'length', W_IntNumber(len(self.funcobj.params)))
         w_obj = create_object(ctx, 'Object')
-        w_obj.Put('constructor', w_func, de=True)
-        w_func.Put('prototype', w_obj)
+        w_obj.Put(ctx, 'constructor', w_func, de=True)
+        w_func.Put(ctx, 'prototype', w_obj)
         if self.funcobj.name is not None:
-            ctx.put(self.funcobj.name, w_func)
+            ctx.Put(ctx, self.funcobj.name, w_func)
 
     def __repr__(self):
         funcobj = self.funcobj
@@ -728,7 +728,7 @@
         self.name = name
 
     def eval(self, ctx, stack):
-        ctx.put(self.name, w_Undefined, dd=True)
+        ctx.Put(ctx, self.name, w_Undefined, dd=True)
 
     def __repr__(self):
         return 'DECLARE_VAR "%s"' % (self.name,)
@@ -758,7 +758,7 @@
         method = stack.pop()
         what = stack.pop().ToObject(ctx)
         args = stack.pop()
-        r1 = what.Get(method.ToString())
+        r1 = what.Get(ctx, method.ToString())
         if not isinstance(r1, W_PrimitiveObject):
             raise ThrowException(W_String("it is not a callable"))
         try:
@@ -792,7 +792,7 @@
                 if self.catchcode is not None:
                     # XXX just copied, I don't know if it's right
                     obj = W_Object()
-                    obj.Put(self.catchparam, e.exception)
+                    obj.Put(ctx, self.catchparam, e.exception)
                     ctx.push_object(obj)
                     try:
                         self.catchcode.run(ctx)

Modified: pypy/branch/js-refactoring/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/jsobj.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/jsobj.py	Tue May 27 02:46:27 2008
@@ -29,7 +29,7 @@
     #def GetValue(self):
     #    return self
 
-    def ToBoolean(self):
+    def ToBoolean(self, ctx):
         raise NotImplementedError()
 
     def ToPrimitive(self, ctx, hint=""):
@@ -45,24 +45,24 @@
     def ToNumber(self, ctx):
         return 0.0
     
-    def ToInt32(self):
+    def ToInt32(self, ctx):
         return int(self.ToNumber(ctx))
     
-    def ToUInt32(self):
+    def ToUInt32(self, ctx):
         return r_uint(0)
     
-    def Get(self, P):
-        raise NotImplementedError
+    def Get(self, ctx, P):
+        raise NotImplementedError()
     
-    def Put(self, P, V, dd=False,
+    def Put(self, ctx, P, V, dd=False,
             ro=False, de=False, it=False):
-        pass
+        raise NotImplementedError()
     
     def PutValue(self, w, ctx):
         pass
     
     def Call(self, ctx, args=[], this=None):
-        raise NotImplementedError
+        raise NotImplementedError()
 
     def __str__(self):
         return self.ToString(ctx=None)
@@ -106,11 +106,6 @@
 w_Null = W_Null()
 
 
-class W_Primitive(W_Root):
-    """unifying parent for primitives"""
-    def ToPrimitive(self, ctx, hint=""):
-        return self
-
 class W_PrimitiveObject(W_Root):
     def __init__(self, ctx=None, Prototype=None, Class='Object',
                  Value=w_Undefined, callfunc=None):
@@ -139,35 +134,35 @@
                 value = args[i]
             except IndexError:
                 value = w_Undefined
-            act.Put(paramname, value)
-        act.Put('this', this)
+            act.Put(ctx, paramname, value)
+        act.Put(ctx, 'this', this)
         w_Arguments = W_Arguments(self, args)
-        act.Put('arguments', w_Arguments)
+        act.Put(ctx, 'arguments', w_Arguments)
         newctx = function_context(self.Scope, act, this)
         val = self.callfunc.run(ctx=newctx)
         return val
     
     def Construct(self, ctx, args=[]):
         obj = W_Object(Class='Object')
-        prot = self.Get('prototype')
+        prot = self.Get(ctx, 'prototype')
         if isinstance(prot, W_PrimitiveObject):
             obj.Prototype = prot
         else: # would love to test this
             #but I fail to find a case that falls into this
-            obj.Prototype = ctx.get_global().Get('Object').Get('prototype')
+            obj.Prototype = ctx.get_global().Get(ctx, 'Object').Get(ctx, 'prototype')
         try: #this is a hack to be compatible to spidermonkey
             self.Call(ctx, args, this=obj)
             return obj
         except ReturnException, e:
             return e.value
         
-    def Get(self, P):
+    def Get(self, ctx, P):
         try:
             return self.propdict[P].value
         except KeyError:
             if self.Prototype is None:
                 return w_Undefined
-        return self.Prototype.Get(P) # go down the prototype chain
+        return self.Prototype.Get(ctx, P) # go down the prototype chain
     
     def CanPut(self, P):
         if P in self.propdict:
@@ -176,7 +171,7 @@
         if self.Prototype is None: return True
         return self.Prototype.CanPut(P)
 
-    def Put(self, P, V, dd=False,
+    def Put(self, ctx, P, V, dd=False,
             ro=False, de=False, it=False):
         try:
             P = self.propdict[P]
@@ -200,12 +195,12 @@
         return True
 
     def internal_def_value(self, ctx, tryone, trytwo):
-        t1 = self.Get(tryone)
+        t1 = self.Get(ctx, tryone)
         if isinstance(t1, W_PrimitiveObject):
             val = t1.Call(ctx, this=self)
             if isinstance(val, W_Primitive):
                 return val
-        t2 = self.Get(trytwo)
+        t2 = self.Get(ctx, trytwo)
         if isinstance(t2, W_PrimitiveObject):
             val = t2.Call(ctx, this=self)
             if isinstance(val, W_Primitive):
@@ -238,6 +233,12 @@
             return 'function'
         else:
             return 'object'
+
+
+class W_Primitive(W_PrimitiveObject):
+    """unifying parent for primitives"""
+    def ToPrimitive(self, ctx, hint=""):
+        return self
     
 def str_builtin(ctx, args, this):
     return W_String(this.ToString(ctx))
@@ -249,13 +250,13 @@
                                    Class, Value, callfunc)
 
     def ToNumber(self, ctx):
-        return self.Get('valueOf').Call(ctx, args=[], this=self).ToNumber(ctx)
+        return self.Get(ctx, 'valueOf').Call(ctx, args=[], this=self).ToNumber(ctx)
 
 class W_NewBuiltin(W_PrimitiveObject):
     def __init__(self, ctx, Prototype=None, Class='function',
                  Value=w_Undefined, callfunc=None):
         if Prototype is None:
-            proto = ctx.get_global().Get('Function').Get('prototype')
+            proto = ctx.get_global().Get(ctx, 'Function').Get(ctx, 'prototype')
             Prototype = proto
 
         W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, callfunc)
@@ -295,10 +296,11 @@
     def __init__(self, callee, args):
         W_PrimitiveObject.__init__(self, Class='Arguments')
         del self.propdict["prototype"]
-        self.Put('callee', callee)
-        self.Put('length', W_IntNumber(len(args)))
+        # XXX None can be dangerous here
+        self.Put(None, 'callee', callee)
+        self.Put(None, 'length', W_IntNumber(len(args)))
         for i in range(len(args)):
-            self.Put(str(i), args[i])
+            self.Put(None, str(i), args[i])
         self.length = len(args)
 
 class ActivationObject(W_PrimitiveObject):
@@ -314,44 +316,52 @@
     def __init__(self, ctx=None, Prototype=None, Class='Array',
                  Value=w_Undefined, callfunc=None):
         W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, callfunc)
-        self.Put('length', W_IntNumber(0))
+        self.Put(ctx, 'length', W_IntNumber(0))
         self.length = r_uint(0)
 
-    def Put(self, P, V, dd=False,
-            ro=False, de=False, it=False):
+    def set_length(self, newlength):
+        if newlength < self.length:
+            i = newlength
+            while i < self.length:
+                key = str(i)
+                if key in self.propdict:
+                    del self.propdict[key]
+                i += 1
         
+        self.length = newlength
+        self.propdict['length'].value = W_FloatNumber(newlength)
+
+    def Put(self, ctx, P, V, dd=False,
+            ro=False, de=False, it=False):
         if not self.CanPut(P): return
-        if P in self.propdict:
-            if P == 'length':
-                try:
-                    res = V.ToUInt32()
-                    if V.ToNumber(ctx) < 0:
-                        raise RangeError()
-                    self.propdict['length'].value = W_IntNumber(res)
-                    self.length = res
-                    return
-                except ValueError:
-                    raise RangeError('invalid array length')
-            else:
-                self.propdict[P].value = V
+        if not P in self.propdict:
+            self.propdict[P] = Property(P, V, dd = dd, ro = ro, de = de, it = it)
         else:
-            self.propdict[P] = Property(P, V,
-            dd = dd, ro = ro, it = it)
-
+            if P != 'length':
+                self.propdict[P].value = V
+            else:
+                length = V.ToUInt32(ctx)
+                if length != V.ToNumber(ctx):
+                    raise RangeError()
+                
+                self.set_length(length)
+                return
+                
         try:
-            index = r_uint(int(P))
+            arrayindex = r_uint(float(P))
         except ValueError:
             return
-        if index < self.length:
-            return
         
-        self.length = index+1
-        self.propdict['length'].value = W_IntNumber(index+1)
-        return
-
+        if (arrayindex < self.length) or (arrayindex != float(P)):
+            return
+        else:
+            if (arrayindex + 1) == 0:
+                raise RangeError()
+            self.set_length(arrayindex+1)
 
 class W_Boolean(W_Primitive):
     def __init__(self, boolval):
+        super(W_Primitive, self).__init__()
         self.boolval = bool(boolval)
     
     def ToObject(self, ctx):
@@ -378,6 +388,7 @@
 
 class W_String(W_Primitive):
     def __init__(self, strval):
+        super(W_Primitive, self).__init__()
         self.strval = strval
 
     def __repr__(self):
@@ -416,7 +427,7 @@
     def ToObject(self, ctx):
         return create_object(ctx, 'Number', Value=self)
 
-    def Get(self, name):
+    def Get(self, ctx, name):
         return w_Undefined
 
     def type(self):
@@ -426,6 +437,7 @@
     """ Number known to be an integer
     """
     def __init__(self, intval):
+        super(W_Primitive, self).__init__()
         self.intval = intmask(intval)
 
     def ToString(self, ctx=None):
@@ -439,10 +451,10 @@
         # XXX
         return float(self.intval)
 
-    def ToInt32(self):
+    def ToInt32(self, ctx):
         return self.intval
 
-    def ToUInt32(self):
+    def ToUInt32(self, ctx):
         return r_uint(self.intval)
 
     def GetPropertyName(self):
@@ -455,7 +467,8 @@
     """ Number known to be a float
     """
     def __init__(self, floatval):
-        self.floatval = floatval
+        super(W_Primitive, self).__init__()
+        self.floatval = float(floatval)
     
     def ToString(self, ctx = None):
         # XXX incomplete, this doesn't follow the 9.8.1 recommendation
@@ -479,12 +492,12 @@
     def ToNumber(self, ctx):
         return self.floatval
 
-    def ToInt32(self):
+    def ToInt32(self, ctx):
         if isnan(self.floatval) or isinf(self.floatval):
             return 0           
         return intmask(self.floatval)
     
-    def ToUInt32(self):
+    def ToUInt32(self, ctx):
         if isnan(self.floatval) or isinf(self.floatval):
             return r_uint(0)
         return r_uint(self.floatval)
@@ -547,7 +560,7 @@
             except KeyError:
                 pass
         # if not, we need to put this thing in current scope
-        self.variable.Put(name, value)
+        self.variable.Put(self.get_global(), name, value)
 
     def delete_identifier(self, name):
         for obj in self.scope:
@@ -562,9 +575,9 @@
                 pass
         return False
 
-    def put(self, name, value, dd=False):
+    def Put(self, ctx, name, value, dd=False):
         assert name is not None
-        self.variable.Put(name, value, dd=dd)
+        self.variable.Put(ctx, name, value, dd=dd)
     
     def get_global(self):
         return self.scope[-1]
@@ -632,7 +645,7 @@
         return len(self.elements_w) == 0
     
 def create_object(ctx, prototypename, callfunc=None, Value=w_Undefined):
-    proto = ctx.get_global().Get(prototypename).Get('prototype')
+    proto = ctx.get_global().Get(ctx, prototypename).Get(ctx, 'prototype')
     obj = W_Object(ctx, callfunc = callfunc,Prototype=proto,
                     Class = proto.Class, Value = Value)
     return obj

Modified: pypy/branch/js-refactoring/pypy/lang/js/test/ecma/conftest.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/test/ecma/conftest.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/test/ecma/conftest.py	Tue May 27 02:46:27 2008
@@ -47,7 +47,7 @@
         cls.testcases = cls.interp.global_context.resolve_identifier('testcases')
         cls.tc = cls.interp.global_context.resolve_identifier('tc')
         # override eval
-        cls.interp.global_context.put('eval', W_Builtin(overriden_evaljs))
+        cls.interp.w_Global.Put(cls.interp.global_context, 'eval', W_Builtin(overriden_evaljs))
         
     init_interp = classmethod(init_interp)
     
@@ -72,9 +72,10 @@
             raise Failed(msg="Javascript Error", excinfo=py.code.ExceptionInfo())
         except:
             raise Failed(excinfo=py.code.ExceptionInfo())
-        testcases = self.interp.global_context.resolve_identifier('testcases')
-        self.tc = self.interp.global_context.resolve_identifier('tc')
-        testcount = testcases.Get('length').ToInt32()
+        ctx = self.interp.global_context
+        testcases = ctx.resolve_identifier('testcases')
+        self.tc = ctx.resolve_identifier('tc')
+        testcount = testcases.Get(ctx, 'length').ToInt32(ctx)
         self.testcases = testcases
         return range(testcount)
 

Modified: pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py	Tue May 27 02:46:27 2008
@@ -45,11 +45,11 @@
     elif isinstance(value, bool):
         assert code_val.ToBoolean() == value
     elif isinstance(value, int):
-        assert code_val.ToInt32() == value
+        assert code_val.ToInt32(jsint.global_context) == value
     elif isinstance(value, float):
-        assert code_val.ToNumber(None) == value
+        assert code_val.ToNumber(jsint.global_context) == value
     else:
-        assert code_val.ToString(ctx) == value
+        assert code_val.ToString(jsint.global_context) == value
 
 def asserte(code, value):
     jsint = interpreter.Interpreter()



More information about the Pypy-commit mailing list