Class: V8::Array

Inherits:
V8Object show all
Includes:
Comparable, Delegated, Enumerable
Defined in:
lib/v8/array.rb,
ext/v8/v8_array.cpp

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegated

#method_missing, #old_method_missing, #old_respond_to?, #respond_to?, #to_s

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Delegated

Class Method Details

.V8::Array.newObject .V8::Array.new(array) ⇒ Object

Returns a new V8 array.



36
37
38
39
40
41
42
43
44
# File 'ext/v8/v8_array.cpp', line 36

static VALUE rb_v8_array_new(VALUE klass, VALUE data)
{
  HandleScope scope;
  VALUE ary = rb_funcall2(data, rb_intern("to_a"), 0, NULL);
  VALUE self = v8_ref_new(klass, to_v8_array(ary));

  v8_set_peer(self);
  return self;
}

Instance Method Details

#push(value) ⇒ Object #<<(value) ⇒ Object

Appends given value to referenced array.



107
108
109
110
111
112
113
114
# File 'ext/v8/v8_array.cpp', line 107

static VALUE rb_v8_array_push(VALUE self, VALUE value)
{
  HandleScope scope;
  Handle<Value> _value = to_v8(value);
  Handle<Array> ary = unwrap(self);
  ary->Set(ary->Length(), _value);  
  return to_ruby(_value);
}

#<=>(other) ⇒ Object



9
10
11
# File 'lib/v8/array.rb', line 9

def <=>(other)
  to_a <=> other
end

#[](key) ⇒ Object #get(key) ⇒ Object

Returns value of specified array entry.

ary = cxt.evaluate("['foo', 'bar'];")
ary[0] # => 'foo'


77
78
79
80
81
# File 'ext/v8/v8_array.cpp', line 77

static VALUE rb_v8_array_get(VALUE self, VALUE key)
{
  HandleScope scope;
  return to_ruby(unwrap(self)->Get(NUM2UINT(key)));
}

#[]=(key) ⇒ Object #set(key, value) ⇒ Object

Sets given value under specified key.



91
92
93
94
95
96
97
# File 'ext/v8/v8_array.cpp', line 91

static VALUE rb_v8_array_set(VALUE self, VALUE key, VALUE value)
{
  HandleScope scope;
  Handle<Value> _value = to_v8(value);
  unwrap(self)->Set(NUM2UINT(key), _value);  
  return to_ruby(_value);
}

#delegateObject



17
18
19
# File 'lib/v8/array.rb', line 17

def delegate
  to_a
end

#each(*args, &block) ⇒ Object



13
14
15
# File 'lib/v8/array.rb', line 13

def each(*args, &block)
  to_a.each(*args, &block)
end

#[](key) ⇒ Object #get(key) ⇒ Object

Returns value of specified array entry.

ary = cxt.evaluate("['foo', 'bar'];")
ary[0] # => 'foo'


77
78
79
80
81
# File 'ext/v8/v8_array.cpp', line 77

static VALUE rb_v8_array_get(VALUE self, VALUE key)
{
  HandleScope scope;
  return to_ruby(unwrap(self)->Get(NUM2UINT(key)));
}

#lengthObject #sizeObject

Returns number of items stored in this array.



128
129
130
131
132
# File 'ext/v8/v8_array.cpp', line 128

VALUE rb_v8_array_length(VALUE self)
{
  HandleScope scope;
  return to_ruby(unwrap(self)->Length());
}

#push(value) ⇒ Object #<<(value) ⇒ Object

Appends given value to referenced array.



107
108
109
110
111
112
113
114
# File 'ext/v8/v8_array.cpp', line 107

static VALUE rb_v8_array_push(VALUE self, VALUE value)
{
  HandleScope scope;
  Handle<Value> _value = to_v8(value);
  Handle<Array> ary = unwrap(self);
  ary->Set(ary->Length(), _value);  
  return to_ruby(_value);
}

#[]=(key) ⇒ Object #set(key, value) ⇒ Object

Sets given value under specified key.



91
92
93
94
95
96
97
# File 'ext/v8/v8_array.cpp', line 91

static VALUE rb_v8_array_set(VALUE self, VALUE key, VALUE value)
{
  HandleScope scope;
  Handle<Value> _value = to_v8(value);
  unwrap(self)->Set(NUM2UINT(key), _value);  
  return to_ruby(_value);
}

#lengthObject #sizeObject

Returns number of items stored in this array.



128
129
130
131
132
# File 'ext/v8/v8_array.cpp', line 128

VALUE rb_v8_array_length(VALUE self)
{
  HandleScope scope;
  return to_ruby(unwrap(self)->Length());
}

#to_aArray

Returns referenced array data represented as ruby array.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
# File 'ext/v8/v8_array.cpp', line 53

static VALUE rb_v8_array_to_a(VALUE self)
{
  HandleScope scope;
  VALUE ary = rb_ary_new();
  Handle<Array> v8ary = unwrap(self);
  
  for (unsigned int i = 0; i < v8ary->Length(); i++) {
    rb_ary_store(ary, i, to_ruby(v8ary->Get(i)));
  }

  return ary;
}