forked from rubinius/rubinius
/
array.cpp
415 lines (302 loc) · 11.1 KB
/
array.cpp
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
#include "builtin/array.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/object.hpp"
#include "builtin/proc.hpp"
#include "builtin/thread.hpp"
#include "builtin/lookuptable.hpp"
#include "arguments.hpp"
#include "dispatch.hpp"
#include "exception_point.hpp"
#include "capi/capi.hpp"
#include "capi/include/ruby.h"
#include <stdarg.h>
using namespace rubinius;
using namespace rubinius::capi;
namespace rubinius {
namespace capi {
Array* capi_get_array(NativeMethodEnvironment* env, VALUE val) {
if(!env) env = NativeMethodEnvironment::get();
Handle* handle = Handle::from(val);
Array* array = c_as<Array>(handle->object());
handle->flush(env);
return array;
}
void capi_update_array(NativeMethodEnvironment* env, VALUE ary_handle) {
if(!env) env = NativeMethodEnvironment::get();
Handle* handle = Handle::from(ary_handle);
handle->update(env);
}
void flush_cached_rarray(NativeMethodEnvironment* env, Handle* handle) {
if(handle->is_rarray()) {
Array* array = as<Array>(handle->object());
RArray* rarray = handle->as_rarray(env);
size_t size = array->size();
if(size != rarray->len) {
Tuple* tuple = Tuple::create(env->state(), rarray->len);
array->tuple(env->state(), tuple);
array->start(env->state(), Fixnum::from(0));
array->total(env->state(), Fixnum::from(rarray->len));
}
for(size_t i = 0; i < size; i++) {
array->set(env->state(), i, env->get_object(rarray->ptr[i]));
}
}
}
void update_cached_rarray(NativeMethodEnvironment* env, Handle* handle) {
if(handle->is_rarray()) {
Array* array = c_as<Array>(handle->object());
RArray* rarray = handle->as_rarray(env);
size_t size = array->size();
if(rarray->len != size) {
delete[] rarray->dmwmb;
rarray->dmwmb = rarray->ptr = new VALUE[size];
rarray->aux.capa = rarray->len = size;
}
for(size_t i = 0; i < size; i++) {
rarray->ptr[i] = env->get_handle(array->get(env->state(), i));
}
}
}
RArray* Handle::as_rarray(NativeMethodEnvironment* env) {
if(type_ != cRArray) {
Array* array = c_as<Array>(object());
size_t size = array->size();
RArray* ary = new RArray;
VALUE* ptr = new VALUE[size];
for(size_t i = 0; i < size; i++) {
ptr[i] = env->get_handle(array->get(env->state(), i));
}
ary->dmwmb = ary->ptr = ptr;
ary->aux.capa = ary->len = size;
ary->aux.shared = Qfalse;
type_ = cRArray;
as_.rarray = ary;
flush_ = flush_cached_rarray;
update_ = update_cached_rarray;
env->state()->shared.global_handles()->move(this,
env->state()->shared.cached_handles());
}
return as_.rarray;
}
}
}
extern "C" {
struct RArray* capi_rarray_struct(VALUE val) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Handle* h = Handle::from(val);
env->check_tracked_handle(h);
return h->as_rarray(env);
}
VALUE rb_Array(VALUE obj_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Object* obj = env->get_object(obj_handle);
if (kind_of<Array>(obj)) {
return obj_handle;
}
Array* array = Array::create(env->state(), 1);
array->set(env->state(), 0, obj);
return env->get_handle(array);
}
VALUE rb_ary_clear(VALUE self_handle) {
return rb_funcall2(self_handle, rb_intern("clear"), 0, NULL);
}
VALUE rb_ary_delete(VALUE self_handle, VALUE item) {
return rb_funcall(self_handle, rb_intern("delete"), 1, item);
}
VALUE rb_ary_dup(VALUE self_handle) {
return rb_funcall2(self_handle, rb_intern("dup"), 0, NULL);
}
/* @todo Check 64-bit? */
VALUE rb_ary_entry(VALUE self_handle, int index) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
return env->get_handle(self->aref(env->state(), Fixnum::from(index)));
}
VALUE rb_ary_each(VALUE self_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
for(size_t i = 0; i < self->size(); i++) {
rb_yield(env->get_handle(self->get(env->state(), i)));
}
return self_handle;
}
VALUE rb_ary_includes(VALUE self, VALUE obj) {
return rb_funcall(self, rb_intern("include?"), 1, obj);
}
VALUE rb_ary_join(VALUE self_handle, VALUE separator_handle) {
return rb_funcall(self_handle, rb_intern("join"), 1, separator_handle);
}
VALUE rb_ary_to_s(VALUE self_handle) {
return rb_funcall(self_handle, rb_intern("to_s"), 0);
}
/** By default, Arrays have space for 16 elements. */
static const unsigned long cCApiArrayDefaultCapacity = 16;
VALUE rb_ary_new() {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* array = Array::create(env->state(), cCApiArrayDefaultCapacity);
return env->get_handle(array);
}
VALUE rb_ary_new2(unsigned long length) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* array = Array::create(env->state(), length);
return env->get_handle(array);
}
VALUE rb_ary_new3(unsigned long length, ...) {
va_list args;
va_start(args, length);
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* array = Array::create(env->state(), length);
array->start(env->state(), Fixnum::from(0));
array->total(env->state(), Fixnum::from(length));
for(unsigned long i = 0; i < length; ++i) {
// @todo determine if we need to check these objects for whether
// they are arrays and flush any caches
Object* object = env->get_object(va_arg(args, VALUE));
array->set(env->state(), i, object);
}
va_end(args);
return env->get_handle(array);
}
VALUE rb_ary_new4(unsigned long length, const VALUE* object_handles) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* array = Array::create(env->state(), length);
array->start(env->state(), Fixnum::from(0));
array->total(env->state(), Fixnum::from(length));
if (object_handles) {
for(std::size_t i = 0; i < length; ++i) {
// @todo determine if we need to check these objects for whether
// they are arrays and flush any caches
Object* object = env->get_object(object_handles[i]);
array->set(env->state(), i, object);
}
}
return env->get_handle(array);
}
VALUE rb_ary_pop(VALUE self_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
Object* obj = self->pop(env->state());
capi_update_array(env, self_handle);
return env->get_handle(obj);
}
VALUE rb_ary_push(VALUE self_handle, VALUE object_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
self->append(env->state(), env->get_object(object_handle));
capi_update_array(env, self_handle);
return self_handle;
}
VALUE rb_ary_reverse(VALUE self_handle) {
return rb_funcall2(self_handle, rb_intern("reverse"), 0, NULL);
}
VALUE rb_ary_shift(VALUE self_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
Object* obj = self->shift(env->state());
capi_update_array(env, self_handle);
return env->get_handle(obj);
}
size_t rb_ary_size(VALUE self_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
return self->size();
}
void rb_ary_store(VALUE self_handle, long int index, VALUE object_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
size_t total = self->size();
if(index < 0) {
index += total;
}
if(index < 0) {
std::ostringstream error;
error << "Index " << (index - total) << " out of range!";
rb_raise(rb_eIndexError, error.str().c_str());
}
self->set(env->state(), index, env->get_object(object_handle));
capi_update_array(env, self_handle);
}
VALUE rb_ary_unshift(VALUE self_handle, VALUE object_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
Array* self = capi_get_array(env, self_handle);
self->unshift(env->state(), env->get_object(object_handle));
capi_update_array(env, self_handle);
return self_handle;
}
VALUE rb_assoc_new(VALUE first, VALUE second) {
return rb_funcall(rb_cArray, rb_intern("[]"), 2, first, second);
}
VALUE rb_ary_aref(int argc, VALUE *argv, VALUE self_handle) {
return rb_funcall2(self_handle, rb_intern("[]"), argc, argv);
}
VALUE rb_each(VALUE ary) {
return rb_funcall(ary, rb_intern("each"), 0);
}
VALUE rb_iterate(VALUE(*ifunc)(VALUE), VALUE ary, VALUE(*cb)(ANYARGS),
VALUE cb_data)
{
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
// Minor optimization.
if(ifunc == rb_each && kind_of<Array>(env->get_object(ary))) {
for(size_t i = 0; i < rb_ary_size(ary); i++) {
(*cb)(rb_ary_entry(ary, i), cb_data, Qnil);
}
return ary;
}
Proc* prc = capi::wrap_c_function((void*)cb, cb_data, ITERATE_BLOCK);
env->set_outgoing_block(env->get_handle(prc));
return (*ifunc)(ary);
}
void rb_mem_clear(VALUE* ary, int len) {
for(int i = 0; i < len; i++) {
ary[i] = Qnil;
}
}
VALUE rb_protect_inspect(VALUE (*func)(VALUE a, VALUE b), VALUE h_obj, VALUE h_arg) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
STATE = env->state();
Thread* thr = Thread::current(state);
LookupTable* rectbl = thr->recursive_objects();
Object* obj = env->get_object(h_obj);
Object* id = obj->id(state);
bool found = false;
rectbl->fetch(state, id, &found);
if(found) {
return (*func)(h_obj, h_arg);
}
rectbl->store(state, id, RBX_Qtrue);
VALUE ret = Qnil;
ExceptionPoint ep(env);
PLACE_EXCEPTION_POINT(ep);
bool unwinding = false;
if(unlikely(ep.jumped_to())) {
unwinding = true;
} else {
ret = (*func)(h_obj, h_arg);
}
ep.pop(env);
// Get the thread and table again, the GC might have fun.
thr = Thread::current(state);
rectbl = thr->recursive_objects();
obj = env->get_object(h_obj);
id = obj->id(state);
rectbl->remove(state, id);
if(unwinding) env->current_ep()->return_to(env);
return ret;
}
VALUE rb_inspecting_p(VALUE h_obj) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
STATE = env->state();
Thread* thr = Thread::current(state);
LookupTable* rectbl = thr->recursive_objects();
Object* obj = env->get_object(h_obj);
Object* id = obj->id(state);
bool found = false;
rectbl->fetch(state, id, &found);
if(found) return Qtrue;
return Qfalse;
}
VALUE rb_ary_freeze(VALUE ary) {
return rb_obj_freeze(ary);
}
}