Class: Mustang::V8::Object

Inherits:
Value show all
Includes:
Comparable, Delegated, Enumerable
Defined in:
lib/mustang/v8/object.rb,
ext/v8/v8_object.cpp

Direct Known Subclasses

Function

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegated

#old_method_missing, #old_respond_to?, #to_s

Methods inherited from Value

#==, #===, #array?, #ary?, #bool?, #boolean?, #date?, #empty?, #external?, #false?, #func?, #function?, #int?, #integer?, #null?, #num?, #number?, #obj?, #object?, #regex?, #regexp?, #str?, #string?, #to_boolean, #to_integer, #to_number, #to_object, #to_string, #true?, #undefined?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

:nodoc:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mustang/v8/object.rb', line 19

def method_missing(meth, *args, &block) # :nodoc:
  if respond_to?(meth)
    property = self[meth]
    if property.is_a?(Function)
      return property.call_on(self, *args, &block)
    else
      return property
    end
  end
  super
end

Class Method Details

.native_newObject



7
# File 'lib/mustang/v8/object.rb', line 7

alias_method :native_new, :new

.V8::Object.newObject .V8::Object.new(hash) ⇒ Object

Returns new V8 object.



51
52
53
54
# File 'ext/v8/v8_object.cpp', line 51

def new(*args, &block)
  res = native_new(*args)
  res.send(:reflect_from, args.first)        
end

Instance Method Details

#<=>(other) ⇒ Object



39
40
41
# File 'lib/mustang/v8/object.rb', line 39

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

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

Returns value of specified object’s property. If property is undefined then returns nil.

obj = cxt.evaluate("{foo: 'bar'};")
obj["foo"] # => 'bar'


87
88
89
90
91
92
93
94
95
96
# File 'ext/v8/v8_object.cpp', line 87

static VALUE rb_v8_object_get(VALUE self, VALUE key)
{
  HandleScope scope;

  if (FIXNUM_P(key)) {
    return to_ruby(unwrap(self)->Get(NUM2UINT(key)));
  } else {
    return to_ruby(unwrap(self)->Get(to_v8(key)->ToString()));
  }
}

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

Sets given value as specified property in current object.

obj = cxt.evaluate("var obj = {foo: 'bar'}; obj;")
obj["bar"] = "foo"
cxt.evaluate("obj.bar") # => 'foo'


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/v8/v8_object.cpp', line 110

static VALUE rb_v8_object_set(VALUE self, VALUE key, VALUE value)
{
  HandleScope scope;
  Handle<Value> _value = to_v8(value);

  if (FIXNUM_P(key)) {
    unwrap(self)->Set(NUM2UINT(key), _value);
  } else {
    unwrap(self)->Set(to_v8(key)->ToString(), _value);
  }
  
  return to_ruby(_value);
}

#delegateObject



47
48
49
# File 'lib/mustang/v8/object.rb', line 47

def delegate
  to_hash
end

#each(*args, &block) ⇒ Object



43
44
45
# File 'lib/mustang/v8/object.rb', line 43

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

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

Returns value of specified object’s property. If property is undefined then returns nil.

obj = cxt.evaluate("{foo: 'bar'};")
obj["foo"] # => 'bar'


87
88
89
90
91
92
93
94
95
96
# File 'ext/v8/v8_object.cpp', line 87

static VALUE rb_v8_object_get(VALUE self, VALUE key)
{
  HandleScope scope;

  if (FIXNUM_P(key)) {
    return to_ruby(unwrap(self)->Get(NUM2UINT(key)));
  } else {
    return to_ruby(unwrap(self)->Get(to_v8(key)->ToString()));
  }
}

#keysArray #propertiesArray

Returns list of property names belonging to an object.

obj = cxt.evaluate("{foo: 'bar', spam: 'eggs'};")
obj.keys # => ['foo', 'spam']

Overloads:



135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/v8/v8_object.cpp', line 135

static VALUE rb_v8_object_keys(VALUE self)
{
  HandleScope scope;
  Handle<Array> v8keys = unwrap(self)->GetPropertyNames();
  VALUE keys = rb_ary_new();
  
  for (unsigned int i = 0; i < v8keys->Length(); i++) {
    rb_ary_push(keys, to_ruby(v8keys->Get(i)));
  }

  return keys;
}

#keysArray #propertiesArray

Returns list of property names belonging to an object.

obj = cxt.evaluate("{foo: 'bar', spam: 'eggs'};")
obj.keys # => ['foo', 'spam']

Overloads:



135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/v8/v8_object.cpp', line 135

static VALUE rb_v8_object_keys(VALUE self)
{
  HandleScope scope;
  Handle<Array> v8keys = unwrap(self)->GetPropertyNames();
  VALUE keys = rb_ary_new();
  
  for (unsigned int i = 0; i < v8keys->Length(); i++) {
    rb_ary_push(keys, to_ruby(v8keys->Get(i)));
  }

  return keys;
}

#respond_to?(meth) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


15
16
17
# File 'lib/mustang/v8/object.rb', line 15

def respond_to?(meth) # :nodoc:
  !self[meth].undefined? or super
end

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

Sets given value as specified property in current object.

obj = cxt.evaluate("var obj = {foo: 'bar'}; obj;")
obj["bar"] = "foo"
cxt.evaluate("obj.bar") # => 'foo'


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'ext/v8/v8_object.cpp', line 110

static VALUE rb_v8_object_set(VALUE self, VALUE key, VALUE value)
{
  HandleScope scope;
  Handle<Value> _value = to_v8(value);

  if (FIXNUM_P(key)) {
    unwrap(self)->Set(NUM2UINT(key), _value);
  } else {
    unwrap(self)->Set(to_v8(key)->ToString(), _value);
  }
  
  return to_ruby(_value);
}

#to_hashObject



35
36
37
# File 'lib/mustang/v8/object.rb', line 35

def to_hash
  Hash[*keys.map {|key| [key.to_s, get(key)] }.flatten(1)]
end