Class: PAC::Runtimes::MustangRuntime::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/pac/runtimes/mustang.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = "") ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
# File 'lib/pac/runtimes/mustang.rb', line 5

def initialize(source = "")
  source = source.encode("UTF-8") if source.respond_to?(:encode)

  @v8_context = ::Mustang::Context.new
  @v8_context.eval(source)
end

Instance Method Details

#call(properties, *args) ⇒ Object



18
19
20
21
22
# File 'lib/pac/runtimes/mustang.rb', line 18

def call(properties, *args)
  unbox @v8_context.eval(properties).call(*args)
rescue NoMethodError => e
  raise ProgramError, e.message
end

#include(mod) ⇒ Object



12
13
14
15
16
# File 'lib/pac/runtimes/mustang.rb', line 12

def include(mod)
  (mod.methods - Module.methods).each do |name|
    @v8_context[name] = mod.method(name)
  end
end

#unbox(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pac/runtimes/mustang.rb', line 24

def unbox(value)
  case value
  when ::Mustang::V8::Array
    value.map { |v| unbox(v) }
  when ::Mustang::V8::Boolean
    value.to_bool
  when ::Mustang::V8::NullClass, ::Mustang::V8::UndefinedClass
    nil
  when ::Mustang::V8::Function
    nil
  when ::Mustang::V8::SyntaxError
    raise RuntimeError, value.message
  when ::Mustang::V8::Error
    raise ProgramError, value.message
  else
    value.respond_to?(:delegate) ? value.delegate : value
  end
end