[pypy-svn] r33957 - in pypy/dist/pypy/lang/js: . test
fijal at codespeak.net
fijal at codespeak.net
Tue Oct 31 15:28:36 CET 2006
Author: fijal
Date: Tue Oct 31 15:28:35 2006
New Revision: 33957
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/jsobj.py
pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(santagada, fijal) - Implemented not-returning function.
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:28:35 2006
@@ -1,5 +1,7 @@
from pypy.annotation.pairtype import extendabletype
+from pypy.lang.js.context import ExecutionContext
+from pypy.lang.js.jsobj import W_Object
class Node(object):
__metaclass__ = extendabletype
@@ -28,6 +30,13 @@
self.left = left
self.right = right
+class Function(Node):
+ def __init__(self, params, body):
+ self.params = params
+ self.body = body
+ w_obj = W_Object({}, body=self)
+ #self.scope = Scope(copy(scope.dict))
+
class Identifier(Node):
def __init__(self, name):
self.name = name
@@ -59,6 +68,10 @@
self.name = name
self.value = value
+class Scope(Node):
+ def __init__(self, dict):
+ self.dict = self.dicts
+
class Script(Node):
def __init__(self, nodes, var_decl, func_decl):
self.nodes = nodes
@@ -113,5 +126,7 @@
return Dot(from_dict(d['0']), from_dict(d['1']))
elif tp == 'INDEX':
return Index(from_dict(d['0']), from_dict(d['1']))
+ elif tp == 'FUNCTION':
+ return Function(d['params'], from_dict(d['body']))
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:28:35 2006
@@ -1,12 +1,17 @@
class ExecutionContext(object):
- def __init__(self, parent = None):
- self.parent = parent
- if parent is None:
- self.globals = {}
- else:
- self.globals = parent.globals
- #self.locals = {}
+ globals = {}
+
+ def __init__(self, parent=None):
+ pass
+
+## def __init__(self, parent = None):
+## self.parent = parent
+## if parent is None:
+## self.globals = {}
+## else:
+## self.globals = parent.globals
+## #self.locals = {}
def assign(self, name, value):
#if name in self.locals:
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:28:35 2006
@@ -25,6 +25,10 @@
w_obj = self.left.call(context).GetValue().ToObject()
name = self.right.get_literal()
return w_obj.Get(name)
+
+class __extend__(Function):
+ def call(self, context=None):
+ return self.body.call()
class __extend__(Plus):
def call(self, context=None):
@@ -63,8 +67,12 @@
class __extend__(Call):
def call(self, context=None):
- assert self.identifier.name == 'print'
- writer(",".join([str(i) for i in self.arglist.call(context)]))
+ name = self.identifier.get_literal()
+ if name == 'print':
+ writer(",".join([i.ToString() for i in self.arglist.call(context)]))
+ else:
+ w_obj = context.access(name)
+ return w_obj.Call()
class __extend__(List):
def call(self, context=None):
@@ -96,3 +104,10 @@
w_obj = w_obj.ToObject()
name = w_member.ToString()
return w_obj.Get(name)
+
+class __extend__(Function):
+ def call(self, context=None):
+ w_obj = W_Object({})
+ w_obj.body = self.body
+ return w_obj
+
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 15:28:35 2006
@@ -71,19 +71,25 @@
raise NotImplementedError("W_Reference.GetValue")
class W_Object(W_Root):
- def __init__(self, dict_w):
+ def __init__(self, dict_w, body=None):
# string --> W_Root
self.dict_w = dict_w
# XXX: more stuff
self.dict_w['toString'] = W_Builtin({}, self.w_string)
+ self.body = body
+ #self.class_ = None
- def Call(self, **kwargs):
- raise SeePage(33)
+ def Call(self, this=None):
+ if self.body:
+ return self.body.call()
+ else:
+ raise SeePage(33)
def w_string(self):
- return W_String(self.ToString())
+ return W_String(str(self))
def DefaultValue(self, hint):
+ assert hint == "string"
tostring_meth = self.Get("toString")
if isinstance(tostring_meth, W_Object):
return tostring_meth.Call(this=self)
@@ -93,6 +99,9 @@
# XXX: check primitiveness of retval
return retval
+ def ToString(self):
+ return str(self.DefaultValue(hint="string"))
+
def Get(self, name):
if name in self.dict_w:
return self.dict_w[name]
@@ -123,8 +132,8 @@
self.dict_w = {}
self.internalfunction = internalfunction
- def Call(self, *args):
- return self.internalfunction(*args)
+ def Call(self, this=None):
+ return self.internalfunction()
class W_List(W_Root):
def __init__(self, list_w):
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:28:35 2006
@@ -53,3 +53,9 @@
def test_object_access_index(self):
self.assert_prints(parse_d('x={d:"x"}; print(x["d"]);'), ["x"])
+
+ def test_function_prints(self):
+ self.assert_prints(parse_d('x=function(){print(3);}; x();'), ["3"])
+
+ #def test_call_print(self):
+ # parse_d("print({});")
More information about the Pypy-commit
mailing list