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

fijal at codespeak.net fijal at codespeak.net
Tue Oct 31 13:23:15 CET 2006


Author: fijal
Date: Tue Oct 31 13:23:13 2006
New Revision: 33943

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/js/jsparse.js
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/parser.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(santagada, fijal) - Added the object creation.


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:23:13 2006
@@ -57,7 +57,18 @@
     def __init__(self, strval):
         self.strval = strval
 
+class ObjectInit(Node):
+    def __init__(self, properties):
+        self.properties = properties
+
+class PropertyInit(Node):
+    def __init__(self, name, value):
+        self.name = name
+        self.value = value
+
 def getlist(d):
+    if 'length' not in d:
+        return []
     lgt = int(d['length'])
     output = [from_dict(d[str(i)]) for i in range(lgt)]
     return output
@@ -83,5 +94,9 @@
         return Assign(from_dict(d['0']), from_dict(d['1']))
     elif tp == 'STRING':
         return String(d['value'])
+    elif tp == 'PROPERTY_INIT':
+        return PropertyInit(from_dict(d['0']), from_dict(d['1']))
+    elif tp == 'OBJECT_INIT':
+        return ObjectInit(getlist(d))
     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:23:13 2006
@@ -1,7 +1,7 @@
 
 from pypy.lang.js.astgen import *
 from pypy.lang.js.context import ExecutionContext
-from pypy.lang.js.jsobj import W_Number, W_String
+from pypy.lang.js.jsobj import W_Number, W_String, W_Object
 
 def writer(x):
     print x
@@ -15,6 +15,10 @@
 class __extend__(Number):
     def call(self, context):
         return W_Number(self.num)
+    
+    def get_literal(self):
+        # XXX Think about a shortcut later
+        return str(W_Number(self.num))
 
 class __extend__(Plus):
     def call(self, context=None):
@@ -41,6 +45,9 @@
 class __extend__(Identifier):
     def call(self, context=None):
         return context.access(self.name)
+    
+    def get_literal(self):
+        return self.name
 
 class __extend__(Script):
     def call(self, context=None):
@@ -60,3 +67,18 @@
 class __extend__(String):
     def call(self, context=None):
         return W_String(self.strval)
+    
+    def get_literal(self):
+        return self.strval
+
+class __extend__(ObjectInit):
+    def call(self, context=None):
+        w_obj = W_Object({})
+        for property in self.properties:
+            name = property.name.get_literal()
+            w_expr = property.value.call(context).GetValue()
+            w_obj.Put(name, w_expr)
+        return w_obj
+        #dict_w = {}
+        #for property in self.properties:
+        #    dict_w[property.name

Modified: pypy/dist/pypy/lang/js/js/jsparse.js
==============================================================================
--- pypy/dist/pypy/lang/js/js/jsparse.js	(original)
+++ pypy/dist/pypy/lang/js/js/jsparse.js	Tue Oct 31 13:23:13 2006
@@ -14,7 +14,7 @@
  *
  * The Original Code is the Narcissus JavaScript engine.
  *
- * The Initial Developer of the Original Code is
+ * The Initial veloper of the Original Code is
  * Brendan Eich <brendan at mozilla.org>.
  * Portions created by the Initial Developer are Copyright (C) 2004
  * the Initial Developer. All Rights Reserved.
@@ -22,7 +22,7 @@
  * Contributor(s):
  *
  * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * either the GNU General zzzPublic License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
@@ -281,9 +281,15 @@
     a.sort(function (a,b) { return (a.id < b.id) ? -1 : 1; });
     const INDENTATION = "    ";
     var n = ++Node.indentLevel;
-    var s = "{\n" + INDENTATION.repeat(n) + "type: " + tokenstr(this.type);
-    for (i = 0; i < a.length; i++)
-        s += ",\n" + INDENTATION.repeat(n) + a[i].id + ": " + a[i].value;
+    var s = "{\n" + INDENTATION.repeat(n) + "'type': '" + tokenstr(this.type) + "'";
+    for (i = 0; i < a.length; i++) {
+        val = a[i].value + ""
+        if (val.search("\n") != -1) {
+            s += ",\n" + INDENTATION.repeat(n) + "'" + a[i].id + "': " + val + " ";
+        } else {
+            s += ",\n" + INDENTATION.repeat(n) + "'" + a[i].id + "': '" + val + "'";
+        }
+    }
     n = --Node.indentLevel;
     s += "\n" + INDENTATION.repeat(n) + "}";
     return s;

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:23:13 2006
@@ -68,12 +68,57 @@
     def __init__(self, dict_w):
         # string --> W_Root
         self.dict_w = dict_w
+        # XXX: more stuff
+        self.dict_w['toString'] = W_Builtin({}, self.w_string)
+
+    def Call(self, **kwargs):
+        raise SeePage(33)
+    
+    def w_string(self):
+        return W_String(self.ToString())
+    
+    def DefaultValue(self, hint):
+        tostring_meth = self.Get("toString")
+        if isinstance(tostring_meth, W_Object):
+            return tostring_meth.Call(this=self)
+        valueof_meth = self.Get("valueOf")
+        if isinstance(valueof_meth, W_Object):
+            retval = valueof_meth.Call(this=self)
+            # XXX: check primitiveness of retval
+            return retval
+    
+    def Get(self, name):
+        if name in self.dict_w:
+            return self.dict_w[name]
+        
+        return w_Undefined
 
     def ToPrimitive(self):
         raise SeePage(37)
 
     def ToString(self):
         raise SeePage(42)
+    
+    def CanPut(self, name):
+        return True
+    
+    def Put(self, name, w_obj):
+        # INSANE - raise some exceptions in case of read only and such
+        if not self.CanPut(name):
+            return # AAAAAAAAAAAAaaaaaaaaaaaa
+        self.dict_w[name] = w_obj
+    
+    def __str__(self):
+        # INSANE
+        return "[object Object]"
+
+class W_Builtin(W_Object):
+    def __init__(self, dict_w, internalfunction):
+        self.dict_w = {}
+        self.internalfunction = internalfunction
+    
+    def Call(self, *args):
+        return self.internalfunction(*args)
 
 class W_List(W_Root):
     def __init__(self, list_w):

Modified: pypy/dist/pypy/lang/js/parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/parser.py	(original)
+++ pypy/dist/pypy/lang/js/parser.py	Tue Oct 31 13:23:13 2006
@@ -28,20 +28,26 @@
 def parse(code_string):
     read_code = read_js_output(code_string)
     #print read_code
-    output = []
-    for line in read_code.split("\n"):
-        m = re.search('^(\s*)(\w+): (.*?)(,)?$', line)
-        if m and (m.group(3) != '{' or m.group(4)):
-            output.append("%s'%s': '%s'," % (m.group(1), m.group(2), m.group(3)))
-        else:
-            m = re.search('^(\s*)(\w+):(.*)$', line)
-            if m:
-                output.append("%s'%s': %s" % (m.group(1), m.group(2), m.group(3)))
-            else:
-                output.append(line)
+    #for line in read_code.split("\n"):
+        #m = re.search('^(\s*)(\w+): +(.*?)(,)?$', line)
+        #if m and (m.group(3) != '{' or m.group(4)):
+        #    output.append("%s'%s': '%s'," % (m.group(1), m.group(2), m.group(3)))
+        #else:
+        #    m = re.search('^(\s*)(\w+):(.*)$', line)
+        #    if m:
+        #        output.append("%s'%s': %s" % (m.group(1), m.group(2), m.group(3)))
+        #    else:
+        #        output.append(line)
 
     #print "\n".join(output)
+    output = read_code.split("\n")
     d = {}
-    exec "code =" + "\n".join(output) in d
+    try:
+        exec "code =" + "\n".join(output) in d
+    except (SyntaxError, NameError):
+        for num, line in enumerate(output):
+            print "%d: %s" % (num + 1, line)
+        open("/tmp/out", "w").write("\n".join(output))
+        raise
     return d['code']
 

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:23:13 2006
@@ -42,3 +42,6 @@
     
     def test_string_num_concat(self):
         self.assert_prints(parse_d('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]"])



More information about the Pypy-commit mailing list