Class: ExecJS::ExternalRuntime::Context

Inherits:
Runtime::Context show all
Defined in:
lib/execjs/external_runtime.rb

Instance Method Summary collapse

Methods included from ExecJS::Encoding

#encode

Constructor Details

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

Returns a new instance of Context.



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

def initialize(runtime, source = "")
  source = encode(source)

  @runtime = runtime
  @source  = source
end

Instance Method Details

#call(identifier, *args) ⇒ Object



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

def call(identifier, *args)
  eval "#{identifier}.apply(this, #{::JSON.generate(args)})"
end

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



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

def eval(source, options = {})
  source = encode(source)

  if /\S/ =~ source
    exec("return eval(#{::JSON.generate("(#{source})", :quirks_mode => true)})")
  end
end

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



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

def exec(source, options = {})
  source = encode(source)
  source = "#{@source}\n#{source}" if @source

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