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

santagada at codespeak.net santagada at codespeak.net
Sun Jun 3 00:12:41 CEST 2007


Author: santagada
Date: Sun Jun  3 00:12:40 2007
New Revision: 43998

Removed:
   pypy/dist/pypy/lang/js/js.cleanup
   pypy/dist/pypy/lang/js/newparser.py
Modified:
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/operations.py
   pypy/dist/pypy/lang/js/test/test_interp.py
   pypy/dist/pypy/lang/js/test/test_parser.py
Log:
now part of the interpreter is using the right rules of prototype inheritance...
Still the stdlibrary needs lots of work, but now it seems like it is worth it.


Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Sun Jun  3 00:12:40 2007
@@ -186,7 +186,7 @@
         w_Global.Put('Array', w_Array)
         w_Global.Put('version', W_Builtin(versionjs))
         
-        #Number
+        #Date
         w_Date = W_Object(Class="Number")
         w_Global.Put('Date', w_Date)
         

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Sun Jun  3 00:12:40 2007
@@ -57,7 +57,8 @@
     def ToString(self):
         return ''
     
-    def ToObject(self):
+    def ToObject(self, ctx):
+        # XXX should raise not implemented
         return self
 
     def ToNumber(self):
@@ -131,9 +132,11 @@
     def __init__(self, ctx=None, Prototype=None, Class='Object',
                  Value=w_Undefined, callfunc=None):
         self.propdict = {}
-        self.propdict['prototype'] = Property('prototype', w_Undefined,
-                                              dd=True, de=True)
         self.Prototype = Prototype
+        if Prototype is None:
+            Prototype = w_Undefined
+        self.propdict['prototype'] = Property('prototype', Prototype,
+                                              dd=True, de=True)
         self.Class = Class
         self.callfunc = callfunc
         if callfunc is not None:
@@ -143,6 +146,8 @@
         self.Value = Value
 
     def Call(self, ctx, args=[], this=None):
+        if self.callfunc is None: # XXX Not sure if I should raise it here
+            raise JsTypeError('not a function')
         act = ActivationObject()
         paramn = len(self.callfunc.params)
         for i in range(paramn):
@@ -164,9 +169,8 @@
         prot = self.Get('prototype')
         if isinstance(prot, W_PrimitiveObject):
             obj.Prototype = prot
-        else:
-            obj.Prototype = ctx.get_global().Get('Object')
-
+        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')
         try: #this is a hack to be compatible to spidermonkey
             self.Call(ctx, args, this=obj)
             return obj
@@ -563,3 +567,7 @@
     def __str__(self):
         return "<" + str(self.base) + " -> " + str(self.property_name) + ">"
     
+def create_object(ctx, prototypename='Object', callfunc=None):
+    proto = ctx.get_global().Get(prototypename).Get('prototype')
+    obj = W_Object(ctx, callfunc = callfunc,Prototype=proto, Class = proto.Class)
+    return obj
\ No newline at end of file

Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py	(original)
+++ pypy/dist/pypy/lang/js/operations.py	Sun Jun  3 00:12:40 2007
@@ -249,14 +249,14 @@
 
 class Member(BinaryOp):
     def eval(self, ctx):
-        w_obj = self.left.eval(ctx).GetValue().ToObject()
+        w_obj = self.left.eval(ctx).GetValue().ToObject(ctx)
         name = self.right.eval(ctx).GetValue().ToString()
         return W_Reference(name, w_obj)
     
 
 class MemberDot(BinaryOp):
     def eval(self, ctx):
-        w_obj = self.left.eval(ctx).GetValue().ToObject()
+        w_obj = self.left.eval(ctx).GetValue().ToObject(ctx)
         name = self.right.get_literal()
         return W_Reference(name, w_obj)
     
@@ -269,10 +269,13 @@
         self.params = params
         
     def eval(self, ctx):
-        #XXX this is wrong, should clone the function prototype
-        w_obj = W_Object(ctx=ctx, callfunc = self)
-        w_obj.Put('prototype', W_Object(ctx=ctx))
-        return w_obj
+        proto = ctx.get_global().Get('Function').Get('prototype')
+        w_func = W_Object(ctx=ctx, Prototype=proto, Class='Function', callfunc=self)
+        w_func.Put('length', W_Number(len(self.params)))
+        w_obj = create_object(ctx, 'Object')
+        w_obj.Put('constructor', w_func, de=True)
+        w_func.Put('prototype', w_obj)
+        return w_func
     
     def execute(self, ctx):
         return self.eval(ctx)
@@ -601,7 +604,7 @@
     opcode = 'INDEX'
     
     def eval(self, ctx):
-        w_obj = self.left.eval(ctx).GetValue().ToObject()
+        w_obj = self.left.eval(ctx).GetValue().ToObject(ctx)
         name= self.right.eval(ctx).GetValue().ToString()
         return W_Reference(name, w_obj)
 
@@ -761,7 +764,7 @@
     opcode = 'OBJECT_INIT'
 
     def eval(self, ctx):
-        w_obj = W_Object()
+        w_obj = create_object(ctx, 'Object')
         for prop in self.nodes:
             name = prop.left.eval(ctx).GetPropertyName()
             w_expr = prop.right.eval(ctx).GetValue()
@@ -915,7 +918,7 @@
         self.body = body
 
     def execute(self, ctx):
-        obj = self.identifier.eval(ctx).GetValue().ToObject()
+        obj = self.identifier.eval(ctx).GetValue().ToObject(ctx)
         ctx.push_object(obj)
 
         try:
@@ -972,7 +975,7 @@
     
     def execute(self, ctx):
         self.vardecl.eval(ctx)
-        obj = self.object.eval(ctx).GetValue().ToObject()
+        obj = self.object.eval(ctx).GetValue().ToObject(ctx)
         for prop in obj.propdict.values():
             if prop.de:
                 continue
@@ -996,7 +999,7 @@
         self.body = body
 
     def execute(self, ctx):
-        obj = self.object.eval(ctx).GetValue().ToObject()
+        obj = self.object.eval(ctx).GetValue().ToObject(ctx)
         for prop in obj.propdict.values():
             if prop.de:
                 continue

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	Sun Jun  3 00:12:40 2007
@@ -529,15 +529,6 @@
     f.bar();
     """, 'debug')
     
-def test_switch():
-    py.test.skip("not ready yet")
-    assertv("""
-    x = 1;
-    switch(x){
-        case 1: 15; break;
-        default: 30;
-    };""", 15)
-
 def test_inplace_assign():
     yield assertv, "x=1; x+=1; x;", 2
     yield assertv, "x=1; x-=1; x;", 0
@@ -567,3 +558,37 @@
 def test_octal_and_hex():
     yield assertv, "010;", 8
     yield assertv, "0xF", 15
+
+def test_switch():
+    py.test.skip("not ready yet")
+    yield assertv, """
+    x = 1;
+    switch(x){
+        case 1: 15; break;
+        default: 30;
+    };""", 15
+    yield assertv, """
+    x = 1;
+    switch(x){
+        case 1: 15; break;
+        default: 30;
+    };""", 15
+
+def test_autoboxing():
+    py.test.skip("not ready yet")
+    yield assertv, "'abc'.charAt(0)", 0
+    yield assertv, "true.toString()", 'true'
+    yield assertv, "5.toString()", '5'
+
+def test_proper_prototype_inheritance():
+    yield assertv, """
+    Object.prototype.my = function() {return 1};
+    x = {};
+    x.my();
+    """, 1
+    yield assertv, """
+    Function.prototype.my = function() {return 1};
+    function x () {};
+    x.my();
+    """, 1
+    
\ No newline at end of file

Modified: pypy/dist/pypy/lang/js/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_parser.py	Sun Jun  3 00:12:40 2007
@@ -4,7 +4,7 @@
 from pypy.rlib.parsing.ebnfparse import parse_ebnf, make_parse_function
 from pypy.rlib.parsing.parsing import ParseError, Rule
 from pypy.rlib.parsing.tree import RPythonVisitor
-from pypy.lang.js.jsobj import empty_context, ThrowException 
+from pypy.lang.js.jsobj import W_Object, global_context, ThrowException 
 from pypy.lang.js.astbuilder import ASTBuilder
 from pypy import conftest
 import sys
@@ -285,7 +285,10 @@
     
     def eval_expr(self, s):
         ast = self.to_ast(s)
-        return ast.eval(empty_context())
+        w_Global = W_Object()
+        w_Object = W_Object(Prototype=W_Object())
+        w_Global.Put('Object', w_Object)
+        return ast.eval(global_context(w_Global))
     
     def test_get_pos(self):
         from pypy.lang.js import operations


More information about the pypy-svn mailing list