javaclass

Changeset

141:c05daf6bcace
2005-01-22 Paul Boddie raw files shortlog changelog graph Fixed the default values used in array initialisation.
javaclass/bytecode.py (file) javaclass/classfile.py (file)
     1.1 --- a/javaclass/bytecode.py	Sat Jan 22 23:34:27 2005 +0100
     1.2 +++ b/javaclass/bytecode.py	Sat Jan 22 23:34:57 2005 +0100
     1.3 @@ -658,6 +658,21 @@
     1.4      else:
     1.5          program.load_global(class_parts[-1])
     1.6  
     1.7 +atypes_to_default_values = {
     1.8 +    4 : 0,      # bool (NOTE: Should be False.)
     1.9 +    5 : u"",    # char
    1.10 +    6 : 0.0,    # float
    1.11 +    7 : 0.0,    # double
    1.12 +    8 : 0,      # byte
    1.13 +    9 : 0,      # short
    1.14 +    10: 0,      # int
    1.15 +    11: 0       # long
    1.16 +}
    1.17 +
    1.18 +def get_default_for_atype(atype):
    1.19 +    global atypes_to_default_values
    1.20 +    return atypes_to_default_values.get(atype)
    1.21 +
    1.22  # Bytecode conversion.
    1.23  
    1.24  class BytecodeReader:
    1.25 @@ -1101,28 +1116,30 @@
    1.26          # NOTE: Does not raise NegativeArraySizeException.
    1.27          # NOTE: Not using the index to type the list/array.
    1.28          index = (arguments[0] << 8) + arguments[1]
    1.29 -        self._newarray(program)
    1.30 -
    1.31 -    def _newarray(self, program):
    1.32 -        program.build_list(0)       # Stack: count, list
    1.33 -        program.rot_two()           # Stack: list, count
    1.34 +        type_name = self.class_file.constants[index - 1].get_python_name()
    1.35 +        default_value = classfile.get_default_for_type(type_name)
    1.36 +        self._newarray(program, type_name)
    1.37 +
    1.38 +    def _newarray(self, program, default_value):
    1.39 +        program.build_list(0)               # Stack: count, list
    1.40 +        program.rot_two()                   # Stack: list, count
    1.41          program.setup_loop()
    1.42          program.load_global("range")
    1.43 -        program.load_const(0)       # Stack: list, count, range, 0
    1.44 -        program.rot_three()         # Stack: list, 0, count, range
    1.45 -        program.rot_three()         # Stack: list, range, 0, count
    1.46 -        program.call_function(2)    # Stack: list, range_list
    1.47 -        program.get_iter()          # Stack: list, iter
    1.48 -        program.for_iter()          # Stack: list, iter, value
    1.49 -        program.pop_top()           # Stack: list, iter
    1.50 -        program.rot_two()           # Stack: iter, list
    1.51 -        program.dup_top()           # Stack: iter, list, list
    1.52 -        program.load_attr("append") # Stack: iter, list, append
    1.53 -        program.load_const(None)    # Stack: iter, list, append, None
    1.54 -        program.call_function(1)    # Stack: iter, list, None
    1.55 -        program.pop_top()           # Stack: iter, list
    1.56 -        program.rot_two()           # Stack: list, iter
    1.57 -        program.end_loop()          # Back to for_iter above
    1.58 +        program.load_const(0)               # Stack: list, count, range, 0
    1.59 +        program.rot_three()                 # Stack: list, 0, count, range
    1.60 +        program.rot_three()                 # Stack: list, range, 0, count
    1.61 +        program.call_function(2)            # Stack: list, range_list
    1.62 +        program.get_iter()                  # Stack: list, iter
    1.63 +        program.for_iter()                  # Stack: list, iter, value
    1.64 +        program.pop_top()                   # Stack: list, iter
    1.65 +        program.rot_two()                   # Stack: iter, list
    1.66 +        program.dup_top()                   # Stack: iter, list, list
    1.67 +        program.load_attr("append")         # Stack: iter, list, append
    1.68 +        program.load_const(default_value)   # Stack: iter, list, append, default
    1.69 +        program.call_function(1)            # Stack: iter, list, default
    1.70 +        program.pop_top()                   # Stack: iter, list
    1.71 +        program.rot_two()                   # Stack: list, iter
    1.72 +        program.end_loop()                  # Back to for_iter above
    1.73  
    1.74      def areturn(self, arguments, program):
    1.75          program.return_value()
    1.76 @@ -1783,7 +1800,9 @@
    1.77          index = (arguments[0] << 8) + arguments[1]
    1.78          dimensions = arguments[2]
    1.79          # Stack: count1, ..., countN-1, countN
    1.80 -        self._newarray(program)             # Stack: count1, ..., countN-1, list
    1.81 +        type_name = self.class_file.constants[index - 1].get_python_name()
    1.82 +        default_value = classfile.get_default_for_type(type_name)
    1.83 +        self._newarray(program, default_value)  # Stack: count1, ..., countN-1, list
    1.84          for dimension in range(1, dimensions):
    1.85              program.rot_two()               # Stack: count1, ..., list, countN-1
    1.86              program.build_list(0)           # Stack: count1, ..., list, countN-1, new-list
    1.87 @@ -1830,8 +1849,10 @@
    1.88  
    1.89      def newarray(self, arguments, program):
    1.90          # NOTE: Does not raise NegativeArraySizeException.
    1.91 -        # NOTE: Not using the arguments to type the list/array.
    1.92 -        self._newarray(program)
    1.93 +        # NOTE: Not completely using the arguments to type the list/array.
    1.94 +        atype = arguments[0]
    1.95 +        default_value = get_default_for_atype(atype)
    1.96 +        self._newarray(program, default_value)
    1.97  
    1.98      def nop(self, arguments, program):
    1.99          pass
     2.1 --- a/javaclass/classfile.py	Sat Jan 22 23:34:27 2005 +0100
     2.2 +++ b/javaclass/classfile.py	Sat Jan 22 23:34:57 2005 +0100
     2.3 @@ -48,6 +48,19 @@
     2.4      "[" : "list"
     2.5      }
     2.6  
     2.7 +type_names_to_default_values = {
     2.8 +    "int" : 0,
     2.9 +    "str" : u"",
    2.10 +    "float" : 0.0,
    2.11 +    "object" : None,
    2.12 +    "bool" : 0, # NOTE: Should be False.
    2.13 +    "list" : []
    2.14 +    }
    2.15 +
    2.16 +def get_default_for_type(type_name):
    2.17 +    global type_names_to_default_values
    2.18 +    return type_names_to_default_values.get(type_name)
    2.19 +
    2.20  PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL,  SUPER,  SYNCHRONIZED, VOLATILE, TRANSIENT, NATIVE, INTERFACE, ABSTRACT, STRICT = \
    2.21  0x0001, 0x0002,  0x0004,    0x0008, 0x0010, 0x0020, 0x0020,       0x0040,   0x0080,    0x0100, 0x0200,    0x0400,   0x0800
    2.22