[pypy-svn] r33959 - in pypy/dist/pypy/lang/js: . test
fijal at codespeak.net
fijal at codespeak.net
Tue Oct 31 15:53:05 CET 2006
Author: fijal
Date: Tue Oct 31 15:53:01 2006
New Revision: 33959
Modified:
pypy/dist/pypy/lang/js/astgen.py
pypy/dist/pypy/lang/js/context.py
pypy/dist/pypy/lang/js/interpreter.py
pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(santagada, fijal) - Simple return
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 15:53:01 2006
@@ -68,6 +68,10 @@
self.name = name
self.value = value
+class Return(Node):
+ def __init__(self, expr):
+ self.expr = expr
+
class Scope(Node):
def __init__(self, dict):
self.dict = self.dicts
@@ -128,5 +132,7 @@
return Index(from_dict(d['0']), from_dict(d['1']))
elif tp == 'FUNCTION':
return Function(d['params'], from_dict(d['body']))
+ elif tp == 'RETURN':
+ return Return(from_dict(d['value']))
else:
raise NotImplementedError("Dont know how to handler %s" % tp)
Modified: pypy/dist/pypy/lang/js/context.py
==============================================================================
--- pypy/dist/pypy/lang/js/context.py (original)
+++ pypy/dist/pypy/lang/js/context.py Tue Oct 31 15:53:01 2006
@@ -14,21 +14,9 @@
## #self.locals = {}
def assign(self, name, value):
- #if name in self.locals:
- # self.locals[name] = value
- #else:
- # if self.parent:
- # self.parent.assign(name, value)
- # else:
self.globals[name] = value
def access(self, name):
- #if name in self.locals:
- # return self.locals[name]
- #else:
- # if self.parent:
- # return self.parent.access(name)
- # else:
if name in self.globals:
return self.globals[name]
raise NameError("%s is not declared" % name)
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 15:53:01 2006
@@ -1,11 +1,15 @@
from pypy.lang.js.astgen import *
from pypy.lang.js.context import ExecutionContext
-from pypy.lang.js.jsobj import W_Number, W_String, W_Object
+from pypy.lang.js.jsobj import W_Number, W_String, W_Object, w_Undefined
def writer(x):
print x
+class ExecutionReturned(Exception):
+ def __init__(self, value):
+ self.value = value
+
class __extend__(Assign):
def call(self, context):
val = self.expr.call(context)
@@ -62,8 +66,12 @@
class __extend__(Script):
def call(self, context=None):
new_context = ExecutionContext(context)
- for node in self.nodes:
- node.call(new_context)
+ try:
+ for node in self.nodes:
+ node.call(new_context)
+ except ExecutionReturned, e:
+ return e.value
+ return w_Undefined
class __extend__(Call):
def call(self, context=None):
@@ -110,4 +118,7 @@
w_obj = W_Object({})
w_obj.body = self.body
return w_obj
-
+
+class __extend__(Return):
+ def call(self, context=None):
+ raise ExecutionReturned(self.expr.call(context))
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 15:53:01 2006
@@ -57,5 +57,5 @@
def test_function_prints(self):
self.assert_prints(parse_d('x=function(){print(3);}; x();'), ["3"])
- #def test_call_print(self):
- # parse_d("print({});")
+ def test_function_returns(self):
+ self.assert_prints(parse_d('x=function(){return 1;}; print(x()+x());'), ["2"])
More information about the Pypy-commit
mailing list