mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
support for loading javascript from file referenced by name
This commit is contained in:
parent
ee10f2ef28
commit
e072c82420
3 changed files with 47 additions and 11 deletions
30
README.rdoc
30
README.rdoc
|
@ -22,16 +22,13 @@ Embed the Mozilla Rhino Javascript interpreter into Ruby
|
|||
# evaluate some simple javascript
|
||||
eval_js "7 * 6" #=> 42
|
||||
|
||||
# evaluate bytes read from any File/IO object:
|
||||
File.open("mysource.js") do |file|
|
||||
eval_js file, "mysource.js"
|
||||
|
||||
#or in the context of a controlled scope
|
||||
Rhino::Context.open do |context|
|
||||
context['foo'] = 'some global variable referenced in mysource.js'
|
||||
context.eval(file, "mysource.js")
|
||||
# that's quick and dirty, but if you want more control over your
|
||||
# environment, use a Context:
|
||||
Rhino::Context.open do |cxt|
|
||||
cxt['foo'] = "bar"
|
||||
cxt.eval('foo') # => "bar"
|
||||
end
|
||||
|
||||
|
||||
# evaluate a ruby function from javascript
|
||||
|
||||
Rhino::Context.open do |context|
|
||||
|
@ -73,6 +70,21 @@ Embed the Mozilla Rhino Javascript interpreter into Ruby
|
|||
Rhino::Context.open(:java => true) do |context|
|
||||
context.eval("java.lang.System.exit()") #it's dangerous!
|
||||
end
|
||||
|
||||
==== Different ways of loading javascript source
|
||||
|
||||
In addition to just evaluating strings, you can also use streams such as files.
|
||||
|
||||
# evaluate bytes read from any File/IO object:
|
||||
File.open("mysource.js") do |file|
|
||||
eval_js file, "mysource.js"
|
||||
end
|
||||
|
||||
# or load it by filename
|
||||
Rhino::Context.open do |context|
|
||||
context.load("mysource.js")
|
||||
end
|
||||
|
||||
|
||||
== REQUIREMENTS:
|
||||
|
||||
|
|
|
@ -92,7 +92,20 @@ module Rhino
|
|||
raise Rhino::RhinoError, e
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Read the contents of <tt>filename</tt> and evaluate it as javascript. Returns the result of evaluating the
|
||||
# javascript. e.g.
|
||||
#
|
||||
# Context.open do |cxt|
|
||||
# cxt.load("path/to/some/lib.js")
|
||||
# end
|
||||
#
|
||||
def load(filename)
|
||||
File.open(filename) do |file|
|
||||
eval file, filename, 1
|
||||
end
|
||||
end
|
||||
|
||||
# Set the maximum number of instructions that this context will execute.
|
||||
# If this instruction limit is exceeded, then a Rhino::RunawayScriptError
|
||||
# will be raised
|
||||
|
|
|
@ -124,7 +124,7 @@ describe Rhino::Context do
|
|||
}.should raise_error
|
||||
end
|
||||
|
||||
describe "compilation" do
|
||||
describe "loading javascript source into the interpreter" do
|
||||
|
||||
it "can take an IO object in the eval method instead of a string" do
|
||||
source = StringIO.new(<<-EOJS)
|
||||
|
@ -147,5 +147,16 @@ five();
|
|||
end
|
||||
end
|
||||
|
||||
it "can load a file into the runtime" do
|
||||
mock(:JavascriptSourceFile).tap do |file|
|
||||
File.should_receive(:open).with("path/to/mysource.js").and_yield(file)
|
||||
Context.open do |cxt|
|
||||
cxt.should_receive(:eval).with(file, "path/to/mysource.js", 1)
|
||||
cxt.load("path/to/mysource.js")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue