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

santagada at codespeak.net santagada at codespeak.net
Thu Jan 11 12:31:20 CET 2007


Author: santagada
Date: Thu Jan 11 12:31:17 2007
New Revision: 36464

Modified:
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/jsparser.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
for and eval working


Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Thu Jan 11 12:31:17 2007
@@ -61,6 +61,9 @@
     temp_tree = parse_bytecode(bytecode)
     return from_tree(temp_tree)
 
+def evaljs(ctx, code):
+    return load_source(code.ToString()).execute(ctx)
+    
 class Interpreter(object):
     """Creates a js interpreter"""
     def __init__(self):
@@ -70,6 +73,7 @@
         self.w_Global.Put('prototype', W_String('Object'))
         self.w_Global.Put('Object', self.w_Object)
         self.global_context = global_context(self.w_Global)
+        self.w_Global.Put('eval', W_Builtin(evaljs, context=True, args=1))
 
     def run(self, script):
         """run the interpreter"""
@@ -288,6 +292,17 @@
         name = op1.ToString()
         return W_Boolean(op2.HasProperty(name))
 
+class Increment(Expression):
+    def __init__(self, op):
+        self.op = op
+    
+    def eval(self, ctx):
+        thing = self.op.eval(ctx)
+        val = thing.GetValue()
+        x = val.ToNumber()
+        resl = Plus(W_Number(x), W_Number(1)).eval(ctx).GetValue()
+        thing.PutValue(resl)
+        return resl
 
 class Index(Expression):
     def __init__(self, left, expr):
@@ -477,6 +492,19 @@
         while self.condition.eval(ctx).ToBoolean():
             self.body.execute(ctx)
 
+class For(Statement):
+    def __init__(self, setup, condition, update, body):
+        self.setup = setup
+        self.condition = condition
+        self.update = update
+        self.body = body
+
+    def execute(self, ctx):
+        self.setup.eval(ctx)
+        while self.condition.eval(ctx).ToBoolean():
+            self.body.execute(ctx)
+            self.update.eval(ctx)
+
 def getlist(t):
     item = gettreeitem(t, 'length')
     if item is None:
@@ -513,6 +541,12 @@
         return Or(from_tree(gettreeitem(t, '0')), from_tree(gettreeitem(t, '1')))
     elif tp == 'AND':
         return And(from_tree(gettreeitem(t, '0')), from_tree(gettreeitem(t, '1')))
+    elif tp == 'FOR':
+        setup = from_tree(gettreeitem(t, 'setup'))
+        condition = from_tree(gettreeitem(t, 'condition'))
+        update = from_tree(gettreeitem(t, 'update'))
+        body = from_tree(gettreeitem(t, 'body'))
+        return For(setup, condition, update, body)
     elif tp == 'FUNCTION':        
         namesimb = gettreeitem(t, 'name')
         name = None
@@ -549,6 +583,8 @@
         return If(condition,thenPart,elsePart)
     elif tp == 'IN':
         return In(from_tree(gettreeitem(t, '0')), from_tree(gettreeitem(t, '1')))
+    elif tp == 'INCREMENT':
+        return Increment(from_tree(gettreeitem(t, '0')))
     elif tp == 'INDEX':
         return Index(from_tree(gettreeitem(t, '0')), from_tree(gettreeitem(t, '1')))
     elif tp == 'LIST':

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Thu Jan 11 12:31:17 2007
@@ -298,13 +298,20 @@
 
 
 class W_Builtin(W_Root):
-    def __init__(self, builtinfunction):
+    def __init__(self, builtinfunction, context=False, args=0):
         #W_Object.__init__(self)
         self.builtinfunction = builtinfunction
+        self.context = context
+        self.args = args
     
     def Call(self, ctx, args=[], this = None):
-        assert len(args) == 0
-        return W_String(self.builtinfunction()) # ???
+        py_args = []
+        if self.context == True:
+            py_args.append(ctx)
+        for i in range(self.args):
+            py_args.append(args[i])
+        res = self.builtinfunction(*py_args)
+        return res
     
 class W_List(W_Root):
     def __init__(self, list_w):

Modified: pypy/dist/pypy/lang/js/jsparser.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsparser.py	(original)
+++ pypy/dist/pypy/lang/js/jsparser.py	Thu Jan 11 12:31:17 2007
@@ -26,6 +26,7 @@
     pipe.stdin.close()
     retval = pipe.stdout.read()
     if not retval.startswith("{"):
+        print stripped_code
         raise JsSyntaxError(retval)
     return retval
 

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	Thu Jan 11 12:31:17 2007
@@ -297,5 +297,22 @@
         print(z)
         """]
         ,["3", "2"])
+    
+    def test_for(self):
+        self.assert_prints("""
+        for (i=0; i<3; i=i+1) {
+            print(i);
+        }
+        print(i);
+        """, ["0","1","2","3"])
+    
+    def test_eval(self):
+        self.assert_prints("""
+        var x = 2;
+        eval('x=x+1; print(x); z=2');
+        print(z);
+        """, ["3","2"])
+
+
 
 



More information about the Pypy-commit mailing list