Class: Rednode::Bindings::Evals::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/rednode/bindings/evals.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, *args) ⇒ Script

Returns a new instance of Script.



5
6
7
8
# File 'lib/rednode/bindings/evals.rb', line 5

def initialize(source, *args)
  @source = source
  @filename = args[0] if args.size > 0
end

Class Method Details

.createContext(properties = {}) ⇒ Object



10
11
12
# File 'lib/rednode/bindings/evals.rb', line 10

def self.createContext(properties = {})
  ::Rednode::Bindings::Evals::Context.new(properties)
end

.runInContext(source, context) ⇒ Object



18
19
20
# File 'lib/rednode/bindings/evals.rb', line 18

def self.runInContext(source, context)
  new(source).runInContext(context)
end

.runInNewContextObject



26
27
28
29
30
31
# File 'lib/rednode/bindings/evals.rb', line 26

def self.runInNewContext
  lambda do |source, *args|
    sandbox, filename = *args
    new(source, filename).runInNewContext(sandbox)
  end
end

.runInThisContext(source, filename = nil) ⇒ Object



46
47
48
# File 'lib/rednode/bindings/evals.rb', line 46

def self.runInThisContext(source, filename = nil)
  new(source, filename).runInThisContext
end

Instance Method Details

#createContext(*args) ⇒ Object



14
15
16
# File 'lib/rednode/bindings/evals.rb', line 14

def createContext(*args)
  self.class.createContext(*args)
end

#runInContext(context) ⇒ Object



22
23
24
# File 'lib/rednode/bindings/evals.rb', line 22

def runInContext(context)
  context.send(:eval, @source)
end

#runInNewContext(sandbox = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rednode/bindings/evals.rb', line 33

def runInNewContext(sandbox = nil)
  newContext = V8::Context.new
  sandbox = nil unless sandbox.kind_of?(V8::Object)
  for key, value in sandbox
    newContext[key] = value
  end if sandbox
  newContext.eval(@source, @filename || "<script>").tap do
    for key, value in newContext.scope
      sandbox[key] = value
    end if sandbox
  end
end

#runInThisContext(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rednode/bindings/evals.rb', line 50

def runInThisContext(*args)
  #mini-hack: there isn't a way to get the current V8::Context
  #as a high level context in therubyracer, so we hack the constructor
  #we wished existed that binds to the underlying current C::Context
  thisContext = V8::Context.allocate
  thisContext.instance_eval do
    @native = V8::C::Context::GetEntered()
    @to = V8::Portal.new(thisContext, V8::Access.new)
    @scope = @to.rb(@native.Global())
  end
  thisContext.eval(@source, @filename || "<script>")
end