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

fijal at codespeak.net fijal at codespeak.net
Tue Apr 1 06:41:53 CEST 2008


Author: fijal
Date: Tue Apr  1 06:41:51 2008
New Revision: 53225

Modified:
   pypy/branch/js-refactoring/pypy/lang/js/jscode.py
   pypy/branch/js-refactoring/pypy/lang/js/operations.py
   pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py
Log:
implement exceptions. They're not that great, but I think better
than before (catching of finally blocks works better)


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 Apr  1 06:41:51 2008
@@ -19,7 +19,8 @@
             opcode = opcodes[i]
             opcode = hint(opcode, deepfreeze=True)
             if isinstance(opcode, op):
-                opcode.eval(ctx, stack)
+                result = opcode.eval(ctx, stack)
+                assert result is None
                 break
         if isinstance(opcode, BaseJump):
             i = opcode.do_jump(stack, i)
@@ -549,6 +550,45 @@
     def eval(self, ctx, stack):
         stack.append(stack[-1])
 
+class THROW(Opcode):
+    def eval(self, ctx, stack):
+        val = stack.pop()
+        raise ThrowException(val)
+
+class TRYCATCHBLOCK(Opcode):
+    def __init__(self, trycode, catchparam, catchcode, finallycode):
+        self.trycode     = trycode
+        self.catchcode   = catchcode
+        self.catchparam  = catchparam
+        self.finallycode = finallycode
+    
+    def eval(self, ctx, stack):
+        try:
+            try:
+                self.trycode.run(ctx)
+            except ThrowException, e:
+                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)
+                    ctx.push_object(obj)
+                    try:
+                        self.catchcode.run(ctx)
+                    finally:
+                        ctx.pop_object()
+                if self.finallycode is not None:
+                    self.finallycode.run(ctx)
+                if not self.catchcode:
+                    raise
+        except ReturnException:
+            # we run finally block here and re-raise the exception
+            if self.finallycode is not None:
+                self.finallycode.run(ctx)
+            raise
+
+    def __repr__(self):
+        return "TRYCATCHBLOCK" # XXX shall we add stuff here???
+
 OpcodeMap = {}
 
 for name, value in locals().items():

Modified: pypy/branch/js-refactoring/pypy/lang/js/operations.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/operations.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/operations.py	Tue Apr  1 06:41:51 2008
@@ -725,9 +725,10 @@
     def __init__(self, pos, exp):
         self.pos = pos
         self.exp = exp
-    
-    def execute(self, ctx):
-        raise ThrowException(self.exp.eval(ctx).GetValue())
+
+    def emit(self, bytecode):
+        self.exp.emit(bytecode)
+        bytecode.emit('THROW')
 
 class Try(Statement):
     def __init__(self, pos, tryblock, catchparam, catchblock, finallyblock):
@@ -736,30 +737,23 @@
         self.catchparam = catchparam
         self.catchblock = catchblock
         self.finallyblock = finallyblock
-    
-    def execute(self, ctx):
-        e = None
-        tryresult = w_Undefined
-        try:
-            tryresult = self.tryblock.execute(ctx)
-        except ThrowException, excpt:
-            e = excpt
-            if self.catchblock is not None:
-                obj = W_Object()
-                obj.Put(self.catchparam.get_literal(), e.exception)
-                ctx.push_object(obj)
-                tryresult = self.catchblock.execute(ctx)
-                ctx.pop_object()
-        
-        if self.finallyblock is not None:
-            tryresult = self.finallyblock.execute(ctx)
-        
-        #if there is no catchblock reraise the exception
-        if (e is not None) and (self.catchblock is None):
-            raise e
-        
-        return tryresult
-    
+
+    def emit(self, bytecode):
+        # a bit naive operator for now
+        trycode = JsCode()
+        self.tryblock.emit(trycode)
+        if self.catchblock:
+            catchcode = JsCode()
+            self.catchblock.emit(catchcode)
+        else:
+            catchcode = None
+        if self.finallyblock:
+            finallycode = JsCode()
+            self.finallyblock.emit(finallycode)
+        else:
+            finallycode = None
+        bytecode.emit('TRYCATCHBLOCK', trycode, self.catchparam.get_literal(),
+                      catchcode, finallycode)    
 
 #class Typeof(UnaryOp):
 #    def eval(self, ctx):

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 Apr  1 06:41:51 2008
@@ -175,7 +175,6 @@
     """, ['', '0'])
 
 def test_throw():
-    py.test.skip("Not implemented")
     assertp("throw(3);", "uncaught exception: 3")
     
 def test_group():
@@ -189,7 +188,6 @@
     yield assertp, "{3; print(5);}", '5'
 
 def test_try_catch_finally():
-    py.test.skip("Not implemented")
     yield assertp, """
     try {
         throw(3);



More information about the Pypy-commit mailing list