[pypy-svn] r35846 - in pypy/dist/pypy/lang/js: . test
santagada at codespeak.net
santagada at codespeak.net
Mon Dec 18 14:26:37 CET 2006
Author: santagada
Date: Mon Dec 18 14:26:25 2006
New Revision: 35846
Added:
pypy/dist/pypy/lang/js/jsparser.py
- copied, changed from r35838, pypy/dist/pypy/lang/js/parser.py
pypy/dist/pypy/lang/js/reference.py
Removed:
pypy/dist/pypy/lang/js/parser.py
pypy/dist/pypy/lang/js/scope.py
Modified:
pypy/dist/pypy/lang/js/astgen.py
pypy/dist/pypy/lang/js/context.py
pypy/dist/pypy/lang/js/interpreter.py
pypy/dist/pypy/lang/js/jsobj.py
pypy/dist/pypy/lang/js/test/test_interp.py
pypy/dist/pypy/lang/js/test/test_parser.py
Log:
Lots of changes, new context and base object to support nested scopes and properprototyping
WARNING: Non working code for now.
Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py (original)
+++ pypy/dist/pypy/lang/js/astgen.py Mon Dec 18 14:26:25 2006
@@ -107,8 +107,6 @@
class Script(Node):
def __init__(self, nodes, var_decl, func_decl):
self.nodes = nodes
- import pdb
- #pdb.set_trace()
[scope_manager.add_variable(id.name, w_Undefined) for id in var_decl]
[scope_manager.add_variable(id.name, id) for id in func_decl]
self.var_decl = var_decl
@@ -159,6 +157,21 @@
def build_interpreter(d):
return from_dict(d)
+# FIXME: Continue the translation from if/elif to this dict map
+build_map = {'ARRAY_INIT':Array,
+ 'ASSIGN': Assign,
+ 'BLOCK': Block}
+
+def from_dict_map(d):
+ if d is None:
+ return d
+ try:
+ build_map[d['type']](d)
+ except KeyError,e:
+ raise NotImplementedError("Don't know how to handle %s" %(d['type'],))
+
+
+
def from_dict(d):
if d is None:
return d
Modified: pypy/dist/pypy/lang/js/context.py
==============================================================================
--- pypy/dist/pypy/lang/js/context.py (original)
+++ pypy/dist/pypy/lang/js/context.py Mon Dec 18 14:26:25 2006
@@ -1,23 +1,25 @@
class ExecutionContext(object):
-
- def __init__(self, parent = None):
- self.parent = parent
- self.locals = {}
- if parent is None:
- self.globals = {}
- else:
- self.globals = parent.globals
-
- def assign(self, name, value):
- if (name not in self.locals) and (name in self.globals):
- self.globals[name] = value
- else:
- self.locals[name] = value
-
- def access(self, name):
- if name in self.locals:
- return self.locals[name]
- elif name in self.globals:
- return self.globals[name]
- raise NameError("%s is not declared" % name)
+ def __init__(self):
+ self.scope = []
+ self.this = None
+ self.variable = None
+ self.property = Property('',w_Undefined) #Attribute flags for new vars
+
+ def get_identifier(self, identifier):
+ """docstring for get_identifier"""
+ pass
+
+
+
+
+class GlobalContext(ExecutionContext):
+ """docstring for GlobalContext"""
+ def __init__(self, global):
+ ExecutionContext.__init__()
+ self.scope.append(global)
+ self.this = global
+ self.variable = global
+
+
+
Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py (original)
+++ pypy/dist/pypy/lang/js/interpreter.py Mon Dec 18 14:26:25 2006
@@ -3,7 +3,6 @@
from pypy.lang.js.context import ExecutionContext
from pypy.lang.js.jsobj import W_Number, W_String, W_Object
from pypy.lang.js.jsobj import w_Undefined, W_Arguments, W_Boolean, NaN
-from pypy.lang.js.scope import scope_manager
def writer(x):
print x
@@ -17,6 +16,27 @@
self.exception = exception
self.args = self.exception
+class Interpreter(object):
+ """Creates a js interpreter"""
+ def __init__(self, script_source=None):
+ self.w_Object = W_Object() #creating Object
+ self.w_Global = W_Object()
+ w_Global.Prototype = w_Object
+ w_Global.Set('prototype', 'Object')
+ w_Global.Set('Object', w_Object)
+ self.global_context = GlobalContext(w_global)
+ if script_source is not none:
+ self.load_source(script_source)
+
+ def load_source(self, script_source):
+ """load a source script text to the interpreter"""
+ pass
+
+ def run(self):
+ """run the interpreter"""
+ pass
+
+
class __extend__(Array):
def call(self, context):
@@ -25,10 +45,7 @@
class __extend__(Assign):
def call(self, context):
- print context.locals.keys(), "|||||", context.globals
- print context.locals['this']
val = self.expr.call(context)
- print val
self.identifier.put(context,val)
class __extend__(Block):
@@ -93,9 +110,7 @@
if self.initialiser is not None:
context.assign(self.name, self.initialiser.call(context))
try:
- print "trying to access", self.name
value = context.access(self.name)
- print "value", value
return value
except NameError:
return scope_manager.get_variable(self.name)
@@ -179,9 +194,6 @@
obj.Class = 'Object'
#it should be undefined... to be completed
obj.dict_w['prototype'] = constructor.dict_w['prototype']
- #nctx = ExecutionContext(context)
- #nctx.assign('this',obj)
- #print nctx.locals.keys()
constructor.Call(context, this = obj)
return obj
@@ -203,9 +215,6 @@
w_expr = property.value.call(context).GetValue()
w_obj.Put(name, w_expr)
return w_obj
- #dict_w = {}
- #for property in self.properties:
- # dict_w[property.name
class __extend__(Plus):
def call(self, context=None):
Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py (original)
+++ pypy/dist/pypy/lang/js/jsobj.py Mon Dec 18 14:26:25 2006
@@ -5,6 +5,21 @@
INFDEF = 1e300 * 1e300
NaN = INFDEF/INFDEF
+# TODO: Use an integer for attributes
+class Properfty(object):
+ def __init__(self, name, value, DontDelete=False,
+ ReadOnly=False, DontEnum=False, Internal=False):
+ self.name = name
+ self.value = value
+ self.DontDelete = DontDelete
+ self.ReadOnly = ReadOnly
+ self.DontEnum = DontEnum
+ self.Internal = Internal
+
+def internal_property(name, value):
+ """return a internal property with the right attributes"""
+ return Property(name, value, True, True, True, True)
+
class W_Root(object):
def GetValue(self):
return self
@@ -27,75 +42,84 @@
def __repr__(self):
return "<%s(%s)>" % (self.__class__.__name__, str(self))
+class W_Primitive(W_Root):
+ """unifying parent for primitives"""
+ pass
+
class W_Object(W_Root):
- def __init__(self, dict_w, function=None):
- # string --> W_Root
- self.dict_w = dict_w
- # XXX: more stuff
- self.dict_w['toString'] = W_Builtin({}, self.w_string)
- # XXX A bit hairy here, we store here a Function, and Script
- # is a self.function.body
- self.dict_w['prototype'] = self
+ def __init__(self, function=None):
+ self.propdict = {}
+ self.propdict['toString'] = Property('toString',
+ W_Builtin(self.__str__)) # FIXME: Not working
+ self.propdict['prototype'] = Property('prototype', w_Undefined,
+ DontDelete=True)
+ self.Prototype = None
+ self.Class = "Object"
self.function = function
- #self.class_ = None
-
- def Call(self, context, args=[], this = None):
- if self.function:
- return self.function.body.call(context=context,
- args=args,
-
- params= self.function.params,
- this = this
- )
+ self.scope = []
+
+ def Call(self, context, args=[], this = None): # FIXME: Context-ng
+ if self.function is not none:
+ return self.function.body.call(context=context, args=args,
+ params=self.function.params,
+ this=this)
else:
print "returning common object"
- return W_Object({})
-
- def w_string(self):
- return W_String(str(self))
-
- def DefaultValue(self, hint):
- #if hint == "string":
- tostring_meth = self.Get("toString")
- if isinstance(tostring_meth, W_Object):
- return tostring_meth.Call(this=self)
- valueof_meth = self.Get("valueOf")
- if isinstance(valueof_meth, W_Object):
- retval = valueof_meth.Call(this=self)
- # XXX: check primitiveness of retval
- return retval
- return w_Undefined
-
- def ToPrimitive(self, hint=""):
- return self.DefaultValue(hint)
-
- def ToNumber(self):
- return self.ToPrimitive("number").ToNumber(hint="number")
-
- def ToString(self):
- return str(self.DefaultValue(hint="string"))
-
- def Get(self, name):
- if name in self.dict_w:
- return self.dict_w[name]
-
- return w_Undefined
-
- def CanPut(self, name):
+ return W_Object()
+
+ def Get(self, P):
+ if P in self.propdict: return self.propdict[P].value
+ if self.prototype is None: return w_Undefined
+ return self.prototype.Get(P) # go down the prototype chain
+
+ def CanPut(self, P):
+ if P in self.propdict:
+ if self.propdict[P].ReadOnly: return False
+ return True
+ if self.Prototype is None: return True
+ return self.Prototype.CanPut(P)
+
+ def Put(self, P, V):
+ if not self.CanPut(P): return
+ if P in self.propdict:
+ self.propdict[P].value = V
+ else:
+ self.propdict[P] = Property(P, V)
+
+ def HasProperty(self, P):
+ if P in self.propdict: return True
+ if self.Prototype is None: return False
+ return self.Prototype.HasProperty(P)
+
+ def Delete(P):
+ if P in self.propdict:
+ if self.propdict[P].DontDelete: return False
+ self.propdict.pop(P)
+ return True
return True
-
- def Put(self, name, w_obj):
- # INSANE - raise some exceptions in case of read only and such
- print "We are insane"
- if not self.CanPut(name):
- return # AAAAAAAAAAAAaaaaaaaaaaaa
- print name,"=", w_obj
- self.dict_w[name] = w_obj
-
+
+ def DefaultValue(self, hint):
+ def internal_def_value(tryone, trytwo):
+ t1 = self.Get(tryone)
+ if isinstance(t1, W_Object):
+ val = t1.Call(this=self)
+ if isinstance(val, W_Primitive):
+ return val
+ t2 = self.Get(trytwo)
+ if isinstance(t2, W_Object):
+ val = t2.Call(this=self)
+ if isinstance(val, W_Primitive):
+ return val
+ raise jsTypeError
+
+ if hint == "String":
+ internal_def_value("toString", "valueOf")
+ else: #suppose hint is "Number" dunno what to do otherwise
+ internal_def_value("valueOf", "toString")
+
def __str__(self):
- # INSANE
- return "[object Object]"
-
+ return "[object %s]"%(self.Class,)
+
class W_Arguments(W_Object):
pass
@@ -137,14 +161,12 @@
class W_String(W_Root):
def __init__(self, strval):
- # XXX: Should be unicode object
self.strval = strval
# def ToString(self):
# return self.strval
def __str__(self):
- # INSANE - should be like 'str' or so
return self.strval
def ToBoolean(self):
@@ -178,12 +200,12 @@
raise NotImplementedError("W_Reference.GetValue")
class W_Builtin(W_Object):
- def __init__(self, dict_w, internalfunction):
- self.dict_w = {}
- self.internalfunction = internalfunction
+ def __init__(self, builtinfunction):
+ W_Object.__init__()
+ self.builtinfunction = builtinfunction
- def Call(self, this=None):
- return self.internalfunction()
+ def Call(self, context, args=[], this = None):
+ return self.builtinfunction(*args)
class W_List(W_Root):
def __init__(self, list_w):
@@ -197,3 +219,12 @@
w_Undefined = W_Undefined()
w_Null = W_Null()
+
+def to_primitive(Value, PreferredType):
+ assert isinstance(Value, W_Root)
+ if isinstance(Value, W_Object):
+ return Value.DefaultValue(PreferredType)
+ return Value
+
+
+
\ No newline at end of file
Added: pypy/dist/pypy/lang/js/reference.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/js/reference.py Mon Dec 18 14:26:25 2006
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+# encoding: utf-8
+"""
+reference.py
+
+Created by Leonardo Santagada on 2006-12-16.
+"""
+
+class Reference(object):
+ """Reference Type"""
+ def __init__(self, propertyname, baseobject=None):
+ self.baseobject = baseobject
+ self.propertyname = propertyname
+
+def get_value(v):
+ if not type(v, Reference):
+ return v
+ if v.baseobject is None:
+ raise ReferenceError
+ return v.baseobject.get(v.propertyname)
+
+def put_value(v, w):
+ if not type(v, Reference):
+ raise ReferenceError
+ base = v.baseobject
+ if v.baseobject is None:
+ base = get_global() #gets the global object of js
+ base.put(v.propertyname, w)
\ No newline at end of file
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 Mon Dec 18 14:26:25 2006
@@ -1,7 +1,7 @@
from pypy.lang.js.astgen import *
from pypy.lang.js import interpreter
-from pypy.lang.js.parser import parse
+from pypy.lang.js.jsparser import parse
from pypy.lang.js.interpreter import ThrowException
from pypy.lang.js.jsobj import W_Number, W_Object
from pypy.lang.js.context import ExecutionContext
Modified: pypy/dist/pypy/lang/js/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_parser.py (original)
+++ pypy/dist/pypy/lang/js/test/test_parser.py Mon Dec 18 14:26:25 2006
@@ -1,4 +1,4 @@
-from pypy.lang.js.parser import read_js_output, JsSyntaxError, parse
+from pypy.lang.js.jsparser import read_js_output, JsSyntaxError, parse
from pypy.lang.js.test.test_interp import js_is_on_path
import py
More information about the Pypy-commit
mailing list