From 72cb2da9ba53fcaaa1a9a905dbfffab6c87ef5bd Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 2 May 2015 20:36:55 +0200 Subject: [PATCH] Enter endless loop after V8::TerminateExecution call, fixes #135 The V8::TerminateExecution does *not* immediately terminate execution as its name might suggest. It just marks the given isolate as "to terminate" and the execution thread checks - from time to time - whether to terminate. For v8 itself this is not problematic as the call is thought to stop long-running scripts executed in the browser context. As v8js exposes this function to JavaScript with the exit() method, this behaviour is confusing. In order to stop code execution right at the exit() call, v8js enters an endless loop and waits for v8 to terminate execution within it. --- v8js_methods.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/v8js_methods.cc b/v8js_methods.cc index 3535cd2..9c59655 100644 --- a/v8js_methods.cc +++ b/v8js_methods.cc @@ -27,7 +27,21 @@ void v8js_commonjs_normalise_identifier(char *base, char *identifier, char *norm /* global.exit - terminate execution */ V8JS_METHOD(exit) /* {{{ */ { - v8::V8::TerminateExecution(info.GetIsolate()); + v8::Isolate *isolate = info.GetIsolate(); + + /* Unfortunately just calling TerminateExecution on the isolate is not + * enough, since v8 just marks the thread as "to be aborted" and doesn't + * immediately do so. Hence we enter an endless loop after signalling + * termination, so we definitely don't execute JS code after the exit() + * statement. */ + v8::Locker locker(isolate); + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + + v8::Local source = V8JS_STR("for(;;);"); + v8::Local script = v8::Script::Compile(source); + v8::V8::TerminateExecution(isolate); + script->Run(); } /* }}} */