# HG changeset patch # User Paul Boddie # Date 1106433297 -3600 # Node ID c05daf6bcacea1e37c4e52d97dc7752017aa5c37 # Parent 1afe376ec3fdb4799821449e2401af449e3dba34 Fixed the default values used in array initialisation. diff -r 1afe376ec3fd -r c05daf6bcace javaclass/bytecode.py --- a/javaclass/bytecode.py Sat Jan 22 23:34:27 2005 +0100 +++ b/javaclass/bytecode.py Sat Jan 22 23:34:57 2005 +0100 @@ -658,6 +658,21 @@ else: program.load_global(class_parts[-1]) +atypes_to_default_values = { + 4 : 0, # bool (NOTE: Should be False.) + 5 : u"", # char + 6 : 0.0, # float + 7 : 0.0, # double + 8 : 0, # byte + 9 : 0, # short + 10: 0, # int + 11: 0 # long +} + +def get_default_for_atype(atype): + global atypes_to_default_values + return atypes_to_default_values.get(atype) + # Bytecode conversion. class BytecodeReader: @@ -1101,28 +1116,30 @@ # NOTE: Does not raise NegativeArraySizeException. # NOTE: Not using the index to type the list/array. index = (arguments[0] << 8) + arguments[1] - self._newarray(program) - - def _newarray(self, program): - program.build_list(0) # Stack: count, list - program.rot_two() # Stack: list, count + type_name = self.class_file.constants[index - 1].get_python_name() + default_value = classfile.get_default_for_type(type_name) + self._newarray(program, type_name) + + def _newarray(self, program, default_value): + program.build_list(0) # Stack: count, list + program.rot_two() # Stack: list, count program.setup_loop() program.load_global("range") - program.load_const(0) # Stack: list, count, range, 0 - program.rot_three() # Stack: list, 0, count, range - program.rot_three() # Stack: list, range, 0, count - program.call_function(2) # Stack: list, range_list - program.get_iter() # Stack: list, iter - program.for_iter() # Stack: list, iter, value - program.pop_top() # Stack: list, iter - program.rot_two() # Stack: iter, list - program.dup_top() # Stack: iter, list, list - program.load_attr("append") # Stack: iter, list, append - program.load_const(None) # Stack: iter, list, append, None - program.call_function(1) # Stack: iter, list, None - program.pop_top() # Stack: iter, list - program.rot_two() # Stack: list, iter - program.end_loop() # Back to for_iter above + program.load_const(0) # Stack: list, count, range, 0 + program.rot_three() # Stack: list, 0, count, range + program.rot_three() # Stack: list, range, 0, count + program.call_function(2) # Stack: list, range_list + program.get_iter() # Stack: list, iter + program.for_iter() # Stack: list, iter, value + program.pop_top() # Stack: list, iter + program.rot_two() # Stack: iter, list + program.dup_top() # Stack: iter, list, list + program.load_attr("append") # Stack: iter, list, append + program.load_const(default_value) # Stack: iter, list, append, default + program.call_function(1) # Stack: iter, list, default + program.pop_top() # Stack: iter, list + program.rot_two() # Stack: list, iter + program.end_loop() # Back to for_iter above def areturn(self, arguments, program): program.return_value() @@ -1783,7 +1800,9 @@ index = (arguments[0] << 8) + arguments[1] dimensions = arguments[2] # Stack: count1, ..., countN-1, countN - self._newarray(program) # Stack: count1, ..., countN-1, list + type_name = self.class_file.constants[index - 1].get_python_name() + default_value = classfile.get_default_for_type(type_name) + self._newarray(program, default_value) # Stack: count1, ..., countN-1, list for dimension in range(1, dimensions): program.rot_two() # Stack: count1, ..., list, countN-1 program.build_list(0) # Stack: count1, ..., list, countN-1, new-list @@ -1830,8 +1849,10 @@ def newarray(self, arguments, program): # NOTE: Does not raise NegativeArraySizeException. - # NOTE: Not using the arguments to type the list/array. - self._newarray(program) + # NOTE: Not completely using the arguments to type the list/array. + atype = arguments[0] + default_value = get_default_for_atype(atype) + self._newarray(program, default_value) def nop(self, arguments, program): pass diff -r 1afe376ec3fd -r c05daf6bcace javaclass/classfile.py --- a/javaclass/classfile.py Sat Jan 22 23:34:27 2005 +0100 +++ b/javaclass/classfile.py Sat Jan 22 23:34:57 2005 +0100 @@ -48,6 +48,19 @@ "[" : "list" } +type_names_to_default_values = { + "int" : 0, + "str" : u"", + "float" : 0.0, + "object" : None, + "bool" : 0, # NOTE: Should be False. + "list" : [] + } + +def get_default_for_type(type_name): + global type_names_to_default_values + return type_names_to_default_values.get(type_name) + PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, SUPER, SYNCHRONIZED, VOLATILE, TRANSIENT, NATIVE, INTERFACE, ABSTRACT, STRICT = \ 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800