forked from rubyjs/therubyrhino
/
context.rb
171 lines (148 loc) · 5.02 KB
/
context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module Rhino
# ==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.
class Context
attr_reader :scope
class << self
# 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.
def open(options = {}, &block)
ContextFactory.new.call do |native|
block.call(new(native, options))
end
end
private :new
end
def initialize(native, options) #:nodoc:
@native = native
@global = NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true))
if with = options[:with]
@scope = To.javascript(with)
@scope.setParentScope(@global.j)
else
@scope = @global
end
unless options[:java]
for package in ["Packages", "java", "org", "com"]
@global.j.delete(package)
end
end
end
# Read a value from the global scope of this context
def [](k)
@scope[k]
end
# 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.
def []=(k,v)
@scope[k] = v
end
# Evaluate a string of javascript in this context:
# * <tt>source</tt> - the javascript source code to evaluate. This can be either a string or an IO object.
# * <tt>source_name</tt> - associated name for this source code. Mainly useful for backtraces.
# * <tt>line_number</tt> - associate this number with the first line of executing source. Mainly useful for backtraces
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
# 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
def instruction_limit=(limit)
@native.setInstructionObserverThreshold(limit);
@native.factory.instruction_limit = limit
end
end
class IOReader < Java::JavaIo::Reader #:nodoc:
def initialize(io)
@io = io
end
def read(charbuffer, offset, length)
begin
str = @io.read(length)
if str.nil?
return -1
else
jstring = Java::JavaLang::String.new(str)
for i in 0 .. jstring.length - 1
charbuffer[i + offset] = jstring.charAt(i)
end
return jstring.length
end
rescue StandardError => e
raise Java::JavaIo::IOException.new, "Failed reading from ruby IO object"
end
end
end
class ContextFactory < J::ContextFactory # :nodoc:
def observeInstructionCount(cxt, count)
raise RunawayScriptError, "script exceeded allowable instruction count" if count > @limit
end
def instruction_limit=(count)
@limit = count
end
end
class RhinoError < StandardError # :nodoc:
def initialize(native)
@native = native
end
def message
@native.cause.details
end
def javascript_backtrace
@native.getScriptStackTrace()
end
end
class RunawayScriptError < StandardError # :nodoc:
end
end