renaming symbol accessors to lsymbol, and clarifying the use of SEXP_USE_SAFE_ACCESSORS

This commit is contained in:
Alex Shinn 2012-01-22 13:53:27 +09:00
parent fd87d5ecd0
commit 5257e7f4d7
4 changed files with 27 additions and 12 deletions

View file

@ -86,6 +86,21 @@
/* uncomment this to add very verbose debugging stats to the native GC */ /* uncomment this to add very verbose debugging stats to the native GC */
/* #define SEXP_USE_DEBUG_GC 1 */ /* #define SEXP_USE_DEBUG_GC 1 */
/* uncomment this to enable "safe" field accessors for primitive types */
/* The sexp union type fields are abstracted away with macros of the */
/* form sexp_<type>_<field>(<obj>), however these are just convenience */
/* macros equivalent to directly accessing the union field, and will */
/* return incorrect results (or segfault) if <obj> isn't of the correct */
/* <type>. Thus you're required to check the types manually before */
/* accessing them. However, to detect errors earlier you can enable */
/* SEXP_USE_SAFE_ACCESSORS, and on invalid accesses chibi will print */
/* a friendly error message and immediately segfault itself so you */
/* can see where the invalid access was made. */
/* Note this is only intended for debugging, and mostly for user code. */
/* If you want to build chibi itself with this option, compilation */
/* may be very slow and using CFLAGS=-O0 is recommended. */
/* #define SEXP_USE_SAFE_ACCESSORS 1 */
/* uncomment this to make the heap common to all contexts */ /* uncomment this to make the heap common to all contexts */
/* By default separate contexts can have separate heaps, */ /* By default separate contexts can have separate heaps, */
/* and are thus thread-safe and independant. */ /* and are thus thread-safe and independant. */

View file

@ -845,8 +845,8 @@ SEXP_API sexp sexp_make_unsigned_integer(sexp ctx, sexp_luint_t x);
#define sexp_string_ref(x, i) (sexp_make_character((unsigned char)sexp_string_data(x)[sexp_unbox_fixnum(i)])) #define sexp_string_ref(x, i) (sexp_make_character((unsigned char)sexp_string_data(x)[sexp_unbox_fixnum(i)]))
#define sexp_string_set(x, i, v) (sexp_string_data(x)[sexp_unbox_fixnum(i)] = sexp_unbox_character(v)) #define sexp_string_set(x, i, v) (sexp_string_data(x)[sexp_unbox_fixnum(i)] = sexp_unbox_character(v))
#define sexp_symbol_data(x) (sexp_field(x, symbol, SEXP_SYMBOL, data)) #define sexp_lsymbol_data(x) (sexp_field(x, symbol, SEXP_SYMBOL, data))
#define sexp_symbol_length(x) (sexp_field(x, symbol, SEXP_SYMBOL, length)) #define sexp_lsymbol_length(x) (sexp_field(x, symbol, SEXP_SYMBOL, length))
#define sexp_port_stream(p) (sexp_pred_field(p, port, sexp_portp, stream)) #define sexp_port_stream(p) (sexp_pred_field(p, port, sexp_portp, stream))
#define sexp_port_name(p) (sexp_pred_field(p, port, sexp_portp, name)) #define sexp_port_name(p) (sexp_pred_field(p, port, sexp_portp, name))

View file

@ -82,7 +82,7 @@ static int sexp_object_compare (sexp ctx, sexp a, sexp b) {
res = strcmp(sexp_string_data(a), sexp_string_data(b)); res = strcmp(sexp_string_data(a), sexp_string_data(b));
break; break;
case SEXP_SYMBOL: case SEXP_SYMBOL:
res = strcmp(sexp_symbol_data(a), sexp_symbol_data(b)); res = strcmp(sexp_lsymbol_data(a), sexp_lsymbol_data(b));
break; break;
default: default:
res = 0; res = 0;
@ -91,7 +91,7 @@ static int sexp_object_compare (sexp ctx, sexp a, sexp b) {
} }
#if SEXP_USE_HUFF_SYMS #if SEXP_USE_HUFF_SYMS
} else if (sexp_lsymbolp(a) && sexp_isymbolp(b)) { } else if (sexp_lsymbolp(a) && sexp_isymbolp(b)) {
res = strcmp(sexp_symbol_data(a), res = strcmp(sexp_lsymbol_data(a),
sexp_string_data(sexp_write_to_string(ctx, b))); sexp_string_data(sexp_write_to_string(ctx, b)));
#endif #endif
} else { } else {
@ -101,7 +101,7 @@ static int sexp_object_compare (sexp ctx, sexp a, sexp b) {
#if SEXP_USE_HUFF_SYMS #if SEXP_USE_HUFF_SYMS
if (sexp_isymbolp(a) && sexp_lsymbolp(b)) if (sexp_isymbolp(a) && sexp_lsymbolp(b))
res = strcmp(sexp_string_data(sexp_write_to_string(ctx, a)), res = strcmp(sexp_string_data(sexp_write_to_string(ctx, a)),
sexp_symbol_data(b)); sexp_lsymbol_data(b));
else else
#endif #endif
res = -1; res = -1;

14
sexp.c
View file

@ -1058,8 +1058,8 @@ sexp sexp_intern(sexp ctx, const char *str, sexp_sint_t len) {
bucket = (sexp_string_hash(p, len-i, res) % SEXP_SYMBOL_TABLE_SIZE); bucket = (sexp_string_hash(p, len-i, res) % SEXP_SYMBOL_TABLE_SIZE);
#endif #endif
for (ls=sexp_context_symbols(ctx)[bucket]; sexp_pairp(ls); ls=sexp_cdr(ls)) for (ls=sexp_context_symbols(ctx)[bucket]; sexp_pairp(ls); ls=sexp_cdr(ls))
if ((sexp_symbol_length(tmp=sexp_car(ls)) == len) if ((sexp_lsymbol_length(tmp=sexp_car(ls)) == len)
&& ! strncmp(str, sexp_symbol_data(tmp), len)) && ! strncmp(str, sexp_lsymbol_data(tmp), len))
return sexp_car(ls); return sexp_car(ls);
/* not found, make a new symbol */ /* not found, make a new symbol */
@ -1565,12 +1565,12 @@ sexp sexp_write_one (sexp ctx, sexp obj, sexp out) {
sexp_write_char(ctx, '"', out); sexp_write_char(ctx, '"', out);
break; break;
case SEXP_SYMBOL: case SEXP_SYMBOL:
str = sexp_symbol_data(obj); str = sexp_lsymbol_data(obj);
c = sexp_symbol_length(obj) > 0 ? EOF : '|'; c = sexp_lsymbol_length(obj) > 0 ? EOF : '|';
for (i=sexp_symbol_length(obj)-1; i>=0; i--) for (i=sexp_lsymbol_length(obj)-1; i>=0; i--)
if (str[i] <= ' ' || str[i] == '\\' || is_separator(str[i])) c = '|'; if (str[i] <= ' ' || str[i] == '\\' || is_separator(str[i])) c = '|';
if (c!=EOF) sexp_write_char(ctx, c, out); if (c!=EOF) sexp_write_char(ctx, c, out);
for (i=sexp_symbol_length(obj); i>0; str++, i--) { for (i=sexp_lsymbol_length(obj); i>0; str++, i--) {
if (str[0] == '\\') sexp_write_char(ctx, '\\', out); if (str[0] == '\\') sexp_write_char(ctx, '\\', out);
sexp_write_char(ctx, str[0], out); sexp_write_char(ctx, str[0], out);
} }
@ -2663,7 +2663,7 @@ sexp sexp_symbol_to_string_op (sexp ctx, sexp self, sexp_sint_t n, sexp sym) {
if (sexp_isymbolp(sym)) return sexp_write_to_string(ctx, sym); if (sexp_isymbolp(sym)) return sexp_write_to_string(ctx, sym);
#endif #endif
sexp_assert_type(ctx, sexp_lsymbolp, SEXP_SYMBOL, sym); sexp_assert_type(ctx, sexp_lsymbolp, SEXP_SYMBOL, sym);
return sexp_c_string(ctx, sexp_symbol_data(sym), sexp_symbol_length(sym)); return sexp_c_string(ctx, sexp_lsymbol_data(sym), sexp_lsymbol_length(sym));
} }
void sexp_init (void) { void sexp_init (void) {