Skip to content

Commit

Permalink
Can freeze Context to lock down definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
project-eutopia committed Jan 10, 2018
1 parent 1809b57 commit 87115b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/keisan/context.rb
Expand Up @@ -14,6 +14,12 @@ def allow_recursive!
@allow_recursive = true
end

def freeze
super
@function_registry.freeze
@variable_registry.freeze
end

# A transient context does not persist variables and functions in this context, but
# rather store them one level higher in the parent context. When evaluating a string,
# the entire operation is done in a transient context that is unique from the calculators
Expand Down
19 changes: 19 additions & 0 deletions spec/keisan/context_spec.rb
Expand Up @@ -17,6 +17,25 @@
expect(my_context.function("sin")).to be_a(Keisan::Function)
end

describe "freeze" do
it "freezes associated registries" do
frozen_context = described_class.new
frozen_context.register_variable!("x", 1)
frozen_context.register_function!("f", Proc.new {|x| x})
frozen_context.freeze

child_context = frozen_context.spawn_child
child_context.register_variable!("x", 2)
child_context.register_function!("f", Proc.new {|x| 2*x})

expect(frozen_context.variable("x").value).to eq 1
expect(child_context.variable("x").value).to eq 2

expect(frozen_context.function("f").call(nil, 3).value).to eq 3
expect(child_context.function("f").call(nil, 3).value).to eq 6
end
end

describe "spawn_child" do
it "has parent context's variables and functions" do
my_context = described_class.new
Expand Down

0 comments on commit 87115b4

Please sign in to comment.