Class: ExecJS::ExternalRuntime::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(runtime, source = "") ⇒ Context

Returns a new instance of Context.



7
8
9
10
11
12
# File 'lib/execjs/external_runtime.rb', line 7

def initialize(runtime, source = "")
  source = source.encode('UTF-8') if source.respond_to?(:encode)

  @runtime = runtime
  @source  = source
end

Instance Method Details

#call(identifier, *args) ⇒ Object



31
32
33
# File 'lib/execjs/external_runtime.rb', line 31

def call(identifier, *args)
  eval "#{identifier}.apply(this, #{MultiJson.encode(args)})"
end

#eval(source, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/execjs/external_runtime.rb', line 14

def eval(source, options = {})
  source = source.encode('UTF-8') if source.respond_to?(:encode)

  if /\S/ =~ source
    exec("return eval(#{MultiJson.encode("(#{source})")})")
  end
end

#exec(source, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/execjs/external_runtime.rb', line 22

def exec(source, options = {})
  source = source.encode('UTF-8') if source.respond_to?(:encode)
  source = "#{@source}\n#{source}" if @source

  compile_to_tempfile(source) do |file|
    extract_result(@runtime.send(:exec_runtime, file.path))
  end
end