qse/ase/stx/object.c

247 lines
6.4 KiB
C
Raw Normal View History

2005-05-06 17:18:29 +00:00
/*
2007-03-22 11:19:28 +00:00
* $Id: object.c,v 1.41 2007-03-22 11:19:28 bacon Exp $
2005-05-06 17:18:29 +00:00
*/
2007-03-22 11:19:28 +00:00
#include <ase/stx/object.h>
#include <ase/stx/memory.h>
#include <ase/stx/symbol.h>
#include <ase/stx/class.h>
#include <ase/stx/misc.h>
2005-05-10 15:15:57 +00:00
2005-05-06 17:18:29 +00:00
/* n: number of instance variables */
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_alloc_word_object (
ase_stx_t* stx, const ase_word_t* data, ase_word_t nfields,
const ase_word_t* variable_data, ase_word_t variable_nfields)
2005-05-06 17:18:29 +00:00
{
2007-03-22 11:19:28 +00:00
ase_word_t idx, n;
ase_stx_word_object_t* obj;
2005-05-06 17:18:29 +00:00
2007-03-22 11:19:28 +00:00
ase_assert (stx->nil == ASE_STX_NIL);
2005-07-05 09:02:13 +00:00
/* bytes to allocated =
* (number of instance variables +
* number of variable instance variables) * word_size
2005-05-06 17:18:29 +00:00
*/
2005-07-05 09:02:13 +00:00
n = nfields + variable_nfields;
2007-03-22 11:19:28 +00:00
idx = ase_stx_memory_alloc (&stx->memory,
n * ase_sizeof(ase_word_t) + ase_sizeof(ase_stx_object_t));
2005-07-05 10:22:35 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed TODO: return a difference value OINDEX_INVALID */
2005-05-08 07:39:51 +00:00
2007-03-22 11:19:28 +00:00
idx = ASE_STX_TO_OINDEX(idx);
obj = ASE_STX_WORD_OBJECT(stx,idx);
2005-05-22 04:34:22 +00:00
obj->header.class = stx->nil;
2007-03-22 11:19:28 +00:00
obj->header.access = (n << 2) | ASE_STX_WORD_INDEXED;
2005-07-05 09:02:13 +00:00
2007-03-22 11:19:28 +00:00
if (variable_data == ASE_NULL) {
2005-07-05 11:15:51 +00:00
while (n > nfields) obj->data[--n] = stx->nil;
2005-07-05 06:26:33 +00:00
}
else {
2005-07-05 09:02:13 +00:00
while (n > nfields) {
n--; obj->data[n] = variable_data[n - nfields];
}
}
2007-03-22 11:19:28 +00:00
if (data == ASE_NULL) {
2005-07-05 09:02:13 +00:00
while (n > 0) obj->data[--n] = stx->nil;
}
else {
while (n > 0) {
n--; obj->data[n] = data[n];
}
2005-07-05 06:26:33 +00:00
}
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
return idx;
2005-05-06 17:18:29 +00:00
}
2005-05-08 07:39:51 +00:00
/* n: number of bytes */
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_alloc_byte_object (
ase_stx_t* stx, const ase_byte_t* data, ase_word_t n)
2005-05-06 17:18:29 +00:00
{
2007-03-22 11:19:28 +00:00
ase_word_t idx;
ase_stx_byte_object_t* obj;
2005-05-06 17:18:29 +00:00
2007-03-22 11:19:28 +00:00
ase_assert (stx->nil == ASE_STX_NIL);
2005-07-05 09:02:13 +00:00
2007-03-22 11:19:28 +00:00
idx = ase_stx_memory_alloc (
&stx->memory, n + ase_sizeof(ase_stx_object_t));
2005-05-08 10:31:25 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-08 07:39:51 +00:00
2007-03-22 11:19:28 +00:00
idx = ASE_STX_TO_OINDEX(idx);
obj = ASE_STX_BYTE_OBJECT(stx,idx);
2005-05-22 04:34:22 +00:00
obj->header.class = stx->nil;
2007-03-22 11:19:28 +00:00
obj->header.access = (n << 2) | ASE_STX_BYTE_INDEXED;
2005-07-04 11:32:41 +00:00
2007-03-22 11:19:28 +00:00
if (data == ASE_NULL) {
2005-07-04 11:32:41 +00:00
while (n-- > 0) obj->data[n] = 0;
}
2005-07-05 06:26:33 +00:00
else {
2005-07-04 11:32:41 +00:00
while (n-- > 0) obj->data[n] = data[n];
}
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
return idx;
2005-05-06 17:18:29 +00:00
}
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_alloc_char_object (
ase_stx_t* stx, const ase_char_t* str)
2005-05-06 17:18:29 +00:00
{
2007-03-22 11:19:28 +00:00
return (str == ASE_NULL)?
ase_stx_alloc_char_objectx (stx, ASE_NULL, 0):
ase_stx_alloc_char_objectx (stx, str, ase_strlen(str));
2005-05-23 15:51:03 +00:00
}
2005-07-05 09:02:13 +00:00
/* n: number of characters */
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_alloc_char_objectx (
ase_stx_t* stx, const ase_char_t* str, ase_word_t n)
2005-05-23 15:51:03 +00:00
{
2007-03-22 11:19:28 +00:00
ase_word_t idx;
ase_stx_char_object_t* obj;
2005-05-06 17:18:29 +00:00
2007-03-22 11:19:28 +00:00
ase_assert (stx->nil == ASE_STX_NIL);
2005-07-05 09:02:13 +00:00
2007-03-22 11:19:28 +00:00
idx = ase_stx_memory_alloc (&stx->memory,
(n + 1) * ase_sizeof(ase_char_t) + ase_sizeof(ase_stx_object_t));
2005-05-08 10:31:25 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-06 17:18:29 +00:00
2007-03-22 11:19:28 +00:00
idx = ASE_STX_TO_OINDEX(idx);
obj = ASE_STX_CHAR_OBJECT(stx,idx);
2005-05-22 04:34:22 +00:00
obj->header.class = stx->nil;
2007-03-22 11:19:28 +00:00
obj->header.access = (n << 2) | ASE_STX_CHAR_INDEXED;
obj->data[n] = ASE_T('\0');
2005-07-04 16:23:13 +00:00
2007-03-22 11:19:28 +00:00
if (str == ASE_NULL) {
while (n-- > 0) obj->data[n] = ASE_T('\0');
2005-07-04 16:23:13 +00:00
}
else {
while (n-- > 0) obj->data[n] = str[n];
}
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
return idx;
2005-05-06 17:18:29 +00:00
}
2005-05-08 13:45:51 +00:00
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_allocn_char_object (ase_stx_t* stx, ...)
2005-05-10 15:15:57 +00:00
{
2007-03-22 11:19:28 +00:00
ase_word_t idx, n = 0;
const ase_char_t* p;
ase_va_list ap;
ase_stx_char_object_t* obj;
2005-05-10 15:15:57 +00:00
2007-03-22 11:19:28 +00:00
ase_assert (stx->nil == ASE_STX_NIL);
2005-07-05 10:22:35 +00:00
2007-03-22 11:19:28 +00:00
ase_va_start (ap, stx);
while ((p = ase_va_arg(ap, const ase_char_t*)) != ASE_NULL) {
n += ase_strlen(p);
2005-05-10 15:15:57 +00:00
}
2007-03-22 11:19:28 +00:00
ase_va_end (ap);
2005-05-10 15:15:57 +00:00
2007-03-22 11:19:28 +00:00
idx = ase_stx_memory_alloc (&stx->memory,
(n + 1) * ase_sizeof(ase_char_t) + ase_sizeof(ase_stx_object_t));
2005-05-10 15:15:57 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed */
2007-03-22 11:19:28 +00:00
idx = ASE_STX_TO_OINDEX(idx);
obj = ASE_STX_CHAR_OBJECT(stx,idx);
2005-05-22 04:34:22 +00:00
obj->header.class = stx->nil;
2007-03-22 11:19:28 +00:00
obj->header.access = (n << 2) | ASE_STX_CHAR_INDEXED;
obj->data[n] = ASE_T('\0');
2005-05-10 15:15:57 +00:00
2007-03-22 11:19:28 +00:00
ase_va_start (ap, stx);
2005-05-10 15:15:57 +00:00
n = 0;
2007-03-22 11:19:28 +00:00
while ((p = ase_va_arg(ap, const ase_char_t*)) != ASE_NULL) {
while (*p != ASE_T('\0')) {
/*ASE_STX_CHAR_AT(stx,idx,n++) = *p++;*/
2005-05-22 04:34:22 +00:00
obj->data[n++] = *p++;
}
2005-05-10 15:15:57 +00:00
}
2007-03-22 11:19:28 +00:00
ase_va_end (ap);
2005-05-10 15:15:57 +00:00
return idx;
}
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_hash_object (ase_stx_t* stx, ase_word_t object)
2005-07-05 09:52:00 +00:00
{
2007-03-22 11:19:28 +00:00
ase_word_t hv;
2005-07-05 09:52:00 +00:00
2007-03-22 11:19:28 +00:00
if (ASE_STX_IS_SMALLINT(object)) {
ase_word_t tmp = ASE_STX_FROM_SMALLINT(object);
hv = ase_stx_hash(&tmp, ase_sizeof(tmp));
2005-07-05 09:52:00 +00:00
}
2007-03-22 11:19:28 +00:00
else if (ASE_STX_IS_CHAR_OBJECT(stx,object)) {
2005-07-19 12:08:04 +00:00
/* the additional null is not taken into account */
2007-03-22 11:19:28 +00:00
hv = ase_stx_hash (ASE_STX_DATA(stx,object),
ASE_STX_SIZE(stx,object) * ase_sizeof(ase_char_t));
2005-07-05 11:15:51 +00:00
}
2007-03-22 11:19:28 +00:00
else if (ASE_STX_IS_BYTE_OBJECT(stx,object)) {
hv = ase_stx_hash (
ASE_STX_DATA(stx,object), ASE_STX_SIZE(stx,object));
2005-07-05 09:52:00 +00:00
}
2005-07-05 11:15:51 +00:00
else {
2007-03-22 11:19:28 +00:00
ase_assert (ASE_STX_IS_WORD_OBJECT(stx,object));
hv = ase_stx_hash (ASE_STX_DATA(stx,object),
ASE_STX_SIZE(stx,object) * ase_sizeof(ase_word_t));
2005-07-05 09:52:00 +00:00
}
return hv;
}
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_instantiate (
ase_stx_t* stx, ase_word_t class, const void* data,
const void* variable_data, ase_word_t variable_nfields)
2005-07-04 16:23:13 +00:00
{
2007-03-22 11:19:28 +00:00
ase_stx_class_t* class_obj;
ase_word_t spec, nfields, new;
2005-07-04 16:23:13 +00:00
int indexable;
2007-03-22 11:19:28 +00:00
ase_assert (class != stx->class_smallinteger);
class_obj = (ase_stx_class_t*)ASE_STX_OBJECT(stx, class);
2005-07-04 16:23:13 +00:00
/* don't instantiate a metaclass whose instance must be
created in a different way */
/* TODO: maybe delete the following line */
2007-03-22 11:19:28 +00:00
ase_assert (class_obj->header.class != stx->class_metaclass);
ase_assert (ASE_STX_IS_SMALLINT(class_obj->spec));
2005-07-04 16:23:13 +00:00
2007-03-22 11:19:28 +00:00
spec = ASE_STX_FROM_SMALLINT(class_obj->spec);
nfields = (spec >> ASE_STX_SPEC_INDEXABLE_BITS);
indexable = spec & ASE_STX_SPEC_INDEXABLE_MASK;
2005-07-04 16:23:13 +00:00
2007-03-22 11:19:28 +00:00
if (indexable == ASE_STX_SPEC_BYTE_INDEXABLE) {
ase_assert (nfields == 0 && data == ASE_NULL);
new = ase_stx_alloc_byte_object(
2005-07-05 09:02:13 +00:00
stx, variable_data, variable_nfields);
2005-07-04 16:23:13 +00:00
}
2007-03-22 11:19:28 +00:00
else if (indexable == ASE_STX_SPEC_CHAR_INDEXABLE) {
ase_assert (nfields == 0 && data == ASE_NULL);
new = ase_stx_alloc_char_objectx(
2005-07-05 09:02:13 +00:00
stx, variable_data, variable_nfields);
2005-07-04 16:23:13 +00:00
}
2007-03-22 11:19:28 +00:00
else if (indexable == ASE_STX_SPEC_WORD_INDEXABLE) {
new = ase_stx_alloc_word_object (
2005-07-05 09:02:13 +00:00
stx, data, nfields, variable_data, variable_nfields);
2005-07-04 16:23:13 +00:00
}
else {
2007-03-22 11:19:28 +00:00
ase_assert (indexable == ASE_STX_SPEC_NOT_INDEXABLE);
ase_assert (variable_nfields == 0 && variable_data == ASE_NULL);
new = ase_stx_alloc_word_object (
stx, data, nfields, ASE_NULL, 0);
2005-07-04 16:23:13 +00:00
}
2007-03-22 11:19:28 +00:00
ASE_STX_CLASS(stx, new) = class;
2005-07-04 16:23:13 +00:00
return new;
}
2005-07-13 14:42:27 +00:00
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_class (ase_stx_t* stx, ase_word_t obj)
2005-07-13 14:42:27 +00:00
{
2007-03-22 11:19:28 +00:00
return ASE_STX_IS_SMALLINT(obj)?
stx->class_smallinteger: ASE_STX_CLASS(stx,obj);
2005-07-13 14:42:27 +00:00
}
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_classof (ase_stx_t* stx, ase_word_t obj)
2005-07-13 14:42:27 +00:00
{
2007-03-22 11:19:28 +00:00
return ASE_STX_IS_SMALLINT(obj)?
stx->class_smallinteger: ASE_STX_CLASS(stx,obj);
2005-07-13 14:42:27 +00:00
}
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_sizeof (ase_stx_t* stx, ase_word_t obj)
2005-07-13 14:42:27 +00:00
{
2007-03-22 11:19:28 +00:00
return ASE_STX_IS_SMALLINT(obj)? 1: ASE_STX_SIZE(stx,obj);
2005-07-13 14:42:27 +00:00
}