Class: Binding

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

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.



3
4
5
6
7
# File 'opal/opal/corelib/binding.rb', line 3

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.



44
45
46
# File 'opal/opal/corelib/binding.rb', line 44

def receiver
  @receiver
end

#source_locationObject (readonly)

Returns the value of attribute source_location.



44
45
46
# File 'opal/opal/corelib/binding.rb', line 44

def source_location
  @source_location
end

Instance Method Details

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



38
39
40
41
42
# File 'opal/opal/corelib/binding.rb', line 38

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

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

#irbObject



108
109
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
# File 'opal/opal/corelib/irb.rb', line 108

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



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

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:



34
35
36
# File 'opal/opal/corelib/binding.rb', line 34

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

#local_variable_get(symbol) ⇒ Object



17
18
19
20
21
# File 'opal/opal/corelib/binding.rb', line 17

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



23
24
25
26
27
28
# File 'opal/opal/corelib/binding.rb', line 23

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



30
31
32
# File 'opal/opal/corelib/binding.rb', line 30

def local_variables
  @scope_variables
end