Class: Binding

Inherits:
Object show all
Defined in:
opal/opal/corelib/binding.rb,
opal/opal/corelib/irb.rb

Overview

backtick_javascript: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jseval, scope_variables = [], receiver = undefined, source_location = nil) ⇒ Binding

Returns a new instance of Binding.



5
6
7
8
9
# File 'opal/opal/corelib/binding.rb', line 5

def initialize(jseval, scope_variables = [], receiver = undefined, source_location = nil)
  @jseval, @scope_variables, @receiver, @source_location = \
    jseval, scope_variables, receiver, source_location
  receiver = js_eval('self') unless `typeof receiver !== undefined`
end

Instance Attribute Details

#receiverObject (readonly)

Returns the value of attribute receiver.



46
47
48
# File 'opal/opal/corelib/binding.rb', line 46

def receiver
  @receiver
end

#source_locationObject (readonly)

Returns the value of attribute source_location.



46
47
48
# File 'opal/opal/corelib/binding.rb', line 46

def source_location
  @source_location
end

Instance Method Details

#eval(str, file = nil, line = nil) ⇒ Object



40
41
42
43
44
# File 'opal/opal/corelib/binding.rb', line 40

def eval(str, file = nil, line = nil)
  return receiver if str == 'self'

  ::Kernel.eval(str, self, file, line)
end

#irbObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
163
164
165
166
167
168
# File 'opal/opal/corelib/irb.rb', line 110

def irb
  ::Opal::IRB.ensure_loaded('opal-replutils')

  silencer = ::Opal::IRB::Silencer.new

  ::Opal::IRB.prepare_console do
    loop do
      print '>> '
      line = gets
      break unless line
      code = ''

      puts line if ::Opal::IRB.browser?

      if line.start_with? 'ls '
        code = line[3..-1]
        mode = :ls
      elsif line == "ls\n"
        code = 'self'
        mode = :ls
      elsif line.start_with? 'show '
        code = line[5..-1]
        mode = :show
      else
        code = line
        mode = :inspect
      end

      js_code = nil

      begin
        silencer.silence do
          js_code = `Opal.compile(code, {irb: true})`
        end
      rescue SyntaxError => e
        if ::Opal::IRB::LINEBREAKS.include?(e.message)
          print '.. '
          line = gets
          return unless line
          puts line if ::Opal::IRB.browser?
          code += line
          retry
        elsif silencer.warnings.empty?
          warn e.full_message
        else
          # Most likely a parser error
          warn silencer.warnings
        end
      end

      if mode == :show
        puts js_code
        return
      end

      puts ::REPLUtils.eval_and_print(js_code, mode, false, self)
    end
  end
end

#js_eval(*args) ⇒ Object



11
12
13
14
15
16
17
# File 'opal/opal/corelib/binding.rb', line 11

def js_eval(*args)
  if @jseval
    @jseval.call(*args)
  else
    ::Kernel.raise 'Evaluation on a Proc#binding is not supported'
  end
end

#local_variable_defined?(value) ⇒ Boolean

Returns:



36
37
38
# File 'opal/opal/corelib/binding.rb', line 36

def local_variable_defined?(value)
  @scope_variables.include?(value)
end

#local_variable_get(symbol) ⇒ Object



19
20
21
22
23
# File 'opal/opal/corelib/binding.rb', line 19

def local_variable_get(symbol)
  js_eval(symbol)
rescue ::Exception
  ::Kernel.raise ::NameError, "local variable `#{symbol}' is not defined for #{inspect}"
end

#local_variable_set(symbol, value) ⇒ Object



25
26
27
28
29
30
# File 'opal/opal/corelib/binding.rb', line 25

def local_variable_set(symbol, value)
  `Opal.Binding.tmp_value = value`
  js_eval("#{symbol} = Opal.Binding.tmp_value")
  `delete Opal.Binding.tmp_value`
  value
end

#local_variablesObject



32
33
34
# File 'opal/opal/corelib/binding.rb', line 32

def local_variables
  @scope_variables
end