Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 363 lines (311 sloc) 8.225 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/*
* MacRuby Symbols.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2010, Apple Inc. All rights reserved.
*/

#include <wctype.h>

#include "ruby.h"
#include "ruby/encoding.h"
#include "encoding.h"
#include "symbol.h"
#include "ruby/node.h"
#include "vm.h"

VALUE rb_cSymbol;

static CFMutableDictionaryRef sym_id = NULL, id_str = NULL;
static long last_id = 0;

typedef struct {
    VALUE klass;
    rb_str_t *str;
    ID id;
    SEL sel;
} rb_sym_t;

#define RSYM(obj) ((rb_sym_t *)(obj))

static rb_sym_t *
sym_alloc(rb_str_t *str, ID id)
{
    rb_sym_t *sym = (rb_sym_t *)malloc(sizeof(rb_sym_t));
    assert(rb_cSymbol != 0);
    sym->klass = rb_cSymbol;
    GC_RETAIN(str); // never released
    sym->str = str;
    sym->id = id;
    sym->sel = NULL; // lazy
    return sym;
}

ID
rb_intern_str(VALUE str)
{
    UChar *chars = NULL;
    long chars_len = 0;
    bool need_free = false;
    rb_str_get_uchars(str, &chars, &chars_len, &need_free);
    assert(chars_len > 0);

    const unsigned long name_hash = rb_str_hash_uchars(chars, chars_len);
    ID id = (ID)CFDictionaryGetValue(sym_id, (const void *)name_hash);
    if (id != 0) {
goto return_id;
    }

    rb_sym_t *sym = NULL;

    switch (chars[0]) {
case '$':
id = ID_GLOBAL;
break;

case '@':
if (chars_len > 1 && chars[1] == '@') {
id = ID_CLASS;
}
else {
id = ID_INSTANCE;
}
break;

default:
if (chars_len > 1 && chars[chars_len - 1] == '=') {
// Attribute assignment.
id = rb_intern_str(rb_str_substr(str, 0, chars_len - 1));
if (!is_attrset_id(id)) {
id = rb_id_attrset(id);
goto id_register;
}
id = ID_ATTRSET;
}
else if (iswupper(chars[0])) {
id = ID_CONST;
}
else {
id = ID_LOCAL;
}
break;
    }

    id |= ++last_id << ID_SCOPE_SHIFT;

id_register:
//printf("register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id);
    sym = sym_alloc(RSTR(str), id);
    CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id);
    CFDictionarySetValue(id_str, (const void *)id, (const void *)sym);

return_id:
    if (need_free && chars != NULL) {
free(chars);
    }
    return id;
}

VALUE
rb_id2str(ID id)
{
    VALUE sym = (VALUE)CFDictionaryGetValue(id_str, (const void *)id);
    if (sym != 0) {
//printf("lookup %ld -> %s\n", id, rb_sym2name(sym));
return sym;
    }

    if (is_attrset_id(id)) {
// Attribute assignment.
ID id2 = (id & ~ID_SCOPE_MASK) | ID_LOCAL;

while ((sym = rb_id2str(id2)) == 0) {
if (!is_local_id(id2)) {
//printf("lookup %ld -> FAIL\n", id);
return 0;
}
id2 = (id & ~ID_SCOPE_MASK) | ID_CONST;
}

VALUE str = rb_str_dup((VALUE)RSYM(sym)->str);
rb_str_cat(str, "=", 1);
rb_intern_str(str);

// Retry one more time.
sym = (VALUE)CFDictionaryGetValue(id_str, (const void *)id);
if (sym != 0) {
//printf("lookup %ld -> %s\n", id, rb_sym2name(sym));
return sym;
}
    }
//printf("lookup %ld -> FAIL\n", id);
    return 0;
}

ID
rb_intern3(const char *name, long len, rb_encoding *enc)
{
    VALUE str = rb_enc_str_new(name, len, enc);
    return rb_intern_str(str);
}

ID
rb_intern2(const char *name, long len)
{
    return rb_intern_str(rb_str_new(name, len));
}

ID
rb_intern(const char *name)
{
    return rb_intern_str(rb_str_new2(name));
}

ID
rb_sym2id(VALUE sym)
{
    return RSYM(sym)->id;
}

VALUE
rb_name2sym(const char *name)
{
    return rb_id2str(rb_intern(name));
}

VALUE
rb_sym2str(VALUE sym)
{
    return (VALUE)RSYM(sym)->str;
}

VALUE
rb_sym_to_s(VALUE sym)
{
    return rb_str_dup(rb_sym2str(sym));
}

const char *
rb_sym2name(VALUE sym)
{
    return RSTRING_PTR(RSYM(sym)->str);
}

/*
* call-seq:
* Symbol.all_symbols => array
*
* Returns an array of all the symbols currently in Ruby's symbol
* table.
*
* Symbol.all_symbols.size #=> 903
* Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink,
* :chown, :EOFError, :$;, :String,
* :LOCK_SH, :"setuid?", :$<,
* :default_proc, :compact, :extend,
* :Tms, :getwd, :$=, :ThreadGroup,
* :wait2, :$>]
*/

static VALUE
rsym_all_symbols(VALUE klass, SEL sel)
{
    VALUE ary = rb_ary_new();
    const long count = CFDictionaryGetCount(id_str);
    if (count >= 0) {
const void **values = (const void **)malloc(sizeof(void *) * count);
CFDictionaryGetKeysAndValues(id_str, NULL, values);
for (long i = 0; i < count; i++) {
rb_ary_push(ary, (VALUE)values[i]);
}
free(values);
    }
    return ary;
}

void
Init_PreSymbol(void)
{
    sym_id = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
    id_str = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
    last_id = 1000;

    // Pre-register parser symbols.
    for (int i = 0; rb_op_tbl[i].token != 0; i++) {
VALUE str = rb_str_new2(rb_op_tbl[i].name);

UChar *chars = NULL;
long chars_len = 0;
bool need_free = false;
rb_str_get_uchars(str, &chars, &chars_len, &need_free);
assert(chars_len > 0);

ID id = rb_op_tbl[i].token;
rb_sym_t *sym = sym_alloc(RSTR(str), id);
unsigned long name_hash = rb_str_hash_uchars(chars, chars_len);

//printf("pre-register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id);

CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id);
CFDictionarySetValue(id_str, (const void *)id, (const void *)sym);

if (need_free && chars != NULL) {
free(chars);
}
    }
}

/*
* call-seq:
* sym == obj => true or false
*
* Equality---If <i>sym</i> and <i>obj</i> are exactly the same
* symbol, returns <code>true</code>. Otherwise, compares them
* as strings.
*/

static VALUE
rsym_equal(VALUE sym, SEL sel, VALUE other)
{
    return sym == other ? Qtrue : Qfalse;
}

/*
* call-seq:
* sym.inspect => string
*
* Returns the representation of <i>sym</i> as a symbol literal.
*
* :fred.inspect #=> ":fred"
*/

static VALUE
rsym_inspect(VALUE sym, SEL sel)
{
    VALUE str = rb_str_new2(":");
    rb_str_concat(str, str_inspect(RSYM(sym)->str, true));
    return str;
}

/*
* call-seq:
* sym.to_proc
*
* Returns a _Proc_ object which respond to the given method by _sym_.
*
* (1..3).collect(&:to_s) #=> ["1", "2", "3"]
*/

static VALUE
rsym_to_proc(VALUE sym, SEL sel)
{
    SEL msel = sel_registerName(rb_id2name(SYM2ID(sym)));
    rb_vm_block_t *b = rb_vm_create_block_calling_sel(msel);
    return rb_proc_alloc_with_block(rb_cProc, b);
}

/*
* call-seq:
* sym.id2name => string
* sym.to_s => string
*
* Returns the name or string corresponding to <i>sym</i>.
*
* :fred.id2name #=> "fred"
*/

static VALUE
rsym_to_s(VALUE sym, SEL sel)
{
    return rb_sym_to_s(sym);
}

/*
* call-seq:
* sym.to_sym => sym
* sym.intern => sym
*
* In general, <code>to_sym</code> returns the <code>Symbol</code>
* corresponding to an object. As <i>sym</i> is already a symbol,
* <code>self</code> is returned in this case.
*/

static VALUE
rsym_to_sym(VALUE sym, SEL sel)
{
    return sym;
}

void
Init_Symbol(void)
{
    // rb_cSymbol is defined earlier in Init_PreVM().
    rb_set_class_path(rb_cSymbol, rb_cObject, "Symbol");
    rb_const_set(rb_cObject, rb_intern("Symbol"), rb_cSymbol);

    rb_undef_alloc_func(rb_cSymbol);
    rb_undef_method(*(VALUE *)rb_cSymbol, "new");
    rb_objc_define_method(*(VALUE *)rb_cSymbol, "all_symbols",
rsym_all_symbols, 0);

    rb_objc_define_method(rb_cSymbol, "==", rsym_equal, 1);
    rb_objc_define_method(rb_cSymbol, "eql?", rsym_equal, 1);
    //rb_objc_define_method(rb_cSymbol, "<=>", rsym_cmp, 1);
    rb_objc_define_method(rb_cSymbol, "inspect", rsym_inspect, 0);
    rb_objc_define_method(rb_cSymbol, "to_proc", rsym_to_proc, 0);
    rb_objc_define_method(rb_cSymbol, "to_s", rsym_to_s, 0);
    rb_objc_define_method(rb_cSymbol, "id2name", rsym_to_s, 0);
    rb_objc_define_method(rb_cSymbol, "description", rsym_to_s, 0);
    rb_objc_define_method(rb_cSymbol, "intern", rsym_to_sym, 0);
    rb_objc_define_method(rb_cSymbol, "to_sym", rsym_to_sym, 0);
}
Something went wrong with that request. Please try again.