@@ -26,7 +26,7 @@ module V8
2626 # cxt.eval('num') # => 5
2727 # end
2828 class Context
29- include V8 ::Error ::Try
29+ # include V8::Error::Try
3030
3131 # @!attribute [r] conversion
3232 # @return [V8::Conversion] conversion behavior for this context
@@ -44,6 +44,10 @@ class Context
4444 # @return [Number] maximum execution time in milliseconds for scripts executed in this context
4545 attr_reader :timeout
4646
47+ # @!attribute [r] isolate
48+ # @return [V8::Isolate]
49+ attr_reader :isolate
50+
4751 # Creates a new context.
4852 #
4953 # If passed the `:with` option, that object will be used as
@@ -62,20 +66,24 @@ class Context
6266 # * :with scope serves as the global scope of the new context
6367 # @yield [V8::Context] the newly created context
6468 def initialize ( options = { } )
65- @conversion = Conversion . new
66- @access = Access . new
67- @timeout = options [ :timeout ]
68- if global = options [ :with ]
69- Context . new . enter do
70- global_template = global . class . to_template . InstanceTemplate ( )
71- @native = V8 ::C ::Context ::New ( nil , global_template )
72- end
73- enter { link global , @native . Global ( ) }
74- else
75- V8 ::C ::Locker ( ) do
76- @native = V8 ::C ::Context ::New ( )
77- end
69+ @isolate = options [ :isolate ] || V8 ::Isolate . new
70+ V8 ::C ::HandleScope ( @isolate . native ) do
71+ @native = V8 ::C ::Context ::New ( @isolate . native )
7872 end
73+ @conversion = Conversion . new
74+ # @access = Access.new
75+ # @timeout = options[:timeout]
76+ # if global = options[:with]
77+ # Context.new.enter do
78+ # global_template = global.class.to_template.InstanceTemplate()
79+ # @native = V8::C::Context::New(nil, global_template)
80+ # end
81+ # enter {link global, @native.Global()}
82+ # else
83+ # V8::C::Locker() do
84+ # @native = V8::C::Context::New()
85+ # end
86+ # end
7987 yield self if block_given?
8088 end
8189
@@ -92,12 +100,11 @@ def eval(source, filename = '<eval>', line = 1)
92100 source = source . read
93101 end
94102 enter do
95- script = try { V8 ::C ::Script ::New ( source . to_s , filename . to_s ) }
96- if @timeout
97- to_ruby try { script . RunWithTimeout ( @timeout ) }
98- else
99- to_ruby try { script . Run ( ) }
100- end
103+ src = V8 ::C ::String ::NewFromUtf8 ( @isolate . native , source . to_s )
104+ filename = V8 ::C ::String ::NewFromUtf8 ( @isolate . native , filename . to_s )
105+ origin = V8 ::C ::ScriptOrigin . new ( filename ) ;
106+ script = V8 ::C ::Script ::Compile ( @native , src , origin ) . FromJust ( )
107+ to_ruby script . Run ( @native ) . FromJust ( )
101108 end
102109 end
103110
@@ -107,7 +114,7 @@ def eval(source, filename = '<eval>', line = 1)
107114 # @return [Object] value the value at `key`
108115 def []( key )
109116 enter do
110- to_ruby ( @native . Global ( ) . Get ( to_v8 ( key ) ) )
117+ to_ruby ( @native . Global ( ) . Get ( @cnative , to_v8 ( key ) ) . FromJust ( ) )
111118 end
112119 end
113120
@@ -117,26 +124,11 @@ def [](key)
117124 # @param [Object] value the value to bind
118125 def []=( key , value )
119126 enter do
120- @native . Global ( ) . Set ( to_v8 ( key ) , to_v8 ( value ) )
127+ @native . Global ( ) . Set ( @native , to_v8 ( key ) , to_v8 ( value ) )
121128 end
122129 return value
123130 end
124131
125- # Destroy this context and release any internal references it may
126- # contain to embedded Ruby objects.
127- #
128- # A disposed context may never again be used for anything, and all
129- # objects created with it will become unusable.
130- def dispose
131- return unless @native
132- @native . Dispose ( )
133- @native = nil
134- V8 ::C ::V8 ::ContextDisposedNotification ( )
135- def self . enter
136- fail "cannot enter a context which has already been disposed"
137- end
138- end
139-
140132 # Returns this context's global object. This will be a `V8::Object`
141133 # if no scope was provided or just an `Object` if a Ruby object
142134 # is serving as the global scope.
@@ -165,7 +157,7 @@ def to_ruby(v8_object)
165157 # @return [V8::C::Object] to pass to V8
166158 # @see V8::Conversion for customizing and extending this mechanism
167159 def to_v8 ( ruby_object )
168- @conversion . to_v8 ( ruby_object )
160+ @conversion . to_v8 ( self , ruby_object )
169161 end
170162
171163 # Marks a Ruby object and a v8 C++ Object as being the same. In other
@@ -241,14 +233,12 @@ def self.current=(context)
241233 def lock_scope_and_enter
242234 current = Context . current
243235 Context . current = self
244- V8 ::C ::Locker ( ) do
245- V8 ::C ::HandleScope ( ) do
246- begin
247- @native . Enter ( )
248- yield if block_given?
249- ensure
250- @native . Exit ( )
251- end
236+ V8 ::C ::HandleScope ( @isolate . native ) do
237+ begin
238+ @native . Enter ( )
239+ yield if block_given?
240+ ensure
241+ @native . Exit ( )
252242 end
253243 end
254244 ensure
0 commit comments