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

fijal at codespeak.net fijal at codespeak.net
Tue Oct 31 13:59:39 CET 2006


Author: fijal
Date: Tue Oct 31 13:59:38 2006
New Revision: 33946

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(santagada, fijal) - Object attribute access.


Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Tue Oct 31 13:59:38 2006
@@ -12,60 +12,71 @@
 #        return output
 #    getlist = staticmethod(getlist)
 
-class Script(Node):
-    def __init__(self, nodes, var_decl, func_decl):
-        self.nodes = nodes
-        self.var_decl = var_decl
-        self.func_decl = func_decl
-
-#    def from_dict(d):
-#        return Script(self.getlist(d), d['varDecl'], d['funcDecl'])
-#    from_dict = staticmethod(from_dict)
 
 class Assign(Node):
     def __init__(self, identifier, expr):
         self.identifier = identifier
         self.expr = expr
 
-class Semicolon(Node):
-    def __init__(self, expr):
-        self.expr = expr
-
-class Plus(Node):
-    def __init__(self, left, right):
-        self.left = left
-        self.right = right
-
-class Number(Node):
-    def __init__(self, num):
-        self.num = num
-
 class Call(Node):
     def __init__(self, identifier, arglist):
         self.identifier = identifier
         self.arglist = arglist
 
+class Dot(Node):
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
 class Identifier(Node):
     def __init__(self, name):
         self.name = name
+        
+class Index(Node):
+    def __init__(self, left, expr):
+        self.left = left
+        self.expr = expr
 
 class List(Node):
     def __init__(self, nodes):
         self.nodes = nodes
-        
-class String(Node):
-    def __init__(self, strval):
-        self.strval = strval
+
+class Number(Node):
+    def __init__(self, num):
+        self.num = num
 
 class ObjectInit(Node):
     def __init__(self, properties):
         self.properties = properties
 
+class Plus(Node):
+    def __init__(self, left, right):
+        self.left = left
+        self.right = right
+
 class PropertyInit(Node):
     def __init__(self, name, value):
         self.name = name
         self.value = value
 
+class Script(Node):
+    def __init__(self, nodes, var_decl, func_decl):
+        self.nodes = nodes
+        self.var_decl = var_decl
+        self.func_decl = func_decl
+
+#    def from_dict(d):
+#        return Script(self.getlist(d), d['varDecl'], d['funcDecl'])
+#    from_dict = staticmethod(from_dict)
+
+class Semicolon(Node):
+    def __init__(self, expr):
+        self.expr = expr
+
+class String(Node):
+    def __init__(self, strval):
+        self.strval = strval
+
 def getlist(d):
     if 'length' not in d:
         return []
@@ -98,5 +109,9 @@
         return PropertyInit(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'OBJECT_INIT':
         return ObjectInit(getlist(d))
+    elif tp == 'DOT':
+        return Dot(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'INDEX':
+        return Index(from_dict(d['0']), from_dict(d['1']))
     else:
         raise NotImplementedError("Dont know how to handler %s" % tp)

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Tue Oct 31 13:59:38 2006
@@ -20,6 +20,12 @@
         # XXX Think about a shortcut later
         return str(W_Number(self.num))
 
+class __extend__(Dot):
+    def call(self, context=None):
+        w_obj = self.left.call(context).GetValue().ToObject()
+        name = self.right.get_literal()
+        return w_obj.Get(name)
+    
 class __extend__(Plus):
     def call(self, context=None):
         left = self.left.call(context).GetValue()
@@ -82,3 +88,11 @@
         #dict_w = {}
         #for property in self.properties:
         #    dict_w[property.name
+
+class __extend__(Index):
+    def call(self, context=None):
+        w_obj = self.left.call(context).GetValue()
+        w_member = self.expr.call(context).GetValue()
+        w_obj = w_obj.ToObject()
+        name = w_member.ToString()
+        return w_obj.Get(name)

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Tue Oct 31 13:59:38 2006
@@ -14,13 +14,16 @@
 
     def ToString(self):
         return str(self)
+    
+    def ToObject(self):
+        return self
 
     def __repr__(self):
         return "<%s(%s)>" % (self.__class__.__name__, str(self))
 
 class W_Undefined(W_Root):
     def __str__(self):
-        return "undefined"
+        return ""
 
 class W_Null(W_Root):
     def __str__(self):
@@ -56,6 +59,9 @@
         if float(int(self.floatval)) == self.floatval:
             return str(int(self.floatval))
         return str(self.floatval)
+    
+    def Get(self, name):
+        return w_Undefined
 
     def ToNumber(self):
         return self.floatval
@@ -96,8 +102,8 @@
     def ToPrimitive(self):
         raise SeePage(37)
 
-    def ToString(self):
-        raise SeePage(42)
+    #def ToString(self):
+    #    raise SeePage(42)
     
     def CanPut(self, name):
         return True

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 Oct 31 13:59:38 2006
@@ -45,3 +45,11 @@
 
     def test_to_string(self):
         self.assert_prints(parse_d("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"])
+
+    def test_object_access_index(self):
+        self.assert_prints(parse_d('x={d:"x"}; print(x["d"]);'), ["x"])



More information about the Pypy-commit mailing list