public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
dgtized (author)
Tue Apr 22 10:17:41 -0700 2008
commit  a58dfb869990f9999b5488ff186ccd367f896aae
tree    298a8edd33d7cacafe68271513e8b7a1b451726e
parent  21d99b3411295f2887f84bca52549f40273baa14
rubinius / vm / objects.hpp
100644 577 lines (469 sloc) 13.909 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#ifndef RBX_OBJECTS_HPP
#define RBX_OBJECTS_HPP
 
#include "vm.hpp"
#include "object.hpp"
#include "type_info.hpp"
 
namespace rubinius {
  class BuiltinType : public Object {
 
  };
 
  class NilClass : public BuiltinType { };
  /* NOTE(t1):
   * This looks scary, but it's pretty simple. We're specializing
   * the kind_of when passed NilClass to just test using nil_p().
   * This makes kind_of smarter, letting us use it everywhere for
   * type checks. */
  template <>
    static bool kind_of<NilClass>(OBJECT obj) {
      return obj == Qnil;
    }
 
  class TrueClass : public BuiltinType { };
  /* See t1 */
  template <>
    static bool kind_of<TrueClass>(OBJECT obj) {
      return obj == Qtrue;
    }
 
  class FalseClass : public BuiltinType { };
  /* See t1 */
  template <>
    static bool kind_of<FalseClass>(OBJECT obj) {
      return obj == Qfalse;
    }
}
 
namespace rubinius {
  class StaticScope : public BuiltinType {
    public:
    const static size_t fields = 3;
    const static object_type type = StaticScopeType;
 
    OBJECT instance_variables;
    Module* module;
    StaticScope* parent;
 
    static StaticScope* create(STATE);
  };
};
 
namespace rubinius {
 
  class String;
  class Hash;
  class Tuple;
 
  class Symbol : public BuiltinType {
    public:
    const static size_t fields = 0;
    const static object_type type = SymbolType;
 
    native_int index() {
      return DATA_STRIP_TAG(this);
    }
 
    static Symbol* from_index(STATE, size_t index) {
      return (Symbol*)DATA_APPLY_TAG(index, DATA_TAG_SYMBOL);
    }
 
    String* to_str(STATE);
  };
 
  /* See t1 */
  template <>
    static bool kind_of<Symbol>(OBJECT obj) {
      return obj->symbol_p();
    }
 
  typedef Symbol* SYMBOL;
 
  class SymbolTable : public BuiltinType {
    public:
    const static size_t fields = 3;
    const static object_type type = SymbolTableType;
 
    OBJECT instance_variables;
    Tuple* symbols;
    Hash* strings;
 
    static SymbolTable* create(STATE);
    SYMBOL lookup(STATE, const char* str, size_t size = 0);
    SYMBOL lookup(STATE, String* str);
    String* find_string(STATE, Symbol* sym);
  };
};
 
namespace rubinius {
  class Tuple : public BuiltinType {
    public:
    const static size_t fields = 0;
    const static object_type type = TupleType;
 
    static Tuple* create(STATE, size_t fields);
    static Tuple* from(STATE, size_t fields, ...);
    static bool is_a(OBJECT obj) {
      return obj->obj_type == TupleType;
    }
 
    OBJECT put(STATE, size_t idx, OBJECT val);
    void copy_from(STATE, Tuple* other, int start, int end);
 
  };
};
 
namespace rubinius {
 
  class LookupTable;
 
  #define HASH_MINSIZE 16
  class Hash : public BuiltinType {
    public:
    const static size_t fields = 7;
    const static object_type type = HashType;
 
    OBJECT instance_variables;
    OBJECT keys;
    Tuple* values;
    OBJECT bins;
    OBJECT entries;
    OBJECT default_value;
    OBJECT default_proc;
 
    static Hash* create(STATE, size_t size = HASH_MINSIZE);
    void setup(STATE, size_t size);
    Hash* dup(STATE);
    static OBJECT entry_new(STATE, hashval hsh, OBJECT key, OBJECT data);
    static OBJECT entry_append(STATE, OBJECT top, OBJECT nxt);
    OBJECT add_entry(STATE, hashval hsh, OBJECT ent);
    void redistribute(STATE);
    OBJECT find_entry(STATE, hashval hsh);
    OBJECT add(STATE, hashval hsh, OBJECT key, OBJECT data);
    OBJECT set(STATE, OBJECT key, OBJECT val);
    OBJECT get(STATE, hashval hsh);
    int lookup(STATE, OBJECT key, hashval hash, OBJECT *value);
    int lookup2(STATE, int (*compare)(STATE, OBJECT, OBJECT),
        OBJECT key, hashval hash, OBJECT *value);
    void assign(STATE, int (*compare)(STATE, OBJECT, OBJECT),
        OBJECT key, hashval hash, OBJECT value);
    OBJECT get_undef(STATE, hashval hsh);
    OBJECT remove(STATE, hashval hsh);
 
    static Hash* from_tuple(STATE, OBJECT tup);
    static Tuple* csm_new(STATE);
    static OBJECT csm_find(STATE, Tuple* csm, OBJECT key);
    static OBJECT csm_add(STATE, Tuple* csm, OBJECT key, OBJECT val);
    static Hash* csm_into_hash(STATE, Tuple* csm);
    static LookupTable* csm_into_lookuptable(STATE, Tuple* csm);
 
  };
};
 
namespace rubinius {
  class RegexpData : public BuiltinType {
    public:
    const static size_t fields = 0;
    const static object_type type = RegexpDataType;
 
    class Info : public TypeInfo {
    public:
      Info(Class* cls) : TypeInfo(cls) { }
      virtual void cleanup(OBJECT obj);
    };
  };
};
 
namespace rubinius {
  class MatchData : public BuiltinType {
    public:
    const static size_t fields = 5;
    const static object_type type = MatchDataType;
 
    OBJECT instance_variables;
    OBJECT source;
    OBJECT regexp;
    OBJECT full;
    OBJECT region;
  };
};
 
namespace rubinius {
  class NormalObject : public BuiltinType {
    public:
    const static size_t fields = 1;
    const static object_type type = ObjectType;
 
    OBJECT instance_variables;
  };
};
 
namespace rubinius {
  class Array : public BuiltinType {
    public:
    const static size_t fields = 4;
    const static object_type type = ArrayType;
 
    OBJECT total;
    Tuple* tuple;
    OBJECT start;
    OBJECT shared;
 
    size_t size() {
      return total->n2i();
    }
 
    static Array* create(STATE, size_t size);
    static Array* from_tuple(STATE, Tuple* tup);
    void setup(STATE, size_t size);
    OBJECT get(STATE, size_t idx);
    OBJECT set(STATE, size_t idx, OBJECT val);
    OBJECT append(STATE, OBJECT val);
    bool includes_p(STATE, OBJECT val);
  };
};
 
namespace rubinius {
  class Exception : public BuiltinType {
    public:
    const static size_t fields = 3;
    const static object_type type = ExceptionType;
 
    static Exception* create(STATE);
    OBJECT instance_variables;
    OBJECT message;
    OBJECT context;
  };
};
 
namespace rubinius {
  class CompiledMethod;
  class MethodContext;
 
  class BlockEnvironment : public BuiltinType {
    public:
    const static size_t fields = 9;
    const static object_type type = BlockEnvType;
 
    OBJECT instance_variables;
    MethodContext* home;
    OBJECT initial_ip;
    OBJECT last_ip;
    OBJECT post_send;
    OBJECT home_block;
    OBJECT local_count;
    OBJECT bonus;
    OBJECT method;
 
    static BlockEnvironment* under_context(STATE, CompiledMethod* cm,
        MethodContext* parent, MethodContext* active);
 
    void call(STATE, size_t args);
  };
};
 
namespace rubinius {
  class IO : public BuiltinType {
    public:
    const static size_t fields = 4;
    const static object_type type = IOType;
 
    OBJECT instance_variables;
    OBJECT descriptor;
    OBJECT buffer;
    OBJECT mode;
 
    static void init(STATE);
    static IO* create(STATE, int fd);
 
    class Buffer : public BuiltinType {
    public:
      const static size_t fields = 4;
      const static object_type type = IOBufferType;
 
      OBJECT instance_variables;
      OBJECT storage;
      OBJECT total;
      OBJECT used;
 
      static Buffer* create(STATE, size_t bytes);
      void reset(STATE);
      String* drain(STATE);
 
      char* byte_address() {
        return (char*)storage->bytes;
      }
 
      size_t left() {
        return total->n2i() - used->n2i();
      }
 
      char* at_unused() {
        char* start = (char*)storage->bytes;
        start += used->n2i();
        return start;
      }
 
      void read_bytes(size_t bytes) {
        used = Object::i2n(used->n2i() + bytes);
      }
 
    };
  };
};
 
namespace rubinius {
  class Regexp : public BuiltinType {
    public:
    const static size_t fields = 4;
    const static object_type type = RegexpType;
 
    OBJECT instance_variables;
    OBJECT source;
    OBJECT data;
    OBJECT names;
 
    static void cleanup(STATE, OBJECT data);
    static void init(STATE);
    static Regexp* create(STATE, String* pattern, OBJECT options, char* err_buf = NULL);
    static char* version(STATE);
 
    OBJECT options(STATE);
    OBJECT match_region(STATE, String* string, OBJECT start, OBJECT end, OBJECT forward);
 
  };
};
 
namespace rubinius {
  class String : public BuiltinType {
    public:
    const static size_t fields = 6;
    const static object_type type = StringType;
 
    static bool is_a(OBJECT obj) {
      return obj->reference_p() && obj->obj_type == StringType;
    }
 
    OBJECT num_bytes;
    OBJECT characters;
    OBJECT encoding;
    OBJECT data;
    OBJECT hash;
    OBJECT shared;
 
    static String* create(STATE, const char* str, size_t bytes = 0);
    static hashval hash_str(const unsigned char *bp, unsigned int sz);
    static int string_equal_p(STATE, OBJECT self, OBJECT other);
 
    size_t size(STATE) {
      return num_bytes->n2i();
    }
 
    size_t size() {
      return num_bytes->n2i();
    }
 
    /* Allows the String object to be cast as a char* */
    operator const char *() {
      return (const char*)(data->bytes);
    }
 
    /* TODO: since we're technically say it's ok to change this, we might
     * want to copy it first. */
    operator char *() {
      return (char*)(data->bytes);
    }
 
    char* byte_address() {
      return (char*)data->bytes;
    }
 
    void unshare(STATE);
    hashval hash_string(STATE);
    SYMBOL to_sym(STATE);
    char* byte_address(STATE);
    String* string_dup(STATE);
    String* append(STATE, String* other);
    String* append(STATE, char* other);
    String* add(STATE, String* other);
    String* add(STATE, char* other);
 
  };
};
 
namespace rubinius {
  class ByteArray : public BuiltinType {
    public:
    const static size_t fields = 0;
    const static object_type type = ByteArrayType;
 
    static ByteArray* create(STATE, size_t bytes);
  };
};
 
namespace rubinius {
  #define LOOKUPTABLE_MIN_SIZE 16
  class LookupTable : public BuiltinType {
    public:
    const static size_t fields = 4;
    const static object_type type = LookupTableType;
 
    OBJECT instance_variables;
    Tuple* values;
    OBJECT bins;
    OBJECT entries;
 
    /* Prototypes */
    static LookupTable* create(STATE, size_t sz = LOOKUPTABLE_MIN_SIZE);
    void setup(STATE, size_t sz);
    OBJECT store(STATE, OBJECT key, OBJECT val);
    OBJECT fetch(STATE, OBJECT key);
    OBJECT fetch(STATE, OBJECT key, bool* found);
    LookupTable* dup(STATE);
    static OBJECT entry_new(STATE, OBJECT key, OBJECT val);
    static OBJECT entry_append(STATE, OBJECT top, OBJECT nxt);
    void redistribute(STATE, size_t size);
    OBJECT find_entry(STATE, OBJECT key);
    OBJECT find(STATE, OBJECT key);
    OBJECT remove(STATE, OBJECT key);
    OBJECT has_key(STATE, OBJECT key);
    static Array* collect(STATE, LookupTable* tbl, OBJECT (*action)(STATE, OBJECT));
    static OBJECT get_key(STATE, OBJECT entry);
    Array* all_keys(STATE);
    static OBJECT get_value(STATE, OBJECT entry);
    Array* all_values(STATE);
    static OBJECT get_entry(STATE, OBJECT entry);
    OBJECT all_entries(STATE);
  };
 
  class MethodTable : public LookupTable {
    public:
    const static object_type type = MTType;
    static MethodTable* create(STATE);
  };
};
 
namespace rubinius {
 
  class Executable : public BuiltinType {
    public:
    const static size_t fields = 4;
    const static object_type type = ExecutableType;
 
    OBJECT instance_variables;
    OBJECT primitive;
    OBJECT required;
    OBJECT serial;
  };
 
 
  class ISeq;
  class MemoryPointer;
  class VMMethod;
 
  class CompiledMethod : public Executable {
    public:
    const static size_t fields = 20;
    const static object_type type = CMethodType;
    const static size_t saved_fields = 16;
 
    SYMBOL name;
    ISeq* iseq;
    OBJECT stack_size;
    OBJECT local_count;
    OBJECT required_args;
    OBJECT total_args;
    OBJECT splat;
    Tuple* literals;
    Tuple* exceptions;
    OBJECT lines;
    OBJECT file;
    OBJECT path;
    OBJECT serial;
    OBJECT bonus;
    MemoryPointer* compiled;
    StaticScope* scope;
 
    static CompiledMethod* create(STATE);
    void post_marshal(STATE);
    size_t number_of_locals();
    void set_scope(StaticScope*);
    VMMethod* vmmethod(STATE);
 
    class Visibility : public BuiltinType {
    public:
      const static size_t fields = 3;
      const static object_type type = CMVisibilityType;
 
      LookupTable* instance_variables;
      SYMBOL visibility;
      Executable* method;
 
      static Visibility* create(STATE);
 
      bool public_p(STATE) {
        return visibility == G(sym_public);
      }
 
      bool private_p(STATE) {
        return visibility == G(sym_private);
      }
 
      bool protected_p(STATE) {
        return visibility == G(sym_protected);
      }
    };
  };
 
  template <>
    static bool kind_of<Executable>(OBJECT obj) {
      if(obj->obj_type == Executable::type ||
         obj->obj_type == CompiledMethod::type) {
        return true;
      }
 
      return false;
    }
};
 
namespace rubinius {
  class Fixnum : public BuiltinType {
  public:
    const static size_t fields = 0;
    const static object_type type = FixnumType;
 
    OBJECT add(STATE, FIXNUM other) {
      return Object::i2n(state, n2i() + other->n2i());
    }
 
    OBJECT sub(STATE, FIXNUM other) {
      return Object::i2n(state, n2i() - other->n2i());
    }
 
    OBJECT multiply(STATE, FIXNUM other) {
      return Object::i2n(state, n2i() * other->n2i());
    }
 
    OBJECT divide(STATE, FIXNUM other) {
      return Object::i2n(state, n2i() / other->n2i());
    }
 
    OBJECT modulo(STATE, FIXNUM other) {
      return Object::i2n(state, n2i() % other->n2i());
    }
  };
 
  typedef Fixnum* FIXNUM;
 
  /* See t1 */
  template <>
    static bool kind_of<Fixnum>(OBJECT obj) {
      return obj->fixnum_p();
    }
}
 
#include "builtin_class.hpp"
#include "builtin_contexts.hpp"
#include "builtin_iseq.hpp"
#include "builtin_bignum.hpp"
#include "builtin_float.hpp"
#include "builtin_list.hpp"
#include "builtin_selector.hpp"
#include "builtin_task.hpp"
#include "builtin_iseq.hpp"
 
#endif