Skip to content

Commit

Permalink
Support Module#remove_const (fixes #314)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Aug 2, 2013
1 parent a93c18c commit 0125bb1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
8 changes: 8 additions & 0 deletions corelib/opal/class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ def public(*)
alias private public
alias protected public

def remove_const(name)
%x{
var old = #{self}._scope[name];
delete #{self}._scope[name];
return old;
}
end

def superclass
`#{self}._super || nil`
end
Expand Down
8 changes: 6 additions & 2 deletions lib/opal/grammar.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/opal/grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ lhs:
result = s(:attrasgn, val[0], "#{val[2]}=".intern, s(:arglist))
}
| primary_value '::' CONSTANT
{
result = s(:colon2, val[0], val[2].intern)
}
| '::@' CONSTANT
| backref

Expand Down
4 changes: 4 additions & 0 deletions lib/opal/grammar_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ def new_assign(lhs, rhs)
when :call, :attrasgn
lhs.last << rhs
lhs
when :colon2
lhs << rhs
lhs[0] = :casgn
lhs
else
raise "Bad lhs for new_assign: #{lhs[0]}"
end
Expand Down
7 changes: 7 additions & 0 deletions lib/opal/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,13 @@ def process_cdecl(sexp, level)
[fragment("$scope.#{const} = ", sexp), process(rhs, :expr)]
end

# s(:casgn, s(:const, ::A), :B, val)
# A::B = 100
def process_casgn(sexp, level)
lhs, const, rhs = sexp
[process(lhs), f("._scope.#{const} = ", sexp), process(rhs)]
end

# s(:return [val])
def process_return(sexp, level)
val = process(sexp.shift || s(:nil), :expr)
Expand Down
23 changes: 23 additions & 0 deletions spec/rubyspec/core/module/remove_const_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../fixtures/constants', __FILE__)

describe "Module#remove_const" do
it "removes the constant specified by a String or Symbol from the receiver's constant table" do
ConstantSpecs::ModuleM::CS_CONST252 = :const252
ConstantSpecs::ModuleM::CS_CONST252.should == :const252

ConstantSpecs::ModuleM.send :remove_const, :CS_CONST252
lambda { ConstantSpecs::ModuleM::CS_CONST252 }.should raise_error(NameError)

ConstantSpecs::ModuleM::CS_CONST253 = :const253
ConstantSpecs::ModuleM::CS_CONST253.should == :const253

ConstantSpecs::ModuleM.send :remove_const, "CS_CONST253"
lambda { ConstantSpecs::ModuleM::CS_CONST253 }.should raise_error(NameError)
end

it "returns the value of the removed constant" do
ConstantSpecs::ModuleM::CS_CONST254 = :const254
ConstantSpecs::ModuleM.send(:remove_const, :CS_CONST254).should == :const254
end
end
7 changes: 7 additions & 0 deletions spec/rubyspec/fixtures/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
CS_FALSE = false

module ConstantSpecs

# Included in ModuleD
module ModuleM
CS_CONST10 = :const10_11
CS_CONST24 = :const24
end

class ClassA
CS_CONST10 = :const10_10
CS_CONST16 = :const16
Expand Down

0 comments on commit 0125bb1

Please sign in to comment.