Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 3bc8588

Browse files
committed
Cleanup v8 on shutdown and add missed value emptiness check
1 parent 393e6c6 commit 3bc8588

File tree

6 files changed

+35
-23
lines changed

6 files changed

+35
-23
lines changed

php_v8.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#include "config.h"
1818
#endif
1919

20+
#include <v8-version.h>
21+
#include <v8.h>
22+
2023
extern "C" {
2124
#include "php.h"
22-
#include <v8-version.h>
2325

2426
#ifdef ZTS
2527
#include "TSRM.h"
@@ -51,6 +53,7 @@ extern zend_module_entry php_v8_module_entry;
5153

5254
ZEND_BEGIN_MODULE_GLOBALS(v8)
5355
bool v8_initialized;
56+
v8::Platform *platform;
5457
ZEND_END_MODULE_GLOBALS(v8)
5558

5659
// Add zend_type support (new since PHP 7.2)

src/php_v8_a.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,29 @@ void php_v8_init()
2929

3030
v8::V8::InitializeICUDefaultLocation(PHP_V8_ICU_DATA_DIR);
3131

32-
// NOTE: if we use snapshot and extenal startup data then we have to initialize it (see https://codereview.chromium.org/315033002/)
32+
// If we use snapshot and extenal startup data then we have to initialize it (see https://codereview.chromium.org/315033002/)
3333
// v8::V8::InitializeExternalStartupData(NULL);
3434
v8::Platform *platform = v8::platform::CreateDefaultPlatform();
3535
v8::V8::InitializePlatform(platform);
3636

3737
// const char *flags = "--no-hard_abort";
3838
// v8::V8::SetFlagsFromString(flags, strlen(flags));
3939

40-
41-
// TODO: remove flags?
42-
/* Set V8 command line flags (must be done before V8::Initialize()!) */
43-
// if (PHP_V8_G(v8_flags)) {
44-
// v8::V8::SetFlagsFromString(PHP_V8_G(v8_flags), strlen(PHP_V8_G(v8_flags)));
45-
// }
46-
4740
/* Initialize V8 */
4841
v8::V8::Initialize();
4942

5043
/* Run only once */
5144
PHP_V8_G(v8_initialized) = true;
45+
PHP_V8_G(platform) = platform;
46+
}
47+
48+
void php_v8_shutdown() {
49+
if (!PHP_V8_G(v8_initialized)) {
50+
return;
51+
}
52+
53+
v8::V8::Dispose();
54+
v8::V8::ShutdownPlatform();
5255

53-
// TODO: probably, not necessary to call it on shutdown
54-
// v8::V8::Dispose();
55-
// v8::V8::ShutdownPlatform();
56+
delete PHP_V8_G(platform);
5657
}

src/php_v8_a.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
#ifndef PHP_V8_A_H
1414
#define PHP_V8_A_H
1515

16+
extern "C" {
17+
#include "php.h"
18+
19+
#ifdef ZTS
20+
#include "TSRM.h"
21+
#endif
22+
};
23+
1624
void php_v8_init();
25+
void php_v8_shutdown();
1726

1827
#endif //PHP_V8_A_H

src/php_v8_isolate.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ static void php_v8_maybe_terminate_execution(php_v8_isolate_t *php_v8_isolate) {
4646
static inline void php_v8_isolate_destroy(php_v8_isolate_t *php_v8_isolate) {
4747
if (php_v8_isolate->isolate) {
4848

49-
// TODO: terminate all executions!
5049
php_v8_maybe_terminate_execution(php_v8_isolate);
5150

5251
while (php_v8_isolate->isolate->InContext()) {
5352
v8::Local<v8::Context> context = php_v8_isolate->isolate->GetEnteredContext();
54-
// context->GetIsolate()->Exit();
5553
context->Exit();
5654
}
5755

58-
// TODO: exit if entered, TODO: maybe, move it to love above?
5956
if (php_v8_isolate->isolate == v8::Isolate::GetCurrent()) {
6057
php_v8_isolate->isolate->Exit();
6158
}
@@ -144,7 +141,6 @@ static zend_object *php_v8_isolate_ctor(zend_class_entry *ce) {
144141
zend_object_std_init(&php_v8_isolate->std, ce);
145142
object_properties_init(&php_v8_isolate->std, ce);
146143

147-
// TODO: inline? module init?
148144
php_v8_init();
149145

150146
php_v8_isolate->create_params = new v8::Isolate::CreateParams();
@@ -422,7 +418,7 @@ static PHP_METHOD(V8Isolate, LowMemoryNotification) {
422418
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
423419
PHP_V8_ENTER_ISOLATE(php_v8_isolate);
424420

425-
isolate->LowMemoryNotification(); // TODO: for some reason it reports memleak
421+
isolate->LowMemoryNotification();
426422
}
427423

428424

@@ -460,7 +456,7 @@ static PHP_METHOD(V8Isolate, IsExecutionTerminating) {
460456
}
461457

462458
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
463-
PHP_V8_ENTER_ISOLATE(php_v8_isolate); // TODO: can we just fetch isolate object here and do not eneter it?
459+
PHP_V8_ENTER_ISOLATE(php_v8_isolate);
464460

465461
RETURN_BOOL(isolate->IsExecutionTerminating());
466462
}
@@ -471,7 +467,7 @@ static PHP_METHOD(V8Isolate, CancelTerminateExecution) {
471467
}
472468

473469
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
474-
PHP_V8_ENTER_ISOLATE(php_v8_isolate); // TODO: can we just fetch isolate object here and do not eneter it?
470+
PHP_V8_ENTER_ISOLATE(php_v8_isolate);
475471

476472
isolate->CancelTerminateExecution();
477473
}
@@ -488,7 +484,7 @@ static PHP_METHOD(V8Isolate, SetCaptureStackTraceForUncaughtExceptions) {
488484
PHP_V8_CHECK_STACK_TRACE_RANGE(frame_limit, "Frame limit is out of range");
489485

490486
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
491-
PHP_V8_ENTER_ISOLATE(php_v8_isolate); // TODO: can we just fetch isolate object here and do not eneter it?
487+
PHP_V8_ENTER_ISOLATE(php_v8_isolate);
492488

493489
isolate->SetCaptureStackTraceForUncaughtExceptions(static_cast<bool>(capture),
494490
static_cast<int>(frame_limit),

src/php_v8_script_compiler.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static PHP_METHOD(V8ScriptCompiler, CompileUnboundScript)
8383

8484
PHP_V8_CHECK_COMPILER_OPTIONS_RANGE(options, "Invalid Script compiler options given. See V8\\ScriptCompiler\\CompileOptions class constants for available options.")
8585

86-
PHP_V8_CONTEXT_FETCH_INTO(php_v8_context_zv, php_v8_context);
86+
PHP_V8_CONTEXT_FETCH_WITH_CHECK(php_v8_context_zv, php_v8_context);
8787

8888
zval *source_string_zv = PHP_V8_SOURCE_READ_SOURCE_STRING(php_v8_source_zv);
8989
zval *origin_zv = PHP_V8_SOURCE_READ_ORIGIN(php_v8_source_zv);
@@ -139,7 +139,7 @@ static PHP_METHOD(V8ScriptCompiler, Compile)
139139

140140
PHP_V8_CHECK_COMPILER_OPTIONS_RANGE(options, "Invalid Script compiler options given. See V8\\ScriptCompiler\\CompileOptions class constants for available options.")
141141

142-
PHP_V8_CONTEXT_FETCH_INTO(php_v8_context_zv, php_v8_context);
142+
PHP_V8_CONTEXT_FETCH_WITH_CHECK(php_v8_context_zv, php_v8_context);
143143

144144
zval *source_string_zv = PHP_V8_SOURCE_READ_SOURCE_STRING(php_v8_source_zv);
145145
zval *origin_zv = PHP_V8_SOURCE_READ_ORIGIN(php_v8_source_zv);
@@ -199,7 +199,7 @@ static PHP_METHOD(V8ScriptCompiler, CompileFunctionInContext)
199199
return;
200200
}
201201

202-
PHP_V8_CONTEXT_FETCH_INTO(php_v8_context_zv, php_v8_context);
202+
PHP_V8_CONTEXT_FETCH_WITH_CHECK(php_v8_context_zv, php_v8_context);
203203

204204
zval *source_string_zv = PHP_V8_SOURCE_READ_SOURCE_STRING(php_v8_source_zv);
205205
zval *origin_zv = PHP_V8_SOURCE_READ_ORIGIN(php_v8_source_zv);

v8.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#endif
1616

1717
#include "php_v8.h"
18+
#include "php_v8_a.h"
1819

1920
#include "php_v8_isolate.h"
2021
#include "php_v8_startup_data.h"
@@ -187,6 +188,7 @@ PHP_MSHUTDOWN_FUNCTION(v8)
187188
/* uncomment this line if you have INI entries
188189
UNREGISTER_INI_ENTRIES();
189190
*/
191+
php_v8_shutdown();
190192
return SUCCESS;
191193
}
192194
/* }}} */
@@ -240,6 +242,7 @@ static PHP_GINIT_FUNCTION(v8)
240242
ZEND_TSRMLS_CACHE_UPDATE();
241243
#endif
242244
v8_globals->v8_initialized = false;
245+
v8_globals->platform = nullptr;
243246
}
244247
/* }}} */
245248

0 commit comments

Comments
 (0)