[pypy-svn] r43686 - in pypy/dist/pypy/lang/js: . test test/ecma
santagada at codespeak.net
santagada at codespeak.net
Sat May 26 19:09:27 CEST 2007
Author: santagada
Date: Sat May 26 19:09:26 2007
New Revision: 43686
Modified:
pypy/dist/pypy/lang/js/astbuilder.py
pypy/dist/pypy/lang/js/interpreter.py
pypy/dist/pypy/lang/js/js_interactive.py
pypy/dist/pypy/lang/js/jsgrammar.txt
pypy/dist/pypy/lang/js/jsobj.py
pypy/dist/pypy/lang/js/jsparser.py
pypy/dist/pypy/lang/js/operations.py
pypy/dist/pypy/lang/js/test/ecma/shell.js
pypy/dist/pypy/lang/js/test/test_interp.py
Log:
Everything needed to start running the ecma tests. ForIn With and their variants suported. Creating objects with the new construct also fixed.
Modified: pypy/dist/pypy/lang/js/astbuilder.py
==============================================================================
--- pypy/dist/pypy/lang/js/astbuilder.py (original)
+++ pypy/dist/pypy/lang/js/astbuilder.py Sat May 26 19:09:26 2007
@@ -163,6 +163,7 @@
pos = self.get_pos(node)
nodes = [self.dispatch(child) for child in node.children]
return operations.VariableDeclList(pos, nodes)
+ visit_variabledeclarationlistnoin = visit_variabledeclarationlist
def visit_propertynameandvalue(self, node):
pos = self.get_pos(node)
@@ -194,16 +195,15 @@
def visit_sourceelements(self, node):
pos = self.get_pos(node)
- self.varlists.append(set())
+ self.varlists.append({})
self.funclists.append({})
nodes=[]
for child in node.children:
node = self.dispatch(child)
if node is not None:
nodes.append(node)
- var_decl = self.varlists.pop()
+ var_decl = self.varlists.pop().keys()
func_decl = self.funclists.pop()
- print var_decl, func_decl
return operations.SourceElements(pos, var_decl, func_decl, nodes)
def functioncommon(self, node, declaration=True):
@@ -231,12 +231,13 @@
def visit_variabledeclaration(self, node):
pos = self.get_pos(node)
identifier = self.dispatch(node.children[0])
- self.varlists[-1].add(identifier.get_literal())
+ self.varlists[-1][identifier.get_literal()] = None
if len(node.children) > 1:
expr = self.dispatch(node.children[1])
else:
expr = None
return operations.VariableDeclaration(pos, identifier, expr)
+ visit_variabledeclarationnoin = visit_variabledeclaration
def visit_expressionstatement(self, node):
return self.dispatch(node.children[0])
@@ -297,6 +298,15 @@
update, i = self.get_next_expr(node, i)
body, i = self.get_next_expr(node, i)
return operations.For(pos, setup, condition, update, body)
+ visit_regularvarfor = visit_regularfor
+
+ def visit_infor(self, node):
+ pos = self.get_pos(node)
+ left = self.dispatch(node.children[1])
+ right = self.dispatch(node.children[2])
+ body= self.dispatch(node.children[3])
+ return operations.ForIn(pos, left, right, body)
+ visit_invarfor = visit_infor
def get_next_expr(self, node, i):
if isinstance(node.children[i], Symbol) and \
@@ -346,4 +356,10 @@
def visit_primaryexpression(self, node):
pos = self.get_pos(node)
return operations.This(pos, 'this')
-
\ No newline at end of file
+
+ def visit_withstatement(self, node):
+ pos = self.get_pos(node)
+ identifier = self.dispatch(node.children[0])
+ body = self.dispatch(node.children[1])
+ return operations.With(pos, identifier, body)
+
\ No newline at end of file
Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py (original)
+++ pypy/dist/pypy/lang/js/interpreter.py Sat May 26 19:09:26 2007
@@ -4,7 +4,7 @@
from pypy.lang.js.astbuilder import ASTBuilder
from pypy.lang.js.operations import *
from pypy.rlib.objectmodel import we_are_translated
-
+from pypy.rlib.streamio import open_file_as_stream
def writer(x):
print x
@@ -14,20 +14,16 @@
temp_tree = parse(script_source)
return astb.dispatch(temp_tree)
-import os.path
-
def load_file(filename):
- # NOT RPYTHON
- base, ext = os.path.splitext(filename)
- f = open(filename)
- t = load_source(f.read())
+ f = open_file_as_stream(filename)
+ t = load_source(f.readall())
f.close()
return t
def evaljs(ctx, args, this):
if len(args) >= 1:
- if isinstance(args[0],W_String):
+ if isinstance(args[0], W_String):
code = args[0]
else:
return args[0]
@@ -109,6 +105,10 @@
return W_String(args[0].ToString())
return W_String('')
+def arrayjs(ctx, args, this):
+ return W_Array()
+
+
def numberjs(ctx, args, this):
if len(args) > 0:
return W_Number(args[0].ToNumber())
@@ -162,7 +162,7 @@
#Global Properties
w_Global.Put('Object', w_Object)
w_Global.Put('Function', w_Function)
- w_Global.Put('Array', W_Array())
+ w_Global.Put('Array', W_Builtin(arrayjs, Class='Array'))
w_Global.Put('version', W_Builtin(versionjs))
#Number
Modified: pypy/dist/pypy/lang/js/js_interactive.py
==============================================================================
--- pypy/dist/pypy/lang/js/js_interactive.py (original)
+++ pypy/dist/pypy/lang/js/js_interactive.py Sat May 26 19:09:26 2007
@@ -147,13 +147,13 @@
res = self.interp.run(load_source("\n".join(self.lines)))
# XXX we should catch more stuff here, like not implemented
# and such
- except (jsparser.JsSyntaxError, ThrowException), e:
+ except (jsparser.ParseError, ThrowException), e:
e_info = sys.exc_info()
if self.debug:
import pdb
pdb.post_mortem(e_info[2])
else:
- if isinstance(e, jsparser.JsSyntaxError):
+ if isinstance(e, jsparser.ParseError):
print "\nSyntax error!"
elif isinstance(e, ThrowException):
print e.exception.ToString()
Modified: pypy/dist/pypy/lang/js/jsgrammar.txt
==============================================================================
--- pypy/dist/pypy/lang/js/jsgrammar.txt (original)
+++ pypy/dist/pypy/lang/js/jsgrammar.txt Sat May 26 19:09:26 2007
@@ -376,10 +376,10 @@
;
#identifier
-variabledeclarationnoin : identifier initialisernoin?
+variabledeclarationnoin : identifier >initialisernoin<?
;
-initialisernoin : "=" assignmentexpressionnoin
+initialisernoin : ["="] assignmentexpressionnoin
;
variabledeclarationlistnoin : variabledeclarationnoin [","] >variabledeclarationlistnoin<
Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py (original)
+++ pypy/dist/pypy/lang/js/jsobj.py Sat May 26 19:09:26 2007
@@ -263,6 +263,9 @@
def Call(self, ctx, args=[], this = None):
return self.callfuncbi(ctx, args, this)
+
+ def Construct(self, ctx, args=[]):
+ return self.callfuncbi(ctx, args, None)
def type(self):
return 'builtin'
@@ -288,7 +291,7 @@
def arraycallbi(ctx, args, this):
return W_Array()
-class W_Array(W_Builtin):
+class W_Array(W_PrimitiveObject):
def __init__(self, ctx=None, Prototype=None, Class='Array',
Value=w_Undefined, callfunc=None):
W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, callfunc)
@@ -296,7 +299,6 @@
self.Put('toString', toString, de=True)
self.Put('length', W_Number(0))
self.length = r_uint(0)
- self.set_builtin_call(arraycallbi)
def Construct(self, ctx, args=[]):
return self
@@ -328,7 +330,8 @@
return
if index < self.length:
return
- self.length = index+1
+
+ self.length = index+1
self.propdict['length'].value = W_Number(index+1)
return
Modified: pypy/dist/pypy/lang/js/jsparser.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsparser.py (original)
+++ pypy/dist/pypy/lang/js/jsparser.py Sat May 26 19:09:26 2007
@@ -14,4 +14,10 @@
parsef = make_parse_function(regexs, rules, eof=True)
def parse(code):
- return parsef(code).visit(ToAST())[0]
+ try:
+ t = parsef(code)
+ except ParseError:
+ code += ';'
+ t = parsef(code)
+
+ return ToAST().transform(t)
Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py (original)
+++ pypy/dist/pypy/lang/js/operations.py Sat May 26 19:09:26 2007
@@ -127,19 +127,19 @@
if op == "=":
val = v3
elif op == "*=":
- val = Mult().mathop(ctx, v1.GetValue(), v3)
+ val = mult(ctx, v1.GetValue(), v3)
elif op == "+=":
- val = Plus().mathop(ctx, v1.GetValue(), v3)
+ val = plus(ctx, v1.GetValue(), v3)
elif op == "/=":
- val = Div().mathop(ctx, v1.GetValue(), v3)
+ val = division(ctx, v1.GetValue(), v3)
elif op == "%=":
- val = Mod().mathop(ctx, v1.GetValue(), v3)
+ val = mod(ctx, v1.GetValue(), v3)
elif op == "&=":
- val = BitwiseAnd().decision(ctx, v1.GetValue().ToInt32(), v3.ToInt32())
+ val = W_Number(v1.GetValue().ToInt32() & v3.ToInt32())
elif op == "|=":
- val = BitwiseOR().decision(ctx, v1.GetValue().ToInt32(), v3.ToInt32())
+ val = W_Number(v1.GetValue().ToInt32() | v3.ToInt32())
elif op == "^=":
- val = BitwiseXOR().decision(ctx, v1.GetValue().ToInt32(), v3.ToInt32())
+ val = W_Number(v1.GetValue().ToInt32() ^ v3.ToInt32())
else:
print op
raise NotImplementedError()
@@ -699,7 +699,6 @@
x = self.expr.eval(ctx).GetValue()
if not isinstance(x, W_PrimitiveObject):
raise TypeError()
-
return x.Construct(ctx=ctx)
@@ -742,9 +741,6 @@
else:
singlequote = True
- if stop < 0:
- print stop
- raise JsSyntaxError()
internalstring = string[1:stop]
for c in internalstring:
@@ -780,9 +776,6 @@
self.nodes = nodes
def execute(self, ctx):
- print self.var_decl
- print ctx
- print repr(ctx.variable)
for varname in self.var_decl:
ctx.variable.Put(varname, w_Undefined)
for funcname, funccode in self.func_decl.items():
@@ -793,14 +786,13 @@
last = w_Undefined
for node in self.nodes:
last = node.execute(ctx)
- print repr(ctx.variable)
return last
except Exception, e:
if isinstance(e, ExecutionReturned) and e.type == 'return':
raise
else:
# TODO: proper exception handling
- sys.stderr.write("exception in line: %s, on: %s%s"%(node.pos.lineno, node, os.linesep))
+ print "exception in line: %s, on: %s%s"%(node.pos.lineno, node, os.linesep)
raise
@@ -850,7 +842,7 @@
e = excpt
if self.catchblock is not None:
obj = W_Object()
- obj.Put(self.catchparam.name, e.exception)
+ obj.Put(self.catchparam.get_literal(), e.exception)
ctx.push_object(obj)
tryresult = self.catchblock.execute(ctx)
ctx.pop_object()
@@ -913,14 +905,13 @@
class With(Statement):
- opcode = "WITH"
-
- def __init__(self, pos, t):
- self.object = get_obj(t, 'object')
- self.body = get_obj(t, 'body')
+ def __init__(self, pos, identifier, body):
+ self.pos = pos
+ self.identifier = identifier
+ self.body = body
def execute(self, ctx):
- obj = self.object.eval(ctx).GetValue().ToObject()
+ obj = self.identifier.eval(ctx).GetValue().ToObject()
ctx.push_object(obj)
try:
@@ -969,10 +960,10 @@
class ForIn(Statement):
- def __init__(self, pos, t):
- self.object = get_obj(t, 'object')
- self.body = get_obj(t, 'body')
- self.iterator = get_obj(t, 'iterator')
+ def __init__(self, pos, iterator, lobject, body):
+ self.iterator = iterator
+ self.object = lobject
+ self.body = body
def execute(self, ctx):
obj = self.object.eval(ctx).GetValue().ToObject()
Modified: pypy/dist/pypy/lang/js/test/ecma/shell.js
==============================================================================
--- pypy/dist/pypy/lang/js/test/ecma/shell.js (original)
+++ pypy/dist/pypy/lang/js/test/ecma/shell.js Sat May 26 19:09:26 2007
@@ -50,7 +50,7 @@
var PASSED = " PASSED!";
var FAILED = " FAILED! expected: ";
-var DEBUG = false;
+var DEBUG = true;
var DESCRIPTION;
var EXPECTED;
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 May 26 19:09:26 2007
@@ -449,7 +449,6 @@
""", 'undefined')
def test_forin():
- py.test.skip("not ready yet")
assertp("""
var x = {a:5};
for(y in x){
@@ -464,7 +463,6 @@
yield assertv, "2 !== 2;", False
def test_with():
- py.test.skip("not ready yet")
assertp("""
var mock = {x:2};
var x=4;
@@ -488,7 +486,6 @@
yield assertv, "2 | 3;", 3
def test_for_vararg():
- py.test.skip("not ready yet")
assertp("""
for (var arg = "", i = 0; i < 2; i++) { print(i);}
""", ['0', '1'])
@@ -520,3 +517,22 @@
case 1: 15; break;
default: 30;
};""", 15)
+
+def test_inplace_assign():
+ yield assertv, "x=1; x+=1; x;", 2
+ yield assertv, "x=2; x*=2; x;", 4
+ yield assertv, "x=2; x/=2; x;", 1
+ yield assertv, "x=4; x%=2; x;", 0
+ yield assertv, "x=2; x&=2; x;", 2
+ yield assertv, "x=0; x|=1; x;", 1
+ yield assertv, "x=2; x^=2; x;", 0
+
+def test_twoarray():
+ assertp("""
+ a1 = new Array();
+ a2 = new Array();
+ a1[0] = 1;
+ print(a1[0]);
+ a2[0] = 2;
+ print(a1[0]);
+ """, ['1', '1'])
More information about the Pypy-commit
mailing list