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 = "", options = {}) ⇒ 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 = "", options = {})
  source = encode(source)

  @runtime = runtime
  @source  = source

  # Test compile context source
  exec("")
end

Instance Method Details

#call(identifier, *args) ⇒ Object



45
46
47
# File 'lib/execjs/external_runtime.rb', line 45

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
37
38
39
40
41
42
43
# 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)

  if ExecJS.cygwin?
    filepath = `cygpath -m #{tmpfile.path}`.rstrip
  else
    filepath = tmpfile.path
  end

  begin
    extract_result(@runtime.exec_runtime(filepath), filepath)
  ensure
    File.unlink(tmpfile)
  end
end