Multiple context in the same isolate

543 views
Skip to first unread message

Sara Abdelhameed

unread,
Feb 9, 2014, 3:46:09 PM2/9/14
to v8-u...@googlegroups.com
Hi, all
I have a small question, can multiple contexts that are created in the same isolate share their objects? is it applicable that we could create two context one after the other in the same isolate scope and then they share their objects to each other?

Thank you, 

Ben Noordhuis

unread,
Feb 9, 2014, 7:26:51 PM2/9/14
to v8-u...@googlegroups.com
That's correct. More precisely: objects created in context A are
accessible by context B when their security tokens match.

New contexts use a default security token. You would have to change
it manually with v8::Context::SetSecurityToken() for tokens to _not_
match.

Sara Abdelhameed

unread,
Feb 15, 2014, 9:21:29 AM2/15/14
to v8-u...@googlegroups.com
Actually, I don't understand what you mean; but suppose if I have this scenario 
1- create 1st context
2- enter it and declare some objects here and do some operations on them
3- then exit this context
4- create 2nd context
5- enter 2nd context
6- here I want to make some operations on object from 1st context 

in the 6th step how can I do this? if I set the security tokens of the first context to any value what this mean? 

thank you in advance 

Ben Noordhuis

unread,
Feb 15, 2014, 11:16:22 AM2/15/14
to v8-u...@googlegroups.com
On Sat, Feb 15, 2014 at 3:21 PM, Sara Abdelhameed
<saraabdel...@gmail.com> wrote:
> Actually, I don't understand what you mean; but suppose if I have this
> scenario
> 1- create 1st context
> 2- enter it and declare some objects here and do some operations on them
> 3- then exit this context
> 4- create 2nd context
> 5- enter 2nd context
> 6- here I want to make some operations on object from 1st context
>
> in the 6th step how can I do this? if I set the security tokens of the first
> context to any value what this mean?
>
> thank you in advance

If you're not interested in access control, then forget what I said
about security tokens. The default is no access control and, going by
your description, is what you're after.

I'm not quite sure what you mean with 'how can I do this?'. Is your
question how to get objects from context A to context B? It might help
if you give some, ah, context by telling what problem you're trying to
solve.

Sara Abdelhameed

unread,
Feb 15, 2014, 11:37:54 AM2/15/14
to v8-u...@googlegroups.com
I want to create 2 context, one after the other and within these 2 context compile and execute javascript, and finally pass this contexts and compiled javascript to two threads to execute the compiled script.
the scenario is:
1- Create first context
2- Enter this context using context scope and create string containing javascript source code like"Hello, World"
3- compile and execute this script
4- Create the 2nd context
5- Enter the 2nd context using context scope and create 2nd string containing javascript code like "Good morning"
6- compile and run the script
7- declare 2 threads like this p_thread thread1, thread2;
8- pass the first context and the first compiled string to the first thread
9- pass the 2nd context and the 2nd compiled string to the 2nd thread
I tried to do this by different strategy but I failed to make it run successfully, so is this scenario feasible? or it is impossible using v8-engine?
thank you,     



--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/SVdgPWTg7S8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Noordhuis

unread,
Feb 15, 2014, 12:41:43 PM2/15/14
to v8-u...@googlegroups.com
On Sat, Feb 15, 2014 at 5:37 PM, Sara Abdelhameed
<saraabdel...@gmail.com> wrote:
> I want to create 2 context, one after the other and within these 2 context
> compile and execute javascript, and finally pass this contexts and compiled
> javascript to two threads to execute the compiled script.
> the scenario is:
> 1- Create first context
> 2- Enter this context using context scope and create string containing
> javascript source code like"Hello, World"
> 3- compile and execute this script
> 4- Create the 2nd context
> 5- Enter the 2nd context using context scope and create 2nd string
> containing javascript code like "Good morning"
> 6- compile and run the script
> 7- declare 2 threads like this p_thread thread1, thread2;
> 8- pass the first context and the first compiled string to the first thread
> 9- pass the 2nd context and the 2nd compiled string to the 2nd thread
> I tried to do this by different strategy but I failed to make it run
> successfully, so is this scenario feasible? or it is impossible using
> v8-engine?
> thank you,

If you intend to have those two threads run in parallel (and I assume
you do), then no, that's not possible. A context is tied to the
isolate it's created in and an isolate is not safe to enter from
multiple threads simultaneously.

For the sake of completeness, you _can_ exit an isolate in one thread
and then enter it again in another thread (see the Locker and Unlocker
classes in v8.h) but I suspect that for you doing so defeats the
purpose of having two threads in the first place.

Sara Abdelhameed

unread,
Feb 15, 2014, 12:58:00 PM2/15/14
to v8-u...@googlegroups.com
Yes, I did that in the threads I lock and unlock the global isolate, and I know that didn't make the threads run in parallel, because I used the same isolate for the two threads, but I want just to run the two threads even if one after the other, is this possible or what do you think?
I could post the code here if you want to take a look on it?
  


Ben Noordhuis

unread,
Feb 16, 2014, 5:19:45 AM2/16/14
to v8-u...@googlegroups.com
On Sat, Feb 15, 2014 at 6:58 PM, Sara Abdelhameed
<saraabdel...@gmail.com> wrote:
> Yes, I did that in the threads I lock and unlock the global isolate, and I
> know that didn't make the threads run in parallel, because I used the same
> isolate for the two threads, but I want just to run the two threads even if
> one after the other, is this possible or what do you think?
> I could post the code here if you want to take a look on it?

Sure, if it's not hundreds or thousands of lines of code. What
particular error are you getting? Did you test with a debug build of
V8?
Reply all
Reply to author
Forward
0 new messages