Class: RubyRunJs::ByteCodeGenerator
- Inherits:
-
Object
- Object
- RubyRunJs::ByteCodeGenerator
- Defined in:
- lib/ruby_run_js/bytecode_generator.rb
Instance Attribute Summary collapse
-
#declared_vars ⇒ Object
readonly
Returns the value of attribute declared_vars.
Instance Method Summary collapse
- #emit(target, *args) ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ ByteCodeGenerator
constructor
A new instance of ByteCodeGenerator.
- #new_label ⇒ Object
- #output_code ⇒ Object
- #parseArrayExpression(elements, node) ⇒ Object
- #parseAssignmentExpression(operator, left, right, node) ⇒ Object
- #parseBinaryExpression(operator, left, right, node) ⇒ Object
- #parseBlockStatement(body, node) ⇒ Object
- #parseBreakStatement(label, node) ⇒ Object
- #parseCallExpression(callee, arguments, node) ⇒ Object
- #parseConditionalExpression(test, consequent, alternate, node) ⇒ Object
- #parseContinueStatement(label, node) ⇒ Object
- #parseDebuggerStatement(node) ⇒ Object
- #parseDoWhileStatement(body, test, node) ⇒ Object
- #parseEmptyStatement(node) ⇒ Object
- #parseExpressionStatement(expression, node) ⇒ Object
- #parseForInStatement(left, right, body, node) ⇒ Object
- #parseForStatement(init, test, update, body, node) ⇒ Object
- #parseFunctionDeclaration(id, params, defaults, body, node) ⇒ Object
- #parseFunctionExpression(id, params, defaults, body, node) ⇒ Object
- #parseIdentifier(name, node) ⇒ Object
- #parseIfStatement(test, consequent, alternate, node) ⇒ Object
- #parseLabeledStatement(label, body, node) ⇒ Object
- #parseLexicalDeclaration(declarations, kind, node) ⇒ Object
- #parseLiteral(value, node) ⇒ Object
- #parseLogicalExpression(left, right, operator, node) ⇒ Object
- #parseMemberExpression(computed, object, property, node) ⇒ Object
- #parseNewExpression(callee, arguments, node) ⇒ Object
- #parseObjectExpression(properties, node) ⇒ Object
- #parseProgram(body, node) ⇒ Object
- #parseProperty(kind, key, computed, value, method, shorthand, node) ⇒ Object
- #parseRestElement(argument, node) ⇒ Object
- #parseReturnStatement(argument, node) ⇒ Object
- #parseSequenceExpression(expressions, node) ⇒ Object
- #parseSwitchCase(test, consequent, node) ⇒ Object
- #parseSwitchStatement(discriminant, cases, node) ⇒ Object
- #parseThisExpression(node) ⇒ Object
- #parseThrowStatement(argument, node) ⇒ Object
- #parseTryStatement(block, handlers, finalizer, node) ⇒ Object
- #parseUnaryExpression(operator, argument, node) ⇒ Object
- #parseUpdateExpression(operator, argument, prefix, node) ⇒ Object
- #parseVariableDeclaration(declarations, kind, node) ⇒ Object
- #parseVariableDeclarator(id, init, node) ⇒ Object
- #parseWhileStatement(test, body, node) ⇒ Object
- #parseWithStatement(object, body, node) ⇒ Object
- #pop_state ⇒ Object
- #push_state ⇒ Object
- #to_key(node) ⇒ Object
Constructor Details
#initialize ⇒ ByteCodeGenerator
Returns a new instance of ByteCodeGenerator.
6 7 8 9 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 6 def initialize init() @label_id = 0 end |
Instance Attribute Details
#declared_vars ⇒ Object (readonly)
Returns the value of attribute declared_vars.
4 5 6 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 4 def declared_vars @declared_vars end |
Instance Method Details
#emit(target, *args) ⇒ Object
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 726 def emit(target, *args) if target.is_a?(Class) @bytecodes.push(target.new(*args)) elsif target.is_a?(Array) target.each do |i| emit(i) end else method_symbol = ('parse' + target[:type].to_s).to_sym send_method = method(method_symbol) args = send_method.parameters.map { |param| target[param[1]] } args[-1] = target send(method_symbol, *args) end end |
#init ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 11 def init @bytecodes = [] @implicit_breaks = [] @implicit_continues = [] @declared_continue_labels = {} @declared_break_labels = {} @function_declaration_codes = [] @declared_vars = [] @state = [] end |
#new_label ⇒ Object
51 52 53 54 55 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 51 def new_label id = @label_id @label_id += 1 id end |
#output_code ⇒ Object
26 27 28 29 30 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 26 def output_code codes = @bytecodes init() codes end |
#parseArrayExpression(elements, node) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 58 def parseArrayExpression(elements, node) elements.each do |e| if e.nil? emit(OPCODES::LOAD_UNDEFINED) else emit(e) end end emit(OPCODES::LOAD_ARRAY, elements.length) end |
#parseAssignmentExpression(operator, left, right, node) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 69 def parseAssignmentExpression(operator, left, right, node) if left[:type] == 'MemberExpression' emit(left[:object]) if left[:computed] emit(left[:property]) emit(right) if operator.length > 1 && operator[-1] == '=' emit(OPCODES::STORE_MEMBER_OP, operator[0...-1]) else emit(OPCODES::STORE_MEMBER) end else emit(right) if operator.length > 1 && operator[-1] == '=' emit(OPCODES::STORE_MEMBER_DOT_OP, left[:property][:name], operator[0...-1]) else emit(OPCODES::STORE_MEMBER_DOT, left[:property][:name]) end end elsif left[:type] == 'Identifier' if ['true', 'false', 'this'].include?(left[:name]) raise make_error('SyntaxError', 'Invalid left-hand side in assignment') end emit(right) if operator.length > 1 && operator[-1] == '=' emit(OPCODES::STORE_OP, left[:name], operator[0...-1]) else emit(OPCODES::STORE, left[:name]) end else raise make_error('SyntaxError', 'Invalid left-hand side in assignment') end end |
#parseBinaryExpression(operator, left, right, node) ⇒ Object
103 104 105 106 107 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 103 def parseBinaryExpression(operator, left, right, node) emit(left) emit(right) emit(OPCODES::BINARY_OP, operator) end |
#parseBlockStatement(body, node) ⇒ Object
109 110 111 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 109 def parseBlockStatement(body, node) emit(body) end |
#parseBreakStatement(label, node) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 113 def parseBreakStatement(label, node) if label.nil? emit(OPCODES::JUMP, @implicit_breaks[-1]) else label = label[:name] unless @declared_break_labels.key?(label) raise make_error('SyntaxError', "Undefined label '#{label}'") else emit(OPCODES::JUMP, @declared_break_labels[label]) end end end |
#parseCallExpression(callee, arguments, node) ⇒ Object
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 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 126 def parseCallExpression(callee, arguments, node) if callee[:type] == 'MemberExpression' emit(callee[:object]) if callee[:computed] emit(callee[:property]) if arguments.length > 0 arguments.each do |a| emit(a) end emit(OPCODES::LOAD_N_TUPLE, arguments.length) emit(OPCODES::CALL_METHOD) else emit(OPCODES::CALL_METHOD_NO_ARGS) end else prop_name = callee[:property][:name] if arguments.length > 0 arguments.each do |a| emit(a) end emit(OPCODES::LOAD_N_TUPLE, arguments.length) emit(OPCODES::CALL_METHOD_DOT, prop_name) else emit(OPCODES::CALL_METHOD_DOT_NO_ARGS, prop_name) end end else emit(callee) if arguments.length > 0 arguments.each do |a| emit(a) end emit(OPCODES::LOAD_N_TUPLE, arguments.length) emit(OPCODES::CALL) else emit(OPCODES::CALL_NO_ARGS) end end end |
#parseConditionalExpression(test, consequent, alternate, node) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 166 def parseConditionalExpression(test, consequent, alternate, node) label_alt = new_label() label_end = new_label() emit(test) emit(OPCODES::JUMP_IF_FALSE, label_alt) emit(consequent) emit(OPCODES::JUMP, label_end) emit(OPCODES::LABEL, label_alt) emit(alternate) emit(OPCODES::LABEL, label_end) end |
#parseContinueStatement(label, node) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 181 def parseContinueStatement(label, node) if label.nil? emit(OPCODES::JUMP, @implicit_continues[-1]) else label = label[:name] if @declared_continue_labels.key?(label) raise make_error('SyntaxError', "Undefined label '#{label}'") else emit(OPCODES::JUMP, @declared_continue_labels[label]) end end end |
#parseDebuggerStatement(node) ⇒ Object
194 195 196 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 194 def parseDebuggerStatement(node) parseEmptyStatement(node) end |
#parseDoWhileStatement(body, test, node) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 198 def parseDoWhileStatement(body, test, node) label_continue = new_label() label_break = new_label() label_do = new_label() emit(OPCODES::JUMP, label_do) emit(OPCODES::LABEL, label_continue) emit(test) emit(OPCODES::JUMP_IF_FALSE, label_break) emit(OPCODES::LABEL, label_do) @implicit_continues.push(label_continue) @implicit_breaks.push(label_break) emit(body) @implicit_continues.pop() @implicit_continues.pop() emit(OPCODES::JUMP, label_continue) emit(OPCODES::LABEL, label_break) end |
#parseEmptyStatement(node) ⇒ Object
219 220 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 219 def parseEmptyStatement(node) end |
#parseExpressionStatement(expression, node) ⇒ Object
222 223 224 225 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 222 def parseExpressionStatement(expression, node) emit(OPCODES::POP) emit(expression) end |
#parseForInStatement(left, right, body, node) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 266 def parseForInStatement(left, right, body, node) label_continue = new_label() label_break = new_label() label_start = new_label() if left[:type] == 'VariableDeclaration' if left[:declarations].length != 1 raise make_error( 'SyntaxError', ' Invalid left-hand side in for-in loop: Must have a single binding.' ) end emit(left) name = left[:declarations][0][:id][:name] elsif left[:type] == 'Identifier' name = left[:name] else raise make_error('SyntaxError', 'Invalid left-hand side in for-loop') end emit(right) emit(OPCODES::FOR_IN, name, label_start, label_continue, label_break) emit(OPCODES::LABEL, label_continue) emit(OPCODES::NOP) emit(OPCODES::LABEL, label_start) @implicit_continues.push(label_continue) @implicit_breaks.push(label_break) emit(OPCODES::LOAD_UNDEFINED) emit(body) @implicit_continues.pop() @implicit_breaks.pop() emit(OPCODES::LABEL, label_break) end |
#parseForStatement(init, test, update, body, node) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 227 def parseForStatement(init, test, update, body, node) label_continue = new_label() label_break = new_label() label_start = new_label() unless init.nil? emit(init) if init[:type] != 'VariableDeclaration' emit(OPCODES::POP) end end emit(OPCODES::JUMP, label_start) emit(OPCODES::LABEL, label_continue) unless update.nil? emit(update) emit(OPCODES::POP) end emit(OPCODES::LABEL, label_start) unless test.nil? emit(test) emit(OPCODES::JUMP_IF_FALSE, label_break) end @implicit_continues.push(label_continue) @implicit_breaks.push(label_break) emit(body) @implicit_continues.pop() @implicit_continues.pop() emit(OPCODES::JUMP, label_continue) emit(OPCODES::LABEL, label_break) end |
#parseFunctionDeclaration(id, params, defaults, body, node) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 303 def parseFunctionDeclaration(id, params, defaults, body, node) push_state() function_start = new_label() function_end = new_label() # just skip the function code when the function is not called emit(OPCODES::JUMP, function_end) emit(OPCODES::LABEL, function_start) cur_index = @bytecodes.length emit(body) emit(OPCODES::RETURN) @bytecodes = @bytecodes[0...cur_index].concat(@function_declaration_codes, @bytecodes[cur_index..]) emit(OPCODES::LABEL, function_end) declared_vars = @declared_vars pop_state() name = id[:name] @declared_vars.push(name) # init the function declarations @function_declaration_codes.push( OPCODES::LOAD_FUNCTION.new(function_start, params.map { |p| p[:name] },\ name, true, declared_vars ) ) @function_declaration_codes.push( OPCODES::STORE.new(name) ) @function_declaration_codes.push( OPCODES::POP.new() ) end |
#parseFunctionExpression(id, params, defaults, body, node) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 344 def parseFunctionExpression(id, params, defaults, body, node) push_state() function_start = new_label() function_end = new_label() # just skip the function code when the function is not being called emit(OPCODES::JUMP, function_end) emit(OPCODES::LABEL, function_start) cur_index = @bytecodes.length emit(body) emit(OPCODES::RETURN) @bytecodes = @bytecodes[0...cur_index].concat(@function_declaration_codes, @bytecodes[cur_index..]) emit(OPCODES::LABEL, function_end) declared_vars = @declared_vars pop_state() name = id.nil? ? nil : id[:name] emit(OPCODES::LOAD_FUNCTION, function_start, params.map { |p| p[:name] },\ name, false, declared_vars) end |
#parseIdentifier(name, node) ⇒ Object
374 375 376 377 378 379 380 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 374 def parseIdentifier(name, node) if name == 'undefined' emit(OPCODES::LOAD_UNDEFINED) else emit(OPCODES::LOAD, name) end end |
#parseIfStatement(test, consequent, alternate, node) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 382 def parseIfStatement(test, consequent, alternate, node) label_alt = new_label() label_end = new_label() emit(test) emit(OPCODES::JUMP_IF_FALSE, label_alt) emit(consequent) emit(OPCODES::JUMP, label_end) emit(OPCODES::LABEL, label_alt) unless alternate.nil? emit(alternate) else emit(OPCODES::NOP) end emit(OPCODES::LABEL, label_end) end |
#parseLabeledStatement(label, body, node) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 401 def parseLabeledStatement(label, body, node) label = label[:name] if ['WhileStatement', 'DoWhileStatement', \ 'ForStatement', 'ForInStatement'].include?(body[:type]) @declared_continue_labels[label] = @label_id + 1 @declared_break_labels[label] = @label_id + 2 emit(body) @declared_break_labels.delete(label) @declared_continue_labels.delete(label) else label_break = new_label() @declared_break_labels[label] = label_break emit(body) emit(OPCODES::LABEL, label_break) @declared_break_labels.delete(label) end end |
#parseLexicalDeclaration(declarations, kind, node) ⇒ Object
675 676 677 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 675 def parseLexicalDeclaration(declarations, kind, node) raise "'Not supported by ECMA 5.1" end |
#parseLiteral(value, node) ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 419 def parseLiteral(value, node) if value == nil emit(OPCODES::LOAD_NULL) elsif value == true || value == false emit(OPCODES::LOAD_BOOLEAN, value) elsif value.is_a?(String) emit(OPCODES::LOAD_STRING, value) elsif value.is_a?(Numeric) emit(OPCODES::LOAD_NUMBER, value.to_f) elsif node[:regexp] != nil emit(OPCODES::LOAD_REGEXP, node[:regexp][:pattern], node[:regexp][:flags]) else raise "Unsupported literal: #{value}" end end |
#parseLogicalExpression(left, right, operator, node) ⇒ Object
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 435 def parseLogicalExpression(left, right, operator, node) label_end = new_label() if operator == '&&' emit(left) emit(OPCODES::JUMP_IF_FALSE_WITHOUT_POP, label_end) emit(OPCODES::POP) emit(right) emit(OPCODES::LABEL, label_end) elsif operator == '||' emit(left) emit(OPCODES::JUMP_IF_TRUE_WITHOUT_POP, label_end) emit(OPCODES::POP) emit(right) emit(OPCODES::LABEL, label_end) else raise "Unknown logical expression: #{operator}" end end |
#parseMemberExpression(computed, object, property, node) ⇒ Object
454 455 456 457 458 459 460 461 462 463 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 454 def parseMemberExpression(computed, object, property, node) if computed emit(object) emit(property) emit(OPCODES::LOAD_MEMBER) else emit(object) emit(OPCODES::LOAD_MEMBER_DOT, property[:name]) end end |
#parseNewExpression(callee, arguments, node) ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 465 def parseNewExpression(callee, arguments, node) emit(callee) if arguments.length > 0 arguments.each do |a| emit(a) end emit(OPCODES::LOAD_N_TUPLE, arguments.length) emit(OPCODES::NEW) else emit(OPCODES::NEW_NO_ARGS) end end |
#parseObjectExpression(properties, node) ⇒ Object
495 496 497 498 499 500 501 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 495 def parseObjectExpression(properties, node) data = properties.map do |prop| emit(prop[:value]) [to_key(prop[:key]), prop[:kind]] end emit(OPCODES::LOAD_OBJECT, data) end |
#parseProgram(body, node) ⇒ Object
503 504 505 506 507 508 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 503 def parseProgram(body, node) cur_index = @bytecodes.length emit(OPCODES::LOAD_UNDEFINED) emit(body) @bytecodes = @bytecodes[0...cur_index].concat(@function_declaration_codes, @bytecodes[cur_index..]) end |
#parseProperty(kind, key, computed, value, method, shorthand, node) ⇒ Object
510 511 512 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 510 def parseProperty(kind, key, computed, value, method, shorthand, node) raise "Not available in ECMA 5.1" end |
#parseRestElement(argument, node) ⇒ Object
514 515 516 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 514 def parseRestElement(argument, node) raise "Not available in ECMA 5.1" end |
#parseReturnStatement(argument, node) ⇒ Object
518 519 520 521 522 523 524 525 526 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 518 def parseReturnStatement(argument, node) emit(OPCODES::POP) if argument.nil? emit(OPCODES::LOAD_UNDEFINED) else emit(argument) end emit(OPCODES::RETURN) end |
#parseSequenceExpression(expressions, node) ⇒ Object
528 529 530 531 532 533 534 535 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 528 def parseSequenceExpression(expressions, node) expressions.each_index do |i| emit(expressions[i]) if i < expressions.length - 1 emit(OPCODES::POP) end end end |
#parseSwitchCase(test, consequent, node) ⇒ Object
537 538 539 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 537 def parseSwitchCase(test, consequent, node) raise "Already implemented in SwitchStatement" end |
#parseSwitchStatement(discriminant, cases, node) ⇒ Object
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 541 def parseSwitchStatement(discriminant, cases, node) emit(discriminant) labels = cases.map { |c| new_label() } tests = cases.map { |c| c[:test] } consequents = cases.map { |c| c[:consequent] } label_switch_end = new_label() cases.length.times do |i| test = tests[i] if test.nil? # default emit(OPCODES::POP) emit(OPCODES::JUMP, labels[i]) else emit(test) emit(OPCODES::JUMP_IF_EQ, labels[i]) end end emit(OPCODES::POP) emit(OPCODES::JUMP, label_switch_end) @implicit_breaks << label_switch_end labels.length.times do |i| emit(OPCODES::LABEL, labels[i]) consequents[i].each do |c| emit(c) end end @implicit_breaks.pop() emit(OPCODES::LABEL, label_switch_end) end |
#parseThisExpression(node) ⇒ Object
576 577 578 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 576 def parseThisExpression(node) emit(OPCODES::LOAD_THIS) end |
#parseThrowStatement(argument, node) ⇒ Object
580 581 582 583 584 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 580 def parseThrowStatement(argument, node) emit(OPCODES::POP) emit(argument) emit(OPCODES::THROW) end |
#parseTryStatement(block, handlers, finalizer, node) ⇒ Object
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 586 def parseTryStatement(block, handlers, finalizer, node) label_try = new_label() label_catch = new_label() label_finally = new_label() label_end = new_label() handler = handlers[0] catch_var_name = handler != nil ? handler[:param][:name] : nil emit(OPCODES::TRY_CATCH_FINALLY, label_try, label_catch, catch_var_name, label_finally, finalizer != nil, label_end) emit(OPCODES::LABEL, label_try) emit(OPCODES::LOAD_UNDEFINED) emit(block) emit(OPCODES::NOP) emit(OPCODES::LABEL, label_catch) emit(OPCODES::LOAD_UNDEFINED) if handler != nil emit(handler[:body]) end emit(OPCODES::NOP) emit(OPCODES::LABEL, label_finally) emit(OPCODES::LOAD_UNDEFINED) if finalizer != nil emit(finalizer) end emit(OPCODES::NOP) emit(OPCODES::LABEL, label_end) end |
#parseUnaryExpression(operator, argument, node) ⇒ Object
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 621 def parseUnaryExpression(operator, argument, node) if operator == 'typeof' && argument[:type] == 'Identifier' emit(OPCODES::TYPEOF, argument[:name]) elsif operator == 'delete' if argument[:type] == 'MemberExpression' emit(argument[:object]) if argument[:property][:type] == 'Identifier' emit(OPCODES::LOAD_STRING, argument[:property][:name]) else emit(argument[:property]) end emit(OPCODES::DELETE_MEMBER) elsif argument[:type] == 'Identifier' emit(OPCODES::DELETE, argument[:name]) else emit(OPCODES::LOAD_BOOLEAN, true) end elsif ['+', '-', '!', '~', 'void', 'typeof'].include?(operator) emit(argument) emit(OPCODES::UNARY_OP, operator) else raise make_error('SyntaxError', \ "Unknown unary operator #{operator}") end end |
#parseUpdateExpression(operator, argument, prefix, node) ⇒ Object
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 647 def parseUpdateExpression(operator, argument, prefix, node) incr = operator == '++' post = !prefix if argument[:type] == 'MemberExpression' if argument[:computed] emit(argument[:object]) emit(argument[:property]) emit(OPCODES::POSTFIX_MEMBER, post, incr) else emit(argument[:object]) name = to_key(argument[:property]) emit(OPCODES::POSTFIX_MEMBER_DOT, post, incr, name) end elsif argument[:type] == 'Identifier' name = to_key(argument) emit(OPCODES::POSTFIX, post, incr, name) else raise make_error('SyntaxError', 'Invalid left-hand side in assignment') end end |
#parseVariableDeclaration(declarations, kind, node) ⇒ Object
669 670 671 672 673 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 669 def parseVariableDeclaration(declarations, kind, node) declarations.each do |d| emit(d) end end |
#parseVariableDeclarator(id, init, node) ⇒ Object
679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 679 def parseVariableDeclarator(id, init, node) name = id[:name] if ['true', 'false', 'this'].include?(name) raise make_error('SyntaxError', 'Invalid left-hand side in assignment') end @declared_vars.push(name) if init != nil emit(init) emit(OPCODES::STORE, name) emit(OPCODES::POP) end end |
#parseWhileStatement(test, body, node) ⇒ Object
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 693 def parseWhileStatement(test, body, node) label_continue = new_label() label_break = new_label() emit(OPCODES::LABEL, label_continue) emit(test) emit(OPCODES::JUMP_IF_FALSE, label_break) @implicit_continues.append(label_continue) @implicit_breaks.append(label_break) emit(body) @implicit_continues.pop() @implicit_breaks.pop() emit(OPCODES::JUMP, label_continue) emit(OPCODES::LABEL, label_break) end |
#parseWithStatement(object, body, node) ⇒ Object
711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 711 def parseWithStatement(object, body, node) label_start = new_label() label_end = new_label() emit(body) emit(OPCODES::WITH, label_start, label_end) emit(OPCODES::LABEL, label_start) emit(OPCODES::LOAD_UNDEFINED) emit(body) emit(OPCODES::NOP) emit(OPCODES::LABEL, label_end) end |
#pop_state ⇒ Object
45 46 47 48 49 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 45 def pop_state() @implicit_breaks, @implicit_continues, \ @declared_continue_labels, @declared_break_labels,\ @function_declaration_codes, @declared_vars = @state.pop() end |
#push_state ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 32 def push_state() @state << [ @implicit_breaks, @implicit_continues,\ @declared_continue_labels, @declared_break_labels,\ @function_declaration_codes, @declared_vars ] @implicit_breaks = [] @implicit_continues = [] @declared_continue_labels = {} @declared_break_labels = {} @function_declaration_codes = [] @declared_vars = [] end |
#to_key(node) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/ruby_run_js/bytecode_generator.rb', line 478 def to_key(node) if node[:type] == 'Identifier' return node[:name] end if node[:type] == 'Literal' v = node[:value] if v.js_type == :Number && v.to_i == v return v.to_i.to_s elsif node[:regexp] != nil return "/#{node[:body]}/#{node[:flags]}" elsif v == nil return 'null' end return v.to_s end end |