Class: V8::Object

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

Direct Known Subclasses

Array, Function

Instance Method Summary collapse

Constructor Details

#initialize(native, context = nil) ⇒ Object

Returns a new instance of Object.

Raises:

  • (ScriptError)


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

def initialize(native, context = nil)
  @native = native
  @context = context || C::Context::GetEntered()
  raise ScriptError, "V8::Object.new called without an open V8 context" unless @context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/v8/object.rb', line 44

def method_missing(name, *args, &block)
  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 Method Details

#[](key) ⇒ Object



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

def [](key)
  @context.enter do
    To.rb(@native.Get(To.v8(key)))
  end
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  value.tap do
    @context.enter do
      @native.Set(To.v8(key), To.v8(value))
    end
  end
end

#eachObject



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

def each
  @context.enter do
    for prop in To.rb(@native.GetPropertyNames())
      yield prop, self[prop]
    end
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_sObject



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

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