object.py 1.57 KB
Newer Older
1 2
from som.vmobjects.abstract_object import AbstractObject

3

4
class Object(AbstractObject):
5 6

    _immutable_fields_ = ["_class", "_fields"]
7 8
    
    # Static field indices and number of object fields
9
    NUMBER_OF_OBJECT_FIELDS = 0
10

11 12 13
    def __init__(self, nilObject, number_of_fields = -1, obj_class = None):
        num_fields = (number_of_fields if number_of_fields != -1
                      else self._get_default_number_of_fields())
14
        self._fields = [nilObject] * num_fields
15
        self._class = obj_class or nilObject
16

17
    def get_class(self, universe):
18
        return self._class
19 20

    def set_class(self, value):
21
        self._class = value
22 23 24

    def get_field_name(self, index):
        # Get the name of the field with the given index
25
        return self._class.get_instance_field_name(index)
26 27 28

    def get_field_index(self, name):
        # Get the index for the field with the given name
Stefan Marr's avatar
Stefan Marr committed
29
        return self._class.lookup_field_index(name)
30 31 32

    def get_number_of_fields(self):
        # Get the number of fields in this object
33
        return len(self._fields)
34 35 36 37 38 39 40

    def _get_default_number_of_fields(self):
        # Return the default number of fields in an object
        return self.NUMBER_OF_OBJECT_FIELDS
    
    def get_field(self, index):
        # Get the field with the given index
Tobias Pape's avatar
Tobias Pape committed
41
        assert isinstance(index, int)
42 43 44 45
        return self._fields[index]
  
    def set_field(self, index, value):
        # Set the field with the given index to the given value
Tobias Pape's avatar
Tobias Pape committed
46
        assert isinstance(index, int)
47
        assert isinstance(value, AbstractObject)
48
        self._fields[index] = value