lib/spectus/level/base.rb in spectus-2.7.0 vs lib/spectus/level/base.rb in spectus-2.7.1

- old
+ new

@@ -63,11 +63,15 @@ self.class.name.split('::').last.to_sym end # @return [Sandbox] The sandbox. def sandbox - Sandbox.new(@req, @negate, @subject, *@challenges) + if Process.respond_to?(:fork) + fork_and_return { Sandbox.new(@req, @negate, @subject, *@challenges) } + else + Sandbox.new(@req, @negate, @subject, *@challenges) + end end # @param state [Sandbox] The sandbox that tested the code. # @param result [Boolean] The result of the test. # @@ -117,9 +121,28 @@ .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-_', ' ') .downcase + end + + # Run the code in a separate process. + # + # @api private + def fork_and_return + read, write = IO.pipe + + pid = fork do + read.close + result = yield + Marshal.dump(result, write) + exit!(0) + end + + write.close + result = read.read + Process.wait(pid) + Marshal.load(result) end end end end