Class: Spade::Context

Inherits:
V8::Context
  • Object
show all
Defined in:
lib/spade/context.rb

Overview

Creates a basic context suitable for running modules. The environments setup in this context will mimic a browser worker thread context, including timeouts and a console. A navigator object is also defined that provides some general information about the context.

Direct Known Subclasses

MainContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Context

Load the spade and racer-loader.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spade/context.rb', line 25

def initialize(opts={})
  @reactor = opts[:reactor]
  @verbose = opts[:verbose]
  super(opts) do |ctx|
    ctx['reactor'] = @reactor
    ctx['console'] = Console.new
    ctx['window']  = ctx.scope
    ctx.eval %[
      (function() {
        var r = reactor;
        setTimeout    = function(c,i) { return r.set_timeout(c,i); };
        setInterval   = function(c,i) { return r.set_interval(c,i); };
        clearTimeout  = function(t) { return r.clear_timeout(t); };
        clearInterval = function(t) { return r.clear_interval(t); };
        navigator     = {
          appName: 'spade',
          appVersion: "#{Spade::VERSION}",
          platform: "#{RUBY_PLATFORM}",
          userAgent: 'spade #{Spade::VERSION}; #{RUBY_PLATFORM}'
        }

        exit = function(status) { return r.exit(status || 0); };
      })();
    ]

    ctx['reactor'] = nil

    yield(ctx) if block_given?
  end
end

Instance Attribute Details

#reactorObject (readonly)

Returns the value of attribute reactor.



17
18
19
# File 'lib/spade/context.rb', line 17

def reactor
  @reactor
end

#verboseObject (readonly)

Returns the value of attribute verbose.



18
19
20
# File 'lib/spade/context.rb', line 18

def verbose
  @verbose
end

Instance Method Details

#require(mod_name) ⇒ Object



20
21
22
# File 'lib/spade/context.rb', line 20

def require(mod_name)
  self.eval("require('#{mod_name}');");
end