From cea2f6a380b52e5723b4538d7fd3649826b00038 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 7 Jun 2011 21:57:12 -0500 Subject: [PATCH] Fix rhino memory limit issue --- lib/execjs/ruby_rhino_runtime.rb | 11 +++++++++++ test/test_runtime.rb | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index 9f21371..b971bd6 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -5,6 +5,7 @@ module ExecJS source = source.encode('UTF-8') if source.respond_to?(:encode) @rhino_context = ::Rhino::Context.new + fix_memory_limit! @rhino_context @rhino_context.eval(source) end @@ -53,6 +54,16 @@ module ExecJS value end end + + private + # Disables bytecode compiling which limits you to 64K scripts + def fix_memory_limit!(context) + if context.respond_to?(:optimization_level=) + context.optimization_level = -1 + else + context.instance_eval { @native.setOptimizationLevel(-1) } + end + end end def name diff --git a/test/test_runtime.rb b/test/test_runtime.rb index c25cfe5..2b4f44e 100644 --- a/test/test_runtime.rb +++ b/test/test_runtime.rb @@ -86,6 +86,11 @@ class TestRuntime < Test::Unit::TestCase assert_equal true, @runtime.exec("return this === (function() {return this})()") end + def test_compile_large_scripts + body = "var foo = 'bar';\n" * 100_000 + assert @runtime.exec("function foo() {\n#{body}\n};\nreturn true") + end + def test_syntax_error assert_raise ExecJS::RuntimeError do @runtime.exec(")")