can i set timeout for v8::Script::Run?

1,674 views
Skip to first unread message

Six

unread,
Aug 12, 2011, 10:54:02 AM8/12/11
to v8-users
hello all, sorry to disturb u.

i have a problem that how i can invoke v8::Script::Run in async method
or can i set a time-out value for it?
because sometimes a js will take too long time.

just like:
// compile
while (some condition) {
result = v8::Script::Run();
// check the result
}

if u have answer, please mail me.
thanks very much!

lei

Mikhail Naganov

unread,
Aug 12, 2011, 11:02:48 AM8/12/11
to v8-u...@googlegroups.com
Hello,

You can interrupt VM execution from a parallel thread by executing
v8::Debug::DebugBreak or v8::Debug::DebugBreakForCommand. Look into
test/cctest/test-debug.cc for examples.

> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
>

lei lu

unread,
Aug 12, 2011, 12:30:25 PM8/12/11
to v8-u...@googlegroups.com
hi,

thank u very much:)
i'll try it right now

thanks
lei


2011/8/12 Mikhail Naganov <mnag...@chromium.org>:

mcot

unread,
Aug 12, 2011, 2:06:02 PM8/12/11
to v8-users
This has been discussed a few times if you search this list.

I think the last solution seemed to be:

Use preemption + terminate execution.

The relevant stuff to look at would be:

v8::Locker::StartPreemption

and

v8::V8::TerminateExecution


Preemption would let you gain control even during infinite loops or
infinite recursion. Terminate execution just stops js execution.


I think what everyone is looking for is something like this for V8,
just baked into the API:

https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_SetOperationCallback

Set a callback function that is automatically called periodically
while JavaScript code runs.

If the callback returns JS_TRUE, the JS engine continues to execute
the script.

If the callback returns JS_FALSE without raising an exception, then
the JS engine immediately stops running the script with an uncatchable
error.




On Aug 12, 12:30 pm, lei lu <lulei...@gmail.com> wrote:
> hi,
>
> thank u very much:)
> i'll try it right now
>
> thanks
> lei
>
> 2011/8/12 Mikhail Naganov <mnaga...@chromium.org>:
>
>
>
>
>
>
>
> > Hello,
>
> > You can interrupt VM execution from a parallel thread by executing
> > v8::Debug::DebugBreak or v8::Debug::DebugBreakForCommand. Look into
> > test/cctest/test-debug.cc for examples.
>

mcot

unread,
Aug 12, 2011, 2:07:17 PM8/12/11
to v8-users
Also... it seems bad to use the debugger api's for this task. For
one, wouldn't you have to compile v8 in debug mode and link against
v8_g?

Mikhail Naganov

unread,
Aug 13, 2011, 5:10:45 PM8/13/11
to v8-u...@googlegroups.com
I think it's OK. This is a part of debugger API, because debugger
needs to be able to intervent into script execution at arbitrary
moments. And you don't need a debug version of V8, you just need to
make sure that you compile with ENABLE_DEBUGGER_SUPPORT defined.
Debugging JS inside V8 isn't the same as debugging V8 itself.

But using preemption and lockers is a good approach as well.

mcot

unread,
Aug 15, 2011, 4:35:00 PM8/15/11
to v8-users
Ok. I made an implementation called "ScriptManager" that uses the
preemption + terminate execution.

http://pastebin.com/M801zbDB

It uses basic stuff inside of V8's platform specific code such as
threads, sleeping and current time. I have done some testing, but I
would like some feedback to see if this method looks good. I'm not
very good at concurrent programming so this does not use any
synchronization methods... If there is an obvious place to add a mutex
or semaphore to avoid a deadlock or get rid of the sleep call or while
loop, please let me know. It seems to me that the terminate execution
call does not have side effects if the javascript is not running when
it gets called. It just does nothing. I think that makes this a bit
easier.

Once we make this better, we should attempt to query the V8 developers
and get this added in the samples directory of the source.
Reply all
Reply to author
Forward
0 new messages