1.1 --- a/rsvp.py Sat May 23 01:46:46 2009 +0200
1.2 +++ b/rsvp.py Sat May 23 02:46:04 2009 +0200
1.3 @@ -121,22 +121,31 @@
1.4
1.5 # Constants.
1.6
1.7 - self.attr_error = objlist.access("__builtins__", "AttributeError").get_value().location
1.8 - self.type_error = objlist.access("__builtins__", "TypeError").get_value().location
1.9 - self.index_error = objlist.access("__builtins__", "IndexError").get_value().location
1.10 + cls = self._get_class("__builtins__", "AttributeError")
1.11 + self.attr_error = cls.location
1.12 + self.attr_error_instance = cls.instance_template_location
1.13 + cls = self._get_class("__builtins__", "TypeError")
1.14 + self.type_error = cls.location
1.15 + self.type_error_instance = cls.instance_template_location
1.16 + cls = self._get_class("__builtins__", "IndexError")
1.17 + self.index_error = cls.location
1.18 + self.index_error_instance = cls.instance_template_location
1.19
1.20 # Native class constants.
1.21
1.22 - cls = objlist.access("__builtins__", "int")
1.23 - self.int_class_location = cls and cls.get_value() and cls.get_value().location
1.24 - self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
1.25 - cls = objlist.access("__builtins__", "list")
1.26 - self.list_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
1.27 + cls = self._get_class("__builtins__", "int")
1.28 + self.int_class = cls.location
1.29 + self.int_instance = cls.instance_template_location
1.30 + cls = self._get_class("__builtins__", "list")
1.31 + self.list_instance = cls.instance_template_location
1.32
1.33 # Debugging attributes.
1.34
1.35 self.breakpoints = set()
1.36
1.37 + def _get_class(self, module, name):
1.38 + return self._objlist.access(module, name).get_value()
1.39 +
1.40 # Debugging methods.
1.41
1.42 def dump(self):
1.43 @@ -245,6 +254,14 @@
1.44 except EmptyPCStack:
1.45 pass
1.46
1.47 + print "Execution terminated",
1.48 + if self.exception is not None:
1.49 + context, ref = self.exception
1.50 + print "with exception:", self.load(ref)
1.51 + print "at address:", self.load(ref + 1)
1.52 + else:
1.53 + print "successfully."
1.54 +
1.55 def execute(self):
1.56
1.57 "Execute code in the memory at the current PC address."
1.58 @@ -395,7 +412,8 @@
1.59 else:
1.60 self.value = self.load(ref + offset)
1.61 else:
1.62 - self.exception = self.attr_error
1.63 + exc = self._MakeObject(2, self.attr_error_instance)
1.64 + self.exception = exc, exc
1.65 return self.RaiseException()
1.66
1.67 def LoadAttrIndexContext(self):
1.68 @@ -407,7 +425,8 @@
1.69 loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute
1.70 self.value = ref, loaded_ref
1.71 else:
1.72 - self.exception = self.attr_error
1.73 + exc = self._MakeObject(2, self.attr_error_instance)
1.74 + self.exception = exc, exc
1.75 return self.RaiseException()
1.76
1.77 def LoadAttrIndexContextCond(self):
1.78 @@ -422,7 +441,8 @@
1.79 else:
1.80 self.value = self.load(ref + offset)
1.81 else:
1.82 - self.exception = self.attr_error
1.83 + exc = self._MakeObject(2, self.attr_error_instance)
1.84 + self.exception = exc, exc
1.85 return self.RaiseException()
1.86
1.87 def StoreAttrIndex(self):
1.88 @@ -432,12 +452,14 @@
1.89 attr_index, class_attr, offset = element
1.90 if attr_index == self.operand:
1.91 if class_attr:
1.92 - self.exception = self.type_error
1.93 + exc = self._MakeObject(2, self.type_error_instance)
1.94 + self.exception = exc, exc
1.95 return self.RaiseException()
1.96 else:
1.97 self.save(ref + offset, self.source)
1.98 else:
1.99 - self.exception = self.attr_error
1.100 + exc = self._MakeObject(2, self.attr_error_instance)
1.101 + self.exception = exc, exc
1.102 return self.RaiseException()
1.103
1.104 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden.
1.105 @@ -468,7 +490,8 @@
1.106 if param_index == self.operand:
1.107 self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated
1.108 else:
1.109 - self.exception = self.type_error
1.110 + exc = self._MakeObject(2, self.type_error_instance)
1.111 + self.exception = exc, exc
1.112 return self.RaiseException()
1.113
1.114 def LoadCallable(self):
1.115 @@ -527,21 +550,24 @@
1.116 if nlocals > 0:
1.117 self_context, self_ref = self.frame_stack[frame + 1]
1.118 if not self._CheckInstance(self_ref, context_context):
1.119 - raise Exception, "CheckFrame %r (%r vs. %r)" % (self.operand, self.load(self_ref), self.load(context_context))
1.120 - #self.exception = self.type_error
1.121 - #return self.RaiseException()
1.122 + #raise Exception, "CheckFrame %r (%r vs. %r)" % (self.operand, self.load(self_ref), self.load(context_context))
1.123 + exc = self._MakeObject(2, self.type_error_instance)
1.124 + self.exception = exc, exc
1.125 + return self.RaiseException()
1.126 else:
1.127 - raise Exception, "CheckFrame %r (no self argument)" % self.operand
1.128 - #self.exception = self.type_error
1.129 - #return self.RaiseException()
1.130 + #raise Exception, "CheckFrame %r (no self argument)" % self.operand
1.131 + exc = self._MakeObject(2, self.type_error_instance)
1.132 + self.exception = exc, exc
1.133 + return self.RaiseException()
1.134
1.135 # Test the frame size.
1.136 # NOTE: Raise a proper exception here.
1.137
1.138 if not ((nargs - ndefaults) <= nlocals and (nlocals <= nargs or has_star)):
1.139 - raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, nlocals, nargs)
1.140 - #self.exception = self.type_error
1.141 - #return self.RaiseException()
1.142 + #raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, nlocals, nargs)
1.143 + exc = self._MakeObject(2, self.type_error_instance)
1.144 + self.exception = exc, exc
1.145 + return self.RaiseException()
1.146
1.147 def FillDefaults(self):
1.148 # NOTE: Make the get_operand method of the instruction provide the
1.149 @@ -617,6 +643,9 @@
1.150 self.exception = self.value[1]
1.151
1.152 def RaiseException(self):
1.153 + # NOTE: Adding the program counter as the first attribute.
1.154 + self.save(self.exception[1] + 1, self.pc)
1.155 + # Jumping to the current handler.
1.156 return self.handler_stack[-1]
1.157
1.158 def PushHandler(self):
1.159 @@ -696,8 +725,9 @@
1.160
1.161 # Test operand suitability.
1.162
1.163 - if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location):
1.164 - self.exception = self.type_error
1.165 + if not self._CheckInstance(left, self.int_class) and self._CheckInstance(right, self.int_class):
1.166 + exc = self._MakeObject(2, self.type_error_instance)
1.167 + self.exception = exc, exc
1.168 return self.RaiseException()
1.169
1.170 # NOTE: Assume single location for data.
1.171 @@ -707,7 +737,7 @@
1.172
1.173 # Make a new object.
1.174
1.175 - addr = self._MakeObject(2, self.int_instance_location)
1.176 + addr = self._MakeObject(2, self.int_instance)
1.177
1.178 # Store the result.
1.179 # NOTE: The data is considered ready to use.
1.180 @@ -728,8 +758,9 @@
1.181
1.182 # Test operand suitability.
1.183
1.184 - if not self._CheckInstance(left, self.int_class_location):
1.185 - self.exception = self.type_error
1.186 + if not self._CheckInstance(left, self.int_class):
1.187 + exc = self._MakeObject(2, self.type_error_instance)
1.188 + self.exception = exc, exc
1.189 return self.RaiseException()
1.190
1.191 # NOTE: Assume single location for data.
1.192 @@ -753,8 +784,9 @@
1.193
1.194 # Test operand suitability.
1.195
1.196 - if not self._CheckInstance(left, self.int_class_location):
1.197 - self.exception = self.type_error
1.198 + if not self._CheckInstance(left, self.int_class):
1.199 + exc = self._MakeObject(2, self.type_error_instance)
1.200 + self.exception = exc, exc
1.201 return self.RaiseException()
1.202
1.203 # NOTE: Assume single location for data.
1.204 @@ -763,7 +795,7 @@
1.205
1.206 # Make a new object.
1.207
1.208 - addr = self._MakeObject(2, self.int_instance_location)
1.209 + addr = self._MakeObject(2, self.int_instance)
1.210
1.211 # Store the result.
1.212 # NOTE: The data is considered ready to use.
1.213 @@ -791,7 +823,7 @@
1.214 args_context, args = self.frame_stack[frame]
1.215 header = self.load(args)
1.216
1.217 - list = self._MakeObject(header.size, self.list_instance_location)
1.218 + list = self._MakeObject(header.size, self.list_instance)
1.219 for i in range(1, header.size):
1.220 self.save(list + i, self.load(args + i))
1.221
1.222 @@ -816,7 +848,8 @@
1.223 elif item_pos < 0 and item_pos >= -nelements:
1.224 item_pos = nelements + item_pos
1.225 else:
1.226 - self.exception = self.index_error
1.227 + exc = self._MakeObject(2, self.index_error_instance)
1.228 + self.exception = exc, exc
1.229 return self.RaiseException()
1.230
1.231 self.result = self.load(obj + 1 + item_pos)