Class: RubyRunJs::ByteCodeExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_run_js/bytecode_executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeByteCodeExecutor

Returns a new instance of ByteCodeExecutor.



6
7
8
9
10
11
# File 'lib/ruby_run_js/bytecode_executor.rb', line 6

def initialize()
  @label2loc = {}
  @codes = []
  @scopes = []
  @func_return_locs = []
end

Instance Attribute Details

#codesObject (readonly)

Returns the value of attribute codes.



4
5
6
# File 'lib/ruby_run_js/bytecode_executor.rb', line 4

def codes
  @codes
end

Instance Method Details

#_run_under_control(ctx, label_start, label_end) ⇒ Object



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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruby_run_js/bytecode_executor.rb', line 109

def _run_under_control(ctx, label_start, label_end)

  loc_start = @label2loc[label_start]
  loc_end = @label2loc[label_end]
  loc = loc_start

  entry_scope = @scopes.length
  stack_length = ctx.stack.length

  if @debug
    puts "Stack when enter run_under_control: "
    print_stack(ctx.stack)
  end

  while loc < @codes.length || @codes.length == loc_end
    if loc >= loc_end && @scopes.length == entry_scope
      if loc != loc_end
        raise "run_under_control must exit from label_end"
      end
      if ctx.stack.length - stack_length != 1
        raise "Stack must have exactly one value when run_under_control exits"
      end
      return [0, ctx.stack.pop]
    end

    if @debug
      puts 'Will execute: ' + @codes[loc].to_s
    end

    status = @codes[loc].eval(ctx)

    if @debug
      print_stack(ctx.stack)
    end

    if status != nil
      if status.is_a?(Integer)
        loc = @label2loc[status]
        if @scopes.length == entry_scope
          if loc < loc_start || loc >= loc_end
            if ctx.stack.length - stack_length != 1
              if @debug
                puts "Stack when exit run_under_control: "
                print_stack(ctx.stack)
              end
              raise "Stack must have exactly one value when run_under_control exits"
            end
            return [2, ctx.stack.pop, status]
          end
        end
      elsif status == :Return
        if @scopes.length == entry_scope
          if ctx.stack.length - stack_length != 1
            raise "Stack must have exactly one value when run_under_control exits"
          end
          return [1, ctx.stack.pop]
        end
        return_value = ctx.stack.pop()
        ctx = @scopes.pop()
        @current_ctx = ctx
        ctx.stack.push(return_value)
        loc = @func_return_locs.pop()
      elsif status.length == 2
        @scopes.push(ctx)
        @func_return_locs.push(loc + 1)

        loc = @label2loc[status[1]]
        ctx = status[0]
        @current_ctx = ctx
      end
      next
    end
    loc += 1
  end
  raise 'internal error - unexpected end of code, will crash'
end

#call_js_func(func, this, args) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ruby_run_js/bytecode_executor.rb', line 186

def call_js_func(func, this, args)
  if func.is_native
    raise "func is native when call_js_func"
  end
  old_scopes = @scopes
  old_return_locs = @func_return_locs

  ctx = func.generate_my_scope(this, args)

  @scopes = [LocalScope.new(nil, nil)]
  @func_return_locs = [@codes.length]

  return_value = run(ctx, @label2loc[func.code], @debug)

  @scopes = old_scopes
  @func_return_locs = old_return_locs

  return_value
end

#compile(bytecodes) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_run_js/bytecode_executor.rb', line 13

def compile(bytecodes)
  i = 0
  length = bytecodes.length
  while i < length
    code = bytecodes[i]
    if code.is_a?(OPCODES::LABEL)
      @label2loc[code.num] = @codes.length
    else
      @codes.push(code)
    end
    i += 1
  end
end


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_run_js/bytecode_executor.rb', line 27

def print_stack(stack)
  puts 'Stack after execute :'
  stack.each do |v|
    if v.is_a?(Array)
      puts "[#{v.map(&:to_s).join(',')}]"
    else
      puts v
    end
  end
  puts ''
end

#run(ctx, start_loc = 0, debug = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_run_js/bytecode_executor.rb', line 39

def run(ctx, start_loc = 0, debug = false)
  @debug = debug
  loc = start_loc
  @current_ctx = ctx
  while loc < @codes.length
    if @debug
      puts 'Will execute: ' + @codes[loc].to_s
    end
    status = @codes[loc].eval(ctx)

    if @debug
      print_stack(ctx.stack)
    end
    if status != nil
      if status.is_a?(Integer)
        loc = @label2loc[status]
      elsif status == :Return
        return_value = ctx.stack.pop()
        ctx = @scopes.pop()
        @current_ctx = ctx
        ctx.stack.push(return_value)
        loc = @func_return_locs.pop()
      elsif status.length == 2
        @scopes.push(ctx)
        @func_return_locs.push(loc + 1)

        loc = @label2loc[status[1]]
        ctx = status[0]
        @current_ctx = ctx
      end
      next
    end

    loc += 1
  end

  if ctx.stack.length != 1
   # raise "Inernal Error: There must be exactly one value on
   #        the top of stack when codes run done " 
  end

  ctx.stack.pop
end

#run_under_control(ctx, label_start, label_end) ⇒ status, value

status = 0 : normal status = 1 : return status = 2 : jump out status = 3 : error

Returns:

  • (status, value)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_run_js/bytecode_executor.rb', line 88

def run_under_control(ctx, label_start, label_end)
  old_stack_length = ctx.stack.length
  old_ret_length = @func_return_locs.length
  old_scope_length = @scopes.length

  begin
    return _run_under_control(ctx, label_start, label_end)
  rescue JsException => e
    ctx.stack.pop([ctx.stack.length - old_stack_length, 0].max)
    @func_return_locs.pop([@func_return_locs.length - old_ret_length, 0].max)
    @scopes.pop([@scopes.length - old_scope_length, 0].max)

    return [3, e]
  else
    if old_stack_length != ctx.stack.length
      raise "Stack must be not changed after calling run_under_control"
    end
  end

end