Class: MiniRacer::Context::ExternalFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_racer.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, callback, parent) ⇒ ExternalFunction

Returns a new instance of ExternalFunction.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mini_racer.rb', line 101

def initialize(name, callback, parent)
  unless String === name
    raise ArgumentError, "parent_object must be a String"
  end
  parent_object, _ , @name = name.rpartition(".")
  @callback = callback
  @parent = parent
  @parent_object_eval = nil
  @parent_object = nil

  unless parent_object.empty?
    @parent_object = parent_object

    @parent_object_eval = ""
    prev = ""
    first = true
    parent_object.split(".").each do |obj|
      prev << obj
      if first
        @parent_object_eval << "if (typeof #{prev} === 'undefined') { #{prev} = {} };\n"
      else
        @parent_object_eval << "#{prev} = #{prev} || {};\n"
      end
      prev << "."
      first = false
    end
    @parent_object_eval << "#{parent_object};"
  end
  notify_v8
end