class CommonJS::Environment
Attributes
runtime[R]
Public Class Methods
new(runtime, options = {})
click to toggle source
# File lib/commonjs/environment.rb, line 7 def initialize(runtime, options = {}) @runtime = runtime @paths = [options[:path]].flatten.map {|path| Pathname(path)} @modules = {} end
Public Instance Methods
native(module_id, impl)
click to toggle source
# File lib/commonjs/environment.rb, line 23 def native(module_id, impl) @modules[module_id] = Module::Native.new(impl) end
new_object()
click to toggle source
# File lib/commonjs/environment.rb, line 27 def new_object @runtime['Object'].new end
require(module_id)
click to toggle source
# File lib/commonjs/environment.rb, line 13 def require(module_id) unless mod = @modules[module_id] filepath = find(module_id) or fail LoadError, "no such module '#{module_id}'" load = @runtime.eval("(function(module, require, exports) {#{File.read(filepath)}})", filepath.expand_path.to_s) @modules[module_id] = mod = Module.new(module_id, self) load.call(mod, mod.require_function, mod.exports) end return mod.exports end
Private Instance Methods
find(module_id)
click to toggle source
# File lib/commonjs/environment.rb, line 33 def find(module_id) # Add `.js` extension if neccessary. target = if File.extname(module_id) == '.js' then module_id else "#{module_id}.js" end if loadpath = @paths.find { |path| path.join(target).exist? } loadpath.join(target) end end