[pypy-svn] r36599 - in pypy/dist/pypy/lang/js: . test
santagada at codespeak.net
santagada at codespeak.net
Fri Jan 12 18:14:17 CET 2007
Author: santagada
Date: Fri Jan 12 18:13:42 2007
New Revision: 36599
Modified:
pypy/dist/pypy/lang/js/interpreter.py
pypy/dist/pypy/lang/js/jsobj.py
pypy/dist/pypy/lang/js/test/test_interp.py
Log:
implemented typeof
Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py (original)
+++ pypy/dist/pypy/lang/js/interpreter.py Fri Jan 12 18:13:42 2007
@@ -109,9 +109,7 @@
self.AssignmentExp = AssignmentExp
def eval(self, ctx):
- #print "Assign LHS = ", self.LHSExp
v1 = self.LHSExp.eval(ctx)
- #print "Assign Exp = ", self.AssignmentExp
v3 = self.AssignmentExp.eval(ctx).GetValue()
v1.PutValue(v3, ctx)
return v3
@@ -159,7 +157,6 @@
return w_Null
else:
w_obj = ctx.resolve_identifier(name).GetValue()
- #print "arglist = ", self.arglist
retval = w_obj.Call(ctx=ctx, args=[i for i in self.arglist.get_args(ctx)])
return retval
@@ -226,7 +223,6 @@
def execute(self, ctx):
temp = self.condition.eval(ctx)
- #print "if condition = ", temp
if temp.ToBoolean():
return self.thenPart.execute(ctx)
else:
@@ -247,7 +243,6 @@
# TODO complete the funcion with strings comparison
s1 = x.ToPrimitive(ctx, 'Number')
s2 = y.ToPrimitive(ctx, 'Number')
- #print "ARC x = %s, y = %s"%(str(s1),str(s2))
if not (isinstance(s1, W_String) and isinstance(s2, W_String)):
s4 = s1.ToNumber()
s5 = s2.ToNumber()
@@ -313,7 +308,10 @@
Implements the Abstract Equality Comparison x == y
not following the specs yet
"""
- r = x.ToNumber() == y.ToNumber()
+ if isinstance(x, W_String) and isinstance(y, W_String):
+ r = x.ToString() == y.ToString()
+ else:
+ r = x.ToNumber() == y.ToNumber()
return r
class Eq(BinaryComparisonOp):
@@ -361,7 +359,6 @@
self.nodes = nodes
def get_args(self, ctx):
- #print "nodes = ", self.nodes
return [node.eval(ctx) for node in self.nodes]
class Minus(BinaryComparisonOp):
@@ -504,6 +501,13 @@
return tryresult
+class Typeof(Expression):
+ def __init__(self, op):
+ self.op = op
+
+ def eval(self, ctx):
+ return W_String(self.op.eval(ctx).GetValue().type())
+
class Undefined(Statement):
def execute(self, ctx):
return None
@@ -513,9 +517,7 @@
self.nodes = nodes
def execute(self, ctx):
- #print self.nodes
for var in self.nodes:
- #print var.name
var.execute(ctx)
class While(Statement):
@@ -683,7 +685,6 @@
node = Return(from_tree(gettreeitem(t, 'value')))
elif tp == 'SCRIPT':
f = gettreeitem(t, 'funDecls')
- # print f.symbol
if f.symbol == "dict":
func_decl = [from_tree(f),]
elif f.symbol == "list":
@@ -692,7 +693,6 @@
func_decl = []
v = gettreeitem(t, 'varDecls')
- # print v.symbol
if v.symbol == "dict":
var_decl = [from_tree(v),]
elif v.symbol == "list":
@@ -725,6 +725,8 @@
catchblock = from_tree(gettreeitem(catch, 'block'))
catchparam = gettreeitem(catch, 'varName').additional_info
node = Try(from_tree(gettreeitem(t, 'tryBlock')), catchblock, finallyblock, catchparam)
+ elif tp == 'TYPEOF':
+ node = Typeof(from_tree(gettreeitem(t, '0')))
elif tp == 'VAR':
node = Vars(getlist(t))
elif tp == 'WHILE':
Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py (original)
+++ pypy/dist/pypy/lang/js/jsobj.py Fri Jan 12 18:13:42 2007
@@ -76,6 +76,9 @@
def __repr__(self):
return "<%s(%s)>" % (self.__class__.__name__, self.ToString())
+
+ def type(self):
+ return NotImplementedError
class W_Primitive(W_Root):
"""unifying parent for primitives"""
@@ -155,6 +158,10 @@
def __str__(self):
return "<Object class: %s>" % self.Class
+ def type(self):
+ #if implements call its function
+ return 'object'
+
class W_Arguments(W_Object):
def __init__(self, callee, args):
@@ -207,6 +214,9 @@
#print "* end of function call return = ", val
return val
+ def type(self):
+ return 'function'
+
class W_Array(W_Object):
def __init__(self, items):
W_Object.__init__(self)
@@ -238,6 +248,10 @@
def ToString(self):
return "undefined"
+
+ def type(self):
+ return 'undefined'
+
class W_Null(W_Root):
def __str__(self):
@@ -246,6 +260,9 @@
def ToBoolean(self):
return False
+ def type(self):
+ return 'object'
+
class W_Boolean(W_Primitive):
def __init__(self, boolval):
self.boolval = bool(boolval)
@@ -262,7 +279,10 @@
def ToBoolean(self):
return self.boolval
-
+
+ def type(self):
+ return 'boolean'
+
class W_String(W_Primitive):
def __init__(self, strval):
self.strval = strval
@@ -276,6 +296,9 @@
def ToBoolean(self):
return bool(self.strval)
+ def type(self):
+ return 'string'
+
class W_Number(W_Primitive):
def __init__(self, floatval):
@@ -300,6 +323,8 @@
def Get(self, name):
return w_Undefined
+ def type(self):
+ return 'number'
class W_Builtin(W_Root):
def __init__(self, builtinfunction, context=False, args=0):
@@ -316,7 +341,10 @@
py_args.append(args[i])
res = self.builtinfunction(*py_args)
return res
-
+
+ def type(self):
+ return 'builtin'
+
class W_List(W_Root):
def __init__(self, list_w):
self.list_w = 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 Fri Jan 12 18:13:42 2007
@@ -335,11 +335,10 @@
print('out')""", ["out"])
def test_typeof(self):
- py.test.skip(" TODO: needed for mozilla test suite")
- self.assert_prints("""
- var x = 3
- typeof x ==
- """)
+ self.assert_result("""
+ var x = 3;
+ typeof x == 'number'
+ """, W_Boolean(True))
def test_switch(self):
py.test.skip(" TODO: needed for mozilla test suite")
More information about the Pypy-commit
mailing list