Module: Johnson::SpiderMonkey::JSLandProxy

Defined in:
lib/johnson/spidermonkey/js_land_proxy.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.autovivified(target, attribute) ⇒ Object



27
28
29
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 27

def self.autovivified(target, attribute)
  target.send(:__johnson_js_properties)[attribute]
end

.autovivified?(target, attribute) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 31

def self.autovivified?(target, attribute)
  target.respond_to?(:__johnson_js_properties) &&
    target.send(:__johnson_js_properties).key?(attribute)
end

.autovivify(target, attribute, value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 36

def self.autovivify(target, attribute, value)
  (class << target; self; end).instance_eval do
    unless target.respond_to?(:__johnson_js_properties)
      define_method(:__johnson_js_properties) do
        @__johnson_js_properties ||= {}
      end
    end
    
    define_method(:"#{attribute}=") do |arg|
      send(:__johnson_js_properties)[attribute] = arg
    end
    
    define_method(:"#{attribute}") do |*args|
      js_prop = send(:__johnson_js_properties)[attribute]
      if js_prop.is_a?(RubyLandProxy) && js_prop.function?
        js_prop.call_using(self, *args)
      else
        js_prop
      end
    end
  end

  target.send(:"#{attribute}=", value)
end

.call_proc_by_oid(oid, *args) ⇒ Object



19
20
21
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 19

def self.call_proc_by_oid(oid, *args)
  id2ref(oid).call(*args)
end

.id2ref(oid) ⇒ Object



23
24
25
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 23

def self.id2ref(oid)
  ObjectSpace._id2ref(oid)
end

.js_property?(target, name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 13

def self.js_property?(target, name)
  # FIXME: that rescue is gross; handles, e.g., "name?"
  (target.send(:instance_variable_defined?, "@#{name}") rescue false) ||
    (target.respond_to?(:js_property?) && target.__send__(:js_property?, name))
end

.send_with_possible_block(target, symbol, args) ⇒ Object



4
5
6
7
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 4

def self.send_with_possible_block(target, symbol, args)
  block = args.pop if args.last.is_a?(RubyLandProxy) && args.last.function?
  target.__send__(symbol, *args, &block)
end

.treat_all_properties_as_methods(target) ⇒ Object



9
10
11
# File 'lib/johnson/spidermonkey/js_land_proxy.rb', line 9

def self.treat_all_properties_as_methods(target)
  def target.js_property?(name); true; end
end