Class: ExecJS::FastNode::ExternalPipedRuntime::Context

Inherits:
Runtime::Context
  • Object
show all
Defined in:
lib/execjs/fastnode/external_piped_runtime.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Context.



62
63
64
65
66
67
68
69
70
71
# File 'lib/execjs/fastnode/external_piped_runtime.rb', line 62

def initialize(runtime, source = "", options = {})
  @runtime = runtime
  @uuid = SecureRandom.uuid

  ObjectSpace.define_finalizer(self, self.class.finalize(@runtime, @uuid))

  source = encode(source)

  raw_exec(source)
end

Class Method Details

.finalize(runtime, uuid) ⇒ Object



73
74
75
# File 'lib/execjs/fastnode/external_piped_runtime.rb', line 73

def self.finalize(runtime, uuid)
  proc { runtime.vm.delete_context(uuid) }
end

Instance Method Details

#call(identifier, *args) ⇒ Object



94
95
96
# File 'lib/execjs/fastnode/external_piped_runtime.rb', line 94

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

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



77
78
79
80
81
# File 'lib/execjs/fastnode/external_piped_runtime.rb', line 77

def eval(source, options = {})
  if /\S/ =~ source
    raw_exec("(#{source})")
  end
end

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



83
84
85
# File 'lib/execjs/fastnode/external_piped_runtime.rb', line 83

def exec(source, options = {})
  raw_exec("(function(){#{source}})()")
end

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



87
88
89
90
91
92
# File 'lib/execjs/fastnode/external_piped_runtime.rb', line 87

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

  result = @runtime.vm.exec(@uuid, source)
  extract_result(result)
end