[pypy-svn] r34170 - in pypy/dist/pypy/lang/js: . test
stephan at codespeak.net
stephan at codespeak.net
Sat Nov 4 15:29:43 CET 2006
Author: stephan
Date: Sat Nov 4 15:29:25 2006
New Revision: 34170
Modified:
pypy/dist/pypy/lang/js/astgen.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:
(stephan, santagada) somewhat working object creation
Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py (original)
+++ pypy/dist/pypy/lang/js/astgen.py Sat Nov 4 15:29:25 2006
@@ -60,7 +60,7 @@
class Gt(BinaryOperator): pass
class Identifier(Node):
- def __init__(self, name, initialiser):
+ def __init__(self, name, initialiser=None):
self.name = name
self.initialiser = initialiser
@@ -81,6 +81,10 @@
class Lt(BinaryOperator): pass
+class New(Node):
+ def __init__(self, identifier):
+ self.identifier = identifier
+
class Number(Node):
def __init__(self, num):
self.num = num
@@ -197,6 +201,8 @@
return List(getlist(d))
elif tp == 'LT':
return Lt(from_dict(d['0']), from_dict(d['1']))
+ elif tp == 'NEW':
+ return New(d['0']['value'])
elif tp == 'NUMBER':
return Number(float(d['value']))
elif tp == 'OBJECT_INIT':
@@ -214,6 +220,8 @@
return Semicolon(from_dict(d['expression']))
elif tp == 'STRING':
return String(d['value'])
+ elif tp == 'THIS':
+ return Identifier(d['value'])
elif tp == 'THROW':
return Throw(from_dict(d['exception']))
elif tp == 'TRY':
Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py (original)
+++ pypy/dist/pypy/lang/js/interpreter.py Sat Nov 4 15:29:25 2006
@@ -126,6 +126,23 @@
def call(self, context=None):
return [node.call(context) for node in self.nodes]
+class __extend__(New):
+ def call(self, context=None):
+ print context.__dict__
+ obj = W_Object({})
+ obj.Class = 'Object'
+ try:
+ constructor = context.access(self.identifier)
+ except NameError:
+ constructor = scope_manager.get_variable(self.identifier)
+ obj.dict_w['prototype'] = constructor.dict_w['prototype']
+ nctx = ExecutionContext(context)
+ nctx.assign('this',obj)
+ constructor.Call(nctx)
+
+ return obj
+
+
class __extend__(Number):
def call(self, context):
return W_Number(self.num)
@@ -165,6 +182,7 @@
class __extend__(Script):
def call(self, context=None, args=(), this=None, params=None):
+ print 'params',params
if params == None:
params = []
ncontext = ExecutionContext(context)
Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py (original)
+++ pypy/dist/pypy/lang/js/jsobj.py Sat Nov 4 15:29:25 2006
@@ -35,6 +35,7 @@
self.dict_w['toString'] = W_Builtin({}, self.w_string)
# XXX A bit hairy here, we store here a Function, and Script
# is a self.function.body
+ self.dict_w['prototype'] = self
self.function = function
#self.class_ = None
@@ -44,7 +45,7 @@
args=args, this=this,
params= self.function.params)
else:
- raise SeePage(33)
+ return W_Object({})
def w_string(self):
return W_String(str(self))
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 Sat Nov 4 15:29:25 2006
@@ -3,7 +3,8 @@
from pypy.lang.js import interpreter
from pypy.lang.js.parser import parse
from pypy.lang.js.interpreter import ThrowException
-from pypy.lang.js.jsobj import W_Number
+from pypy.lang.js.jsobj import W_Number, W_Object
+from pypy.lang.js.context import ExecutionContext
import py.test
import sys
@@ -26,8 +27,10 @@
def assert_prints(self, code, assval):
l = []
interpreter.writer = l.append
+ ctx = ExecutionContext()
+ ctx.globals['Object'] = W_Object({})
try:
- code.call()
+ code.call(ctx)
except ThrowException, excpt:
l.append("uncaught exception: "+str(excpt.exception))
assert l == assval
@@ -210,3 +213,18 @@
}
print(i);
"""), ["0","1","2","3"])
+
+ def test_object_creation(self):
+ self.assert_prints(parse_d("""
+ o = new Object();
+ print(o);
+ """), ["[object Object]"])
+
+ def test_new_with_function(self):
+ self.assert_prints(parse_d("""
+ x = function() {this.info = 'hello';};
+ o = new x();
+ print(o.info);
+ """), ["hello"])
+
+
More information about the Pypy-commit
mailing list