Class: Bluenode::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/bluenode/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir = Dir.pwd, env = ENV.dup, stdout = $stdout, stderr = $stderr) ⇒ Context

Returns a new instance of Context.



39
40
41
42
43
44
# File 'lib/bluenode/context.rb', line 39

def initialize(basedir = Dir.pwd, env = ENV.dup, stdout = $stdout, stderr = $stderr)
  @runtime  = V8::Context.new
  @basedir  = basedir.to_s

  startup env, stdout, stderr
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



37
38
39
# File 'lib/bluenode/context.rb', line 37

def basedir
  @basedir
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



37
38
39
# File 'lib/bluenode/context.rb', line 37

def runtime
  @runtime
end

Class Method Details

.main(basedir = Dir.pwd) ⇒ Object



7
8
9
10
# File 'lib/bluenode/context.rb', line 7

def main(basedir = Dir.pwd)
  context = new(basedir)
  context.require './'
end

.node_module_paths(from) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bluenode/context.rb', line 12

def node_module_paths(from)
  from = File.expand_path(from)

  split_re  = windows? && /[\/\\]/ || /\//
  paths     = []
  parts     = from.split(split_re)
  tip       = parts.size - 1

  while tip >= 0
    if parts[tip] != 'node_modules'
      dir = File.join(*parts[0..tip], 'node_modules')
      paths << dir
    end

    tip -= 1
  end

  paths
end

.windows?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/bluenode/context.rb', line 32

def windows?
  @windows ||= !!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
end

Instance Method Details

#eval_as_module(script, name = '[eval]', is_main = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bluenode/context.rb', line 50

def eval_as_module(script, name = '[eval]', is_main = false)
  Module.new(self, name).tap do |mod|
    mod.filename  = File.join(basedir, name)
    mod.paths     = self.class.node_module_paths(basedir)

    if is_main
      runtime['process'].mainModule = mod
      mod.id = '.'
    end

    mod.send :compile, script, "#{name}-wrapper"
  end
end

#extensionsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bluenode/context.rb', line 91

def extensions
  @extensions ||= {}.tap do |extensions|
    extensions['.js'] = lambda do |mod, filename|
      mod.send :compile, File.read(filename), filename
    end

    extensions['.json'] = lambda do |mod, filename|
      begin
        mod.exports = JSON.parse(File.read(filename))
      rescue => exc
        raise $!, "#{filename}: #{$!}", $!.backtrace
      end
    end

    extensions['.node'] = lambda do |mod, filename|
      raise 'requiring modules with a .node extension is not supported'
    end
  end
end

#find_path(request, paths) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bluenode/context.rb', line 131

def find_path(request, paths)
  exts  = extensions.keys
  paths = [''] if request[0] == '/'
  trailing_slash = request[-1] == '/'
  cache_key = { request: request, paths: paths }.to_json

  return path_cache[cache_key] if path_cache.key?(cache_key)

  i, ii = 0, paths.size

  while i < ii
    base_path = File.expand_path(request, paths[i])
    filename = nil

    unless trailing_slash
      filename = try_file(base_path)
      filename = try_extensions(base_path, exts) unless filename
    end

    filename = try_package(base_path, exts) unless filename
    filename = try_extensions(File.expand_path('index', base_path), exts) unless filename

    if filename
      path_cache[cache_key] = filename
      return filename
    end

    i += 1
  end

  false
end

#global_pathsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/bluenode/context.rb', line 111

def global_paths
  @global_paths ||= begin
    home_dir  = self.class.windows? && ENV['USERPROFILE'] || ENV['HOME']
    exec_path = `which node`.chomp

    paths = [File.expand_path(File.join('..', '..', 'lib', 'node'), exec_path)]

    if home_dir
      paths.unshift File.expand_path('.node_libraries', home_dir)
      paths.unshift File.expand_path('.node_modules', home_dir)
    end

    if node_path = ENV['NODE_PATH']
      paths = node_path.split(File::PATH_SEPARATOR).concat(paths)
    end

    paths
  end
end

#modulesObject



87
88
89
# File 'lib/bluenode/context.rb', line 87

def modules
  @modules ||= {}
end

#new_function(callable, props = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bluenode/context.rb', line 75

def new_function(callable, props = {})
  @runtime.enter do
    function = @runtime.to_v8(callable)

    props.each do |name, value|
      function.Set @runtime.to_v8(name.to_s), @runtime.to_v8(value)
    end

    function
  end
end

#new_objectObject



71
72
73
# File 'lib/bluenode/context.rb', line 71

def new_object
  @runtime['Object'].new
end

#processObject



46
47
48
# File 'lib/bluenode/context.rb', line 46

def process
  @runtime['process']
end

#require(path) ⇒ Object



64
65
66
67
68
69
# File 'lib/bluenode/context.rb', line 64

def require(path)
  script  = "module.exports = require('#{path}')"
  mod     = eval_as_module(script, '[main]', true)

  mod.exports
end