[pypy-svn] r53258 - in pypy/branch/js-refactoring/pypy/lang/js: . test
fijal at codespeak.net
fijal at codespeak.net
Wed Apr 2 19:04:07 CEST 2008
Author: fijal
Date: Wed Apr 2 19:04:06 2008
New Revision: 53258
Modified:
pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py
pypy/branch/js-refactoring/pypy/lang/js/baseop.py
pypy/branch/js-refactoring/pypy/lang/js/jscode.py
pypy/branch/js-refactoring/pypy/lang/js/operations.py
pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py
Log:
Some general progress. Only few things left (notable delete, forin, with)
Modified: pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py Wed Apr 2 19:04:06 2008
@@ -37,12 +37,12 @@
}
UNOP_TO_CLS = {
#'~': operations.BitwiseNot,
- #'!': operations.Not,
+ '!': operations.Not,
'+': operations.UPlus,
'-': operations.UMinus,
- #'typeof': operations.Typeof,
- #'void': operations.Void,
- #'delete': operations.Delete,
+ 'typeof': operations.Typeof,
+ 'void': operations.Void,
+ 'delete': operations.Delete,
}
LISTOP_TO_CLS = {
'[': operations.Array,
@@ -431,7 +431,7 @@
condition = self.dispatch(node.children[0])
truepart = self.dispatch(node.children[2])
falsepart = self.dispatch(node.children[3])
- return operations.Conditional(pos, condition, truepart, falsepart)
+ return operations.If(pos, condition, truepart, falsepart)
def visit_trystatement(self, node):
pos = self.get_pos(node)
Modified: pypy/branch/js-refactoring/pypy/lang/js/baseop.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/baseop.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/baseop.py Wed Apr 2 19:04:06 2008
@@ -62,6 +62,7 @@
return W_IntNumber(ileft % iright)
def division(ctx, nleft, nright):
+ # XXX optimise for ints and floats
fleft = nleft.ToNumber()
fright = nright.ToNumber()
if fright == 0:
@@ -211,3 +212,8 @@
except JsTypeError:
raise ThrowException(W_String('it is not a constructor'))
return res
+
+def uminus(obj):
+ if isinstance(obj, W_IntNumber):
+ return W_IntNumber(-obj.intval)
+ return W_FloatNumber(-obj.ToNumber())
Modified: pypy/branch/js-refactoring/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/jscode.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/jscode.py Wed Apr 2 19:04:06 2008
@@ -1,12 +1,12 @@
from pypy.lang.js.jsobj import W_IntNumber, W_FloatNumber, W_String,\
W_Array, W_PrimitiveObject, ActivationObject,\
- create_object, W_Object, w_Undefined, W_Boolean, newbool,\
- w_True, w_False, W_List
+ create_object, W_Object, w_Undefined, newbool,\
+ w_True, w_False, W_List, w_Null
from pypy.lang.js.execution import JsTypeError, ReturnException, ThrowException
from pypy.rlib.unroll import unrolling_iterable
from pypy.lang.js.baseop import plus, sub, compare, AbstractEC, StrictEC,\
- compare_e, increment, commonnew
+ compare_e, increment, commonnew, mult, division, uminus, mod
from pypy.rlib.jit import hint
class AlreadyRun(Exception):
@@ -195,6 +195,13 @@
def __repr__(self):
return 'LOAD_INTCONSTANT %s' % (self.w_intvalue.intval,)
+class LOAD_BOOLCONSTANT(Opcode):
+ def __init__(self, value):
+ self.boolval = value
+
+ def eval(self, ctx, stack):
+ stack.append(newbool(self.boolval))
+
class LOAD_FLOATCONSTANT(Opcode):
def __init__(self, value):
self.w_floatvalue = W_FloatNumber(float(value))
@@ -222,6 +229,10 @@
def eval(self, ctx, stack):
stack.append(w_Undefined)
+class LOAD_NULL(Opcode):
+ def eval(self, ctx, stack):
+ stack.append(w_Null)
+
class LOAD_VARIABLE(Opcode):
def __init__(self, identifier):
self.identifier = identifier
@@ -353,7 +364,20 @@
if not isinstance(right, W_Object):
raise ThrowException(W_String("TypeError"))
name = left.ToString(ctx)
- return W_Boolean(right.HasProperty(name))
+ return newbool(right.HasProperty(name))
+
+class TYPEOF(BaseUnaryOperation):
+ def eval(self, ctx, stack):
+ one = stack.pop()
+ stack.append(W_String(one.type()))
+
+#class Typeof(UnaryOp):
+# def eval(self, ctx):
+# val = self.expr.eval(ctx)
+# if isinstance(val, W_Reference) and val.GetBase() is None:
+# return W_String("undefined")
+# return W_String(val.GetValue().type())
+
class ADD(BaseBinaryOperation):
@staticmethod
@@ -365,21 +389,36 @@
def operation(ctx, op1, op2):
return W_IntNumber(op1&op2)
-
class MUL(BaseBinaryOperation):
- pass
+ @staticmethod
+ def operation(ctx, op1, op2):
+ return mult(ctx, op1, op2)
class DIV(BaseBinaryOperation):
- pass
+ @staticmethod
+ def operation(ctx, op1, op2):
+ return division(ctx, op1, op2)
class MOD(BaseBinaryOperation):
- pass
+ @staticmethod
+ def operation(ctx, op1, op2):
+ return mod(ctx, op1, op2)
class UPLUS(BaseUnaryOperation):
- pass
+ def eval(self, ctx, stack):
+ if isinstance(stack[-1], W_IntNumber):
+ return
+ if isinstance(stack[-1], W_FloatNumber):
+ return
+ stack.append(stack.pop().ToNumber(ctx))
class UMINUS(BaseUnaryOperation):
- pass
+ def eval(self, ctx, stack):
+ stack.append(uminus(stack.pop()))
+
+class NOT(BaseUnaryOperation):
+ def eval(self, ctx, stack):
+ stack.append(newbool(not stack.pop().ToBoolean()))
class INCR(BaseUnaryOperation):
pass
Modified: pypy/branch/js-refactoring/pypy/lang/js/operations.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/operations.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/operations.py Wed Apr 2 19:04:06 2008
@@ -409,13 +409,6 @@
else:
bytecode.emit('LABEL', one)
- def execute(self, ctx):
- temp = self.condition.eval(ctx).GetValue()
- if temp.ToBoolean():
- return self.thenPart.execute(ctx)
- else:
- return self.elsePart.execute(ctx)
-
#class Group(UnaryOp):
# def eval(self, ctx):
# return self.expr.eval(ctx)
@@ -501,7 +494,7 @@
StrictNe = create_binary_op('ISNOT')
In = create_binary_op('IN')
-
+Typeof = create_unary_op('TYPEOF')
# class Delete(UnaryOp):
# """
@@ -515,6 +508,14 @@
# r4 = r1.GetPropertyName()
# return W_Boolean(r3.Delete(r4))
+class Delete(Expression):
+ def __init__(self, pos, what):
+ self.pos = pos
+ self.what = what
+
+ #def emit(self, bytecode):
+ #
+
#class Index(BinaryOp):
# def eval(self, ctx):
# w_obj = self.left.eval(ctx).GetValue().ToObject(ctx)
@@ -540,9 +541,8 @@
Sub = create_binary_op('SUB')
class Null(Expression):
- def eval(self, ctx):
- return w_Null
-
+ def emit(self, bytecode):
+ bytecode.emit('LOAD_NULL')
##############################################################################
#
@@ -721,13 +721,6 @@
bytecode.emit('TRYCATCHBLOCK', trycode, self.catchparam.get_literal(),
catchcode, finallycode)
-#class Typeof(UnaryOp):
-# def eval(self, ctx):
-# val = self.expr.eval(ctx)
-# if isinstance(val, W_Reference) and val.GetBase() is None:
-# return W_String("undefined")
-# return W_String(val.GetValue().type())
-
class VariableDeclaration(Expression):
def __init__(self, pos, identifier, expr=None):
self.pos = pos
@@ -782,11 +775,15 @@
def execute(self, ctx):
return self.body.eval(ctx)
-#class Void(UnaryOp):
-# def eval(self, ctx):
-# self.expr.eval(ctx)
-# return w_Undefined
+class Void(Expression):
+ def __init__(self, pos, expr):
+ self.pos = pos
+ self.expr = expr
+ def emit(self, bytecode):
+ self.expr.emit(bytecode)
+ bytecode.emit('POP')
+ bytecode.emit('LOAD_UNDEFINED')
class With(Statement):
def __init__(self, pos, identifier, body):
@@ -916,15 +913,11 @@
def __init__(self, pos, boolval):
self.pos = pos
self.bool = boolval
-
- def eval(self, ctx):
- return W_Boolean(self.bool)
-
-#class Not(UnaryOp):
-# def eval(self, ctx):
-# return W_Boolean(not self.expr.eval(ctx).GetValue().ToBoolean())
+ def emit(self, bytecode):
+ bytecode.emit('LOAD_BOOLCONSTANT', self.bool)
+Not = create_unary_op('NOT')
UMinus = create_unary_op('UMINUS')
UPlus = create_unary_op('UPLUS')
Modified: pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/test/test_interp.py Wed Apr 2 19:04:06 2008
@@ -5,6 +5,7 @@
from pypy.lang.js.jsobj import W_Object, ExecutionContext, W_Root, w_Null
from pypy.lang.js.execution import ThrowException
from pypy.lang.js.jscode import JsCode, POP
+from pypy.lang.js.baseop import AbstractEC
def test_simple():
bytecode = JsCode()
@@ -44,7 +45,7 @@
code_val = excpt.exception
print code_val, value
if isinstance(value, W_Root):
- assert AEC(jsint.global_context, code_val, value) == True
+ assert AbstractEC(jsint.global_context, code_val, value) == True
elif isinstance(value, bool):
assert code_val.ToBoolean() == value
elif isinstance(value, int):
@@ -465,6 +466,7 @@
assertp("print(+1);", '1')
def test_delete():
+ py.test.skip("Unsupported")
assertp("""
var x = {};
x.y = 1;
More information about the pypy-svn
mailing list