Class: Isomorfeus::Speednode::Runtime::Context

Inherits:
ExecJS::Runtime::Context
  • Object
show all
Defined in:
lib/isomorfeus/speednode/runtime.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Context.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/isomorfeus/speednode/runtime.rb', line 93

def initialize(runtime, source = "", options = {})
  @runtime = runtime
  @uuid = SecureRandom.uuid
  @permissive = !!options[:permissive]

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

  begin
    source = encode(source)
  rescue
    source = source.force_encoding('UTF-8')
  end

  @permissive ? raw_execp(source) : raw_exec(source)
end

Class Method Details

.finalize(runtime, uuid) ⇒ Object



109
110
111
# File 'lib/isomorfeus/speednode/runtime.rb', line 109

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

Instance Method Details

#call(identifier, *args) ⇒ Object



113
114
115
# File 'lib/isomorfeus/speednode/runtime.rb', line 113

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

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



117
118
119
120
121
# File 'lib/isomorfeus/speednode/runtime.rb', line 117

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

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



123
124
125
# File 'lib/isomorfeus/speednode/runtime.rb', line 123

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

#permissive?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/isomorfeus/speednode/runtime.rb', line 127

def permissive?
  @permissive
end

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



131
132
133
134
135
# File 'lib/isomorfeus/speednode/runtime.rb', line 131

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

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



137
138
139
# File 'lib/isomorfeus/speednode/runtime.rb', line 137

def permissive_exec(source, options = {})
  raw_execp("(function(){#{source}})()")
end

#raw_exec(source) ⇒ Object



141
142
143
144
145
146
# File 'lib/isomorfeus/speednode/runtime.rb', line 141

def raw_exec(source)
  source = encode(source)

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

#raw_execp(source) ⇒ Object



148
149
150
151
152
153
# File 'lib/isomorfeus/speednode/runtime.rb', line 148

def raw_execp(source)
  source = encode(source)

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