class Less::JavaScript::RhinoContext

Public Class Methods

instance() click to toggle source
# File lib/less/java_script/rhino_context.rb, line 17
def self.instance
  return new # NOTE: for Rhino a context should be kept open per thread !
end
new(globals = nil) click to toggle source
# File lib/less/java_script/rhino_context.rb, line 21
def initialize(globals = nil)
  @rhino_context = Rhino::Context.new :java => true
  if @rhino_context.respond_to?(:version)
    @rhino_context.version = '1.8'
    apply_1_8_compatibility! if @rhino_context.version.to_s != '1.8'
  else
    apply_1_8_compatibility!
  end
  fix_memory_limit! @rhino_context
  globals.each { |key, val| @rhino_context[key] = val } if globals
end

Public Instance Methods

call(properties, *args) click to toggle source
# File lib/less/java_script/rhino_context.rb, line 53
def call(properties, *args)
  options = args.last.is_a?(::Hash) ? args.pop : {} # extract_option!
  
  source_name = options[:source_name] || "<eval>"
  line_number = options[:line_number] || 1
  @rhino_context.eval(properties, source_name, line_number).call(*args)
rescue Rhino::JSError => e
  handle_js_error(e)
end
eval(source, options = {}) click to toggle source
# File lib/less/java_script/rhino_context.rb, line 43
def eval(source, options = {})
  source = source.encode('UTF-8') if source.respond_to?(:encode)
  
  source_name = options[:source_name] || "<eval>"
  line_number = options[:line_number] || 1
  @rhino_context.eval("(#{source})", source_name, line_number)
rescue Rhino::JSError => e
  handle_js_error(e)
end
exec(&block) click to toggle source
# File lib/less/java_script/rhino_context.rb, line 37
def exec(&block)
  @rhino_context.open(&block)
rescue Rhino::JSError => e
  handle_js_error(e)
end
method_missing(symbol, *args) click to toggle source
Calls superclass method
# File lib/less/java_script/rhino_context.rb, line 63
def method_missing(symbol, *args)
  if @rhino_context.respond_to?(symbol)
    @rhino_context.send(symbol, *args)
  else
    super
  end
end
unwrap() click to toggle source
# File lib/less/java_script/rhino_context.rb, line 33
def unwrap
  @rhino_context
end

Private Instance Methods

apply_1_8_compatibility!() click to toggle source
# File lib/less/java_script/rhino_context.rb, line 96
def apply_1_8_compatibility!
  # TODO rather load ecma-5.js ...
  @rhino_context.eval("
    // String
    if ( ! String.prototype.trim ) {  
      String.prototype.trim = function () {  
        return this.replace(/^\s+|\s+$/g,'');  
      };  
    }
  ")
end
fix_memory_limit!(context) click to toggle source

Disables bytecode compiling which limits you to 64K scripts

# File lib/less/java_script/rhino_context.rb, line 88
def fix_memory_limit!(context)
  if context.respond_to?(:optimization_level=)
    context.optimization_level = -1
  else
    context.instance_eval { @native.setOptimizationLevel(-1) }
  end
end
handle_js_error(e) click to toggle source
# File lib/less/java_script/rhino_context.rb, line 73
def handle_js_error(e)
  if e.value && ( e.value['message'] || e.value['type'].is_a?(String) )
    raise Less::ParseError.new(e, e.value) # LessError
  end
  if e.unwrap.to_s == "missing closing `}`"
    raise Less::ParseError.new(e.unwrap.to_s)
  end
  if e.message && e.message[0, 12] == "Syntax Error"
    raise Less::ParseError.new(e)
  else
    raise Less::Error.new(e)
  end          
end