[pypy-svn] r35904 - in pypy/dist/pypy/lang/js: . test

santagada at codespeak.net santagada at codespeak.net
Tue Dec 19 21:29:15 CET 2006


Author: santagada
Date: Tue Dec 19 21:28:59 2006
New Revision: 35904

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/context.py
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/reference.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
17 tests passing, and now following the specs!!!


Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Tue Dec 19 21:28:59 2006
@@ -22,9 +22,9 @@
         self.items = items
 
 class Assign(Node):
-    def __init__(self, identifier, expr):
-        self.identifier = identifier
-        self.expr = expr
+    def __init__(self, LHSExp, AssignmentExp):
+        self.LHSExp = LHSExp
+        self.AssignmentExp = AssignmentExp
 
 class Block(Node):
     def __init__(self, nodes):

Modified: pypy/dist/pypy/lang/js/context.py
==============================================================================
--- pypy/dist/pypy/lang/js/context.py	(original)
+++ pypy/dist/pypy/lang/js/context.py	Tue Dec 19 21:28:59 2006
@@ -11,6 +11,14 @@
         self.variable = None
         self.property = Property('',w_Undefined) #Attribute flags for new vars
     
+    def assign(self, name, value):
+        """
+        assign to property name, creating it if it doesn't exist
+        """
+        pass
+        #ref = self.resolve_identifier(name)
+        #if ref.
+        
     def push_object(self, obj):
         """push object into scope stack"""
         self.scope.insert(0, obj)
@@ -22,9 +30,9 @@
     def resolve_identifier(self, identifier):
         for obj in self.scope:
             if obj.HasProperty(identifier):
-                return Reference(property_name, obj)
+                return Reference(identifier, obj)
         
-        return Reference(property_name)
+        return Reference(identifier)
     
 
 def global_context(w_global):

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Tue Dec 19 21:28:59 2006
@@ -1,7 +1,7 @@
 
 from pypy.lang.js.astgen import *
 from pypy.lang.js.jsparser import parse
-from pypy.lang.js.context import ExecutionContext
+from pypy.lang.js.context import *
 from pypy.lang.js.jsobj import W_Number, W_String, W_Object 
 from pypy.lang.js.jsobj import w_Undefined, W_Arguments, W_Boolean, NaN
 
@@ -22,11 +22,11 @@
     def __init__(self, script_source=None):
         self.w_Object = W_Object() #creating Object
         self.w_Global = W_Object()
-        w_Global.Prototype = w_Object
-        w_Global.Set('prototype', 'Object')
-        w_Global.Set('Object', w_Object)
-        self.global_context = GlobalContext(w_global)
-        if script_source is not none:
+        self.w_Global.Prototype = self.w_Object
+        self.w_Global.Put('prototype', 'Object')
+        self.w_Global.Put('Object', self.w_Object)
+        self.global_context = global_context(self.w_Global)
+        if script_source is not None:
             self.load_source(script_source)
     
     def load_source(self, script_source):
@@ -36,7 +36,7 @@
     
     def run(self):
         """run the interpreter"""
-        self.script.Call(self.global_context)
+        self.script.call(self.global_context)
 
         
 
@@ -46,9 +46,11 @@
         return W_Array(d)
 
 class __extend__(Assign):
-    def call(self, context):
-        val = self.expr.call(context)
-        self.identifier.put(context,val)
+    def call(self, ctx):
+        v1 = self.LHSExp.call(ctx)
+        v3 = self.AssignmentExp.call(ctx).GetValue()
+        v1.PutValue(v3, ctx)
+        return v3
 
 class __extend__(Block):
     def call(self, context):
@@ -64,7 +66,7 @@
     def call(self, context):
         name = self.identifier.get_literal()
         if name == 'print':
-            writer(",".join([i.ToString() for i in self.arglist.call(context)]))
+            writer(",".join([i.GetValue().ToString() for i in self.arglist.call(context)]))
         else:
             backup_scope = scope_manager.current_scope
             
@@ -108,14 +110,11 @@
        return w_obj
 
 class __extend__(Identifier):
-    def call(self, context):
-        if self.initialiser is not None:
-            context.assign(self.name, self.initialiser.call(context))
-        try:
-            value = context.access(self.name)
-            return value
-        except NameError:
-            return scope_manager.get_variable(self.name)
+    def call(self, ctx):
+        # if self.initialiser is not None:
+        #     context.assign(self.name, self.initialiser.call(context))
+        value = ctx.resolve_identifier(self.name)
+        return value
 
     def put(self, context, val, obj=None):            
         context.assign(self.name, val)
@@ -222,8 +221,8 @@
     def call(self, context=None):
         left = self.left.call(context).GetValue()
         right = self.right.call(context).GetValue()
-        prim_left = left.ToPrimitive()
-        prim_right = right.ToPrimitive()
+        prim_left = left.ToPrimitive('Number')
+        prim_right = right.ToPrimitive('Number')
         # INSANE
         if isinstance(prim_left, W_String) or isinstance(prim_right, W_String):
             str_left = prim_left.ToString()
@@ -305,16 +304,16 @@
         return tryresult
 
 class __extend__(Undefined):
-    def call(self, context=None):
+    def call(self, ctx):
         return None
 
 class __extend__(Vars):
-    def call(self, context=None):
+    def call(self, ctx):
         for var in self.nodes:
-            var.call(context)
+            var.call(ctx)
 
 class __extend__(While):
-    def call(self, context=None):
-        while self.condition.call(context).ToBoolean():
-            self.body.call(context)
+    def call(self, ctx):
+        while self.condition.call(ctx).ToBoolean():
+            self.body.call(ctx)
 

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Tue Dec 19 21:28:59 2006
@@ -24,26 +24,29 @@
         return self
 
     def ToBoolean(self):
-        return bool(self)
+        return False
 
     def ToPrimitive(self, hint=""):
         return self
 
-    #def ToNumber(self):
-    #    return int(self.ToPrimitive(hint="number"))
-
     def ToString(self):
         return str(self)
     
     def ToObject(self):
         return self
 
+    def ToNumber(self):
+        return NaN
+    
     def __repr__(self):
         return "<%s(%s)>" % (self.__class__.__name__, str(self))
 
 class W_Primitive(W_Root):
     """unifying parent for primitives"""
-    pass    
+    def ToPrimitive(self, PreferredType):
+        return self
+
+    
 
 class W_Object(W_Root):
     def __init__(self, function=None):
@@ -116,6 +119,8 @@
         else: #suppose hint is "Number" dunno what to do otherwise
             internal_def_value("valueOf", "toString")
     
+    ToPrimitive = DefaultValue
+
     def __str__(self):
         return "[object %s]"%(self.Class,)
     
@@ -147,31 +152,24 @@
     def ToBoolean(self):
         return False
 
-class W_Boolean(W_Root):
+class W_Boolean(W_Primitive):
     def __init__(self, boolval):
         self.boolval = bool(boolval)
 
-    def __str__(self):
+    def ToString(self):
         if self.boolval:
             return "true"
         return "false"
     
     def ToNumber(self):
         if self.boolval:
-            return 1
-        return 0
-
-    def ToBoolean(self):
-        return self.boolval
-
+            return 1.0
+        return 0.0
     
-class W_String(W_Root):
+class W_String(W_Primitive):
     def __init__(self, strval):
         self.strval = strval
 
-#    def ToString(self):
-#        return self.strval
-
     def __str__(self):
         return self.strval
 
@@ -179,13 +177,11 @@
         return bool(self.strval)
 
 
-class W_Number(W_Root):
+class W_Number(W_Primitive):
     def __init__(self, floatval):
         self.floatval = floatval
 
-    def __str__(self):
-        # XXX: more attention
-        # cough, cough
+    def ToString(self):
         if str(self.floatval) == str(NaN):
             return 'NaN'
         if float(int(self.floatval)) == self.floatval:
@@ -193,21 +189,22 @@
         return str(self.floatval)
     
     def ToBoolean(self):
-        return bool(self.floatval)
+        return W_Boolean(bool(self.floatval))
 
+    def ToNumber(self):
+        return self.floatval
+    
     def Get(self, name):
         return w_Undefined
 
-    def ToNumber(self):
-        return self.floatval
 
 class W_Reference(W_Root):
     def GetValue(self):
         raise NotImplementedError("W_Reference.GetValue")
 
-class W_Builtin(W_Object):
+class W_Builtin(W_Root):
     def __init__(self, builtinfunction):
-        W_Object.__init__()
+        #W_Object.__init__(self)
         self.builtinfunction = builtinfunction
     
     def Call(self, context, args=[], this = None):
@@ -226,11 +223,4 @@
 w_Undefined = W_Undefined()
 w_Null = W_Null()
 
-def to_primitive(Value, PreferredType):
-    assert isinstance(Value, W_Root)
-    if isinstance(Value, W_Object):
-        return Value.DefaultValue(PreferredType)
-    return Value
-
-
         
\ No newline at end of file

Modified: pypy/dist/pypy/lang/js/reference.py
==============================================================================
--- pypy/dist/pypy/lang/js/reference.py	(original)
+++ pypy/dist/pypy/lang/js/reference.py	Tue Dec 19 21:28:59 2006
@@ -6,17 +6,13 @@
         self.baseobject = baseobject
         self.propertyname = propertyname
         
-def get_value(v):
-    if not type(v, Reference):
-        return v
-    if v.baseobject is None:
-        raise ReferenceError
-    return v.baseobject.get(v.propertyname)
+    def GetValue(self):
+        if self.baseobject is None:
+            raise ReferenceError
+        return self.baseobject.Get(self.propertyname)
 
-def put_value(v, w, context):
-    if not type(v, Reference):
-        raise ReferenceError
-    base = v.baseobject
-    if v.baseobject is None:
-        base = context.scope[-1]
-    base.put(v.propertyname, w)
\ No newline at end of file
+    def PutValue(self, w, ctx):
+        base = self.baseobject
+        if self.baseobject is None:
+            base = ctx.scope[-1]
+        base.Put(self.propertyname, w)
\ No newline at end of file

Modified: pypy/dist/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_interp.py	Tue Dec 19 21:28:59 2006
@@ -1,5 +1,4 @@
 import py.test
-py.test.skip("not finished yet")
 
 from pypy.lang.js.astgen import *
 from pypy.lang.js import interpreter
@@ -34,58 +33,57 @@
     def assert_prints(self, code, assval):
         l = []
         interpreter.writer = l.append
-        ctx = ExecutionContext()
-        ctx.globals['Object'] = W_Object({})
+        js_int = interpreter.Interpreter(code)
         try:
-            code.call(ctx, first = True)
+            js_int.run()
         except ThrowException, excpt:
             l.append("uncaught exception: "+str(excpt.exception))
         assert l == assval
     
     def assert_result(self, code, result):
-        r = code.call()
+        r = code.call(ExecutionContext())
         assert r.ToString() == result.ToString()
         
     def test_interp_parse(self):
-        self.assert_prints(parse_d("print(1+1)"), ["2"])
-        self.assert_prints(parse_d("print(1+2+3); print(1)"), ["6", "1"])
-        self.assert_prints(parse_d("print(1,2,3);\n"), ["1,2,3"])
+        self.assert_prints("print(1+1)", ["2"])
+        self.assert_prints("print(1+2+3); print(1)", ["6", "1"])
+        self.assert_prints("print(1,2,3);\n", ["1,2,3"])
 
     def test_var_assign(self):
-        self.assert_prints(parse_d("x=3;print(x);"), ["3"])
-        self.assert_prints(parse_d("x=3;y=4;print(x+y);"), ["7"])
+        self.assert_prints("x=3;print(x);", ["3"])
+        self.assert_prints("x=3;y=4;print(x+y);", ["7"])
 
     def test_string_var(self):
-        self.assert_prints(parse_d('print(\"sss\");'), ["sss"])
+        self.assert_prints('print(\"sss\");', ["sss"])
     
     def test_string_concat(self):
-        self.assert_prints(parse_d('x="xxx"; y="yyy"; print(x+y);'), ["xxxyyy"])
+        self.assert_prints('x="xxx"; y="yyy"; print(x+y);', ["xxxyyy"])
     
     def test_string_num_concat(self):
-        self.assert_prints(parse_d('x=4; y="x"; print(x+y, y+x);'), ["4x,x4"])
+        self.assert_prints('x=4; y="x"; print(x+y, y+x);', ["4x,x4"])
 
     def test_to_string(self):
-        self.assert_prints(parse_d("x={}; print(x);"), ["[object Object]"])
+        self.assert_prints("x={}; print(x);", ["[object Object]"])
 
     def test_object_access(self):
-        self.assert_prints(parse_d("x={d:3}; print(x.d);"), ["3"])
-        self.assert_prints(parse_d("x={d:3}; print(x.d.d);"), [""])
-        self.assert_prints(parse_d("x={d:3, z:4}; print(x.d+x.z);"), ["7"])
+        self.assert_prints("x={d:3}; print(x.d);", ["3"])
+        self.assert_prints("x={d:3}; print(x.d.d);", [""])
+        self.assert_prints("x={d:3, z:4}; print(x.d+x.z);", ["7"])
 
     def test_object_access_index(self):
-        self.assert_prints(parse_d('x={d:"x"}; print(x["d"]);'), ["x"])
+        self.assert_prints('x={d:"x"}; print(x["d"]);', ["x"])
     
     def test_function_prints(self):
-        self.assert_prints(parse_d('x=function(){print(3);}; x();'), ["3"])
+        self.assert_prints('x=function(){print(3);}; x();', ["3"])
     
     def test_function_returns(self):
-        self.assert_prints(parse_d('x=function(){return 1;}; print(x()+x());'), ["2"])
+        self.assert_prints('x=function(){return 1;}; print(x()+x());', ["2"])
     
     def test_var_declartion(self):
-        self.assert_prints(parse_d('var x = 3; print(x+x);'), ["6"])
+        self.assert_prints('var x = 3; print(x+x);', ["6"])
     
     def test_var_scoping(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         var y;
         var p;
         p = 0;
@@ -96,90 +94,90 @@
         };
         var z = 2;
         print(x(), y, p);
-        """), ["5,3,0"])
+        """, ["5,3,0"])
 
     def test_function_args(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = function (t,r) {
                return t+r;
         };
         print(x(2,3));
-        """), ["5"])
+        """, ["5"])
 
     def test_function_less_args(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = function (t, r) {
                 return t + r;
         };
         print(x(2));
-        """), ["NaN"])
+        """, ["NaN"])
 
     def test_function_more_args(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = function (t, r) {
                 return t + r;
         };
         print(x(2,3,4));
-        """), ["5"])
+        """, ["5"])
 
     def test_function_has_var(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = function () {
                 var t = 'test';
                 return t;
         };
         print(x());
-        """), ["test"])
+        """, ["test"])
 
     def test_function_arguments(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = function () {
                 r = arguments[0];
                 t = arguments[1];
                 return t + r;
         };
         print(x(2,3));
-        """), ["5"])
+        """, ["5"])
 
 
     def test_index(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = {1:"test"};
         print(x[1]);
-        """), ["test"])
+        """, ["test"])
 
     def test_array_initializer(self):
         py.test.skip('not ready yet')
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         x = [];
         print(x);
-        """), ["[]"])
+        """, ["[]"])
 
     def test_throw(self):
-        self.assert_prints(parse_d("throw(3)"), ["uncaught exception: 3"])
+        self.assert_prints("throw(3)", ["uncaught exception: 3"])
         
     def test_group(self):
-        self.assert_prints(parse_d("print((2+1))"), ["3"])
+        self.assert_prints("print((2+1))", ["3"])
 
     def test_comma(self):
-        self.assert_prints(parse_d("print((500,3))"), ["3"])
+        self.assert_prints("print((500,3))", ["3"])
     
     def test_try_catch(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         try {
             throw(3);
         }
         catch (x) {
             print(x);
         }
-        """), ["3"])
+        """, ["3"])
     
     def test_block(self):
-        self.assert_result(parse_d("{ 5}"), W_Number(5))
-        self.assert_result(parse_d("{3; 5}"), W_Number(5))
+        self.assert_result("{ 5}", W_Number(5))
+        self.assert_result("{3; 5}", W_Number(5))
     
     def test_try_catch_finally(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         try {
             throw(3);
         }
@@ -189,71 +187,70 @@
         finally {
             print(5)
         }
-        """), ["3", "5"])
+        """, ["3", "5"])
         
     def test_if_then(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         if (1) {
             print(1);
         }
-        """), ["1"])
+        """, ["1"])
 
     def test_if_then_else(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         if (0) {
             print(1);
         } else {
             print(2);
         }
-        """), ["2"])
+        """, ["2"])
 
     def test_compare(self):
-        self.assert_prints(parse_d("print(1>0)"),["true"])
-        self.assert_prints(parse_d("print(0>1)"),["false"])
-        self.assert_prints(parse_d("print(0>0)"),["false"])
-        self.assert_prints(parse_d("print(1<0)"),["false"])
-        self.assert_prints(parse_d("print(0<1)"),["true"])
-        self.assert_prints(parse_d("print(0<0)"),["false"])
+        self.assert_prints("print(1>0)",["true"])
+        self.assert_prints("print(0>1)",["false"])
+        self.assert_prints("print(0>0)",["false"])
+        self.assert_prints("print(1<0)",["false"])
+        self.assert_prints("print(0<1)",["true"])
+        self.assert_prints("print(0<0)",["false"])
 
 
     def test_while(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         i = 0;
         while (i<3) {
             print(i);
             i = i+1;
         }
         print(i);
-        """), ["0","1","2","3"])
+        """, ["0","1","2","3"])
 
     def test_object_creation(self):
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         o = new Object();
         print(o);
-        """), ["[object Object]"])
+        """, ["[object Object]"])
 
     def test_new_with_function(self):
         
         
-        c= parse_d("""
+        c= """
         x = function() {this.info = 'hello';};
         o = new x();
         print(o.info);
-        """)
+        """
         print c
-        self.assert_prints(c
-        , ["hello"])
+        self.assert_prints(c, ["hello"])
 
     def test_vars(self):
-        self.assert_prints(parse_d("""
-        var x;x=3; print(x)"""), ["3"])
+        self.assert_prints("""
+        var x;x=3; print(x)""", ["3"])
         
     def test_fun_decl(self):
         py.test.skip("still not ready")
-        self.assert_prints(parse_d("""
+        self.assert_prints("""
         function x () { print('i work')}
         x()
-        """), ["i work"])
+        """, ["i work"])
     
         
 



More information about the Pypy-commit mailing list