Skip to content

Commit

Permalink
Merge branch 'array_v2'
Browse files Browse the repository at this point in the history
Conflicts:
	src/value.h
  • Loading branch information
kaz0505 committed Oct 11, 2016
2 parents 595deed + 4a96b32 commit d460137
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/c_array.c
Expand Up @@ -10,9 +10,10 @@
static int array_size(mrb_value *v)
{
mrb_value *array = v->value.obj;
return array->value.array_len;
return array->value.i;
}


// Array = empty?
static void c_array_empty(mrb_vm *vm, mrb_value *v)
{
Expand All @@ -36,7 +37,7 @@ static void c_array_get(mrb_vm *vm, mrb_value *v)
int pos = GET_INT_ARG(0);
mrb_value *array = v->value.obj;

if( pos >= 0 && pos < array->value.array_len ){
if( pos >= 0 && pos < array->value.i ){
*v = array[pos+1];
} else {
SET_NIL_RETURN();
Expand All @@ -49,7 +50,7 @@ static void c_array_set(mrb_vm *vm, mrb_value *v)
int pos = GET_INT_ARG(0);
mrb_value *array = v->value.obj;

if( pos >= 0 && pos < array->value.array_len ){
if( pos >= 0 && pos < array->value.i ){
array[pos+1] = GET_ARG(1);
} else {
SET_NIL_RETURN();
Expand All @@ -63,11 +64,12 @@ static void c_array_plus(mrb_vm *vm, mrb_value *v)
// use free and alloc
mrb_value *array1 = v->value.array;
mrb_value *array2 = GET_ARY_ARG(0).value.array;
int len1 = array1->value.array_len;
int len2 = array2->value.array_len;
int len1 = array1->value.i;
int len2 = array2->value.i;
mrb_value *new_array = (mrb_value *)mrbc_alloc(vm, sizeof(mrb_value)*(len1+len2+1));

new_array->value.array_len = len1+len2;
new_array->tt = MRB_TT_FIXNUM;
new_array->value.i = len1+len2;
mrb_value *p = new_array + 1;
int i;
for( i=0 ; i<len1 ; i++ ){
Expand All @@ -85,7 +87,7 @@ static void c_array_plus(mrb_vm *vm, mrb_value *v)

static void c_array_index(mrb_vm *vm, mrb_value *v)
{
int len = v->value.array->value.array_len;
int len = v->value.array->value.i;
mrb_value *array = v->value.array + 1;
mrb_value value = GET_ARG(0);

Expand Down
1 change: 0 additions & 1 deletion src/value.h
Expand Up @@ -87,7 +87,6 @@ typedef struct RObject {
struct RClass *cls; // MRB_TT_CLASS : link to class
struct RProc *proc; // MRB_TT_PROC : link to proc
struct RObject *array; // MRB_TT_ARRAY : array of objects
int32_t array_len; // MRB_TT_ARRAY : array length
struct RObject *range; // MRB_TT_RANGE : link to range
double d; // MRB_TT_FLOAT : float
char *str; // MRB_TT_STRING : C-string
Expand Down
3 changes: 2 additions & 1 deletion src/vm.c
Expand Up @@ -838,7 +838,8 @@ inline static int op_array( mrb_vm *vm, uint32_t code, mrb_value *regs )
// ptr[1..] : array elements
ptr = (mrb_value*)mrbc_alloc(vm, sizeof(mrb_value)*(arg_c + 1));
v.value.obj = ptr;
ptr->value.array_len = arg_c;
ptr->tt = MRB_TT_FIXNUM;
ptr->value.i = arg_c;

p = ptr + 1;
while( arg_c > 0 ){
Expand Down

0 comments on commit d460137

Please sign in to comment.