Class: Rhino::Context
Overview
Overview
All Javascript must be executed in a context which represents the execution environment in
which scripts will run. The environment consists of the standard javascript objects
and functions like Object, String, Array, etc... as well as any objects or functions which
have been defined in it. e.g.
Context.open do |cxt|
cxt['num'] = 5
cxt.eval('num + 5') #=> 10
end
Multiple Contexts.
The same object may appear in any number of contexts, but only one context may be executing javascript code in any given thread. If a new context is opened in a thread in which a context is already opened, the second context will “mask” the old context e.g.
six = 6
Context.open do |cxt|
cxt['num'] = 5
cxt.eval('num') # => 5
Context.open do |cxt|
cxt['num'] = 10
cxt.eval('num') # => 10
cxt.eval('++num') # => 11
end
cxt.eval('num') # => 5
end
Notes
While there are many similarities between Rhino::Context and Java::OrgMozillaJavascript::Context, they are not the same thing and should not be confused.
Instance Attribute Summary collapse
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
-
.open(options = {}, &block) ⇒ Object
initalize a new context with a fresh set of standard objects.
Instance Method Summary collapse
-
#[](k) ⇒ Object
Read a value from the global scope of this context.
-
#[]=(k, v) ⇒ Object
Set a value in the global scope of this context.
-
#eval(source, source_name = "<eval>", line_number = 1) ⇒ Object
Evaluate a string of javascript in this context: *
source
- the javascript source code to evaluate. -
#initialize(native, options) ⇒ Context
constructor
:nodoc:.
-
#instruction_limit=(limit) ⇒ Object
Set the maximum number of instructions that this context will execute.
-
#load(filename) ⇒ Object
Read the contents of
filename
and evaluate it as javascript.
Constructor Details
#initialize(native, options) ⇒ Context
:nodoc:
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rhino/context.rb', line 51 def initialize(native, ) #:nodoc: @native = native @global = NativeObject.new(@native.initStandardObjects(nil, [:sealed] == true)) if with = [:with] @scope = To.javascript(with) @scope.setParentScope(@global.j) else @scope = @global end unless [:java] for package in ["Packages", "java", "org", "com"] @global.j.delete(package) end end end |
Instance Attribute Details
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
36 37 38 |
# File 'lib/rhino/context.rb', line 36 def scope @scope end |
Class Method Details
.open(options = {}, &block) ⇒ Object
initalize a new context with a fresh set of standard objects. All operations on the context should be performed in the block that is passed.
42 43 44 45 46 |
# File 'lib/rhino/context.rb', line 42 def open( = {}, &block) ContextFactory.new.call do |native| block.call(new(native, )) end end |
Instance Method Details
#[](k) ⇒ Object
Read a value from the global scope of this context
68 69 70 |
# File 'lib/rhino/context.rb', line 68 def [](k) @scope[k] end |
#[]=(k, v) ⇒ Object
Set a value in the global scope of this context. This value will be visible to all the javascript that is executed in this context.
74 75 76 |
# File 'lib/rhino/context.rb', line 74 def []=(k,v) @scope[k] = v end |
#eval(source, source_name = "<eval>", line_number = 1) ⇒ Object
Evaluate a string of javascript in this context:
-
source
- the javascript source code to evaluate. This can be either a string or an IO object. -
source_name
- associated name for this source code. Mainly useful for backtraces. -
line_number
- associate this number with the first line of executing source. Mainly useful for backtraces
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rhino/context.rb', line 82 def eval(source, source_name = "<eval>", line_number = 1) begin scope = To.javascript(@scope) if IO === source || StringIO === source result = @native.evaluateReader(scope, IOReader.new(source), source_name, line_number, nil) else result = @native.evaluateString(scope, source.to_s, source_name, line_number, nil) end To.ruby result rescue J::RhinoException => e raise Rhino::RhinoError, e end end |
#instruction_limit=(limit) ⇒ Object
Set the maximum number of instructions that this context will execute. If this instruction limit is exceeded, then a Rhino::RunawayScriptError will be raised
112 113 114 115 |
# File 'lib/rhino/context.rb', line 112 def instruction_limit=(limit) @native.setInstructionObserverThreshold(limit); @native.factory.instruction_limit = limit end |
#load(filename) ⇒ Object
Read the contents of filename
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
103 104 105 106 107 |
# File 'lib/rhino/context.rb', line 103 def load(filename) File.open(filename) do |file| eval file, filename, 1 end end |