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.



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

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

  @runtime = runtime
  @source  = source

  # Test compile context source
  exec("")
end

Instance Method Details

#call(identifier, *args) ⇒ Object



38
39
40
# File 'lib/execjs/external_runtime.rb', line 38

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

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



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

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/execjs/external_runtime.rb', line 25

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

  tmpfile = write_to_tempfile(source)
  begin
    extract_result(@runtime.exec_runtime(tmpfile.path), tmpfile.path)
  ensure
    File.unlink(tmpfile)
  end
end