Class: ExecJS::ExternalRuntime::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(runtime, source = "", options = {}) ⇒ Context

Returns a new instance of Context.



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

def initialize(runtime, source = "", options = {})
  source = source.encode(Encoding::UTF_8)

  @runtime = runtime
  @source  = source

  # Test compile context source
  exec("")
end

Instance Method Details

#call(identifier, *args) ⇒ Object



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

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

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



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

def eval(source, options = {})
  source = source.encode(Encoding::UTF_8)

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

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/execjs/external_runtime.rb', line 26

def exec(source, options = {})
  source = source.encode(Encoding::UTF_8)
  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