mini_racer 0.1.14 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module MiniRacer
2
- VERSION = "0.1.14"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/mini_racer.rb CHANGED
@@ -59,15 +59,9 @@ module MiniRacer
59
59
  raise ArgumentError, "snapshot must be a Snapshot object, passed a #{snapshot.inspect}"
60
60
  end
61
61
 
62
- @lock = Mutex.new
63
-
64
62
  # defined in the C class
65
63
  init_with_snapshot(snapshot)
66
64
  end
67
-
68
- def with_lock
69
- @lock.synchronize { yield }
70
- end
71
65
  end
72
66
 
73
67
  class Platform
@@ -136,8 +130,6 @@ module MiniRacer
136
130
  end
137
131
  end
138
132
 
139
- attr_reader :isolate
140
-
141
133
  def initialize(options = nil)
142
134
  options ||= {}
143
135
 
@@ -148,8 +140,11 @@ module MiniRacer
148
140
  @max_memory = nil
149
141
  @current_exception = nil
150
142
  @timeout = options[:timeout]
151
- @max_memory = options[:max_memory]
152
- @isolate = options[:isolate] || Isolate.new(options[:snapshot])
143
+ if options[:max_memory].is_a?(Numeric) && options[:max_memory] > 0
144
+ @max_memory = options[:max_memory]
145
+ end
146
+ # false signals it should be fetched if requested
147
+ @isolate = options[:isolate] || false
153
148
  @disposed = false
154
149
 
155
150
  @callback_mutex = Mutex.new
@@ -157,10 +152,14 @@ module MiniRacer
157
152
  @thread_raise_called = false
158
153
  @eval_thread = nil
159
154
 
160
- isolate.with_lock do
161
- # defined in the C class
162
- init_with_isolate(@isolate)
163
- end
155
+ # defined in the C class
156
+ init_unsafe(options[:isolate], options[:snapshot])
157
+ end
158
+
159
+ def isolate
160
+ return @isolate if @isolate != false
161
+ # defined in the C class
162
+ @isolate = create_isolate_value
164
163
  end
165
164
 
166
165
  def load(filename)
@@ -174,7 +173,7 @@ module MiniRacer
174
173
  filename = options && options[:filename].to_s
175
174
 
176
175
  @eval_thread = Thread.current
177
- isolate.with_lock do
176
+ isolate_mutex.synchronize do
178
177
  @current_exception = nil
179
178
  timeout do
180
179
  eval_unsafe(str, filename)
@@ -184,41 +183,67 @@ module MiniRacer
184
183
  @eval_thread = nil
185
184
  end
186
185
 
187
- def dispose
188
- if !@disposed
189
- isolate.with_lock do
190
- dispose_unsafe
186
+ def call(function_name, *arguments)
187
+ raise(ContextDisposedError, 'attempted to call function on a disposed context!') if @disposed
188
+
189
+ @eval_thread = Thread.current
190
+ isolate_mutex.synchronize do
191
+ timeout do
192
+ call_unsafe(function_name, *arguments)
191
193
  end
192
- @disposed = true
193
194
  end
195
+ ensure
196
+ @eval_thread = nil
197
+ end
198
+
199
+ def dispose
200
+ return if @disposed
201
+ isolate_mutex.synchronize do
202
+ dispose_unsafe
203
+ end
204
+ @disposed = true
205
+ @isolate = nil # allow it to be garbage collected, if set
194
206
  end
195
207
 
196
208
 
197
209
  def attach(name, callback)
210
+ raise(ContextDisposedError, 'attempted to call function on a disposed context!') if @disposed
198
211
 
199
212
  wrapped = lambda do |*args|
200
213
  begin
201
- @callback_mutex.synchronize{
202
- @callback_running = true
203
- }
204
214
 
205
- callback.call(*args)
206
- ensure
207
- @callback_mutex.synchronize {
208
- @callback_running = false
215
+ r = nil
216
+
217
+ begin
218
+ @callback_mutex.synchronize{
219
+ @callback_running = true
220
+ }
221
+ r = callback.call(*args)
222
+ ensure
223
+ @callback_mutex.synchronize{
224
+ @callback_running = false
225
+ }
226
+ end
209
227
 
210
- # this is some odd code, but it is required
211
- # if we raised on this thread we better wait for it
212
- # otherwise we may end up raising in an unsafe spot
228
+ # wait up to 2 seconds for this to be interrupted
229
+ # will very rarely be called cause #raise is called
230
+ # in another mutex
231
+ @callback_mutex.synchronize {
213
232
  if @thread_raise_called
214
- sleep 0.1
233
+ sleep 2
215
234
  end
235
+ }
236
+
237
+ r
238
+
239
+ ensure
240
+ @callback_mutex.synchronize {
216
241
  @thread_raise_called = false
217
242
  }
218
243
  end
219
244
  end
220
245
 
221
- isolate.with_lock do
246
+ isolate_mutex.synchronize do
222
247
  external = ExternalFunction.new(name, wrapped, self)
223
248
  @functions["#{name}"] = external
224
249
  end
@@ -243,7 +268,7 @@ module MiniRacer
243
268
 
244
269
  rp,wp = IO.pipe
245
270
 
246
- Thread.new do
271
+ t = Thread.new do
247
272
  begin
248
273
  result = IO.select([rp],[],[],(@timeout/1000.0))
249
274
  if !result
@@ -263,6 +288,10 @@ module MiniRacer
263
288
  end
264
289
 
265
290
  wp.write("done")
291
+
292
+ # ensure we do not leak a thread in state
293
+ t.join
294
+
266
295
  rval
267
296
 
268
297
  end
@@ -286,8 +315,33 @@ module MiniRacer
286
315
  # `size` and `warmup!` public methods are defined in the C class
287
316
  class Snapshot
288
317
  def initialize(str = '')
318
+ # ensure it first can load
319
+ begin
320
+ ctx = MiniRacer::Context.new
321
+ ctx.eval(str)
322
+ rescue MiniRacer::RuntimeError => e
323
+ raise MiniRacer::SnapshotError.new, e.message
324
+ end
325
+
326
+ @source = str
327
+
289
328
  # defined in the C class
290
329
  load(str)
291
330
  end
331
+
332
+ def warmup!(src)
333
+ # we have to do something here
334
+ # we are bloating memory a bit but it is more correct
335
+ # than hitting an exception when attempty to compile invalid source
336
+ begin
337
+ ctx = MiniRacer::Context.new
338
+ ctx.eval(@source)
339
+ ctx.eval(src)
340
+ rescue MiniRacer::RuntimeError => e
341
+ raise MiniRacer::SnapshotError.new, e.message
342
+ end
343
+
344
+ warmup_unsafe!(src)
345
+ end
292
346
  end
293
347
  end
data/mini_racer.gemspec CHANGED
@@ -25,10 +25,10 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "minitest", "~> 5.0"
26
26
  spec.add_development_dependency "rake-compiler"
27
27
 
28
- spec.add_dependency 'libv8', '~> 5.9'
28
+ spec.add_dependency 'libv8', '>= 6.3'
29
29
  spec.require_paths = ["lib", "ext"]
30
30
 
31
31
  spec.extensions = ["ext/mini_racer_extension/extconf.rb"]
32
32
 
33
- spec.required_ruby_version = '>= 2.0'
33
+ spec.required_ruby_version = '>= 2.3'
34
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_racer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-24 00:00:00.000000000 Z
11
+ date: 2018-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: libv8
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '5.9'
75
+ version: '6.3'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '5.9'
82
+ version: '6.3'
83
83
  description: Minimal embedded v8 engine for Ruby
84
84
  email:
85
85
  - sam.saffron@gmail.com
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - - ">="
118
118
  - !ruby/object:Gem::Version
119
- version: '2.0'
119
+ version: '2.3'
120
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  requirements: []
126
126
  rubyforge_project:
127
- rubygems_version: 2.5.2
127
+ rubygems_version: 2.7.6
128
128
  signing_key:
129
129
  specification_version: 4
130
130
  summary: Minimal embedded v8 for Ruby