Class: V8::Object
- Includes:
- Comparable, Delegated, Enumerable
- Defined in:
- lib/v8/object.rb,
ext/v8/v8_object.cpp
Direct Known Subclasses
Class Method Summary collapse
- .native_new ⇒ Object
-
.new(*args) ⇒ Object
Returns new V8 object.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#[](key) ⇒ Object
Returns value of specified object’s property.
-
#[]=(key, value) ⇒ Object
Sets given value as specified property in current object.
- #delegate ⇒ Object
- #each(*args, &block) ⇒ Object
-
#get(key) ⇒ Object
Returns value of specified object’s property.
-
#keys ⇒ Object
Returns list of property names belonging to an object.
- #method_missing(meth, *args, &block) ⇒ Object
-
#properties ⇒ Object
Returns list of property names belonging to an object.
- #respond_to?(meth) ⇒ Boolean
-
#set(key, value) ⇒ Object
Sets given value as specified property in current object.
- #to_hash ⇒ Object
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?
Methods inherited from Data
#empty?, #error?, #null?, #undefined?, #value?
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/v8/object.rb', line 18 def method_missing(meth, *args, &block) if respond_to?(meth) property = self[meth] if property.is_a?(V8::Function) return property.call_on(self, *args, &block) else return property end end super end |
Class Method Details
.native_new ⇒ Object
6 |
# File 'lib/v8/object.rb', line 6 alias_method :native_new, :new |
Instance Method Details
#<=>(other) ⇒ Object
40 41 42 |
# File 'lib/v8/object.rb', line 40 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'
85 86 87 88 89 90 91 92 93 94 |
# File 'ext/v8/v8_object.cpp', line 85
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'
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'ext/v8/v8_object.cpp', line 108
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);
}
|
#delegate ⇒ Object
48 49 50 |
# File 'lib/v8/object.rb', line 48 def delegate to_hash end |
#each(*args, &block) ⇒ Object
44 45 46 |
# File 'lib/v8/object.rb', line 44 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'
85 86 87 88 89 90 91 92 93 94 |
# File 'ext/v8/v8_object.cpp', line 85
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()));
}
}
|
#keys ⇒ Array #properties ⇒ Array
Returns list of property names belonging to an object.
obj = cxt.evaluate("{foo: 'bar', spam: 'eggs'};")
obj.keys # => ['foo', 'spam']
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'ext/v8/v8_object.cpp', line 133
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;
}
|
#keys ⇒ Array #properties ⇒ Array
Returns list of property names belonging to an object.
obj = cxt.evaluate("{foo: 'bar', spam: 'eggs'};")
obj.keys # => ['foo', 'spam']
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'ext/v8/v8_object.cpp', line 133
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
14 15 16 |
# File 'lib/v8/object.rb', line 14 def respond_to?(meth) !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'
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'ext/v8/v8_object.cpp', line 108
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_hash ⇒ Object
36 37 38 |
# File 'lib/v8/object.rb', line 36 def to_hash Hash[*keys.map {|key| [key.to_s, get(key)] }.flatten(1)] end |