~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/translator/js/jts.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" JavaScript type system
 
2
"""
 
3
 
 
4
from pypy.rpython.ootypesystem import ootype
 
5
from pypy.rpython.lltypesystem import lltype
 
6
from pypy.translator.cli import oopspec
 
7
 
 
8
from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
 
9
from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong, Primitive
 
10
from pypy.rpython.lltypesystem.lltype import Char, UniChar
 
11
from pypy.rpython.ootypesystem.ootype import String, _string, List, StaticMethod
 
12
from pypy.rlib.objectmodel import Symbolic
 
13
 
 
14
from pypy.translator.js.log import log
 
15
 
 
16
from types import FunctionType
 
17
from pypy.rpython.extfunc import is_external
 
18
 
 
19
try:
 
20
    set
 
21
except NameError:
 
22
    from sets import Set as set
 
23
 
 
24
class JTS(object):
 
25
    """ Class implementing JavaScript type system
 
26
    calls with mapping similiar to cts
 
27
    """
 
28
    def __init__(self, db):
 
29
        self.db = db
 
30
    
 
31
    #def __class(self, name):
 
32
    #    return name.replace(".", "_")
 
33
 
 
34
    def escape_name(self, name):
 
35
        return name.replace('.', '_')
 
36
    
 
37
    def llvar_to_cts(self, var):
 
38
        return 'var ', var.name
 
39
    
 
40
    def lltype_to_cts(self, t):
 
41
        if isinstance(t, ootype.Instance):
 
42
            self.db.pending_class(t)
 
43
            return self.escape_name(t._name)
 
44
        elif isinstance(t, ootype.List):
 
45
            return "Array"
 
46
        elif isinstance(t, lltype.Primitive):
 
47
            return "var"
 
48
        elif isinstance(t, ootype.Record):
 
49
            return "Object"
 
50
        elif isinstance(t, ootype.String.__class__):
 
51
            return '""'
 
52
        elif isinstance(t, ootype.Dict):
 
53
            return "Object"
 
54
        elif isinstance(t, ootype.DictItemsIterator):
 
55
            return "Object"
 
56
        elif t is ootype.StringBuilder:
 
57
            return "StringBuilder"
 
58
        #return "var"
 
59
        raise NotImplementedError("Type %r" % (t,))
 
60
    
 
61
    def graph_to_signature(self, graph, is_method = False, func_name = None):
 
62
        func_name = func_name or self.db.get_uniquename(graph,graph.name)
 
63
        
 
64
        args = [arg for arg in graph.getargs() if
 
65
                arg.concretetype is not ootype.Void]
 
66
        if is_method:
 
67
            args = args[1:]
 
68
 
 
69
        return func_name,args
 
70
    
 
71
    def method_signature(self, obj, name):
 
72
        # TODO: use callvirt only when strictly necessary
 
73
        if isinstance(obj, ootype.Instance):
 
74
            owner, meth = obj._lookup(name)
 
75
            METH = meth._TYPE
 
76
            return obj._name, METH.ARGS
 
77
        elif isinstance(obj, ootype.BuiltinType):
 
78
            meth = oopspec.get_method(obj, name)
 
79
            class_name = self.lltype_to_cts(obj)
 
80
            return class_name,meth.ARGS
 
81
        else:
 
82
            assert False
 
83
    
 
84
    def obj_name(self, obj):
 
85
        return self.lltype_to_cts(obj)
 
86
    
 
87
    def primitive_repr(self, _type, v):
 
88
        if _type is Bool:
 
89
            if v == False:
 
90
                val = 'false'
 
91
            else:
 
92
                val = 'true'
 
93
        elif _type is Void:
 
94
            val = 'undefined'
 
95
        elif isinstance(_type,String.__class__):
 
96
            val = '%r'%v._str
 
97
        elif isinstance(_type,List):
 
98
            # FIXME: It's not ok to use always empty list
 
99
            val = "[]"
 
100
        elif isinstance(_type,StaticMethod):
 
101
            if hasattr(v, 'graph') and not is_external(v):
 
102
                self.db.pending_function(v.graph)
 
103
            else:
 
104
                self.db.pending_abstract_function(v)
 
105
            val = v._name
 
106
            val = val.replace('.', '_')
 
107
            if val == '?':
 
108
                val = 'undefined'
 
109
        elif _type is UniChar or _type is Char:
 
110
            #log("Constant %r"%v)
 
111
            s = repr(v)
 
112
            if s.startswith('u'):
 
113
                s = s[1:]
 
114
            if s != "'\''":
 
115
                s.replace("'", '"')
 
116
            val = s
 
117
        elif isinstance(v, Symbolic):
 
118
            val = v.expr
 
119
        elif isinstance(_type, Primitive):
 
120
            #log("Type: %r"%_type)
 
121
            val = str(v)
 
122
        else:
 
123
            assert False, "Unknown constant %r"%_type
 
124
            val = str(v)
 
125
        return val
 
126
    
 
127
    #def lltype_to_cts(self, t, include_class=True):
 
128
    #    return 'var'
 
129
##        if isinstance(t, ootype.Instance):
 
130
##            self.db.pending_class(t)
 
131
##            return self.__class(t._name, include_class)
 
132
##        elif isinstance(t, ootype.Record):
 
133
##            name = self.db.pending_record(t)
 
134
##            return self.__class(name, include_class)
 
135
##        elif isinstance(t, ootype.StaticMethod):
 
136
##            return 'void' # TODO: is it correct to ignore StaticMethod?
 
137
##        elif isinstance(t, ootype.List):
 
138
##            item_type = self.lltype_to_cts(t._ITEMTYPE)
 
139
##            return self.__class(PYPY_LIST % item_type, include_class)
 
140
##        elif isinstance(t, ootype.Dict):
 
141
##            key_type = self.lltype_to_cts(t._KEYTYPE)
 
142
##            value_type = self.lltype_to_cts(t._VALUETYPE)
 
143
##            return self.__class(PYPY_DICT % (key_type, value_type), include_class)
 
144
##        elif isinstance(t, ootype.DictItemsIterator):
 
145
##            key_type = self.lltype_to_cts(t._KEYTYPE)
 
146
##            value_type = self.lltype_to_cts(t._VALUETYPE)
 
147
##            return self.__class(PYPY_DICT_ITEMS_ITERATOR % (key_type, value_type), include_class)
 
148
##
 
149
##        return _get_from_dict(_lltype_to_cts, t, 'Unknown type %s' % t)