Author: Stephan <[email protected]>
Branch:
Changeset: r178:7b65c9e29faf
Date: 2012-03-01 15:59 +0100
http://bitbucket.org/pypy/lang-js/changeset/7b65c9e29faf/
Log: pass arguments to builtins as array
diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -36,7 +36,7 @@
return t
def make_loadjs(interp):
- def f(this, *args):
+ def f(this, args):
filename = str(args[0].ToString())
t = load_file(filename)
interp.run(t)
@@ -366,11 +366,17 @@
w_Boolean = W_BooleanConstructor(ctx)
w_Global.Put('Boolean', w_Boolean)
+ # 15.6.3
+ w_Boolean.Put('length', _w(1), flags = allon)
+
# 15.6.4
from js.jsobj import W_BooleanObject
w_BooleanPrototype = W_BooleanObject(False)
w_BooleanPrototype._prototype_ = W__Object._prototype_
+ # 15.6.3.1
+ w_Boolean.Put('prototype', w_BooleanPrototype, flags = allon)
+
# 15.6.4.1
w_BooleanPrototype.Put('constructor', w_Boolean)
@@ -378,6 +384,9 @@
# 15.6.4.2
put_native_function(w_BooleanPrototype, 'toString',
boolean_builtins.to_string)
+ # 15.6.4.3
+ put_native_function(w_BooleanPrototype, 'valueOf',
boolean_builtins.value_of)
+
# 15.6.3.1
W_BooleanObject._prototype_ = w_BooleanPrototype
@@ -598,6 +607,8 @@
put_native_function(w_Global, 'unescape', global_builtins.unescape)
+ put_native_function(w_Global, 'version', global_builtins.version)
+
w_Global.Put('this', w_Global)
## debugging
diff --git a/js/builtins_array.py b/js/builtins_array.py
--- a/js/builtins_array.py
+++ b/js/builtins_array.py
@@ -1,15 +1,13 @@
-from js.jsobj import isnull_or_undefined, _w
+from js.jsobj import isnull_or_undefined, _w, w_Undefined
# 15.4.4.7
-def push(this, *args):
- from collections import deque
+def push(this, args):
o = this.ToObject()
lenVal = o.Get('length')
n = lenVal.ToUInt32()
- items = deque(args)
- while(items):
- e = items.popleft()
+ for item in args:
+ e = item
o.Put(str(n), e)
n = n + 1
@@ -18,16 +16,16 @@
return o
# 15.4.4.2
-def to_string(this, *args):
+def to_string(this, args):
array = this.ToObject()
func = array.Get('join')
if func.IsCallable():
- return func.Call([], this = this)
+ return func.Call(this = this).ToString()
else:
- return object_to_string(this)
+ return this.ToString()
# 15.4.4.5
-def join(this, *args):
+def join(this, args):
o = this.ToObject()
lenVal = o.Get('length')
length = lenVal.ToUInt32()
@@ -60,7 +58,7 @@
return r
# 15.4.4.6
-def pop(this, *args):
+def pop(this, args):
o = this.ToObject()
lenVal = o.Get('length')
l = lenVal.ToUInt32()
@@ -77,16 +75,16 @@
return element
# 15.4.4.8
-def reverse(this, *args):
+def reverse(this, args):
o = this.ToObject()
- length = o.Get('lenght').ToUInt32()
+ length = o.Get('length').ToUInt32()
import math
- middle = math.floor(lenght/2)
+ middle = math.floor(length/2)
lower = 0
while lower != middle:
- upper = lenght - lower - 1
+ upper = length - lower - 1
lowerP = str(lower)
upperP = str(upper)
lowerValue = o.Get(lowerP)
diff --git a/js/builtins_boolean.py b/js/builtins_boolean.py
--- a/js/builtins_boolean.py
+++ b/js/builtins_boolean.py
@@ -1,4 +1,6 @@
# 15.6.4.2
-def to_string(this, *args):
- # TODO throw type error
+def to_string(this, args):
+
+def value_of(this, args):
+ return this.ToBoolean()
return this.ToString()
diff --git a/js/builtins_date.py b/js/builtins_date.py
--- a/js/builtins_date.py
+++ b/js/builtins_date.py
@@ -1,3 +1,3 @@
# 15.9.5.9
-def get_time(this, *args):
+def get_time(this, args):
return this
diff --git a/js/builtins_function.py b/js/builtins_function.py
--- a/js/builtins_function.py
+++ b/js/builtins_function.py
@@ -1,38 +1,40 @@
from js.jsobj import isnull_or_undefined
-def to_string(this, *args):
+def to_string(this, args):
return this.ToString()
-def empty(this, *args):
+def empty(this, args):
from js.jsobj import w_Undefined
return w_Undefined
# 15.3.4.4 Function.prototype.call
-def call(this, *args):
- if len(args) >= 1:
- if isnull_or_undefined(args[0]):
- thisArg = self.ctx.get_global()
- else:
- thisArg = args[0]
- callargs = args[1:]
- else:
- thisArg = self.ctx.get_global()
- callargs = []
- return this.Call(callargs, this = thisArg)
+def call(this, args):
+ pass
+ #if len(args) >= 1:
+ #if isnull_or_undefined(args[0]):
+ #thisArg = this.ctx.get_global()
+ #else:
+ #thisArg = args[0]
+ #callargs = args[1:]
+ #else:
+ #thisArg = this.ctx.get_global()
+ #callargs = []
+ #return this.Call(callargs, this = thisArg)
# 15.3.4.3 Function.prototype.apply (thisArg, argArray)
-def apply(this, *args):
- try:
- if isnull_or_undefined(args[0]):
- thisArg = self.ctx.get_global()
- else:
- thisArg = args[0].ToObject()
- except IndexError:
- thisArg = self.ctx.get_global()
+def apply(this, args):
+ pass
+ #try:
+ #if isnull_or_undefined(args[0]):
+ #thisArg = this.ctx.get_global()
+ #else:
+ #thisArg = args[0].ToObject()
+ #except IndexError:
+ #thisArg = this.ctx.get_global()
- try:
- arrayArgs = args[1]
- callargs = arrayArgs.tolist()
- except IndexError:
- callargs = []
- return this.Call(callargs, this=thisArg)
+ #try:
+ #arrayArgs = args[1]
+ #callargs = arrayArgs.tolist()
+ #except IndexError:
+ #callargs = []
+ #return this.Call(callargs, this=thisArg)
diff --git a/js/builtins_global.py b/js/builtins_global.py
--- a/js/builtins_global.py
+++ b/js/builtins_global.py
@@ -1,13 +1,15 @@
from pypy.rlib.rfloat import NAN, INFINITY, isnan, isinf
+from js.jsobj import W_String
+from js.execution import JsTypeError
# 15.1.2.4
-def is_nan(this, *args):
+def is_nan(this, args):
if len(args) < 1:
return True
return isnan(args[0].ToNumber())
# 15.1.2.5
-def is_finite(this, *args):
+def is_finite(this, args):
if len(args) < 1:
return True
n = args[0].ToNumber()
@@ -17,7 +19,7 @@
return True
# 15.1.2.2
-def parse_int(this, *args):
+def parse_int(this, args):
if len(args) < 1:
return NAN
s = args[0].ToString().strip(" ")
@@ -37,7 +39,7 @@
return n
# 15.1.2.3
-def parse_float(this, *args):
+def parse_float(this, args):
if len(args) < 1:
return NAN
s = args[0].ToString().strip(" ")
@@ -47,20 +49,20 @@
n = NAN
return n
-def alert(this, *args):
+def alert(this, args):
pass
def writer(x):
print x
-def printjs(this, *args):
+def printjs(this, args):
writer(",".join([i.ToString() for i in args]))
def _ishex(ch):
return ((ch >= 'a' and ch <= 'f') or (ch >= '0' and ch <= '9') or
(ch >= 'A' and ch <= 'F'))
-def unescape(this, *args):
+def unescape(this, args):
# XXX consider using StringBuilder here
res = []
w_string = args[0]
@@ -85,10 +87,13 @@
res.append(ch)
return ''.join(res)
-def pypy_repr(this, *args):
+def pypy_repr(this, args):
o = args[0]
return repr(o)
-def inspect(this, *args):
- import pdb; pdb.set_trace();
+def inspect(this, args):
+ pass
+ #import pdb; pdb.set_trace();
+def version(this, args):
+ return '1.0'
diff --git a/js/builtins_math.py b/js/builtins_math.py
--- a/js/builtins_math.py
+++ b/js/builtins_math.py
@@ -4,7 +4,7 @@
from pypy.rlib.rfloat import NAN, INFINITY, isnan, isinf
# 15.8.2.9
-def floor(this, *args):
+def floor(this, args):
if len(args) < 1:
return NAN
@@ -17,40 +17,41 @@
return pos
# 15.8.2.1
-def abs(this, *args):
+py_abs = abs
+def abs(this, args):
val = args[0]
- if isinstance(val, W_IntNumber):
- if val.ToInteger() > 0:
- return val # fast path
- return -val.ToInteger()
- return abs(args[0].ToNumber())
+ #if isinstance(val, W_IntNumber):
+ #if val.ToInteger() > 0:
+ #return val # fast path
+ #return -val.ToInteger()
+ return py_abs(val.ToNumber())
# 15.8.2.15
-def round(this, *args):
- return floor(this, *args)
+def round(this, args):
+ return floor(this, args)
# 15.8.2.13
-def pow(this, *args):
+def pow(this, args):
return math.pow(args[0].ToNumber(), args[1].ToNumber())
# 15.8.2.17
-def sqrt(this, *args):
+def sqrt(this, args):
return math.sqrt(args[0].ToNumber())
# 15.8.2.10
-def log(this, *args):
+def log(this, args):
return math.log(args[0].ToNumber())
# 15.8.2.11
py_min = min
-def min(this, *args):
+def min(this, args):
a = args[0].ToNumber()
b = args[1].ToNumber()
return py_min(a, b)
# 15.8.2.12
py_max = max
-def max(this, *args):
+def max(this, args):
a = args[0].ToNumber()
b = args[1].ToNumber()
return py_max(a, b)
@@ -60,7 +61,7 @@
_random = rrandom.Random(int(time.time()))
# 15.8.2.14
-def random(this, *args):
+def random(this, args):
return _random.random()
# 15.8.1.1
diff --git a/js/builtins_number.py b/js/builtins_number.py
--- a/js/builtins_number.py
+++ b/js/builtins_number.py
@@ -1,4 +1,4 @@
# 15.7.4.2
-def to_string(this, *args):
+def to_string(this, args):
# TODO radix, see 15.7.4.2
return this.ToString()
diff --git a/js/builtins_object.py b/js/builtins_object.py
--- a/js/builtins_object.py
+++ b/js/builtins_object.py
@@ -1,5 +1,5 @@
-def to_string(this, *args):
+def to_string(this, args):
return "[object %s]" % (this.Class(), )
-def value_of(this, *args):
+def value_of(this, args):
return this
diff --git a/js/builtins_string.py b/js/builtins_string.py
--- a/js/builtins_string.py
+++ b/js/builtins_string.py
@@ -1,7 +1,9 @@
-from js.jsobj import _w
+from js.jsobj import _w, w_Undefined, W_String
+from pypy.rlib.rfloat import NAN, INFINITY, isnan
+from js.execution import ThrowException
# 15.5.3.2
-def from_char_code(this, *args):
+def from_char_code(this, args):
temp = []
for arg in args:
i = arg.ToInt32() % 65536 # XXX should be uint16
@@ -9,7 +11,7 @@
return ''.join(temp)
# 15.5.4.4
-def char_at(this, *args):
+def char_at(this, args):
string = this.ToString()
if len(args)>=1:
pos = args[0].ToInt32()
@@ -20,7 +22,7 @@
return string[pos]
#15.5.4.5
-def char_code_at(this, *args):
+def char_code_at(this, args):
string = this.ToString()
if len(args)>=1:
pos = args[0].ToInt32()
@@ -32,14 +34,14 @@
return ord(char)
#15.5.4.6
-def concat(this, *args):
+def concat(this, args):
string = this.ToString()
others = [obj.ToString() for obj in args]
string += ''.join(others)
return string
# 15.5.4.7
-def index_of(this, *args):
+def index_of(this, args):
string = this.ToString()
if len(args) < 1:
return -1
@@ -55,7 +57,7 @@
return string.find(substr, pos)
# 15.5.4.8
-def last_index_of(this, *args):
+def last_index_of(this, args):
string = this.ToString()
if len(args) < 1:
return -1
@@ -76,7 +78,7 @@
return string.rfind(substr, 0, endpos)
# 15.5.4.14
-def split(this, *args):
+def split(this, args):
string = this.ToString()
if len(args) < 1 or args[0] is w_Undefined:
@@ -101,7 +103,7 @@
return w_array
# 15.5.4.15
-def substring(this, *args):
+def substring(this, args):
string = this.ToString()
size = len(string)
if len(args) < 1:
@@ -119,12 +121,12 @@
return string[start:end]
# 15.5.4.16
-def to_lower_case(this, *args):
+def to_lower_case(this, args):
string = this.ToString()
return string.lower()
# 15.5.4.18
-def to_upper_case(this, *args):
+def to_upper_case(this, args):
string = this.ToString()
return string.upper()
diff --git a/js/js_interactive.py b/js/js_interactive.py
--- a/js/js_interactive.py
+++ b/js/js_interactive.py
@@ -11,7 +11,7 @@
from js.jsobj import W_String, ThrowException, w_Undefined, W_Boolean
from pypy.rlib.streamio import open_file_as_stream
-sys.setrecursionlimit(100)
+#sys.setrecursionlimit(100)
import code
sys.ps1 = 'js> '
@@ -34,18 +34,18 @@
DEBUG = False
-def debugjs(this, *args):
+def debugjs(this, args):
global DEBUG
DEBUG = not DEBUG
return W_Boolean(DEBUG)
-def tracejs(this, *args):
+def tracejs(this, args):
arguments = args
import pdb
pdb.set_trace()
return w_Undefined
-def quitjs(this, *args):
+def quitjs(this, args):
sys.exit(0)
class JSInterpreter(code.InteractiveConsole):
diff --git a/js/jscode.py b/js/jscode.py
--- a/js/jscode.py
+++ b/js/jscode.py
@@ -183,6 +183,9 @@
code = ''
params = []
+ def __init__(self):
+ pass
+
def run(self, ctx, args=[], this=None):
raise NotImplementedError
@@ -216,7 +219,7 @@
def _native_function(fn):
from js.jsobj import _w
def f(this, args):
- res = fn(this, *args)
+ res = fn(this, args)
return _w(res)
return f
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -21,11 +21,13 @@
pass
class W___Root(object):
- pass
+ #_settled_ = True
+ def __init__(self):
+ pass
class W__Root(W___Root):
- _settled_ = True
- _attrs_ = []
+ #_settled_ = True
+ #_attrs_ = []
_type_ = ''
def __str__(self):
@@ -59,8 +61,8 @@
return r_uint32(self.ToInteger())
class W_Root(W___Root):
- _settled_ = True
- _attrs_ = []
+ #_settled_ = True
+ #_attrs_ = []
def __init__(self):
pass
@@ -184,6 +186,7 @@
W__Root.__init__(self)
self._property_map = root_map()
self._property_values = []
+ #self._set_property('prototype', self._prototype_, DONT_ENUM |
DONT_DELETE)
def __repr__(self):
#keys = self._property_map.keys()
@@ -295,6 +298,7 @@
assert self.Prototype() is not self
return self.Prototype().Get(P) # go down the prototype chain
+ # 8.12.4
def CanPut(self, P):
if self._has_property(P):
if self._get_property_flags(P) & READ_ONLY: return False
@@ -305,7 +309,10 @@
assert self.Prototype() is not self
return self.Prototype().CanPut(P)
+ # 8.12.5
def Put(self, P, V, flags = 0):
+ if not self.CanPut(P): return
+
# TODO: ???
if self._has_property(P):
self._set_property_value(P, V)
@@ -313,7 +320,6 @@
self._set_property_flags(P, f)
return
- if not self.CanPut(P): return
self._set_property(P, V, flags)
def GetPropertyName(self):
@@ -346,12 +352,12 @@
def ToString(self):
return self.PrimitiveValue().ToString()
+ def ToNumber(self):
+ return self.PrimitiveValue().ToNumber()
+
class W_BooleanObject(W__PrimitiveObject):
_class_ = 'Boolean'
- def ToString(self):
- return self.PrimitiveValue().ToString()
-
class W_NumericObject(W__PrimitiveObject):
_class_ = 'Number'
@@ -482,8 +488,10 @@
# 15.5.2
class W_StringConstructor(W_BasicFunction):
def Call(self, args=[], this=None):
- assert False
- return w_Undefined
+ if len(args) >= 1:
+ return W_String(args[0].ToString())
+ else:
+ return W_String('')
def Construct(self, args=[]):
return self.Call(args).ToObject()
@@ -503,7 +511,7 @@
class W_DateConstructor(W_BasicFunction):
def Call(self, args=[], this=None):
import time
- return W_DateObject(int(time.time()*1000))
+ return W_DateObject(_w(int(time.time()*1000)))
# 15.7.2.1
def Construct(self, args=[]):
@@ -535,7 +543,7 @@
def __init__(self, callee, args):
W_BasicObject.__init__(self)
self._delete_property('prototype')
- self.Put('callee', callee)
+ #self.Put('callee', callee)
self.Put('length', W_IntNumber(len(args)))
for i in range(len(args)):
self.Put(str(i), args[i])
@@ -574,7 +582,7 @@
self._delete_property(key)
i += 1
- self.length = int(newlength)
+ self.length = intmask(newlength)
self._set_property_value('length', _w(self.length))
def Put(self, P, V, flags = 0):
@@ -640,6 +648,7 @@
_type_ = 'string'
def __init__(self, strval):
+ assert isinstance(strval, str)
W__Primitive.__init__(self)
self._strval_ = strval
@@ -717,10 +726,11 @@
return intmask(rffi.cast(rffi.UINT, n))
class W_FloatNumber(W_Number):
- _immutable_fields_ = ['_floatval_']
+ #_immutable_fields_ = ['_floatval_']
""" Number known to be a float
"""
def __init__(self, floatval):
+ assert isinstance(floatval, float)
W_Number.__init__(self)
self._floatval_ = float(floatval)
@@ -827,6 +837,9 @@
return w_True
return w_False
+from pypy.rlib.objectmodel import specialize
+
[email protected](0)
def _w(value):
if isinstance(value, W___Root):
return value
diff --git a/js/opcodes.py b/js/opcodes.py
--- a/js/opcodes.py
+++ b/js/opcodes.py
@@ -111,7 +111,7 @@
self.identifier = identifier
def eval(self, ctx):
- ctx.append(ctx.resolve_identifier(ctx, self.identifier))
+ ctx.append(ctx.resolve_identifier(self.identifier))
def __repr__(self):
return 'LOAD_VARIABLE "%s"' % (self.identifier,)
@@ -221,7 +221,7 @@
def eval(self, ctx):
try:
- var = ctx.resolve_identifier(ctx, self.name)
+ var = ctx.resolve_identifier(self.name)
ctx.append(W_String(var.type()))
except ThrowException:
ctx.append(W_String('undefined'))
@@ -259,7 +259,8 @@
op2 = ctx.pop().ToUInt32()
op1 = ctx.pop().ToUInt32()
# XXX check if it could fit into int
- ctx.append(W_FloatNumber(op1 >> (op2 & 0x1F)))
+ f = float(op1 >> (op2 & 0x1F))
+ ctx.append(W_FloatNumber(f))
class RSH(BaseBinaryBitwiseOp):
def eval(self, ctx):
diff --git a/js/test/test_interp.py b/js/test/test_interp.py
--- a/js/test/test_interp.py
+++ b/js/test/test_interp.py
@@ -19,8 +19,8 @@
def assertp(code, prints):
l = []
- from js import builtins
- builtins.writer = l.append
+ import js.builtins_global
+ js.builtins_global.writer = l.append
jsint = interpreter.Interpreter()
ctx = jsint.w_Global
try:
diff --git a/js/test/test_jscode.py b/js/test/test_jscode.py
--- a/js/test/test_jscode.py
+++ b/js/test/test_jscode.py
@@ -37,8 +37,8 @@
from js.jsexecution_context import make_global_context
from js.jsobj import _w
- def f(this, a, b):
- return a.ToInteger() + b.ToInteger()
+ def f(this, args):
+ return args[0].ToInteger() + args[1].ToInteger()
nf = Js_NativeFunction(f, 'foo')
assert nf.name == 'foo'
diff --git a/js/test/test_jsobj.py b/js/test/test_jsobj.py
--- a/js/test/test_jsobj.py
+++ b/js/test/test_jsobj.py
@@ -7,7 +7,7 @@
assert n.ToUInt32() == 0x80000000
def test_floatnumber():
- n = W_FloatNumber(0x80000000)
+ n = W_FloatNumber(float(0x80000000))
assert n.ToInt32() == -0x80000000
assert n.ToUInt32() == 0x80000000
@@ -133,7 +133,7 @@
ctx = make_global_context()
- def f(this, a, b):
+ def f(this, args):
return 1
nf = Js_NativeFunction(f)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit