lib/opal/nodes/constants.rb in opal-0.10.6 vs lib/opal/nodes/constants.rb in opal-0.11.0.rc1

- old
+ new

@@ -1,78 +1,58 @@ +# frozen_string_literal: true require 'opal/nodes/base' module Opal module Nodes class ConstNode < Base handle :const - children :name + children :const_scope, :name def compile - if name == :DATA and compiler.eof_content + if magical_data_const? push("$__END__") + elsif const_scope + push "Opal.const_get_qualified(", recv(const_scope), ", '#{name}')" + elsif compiler.eval? + push "Opal.const_get_relative($nesting, '#{name}')" else - push "$scope.get('#{name}')" + push "Opal.const_get_relative($nesting, '#{name}')" end end + + # Ruby has a magical const DATA + # that should be processed in a different way: + # 1. When current file contains __END__ in the end of the file + # DATA const should be resolved to the string located after __END__ + # 2. When current file doesn't have __END__ section + # DATA const should be resolved to a regular ::DATA constant + def magical_data_const? + const_scope.nil? && name == :DATA and compiler.eof_content + end end - class ConstDeclarationNode < Base - handle :cdecl + # ::CONST + # s(:const, s(:cbase), :CONST) + class CbaseNode < Base + handle :cbase - children :name, :base - def compile - push expr(base) - wrap "Opal.cdecl($scope, '#{name}', ", ")" + push "'::'" end end class ConstAssignNode < Base handle :casgn children :base, :name, :value def compile - push "Opal.casgn(" - push expr(base) - push ", '#{name}', " - push expr(value) - push ")" - end - end - - class ConstGetNode < Base - handle :colon2 - - children :base, :name - - def compile - push "((" - push expr(base) - push ").$$scope.get('#{name}'))" - end - end - - class TopConstNode < Base - handle :colon3 - - children :name - - def compile - push "Opal.get('#{name}')" - end - end - - class TopConstAssignNode < Base - handle :casgn3 - - children :name, :value - - def compile - push "Opal.casgn(Opal.Object, '#{name}', " - push expr(value) - push ")" + if base + push "Opal.const_set(", expr(base), ", '#{name}', ", expr(value), ")" + else + push "Opal.const_set($nesting[0], '#{name}', ", expr(value), ")" + end end end end end