4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "st-object.h"
26 #include "st-universe.h"
27 #include "st-behavior.h"
28 #include "st-small-integer.h"
29 #include "st-association.h"
32 #include "st-byte-array.h"
33 #include "st-symbol.h"
34 #include "st-object.h"
35 #include "st-character.h"
36 #include "st-unicode.h"
39 st_object_equal (st_oop object
, st_oop other
)
41 if (st_object_class (object
) == st_smi_class
)
42 return st_smi_equal (object
, other
);
44 if (st_object_class (object
) == st_float_class
)
45 return st_float_equal (object
, other
);
47 if (st_object_class (object
) == st_character_class
)
48 return st_character_equal (object
, other
);
50 if (st_object_class (object
) == st_association_class
)
51 return st_association_equal (object
, other
);
53 if (st_object_class (object
) == st_symbol_class
)
54 return st_symbol_equal (object
, other
);
56 if (st_object_class (object
) == st_byte_array_class
||
57 st_object_class (object
) == st_string_class
)
58 return st_byte_array_equal (object
, other
);
60 return object
== other
;
64 st_object_hash (st_oop object
)
66 if (st_object_class (object
) == st_smi_class
)
67 return st_smi_hash (object
);
69 if (st_object_class (object
) == st_byte_array_class
||
70 st_object_class (object
) == st_string_class
||
71 st_object_class (object
) == st_symbol_class
)
72 return st_byte_array_hash (object
);
74 if (st_object_class (object
) == st_float_class
)
75 return st_float_hash (object
);
77 if (st_object_class (object
) == st_character_class
)
78 return st_character_hash (object
);
80 if (st_object_class (object
) == st_association_class
)
81 return st_association_hash (object
);
83 return st_smi_value (ST_HEADER (object
)->hash
);
87 st_object_printString (st_oop object
)
92 class_name
= (char *) st_byte_array_bytes (ST_CLASS (st_object_class (object
))->name
);
95 if (st_object_is_smi (object
))
96 string
= st_strdup_printf ("%li", st_smi_value (object
));
99 else if (st_object_class (object
) == st_float_class
)
100 string
= st_strdup_printf ("%g", st_float_value (object
));
103 else if (st_object_is_string (object
))
104 string
= st_strdup_printf ("'%s'", (char *) st_byte_array_bytes (object
));
107 else if (st_object_is_symbol (object
))
108 string
= st_strdup_printf ("#%s", (char *) st_byte_array_bytes (object
));
111 else if (st_object_class (object
) == st_character_class
) {
112 char outbuf
[6] = { 0 };
113 st_unichar_to_utf8 (st_character_value (object
), outbuf
);
114 string
= st_strdup_printf ("$%s", outbuf
);
118 string
= st_strdup_printf ("%s", class_name
);
123 int st_current_hash
= 1;
126 st_object_initialize_header (st_oop object
, st_oop
class)
128 ST_HEADER (object
)->mark
= 0 | ST_MARK_TAG
;
129 ST_HEADER (object
)->hash
= st_smi_new (st_current_hash
++);
130 ST_HEADER (object
)->class = class;
131 st_object_set_format (object
, st_smi_value (ST_BEHAVIOR (class)->format
));
133 st_assert (st_object_format (object
) == (st_smi_value (ST_BEHAVIOR (class)->format
)));
137 st_object_initialize_body (st_oop object
, st_smi instance_size
)
141 fields
= ST_HEADER (object
)->fields
;
143 for (st_smi i
= 0; i
< instance_size
; i
++)
148 allocate (st_space
*space
, st_oop
class)
150 st_smi instance_size
;
153 instance_size
= st_smi_value (ST_BEHAVIOR (class)->instance_size
);
154 object
= st_space_allocate_object (space
, class, ST_SIZE_OOPS (struct st_header
) + instance_size
);
156 st_object_initialize_body (object
, instance_size
);
164 object_copy (st_oop object
)
168 st_smi instance_size
;
170 class = ST_HEADER (object
)->class;
171 instance_size
= st_smi_value (ST_BEHAVIOR (class)->instance_size
);
172 copy
= st_object_new (memory
->moving_space
, class);
174 st_oops_copy (ST_HEADER (copy
)->fields
,
175 ST_HEADER (object
)->fields
,
182 object_size (st_oop object
)
184 return (sizeof (struct st_header
) / sizeof (st_oop
)) + st_smi_value (ST_BEHAVIOR (ST_HEADER (object
)->class)->instance_size
);
188 object_contents (st_oop object
, struct contents
*contents
)
190 contents
->oops
= ST_HEADER (object
)->fields
;
191 contents
->size
= st_smi_value (ST_BEHAVIOR (ST_HEADER (object
)->class)->instance_size
);
195 st_object_descriptor (void)
197 static st_descriptor __descriptor
=
198 { .allocate
= allocate
,
199 .allocate_arrayed
= NULL
,
202 .contents
= object_contents
,
205 return & __descriptor
;