Class: Mustang::V8::Array

Inherits:
V8Object
  • Object
show all
Includes:
Comparable, Delegated, Enumerable
Defined in:
lib/mustang/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
45
46
# File 'ext/v8/v8_array.cpp', line 36

static VALUE rb_v8_array_new(VALUE klass, VALUE data)
{
  HandleScope scope;
  PREVENT_CREATION_WITHOUT_CONTEXT();
  
  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.



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

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



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

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'


79
80
81
82
83
# File 'ext/v8/v8_array.cpp', line 79

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.



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

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



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

def delegate
  to_a
end

#each(*args, &block) ⇒ Object



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

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'


79
80
81
82
83
# File 'ext/v8/v8_array.cpp', line 79

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.



130
131
132
133
134
# File 'ext/v8/v8_array.cpp', line 130

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.



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

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.



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

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.



130
131
132
133
134
# File 'ext/v8/v8_array.cpp', line 130

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:



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

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;
}