[pypy-svn] r36989 - in pypy/dist/pypy/lang/js: . test
santagada at codespeak.net
santagada at codespeak.net
Fri Jan 19 13:52:14 CET 2007
Author: santagada
Date: Fri Jan 19 13:52:11 2007
New Revision: 36989
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:
more work on the interpreter... there still something wrong with prototypes
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 19 13:52:11 2007
@@ -4,6 +4,8 @@
from pypy.lang.js.jsobj import *
from pypy.rlib.parsing.ebnfparse import Symbol, Nonterminal
+DEBUG = False
+
class Node(object):
def init_common(self, type='', value='', lineno=0, start=0, end=0):
self.type = type
@@ -45,6 +47,8 @@
def eval(self, ctx):
s2 = self.left.eval(ctx).GetValue()
s4 = self.right.eval(ctx).GetValue()
+ if DEBUG:
+ print "bincomp, op1 and op2 ", s2, s4
return self.decision(ctx, s2, s4)
def decision(self, ctx, op1, op2):
@@ -86,10 +90,21 @@
if len(args) > 0:
return W_Boolean(args[0].ToBoolean())
return W_Boolean(False)
+
+def numberjs(ctx, args, this):
+ if len(args) > 0:
+ return W_Number(args[0].ToNumber())
+ return W_Number(0)
def absjs(ctx, args, this):
return W_Number(abs(args[0].ToNumber()))
-
+
+def floorjs(ctx, args, this):
+ return W_Number(math.floor(args[0].ToNumber()))
+
+def versionjs(ctx, args, this):
+ return w_Undefined
+
class Interpreter(object):
"""Creates a js interpreter"""
def __init__(self):
@@ -116,13 +131,21 @@
w_math = W_Object(Class='Math')
w_Global.Put('Math', w_math)
w_math.Put('abs', W_Builtin(absjs, Class='function'))
+ w_math.Put('floor', W_Builtin(floorjs, Class='function'))
+
#Global Properties
w_Global.Put('Object', w_Object)
w_Global.Put('Function', w_Function)
w_Global.Put('Array', W_Array())
- # w_Global.Put('Math', )
-
+ w_Global.Put('version', W_Builtin(versionjs))
+
+ w_Number = W_Builtin(numberjs, Class="Number")
+ w_Number.Put('NaN', W_Number(NaN))
+ w_Number.Put('POSITIVE_INFINITY', W_Number(Infinity))
+ w_Number.Put('NEGATIVE_INFINITY', W_Number(-Infinity))
+
+ w_Global.Put('Number', w_Number)
w_Global.Put('eval', W_Builtin(evaljs))
w_Global.Put('print', W_Builtin(printjs))
w_Global.Put('isNaN', W_Builtin(isnanjs))
@@ -272,7 +295,7 @@
def eval(self, ctx):
if self.initialiser is not None:
ref = ctx.resolve_identifier(self.name)
- ref.PutValue(self.initialiser.eval(ctx), ctx)
+ ref.PutValue(self.initialiser.eval(ctx).GetValue(), ctx)
return ctx.resolve_identifier(self.name)
def get_literal(self):
@@ -286,7 +309,7 @@
self.elsePart = elsePart
def execute(self, ctx):
- temp = self.condition.eval(ctx)
+ temp = self.condition.eval(ctx).GetValue()
if temp.ToBoolean():
return self.thenPart.execute(ctx)
else:
@@ -372,6 +395,11 @@
Implements the Abstract Equality Comparison x == y
not following the specs yet
"""
+ objtype = x.GetValue().type()
+ if objtype == y.GetValue().type():
+ if objtype == "undefined" or objtype == "null":
+ return True
+
if isinstance(x, W_String) and isinstance(y, W_String):
r = x.ToString() == y.ToString()
else:
@@ -482,7 +510,8 @@
def decision(self, ctx, op1, op2):
prim_left = op1.ToPrimitive(ctx, 'Number')
prim_right = op2.ToPrimitive(ctx, 'Number')
- print "plus", self.left, op1, prim_left, "+", self.right, op2, prim_right
+ if DEBUG:
+ print "plus", self.left, op1, prim_left, "+", self.right, op2, prim_right
if isinstance(prim_left, W_String) or isinstance(prim_right, W_String):
str_left = prim_left.ToString()
str_right = prim_right.ToString()
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 19 13:52:11 2007
@@ -1,5 +1,6 @@
# encoding: utf-8
+DEBUG = False
class SeePage(NotImplementedError):
pass
@@ -109,7 +110,7 @@
return False
def type(self):
- return 'object'
+ return 'null'
w_Undefined = W_Undefined()
w_Null = W_Null()
@@ -294,7 +295,7 @@
def Put(self, P, V):
try:
x = int(P)
- print "puting", V, 'in', x
+ # print "puting", V, 'in', x
if x > self.Get('length').ToNumber() - 1:
self.propdict['length'].value = W_Number(x)
currsize = len(self.array)
@@ -315,7 +316,7 @@
if x > (len(self.array)-1):
return w_Undefined
else:
- print "returning", self.array[x], 'in', x
+ # print "returning", self.array[x], 'in', x
return self.array[x]
except ValueError:
return W_PrimitiveObject.Get(self, P)
@@ -481,8 +482,9 @@
exception = "ReferenceError: %s is not defined"%(self.property_name,)
raise ThrowException(W_String(exception))
- # print "ref base: %s, prop: %s, getresult: %s"%(self.base,
- # self.property_name, self.base.Get(self.property_name))
+ if DEBUG:
+ print "ref base: %s, prop: %s, getresult: %s"%(self.base,
+ self.property_name, 'self.base.Get(self.property_name)')
return self.base.Get(self.property_name)
def PutValue(self, w, ctx):
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 19 13:52:11 2007
@@ -37,6 +37,7 @@
js_int.run(load_source(codepiece))
except ThrowException, excpt:
l.append("uncaught exception: "+str(excpt.exception))
+ print l, assval
assert l == assval
def assert_result(self, code, result):
@@ -391,7 +392,7 @@
print(Math.floor(3.2))
print(null)
print(-z)
- """, ['10', '2', 'false', '3', '', '-2'])
+ """, ['10', '2', 'false', '3', 'NaN', 'inf', '-inf', '3', '', '-2'])
def test_globalproperties(self):
self.assert_prints( """
@@ -435,4 +436,11 @@
for(i=0; i<3; i++){
print(x[i]);
}
- """, ['1', '2', '3'])
\ No newline at end of file
+ """, ['1', '2', '3'])
+
+ def test_array_length(self):
+ self.assert_prints("""
+ var testcases = new Array();
+ var tc = testcases.length;
+ print('tc'+tc)
+ """, ['tc0'])
\ No newline at end of file
More information about the Pypy-commit
mailing list