Class: V8::Object

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/v8/object.rb

Direct Known Subclasses

Array, Function

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(native = nil) ⇒ Object

Returns a new instance of Object


6
7
8
9
10
# File 'lib/v8/object.rb', line 6

def initialize(native = nil)
  @context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context"
  @native = block_given? ? yield : native || V8::C::Object::New()
  @context.link self, @native
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/v8/object.rb', line 59

def method_missing(name, *args, &block)
  if name.to_s =~ /(.*)=$/
    if args.length > 1
      self[$1] = args
      return args
    else
      self[$1] = args.first
      return args
    end
  end
  return super(name, *args, &block) unless self.respond_to?(name)
  property = self[name]
  if property.kind_of?(V8::Function)
    property.methodcall(self, *args)
  elsif args.empty?
    property
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 0)" unless args.empty?
  end
end

Instance Attribute Details

#nativeObject (readonly) Also known as: to_v8

Returns the value of attribute native


3
4
5
# File 'lib/v8/object.rb', line 3

def native
  @native
end

Instance Method Details

#[](key) ⇒ Object


12
13
14
15
16
# File 'lib/v8/object.rb', line 12

def [](key)
  @context.enter do
    @context.to_ruby @native.Get(@context.to_v8(key))
  end
end

#[]=(key, value) ⇒ Object


18
19
20
21
22
23
# File 'lib/v8/object.rb', line 18

def []=(key, value)
  @context.enter do
    @native.Set(@context.to_v8(key), @context.to_v8(value))
  end
  return value
end

#eachObject


39
40
41
42
43
44
45
46
47
# File 'lib/v8/object.rb', line 39

def each
  @context.enter do
    names = @native.GetPropertyNames()
    0.upto(names.Length() - 1) do |i|
      name = names.Get(i)
      yield @context.to_ruby(name), @context.to_ruby(@native.Get(name))
    end
  end
end

#keysObject


25
26
27
28
29
30
# File 'lib/v8/object.rb', line 25

def keys
  @context.enter do
    names = @native.GetPropertyNames()
    0.upto( names.Length() - 1).to_enum.map {|i| @context.to_ruby names.Get(i)}
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/v8/object.rb', line 55

def respond_to?(method)
  super or self[method] != nil
end

#to_sObject


49
50
51
52
53
# File 'lib/v8/object.rb', line 49

def to_s
  @context.enter do
    @context.to_ruby @native.ToString()
  end
end

#valuesObject


32
33
34
35
36
37
# File 'lib/v8/object.rb', line 32

def values
  @context.enter do
    names = @native.GetPropertyNames()
    0.upto( names.Length() - 1).to_enum.map {|i| @context.to_ruby @native.Get(names.Get(i))}
  end
end