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

Commit f780fe9

Browse files
committed
Add Isolate::SetRAILMode(), closes #60
1 parent 3049be1 commit f780fe9

15 files changed

+157
-12
lines changed

src/php_v8_enums.cc

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ zend_class_entry* php_v8_property_handler_flags_class_entry;
2525
zend_class_entry *php_v8_property_filter_class_entry;
2626
zend_class_entry *php_v8_key_collection_mode_class_entry;
2727
zend_class_entry *php_v8_index_filter_class_entry;
28+
zend_class_entry *php_v8_rail_mode_class_entry;
2829

2930

3031
static const zend_function_entry php_v8_enum_methods[] = {
@@ -126,5 +127,17 @@ PHP_MINIT_FUNCTION (php_v8_enums) {
126127
zend_declare_class_constant_long(this_ce, ZEND_STRL("SKIP_INDICES"), static_cast<zend_long>(v8::IndexFilter::kSkipIndices));
127128
#undef this_ce
128129

130+
// v8::RAILMode
131+
#define this_ce php_v8_index_filter_class_entry
132+
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "RAILMode", php_v8_enum_methods);
133+
this_ce = zend_register_internal_class(&ce);
134+
this_ce->ce_flags |= ZEND_ACC_FINAL;
135+
136+
zend_declare_class_constant_long(this_ce, ZEND_STRL("PERFORMANCE_RESPONSE"), static_cast<zend_long>(v8::RAILMode::PERFORMANCE_RESPONSE));
137+
zend_declare_class_constant_long(this_ce, ZEND_STRL("PERFORMANCE_ANIMATION"), static_cast<zend_long>(v8::RAILMode::PERFORMANCE_ANIMATION));
138+
zend_declare_class_constant_long(this_ce, ZEND_STRL("PERFORMANCE_IDLE"), static_cast<zend_long>(v8::RAILMode::PERFORMANCE_IDLE));
139+
zend_declare_class_constant_long(this_ce, ZEND_STRL("PERFORMANCE_LOAD"), static_cast<zend_long>(v8::RAILMode::PERFORMANCE_LOAD));
140+
#undef this_ce
141+
129142
return SUCCESS;
130143
}

src/php_v8_enums.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern zend_class_entry* php_v8_property_handler_flags_class_entry;
3131
extern zend_class_entry* php_v8_property_filter_class_entry;
3232
extern zend_class_entry* php_v8_key_collection_mode_class_entry;
3333
extern zend_class_entry* php_v8_index_filter_class_entry;
34+
extern zend_class_entry *php_v8_rail_mode_class_entry;
3435

3536

3637
#define PHP_V8_ACCESS_CONTROL_FLAGS ( 0 \
@@ -77,12 +78,11 @@ extern zend_class_entry* php_v8_index_filter_class_entry;
7778
| static_cast<long>(v8::KeyCollectionMode::kIncludePrototypes) \
7879
)
7980

80-
#define PHP_V8_INDEX_FILTER_FLAGS ( 0 \
81-
| static_cast<long>(v8::IndexFilter::kIncludeIndices) \
82-
| static_cast<long>(v8::IndexFilter::kSkipIndices) \
81+
#define PHP_V8_INDEX_FILTER_FLAGS ( 0 \
82+
| static_cast<long>(v8::IndexFilter::kIncludeIndices) \
83+
| static_cast<long>(v8::IndexFilter::kSkipIndices) \
8384
)
8485

85-
8686
PHP_MINIT_FUNCTION (php_v8_enums);
8787

8888
#endif //PHP_V8_ENUMS_H

src/php_v8_isolate.cc

+21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "php_v8_stack_trace.h"
2424
#include "php_v8_object.h"
2525
#include "php_v8_value.h"
26+
#include "php_v8_enums.h"
2627
#include "php_v8_a.h"
2728
#include "php_v8.h"
2829

@@ -424,6 +425,21 @@ static PHP_METHOD(Isolate, lowMemoryNotification) {
424425
isolate->LowMemoryNotification();
425426
}
426427

428+
static PHP_METHOD(Isolate, setRAILMode) {
429+
zend_long rail_mode = -1;
430+
431+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &rail_mode) == FAILURE) {
432+
return;
433+
}
434+
435+
PHP_V8_CHECK_ISOLATE_RAIL_MODE(rail_mode, "Invalid RAIL mode given. See V8\\RAILMode class constants for available values.")
436+
437+
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
438+
PHP_V8_ENTER_ISOLATE(php_v8_isolate);
439+
440+
isolate->SetRAILMode(static_cast<v8::RAILMode>(rail_mode));
441+
}
442+
427443
static PHP_METHOD(Isolate, terminateExecution) {
428444
if (zend_parse_parameters_none() == FAILURE) {
429445
return;
@@ -549,6 +565,10 @@ PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_idleNotificationDeadline,
549565
ZEND_ARG_INFO(0, deadline_in_seconds)
550566
ZEND_END_ARG_INFO()
551567

568+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_VOID_INFO_EX(arginfo_setRAILMode, 1)
569+
ZEND_ARG_TYPE_INFO(0, rail_mode, IS_LONG, 0)
570+
ZEND_END_ARG_INFO()
571+
552572
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_VOID_INFO_EX(arginfo_lowMemoryNotification, 0)
553573
ZEND_END_ARG_INFO()
554574

@@ -588,6 +608,7 @@ static const zend_function_entry php_v8_isolate_methods[] = {
588608
PHP_V8_ME(Isolate, throwException, ZEND_ACC_PUBLIC)
589609
PHP_V8_ME(Isolate, idleNotificationDeadline, ZEND_ACC_PUBLIC)
590610
PHP_V8_ME(Isolate, lowMemoryNotification, ZEND_ACC_PUBLIC)
611+
PHP_V8_ME(Isolate, setRAILMode, ZEND_ACC_PUBLIC)
591612
PHP_V8_ME(Isolate, terminateExecution, ZEND_ACC_PUBLIC)
592613
PHP_V8_ME(Isolate, isExecutionTerminating, ZEND_ACC_PUBLIC)
593614
PHP_V8_ME(Isolate, cancelTerminateExecution, ZEND_ACC_PUBLIC)

src/php_v8_isolate.h

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ inline v8::Local<v8::Private> php_v8_isolate_get_key_local(php_v8_isolate_t *php
122122
return; \
123123
}
124124

125+
#define PHP_V8_CHECK_ISOLATE_RAIL_MODE(mode, message) \
126+
if (mode < static_cast<zend_long>(v8::RAILMode::PERFORMANCE_RESPONSE) \
127+
|| mode > static_cast<zend_long>(v8::RAILMode::PERFORMANCE_LOAD)) { \
128+
PHP_V8_THROW_VALUE_EXCEPTION(message); \
129+
return; \
130+
}
131+
125132

126133
struct _php_v8_isolate_t {
127134
v8::Isolate *isolate;

stubs/src/AccessControl.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace V8;
1717

18-
class AccessControl
18+
final class AccessControl
1919
{
2020
const DEFAULT_ACCESS = 0; // do not allow cross-context access
2121
const ALL_CAN_READ = 1; // all cross-context reads are allowed

stubs/src/ConstructorBehavior.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace V8;
1717

1818

19-
class ConstructorBehavior
19+
final class ConstructorBehavior
2020
{
2121
const THROW = 0;
2222
const ALLOW = 1;

stubs/src/IntegrityLevel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Integrity level for objects.
2020
*/
21-
class IntegrityLevel
21+
final class IntegrityLevel
2222
{
2323
const FROZEN = 0;
2424
const SEALED = 1;

stubs/src/Isolate.php

+15
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ public function lowMemoryNotification()
188188
{
189189
}
190190

191+
/**
192+
* Optional notification to tell V8 the current performance requirements
193+
* of the embedder based on RAIL.
194+
* V8 uses these notifications to guide heuristics.
195+
* This is an unfinished experimental feature. Semantics and implementation
196+
* may change frequently.
197+
*
198+
* @param int $rail_mode
199+
*
200+
* @return void
201+
*/
202+
public function setRAILMode(int $rail_mode)
203+
{
204+
}
205+
191206
/**
192207
* Check if V8 is dead and therefore unusable. This is the case after
193208
* fatal errors such as out-of-memory situations.

stubs/src/KeyCollectionMode.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Keys/Properties filter to limits the range of collected properties
2020
*/
21-
class KeyCollectionMode
21+
final class KeyCollectionMode
2222
{
2323
const kOwnOnly = 0; // limits the collected properties to the given Object only. kIncludesPrototypes
2424
const kIncludesPrototypes = 1; // will include all keys of the objects's prototype chain as well.

stubs/src/PropertyAttribute.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace V8;
1717

1818

19-
class PropertyAttribute
19+
final class PropertyAttribute
2020
{
2121
const NONE = 0;
2222
const READ_ONLY = 1;

stubs/src/PropertyFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Property filter bits. They can be or'ed to build a composite filter.
2020
*/
21-
class PropertyFilter
21+
final class PropertyFilter
2222
{
2323
const ALL_PROPERTIES = 0;
2424
const ONLY_WRITABLE = 1;

stubs/src/PropertyHandlerFlags.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Configuration flags for V8\NamedPropertyHandlerConfiguration or
2121
* V8\IndexedPropertyHandlerConfiguration.
2222
*/
23-
class PropertyHandlerFlags
23+
final class PropertyHandlerFlags
2424
{
2525
/**
2626
* None.

stubs/src/RAILMode.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* This file is part of the pinepain/php-v8 PHP extension.
5+
*
6+
* Copyright (c) 2015-2017 Bogdan Padalko <[email protected]>
7+
*
8+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
9+
*
10+
* For the full copyright and license information, please view the
11+
* LICENSE file that was distributed with this source or visit
12+
* http://opensource.org/licenses/MIT
13+
*/
14+
15+
16+
namespace V8;
17+
18+
19+
/**
20+
* Option flags passed to the SetRAILMode function.
21+
* See documentation https://developers.google.com/web/tools/chrome-devtools/profile/evaluate-performance/rail
22+
*/
23+
final class RAILMode
24+
{
25+
/**
26+
* Response performance mode: In this mode very low virtual machine latency is provided.
27+
* V8 will try to avoid JavaScript execution interruptions. Throughput may be throttled.
28+
*/
29+
const PERFORMANCE_RESPONSE = 0;
30+
/**
31+
* Animation performance mode: In this mode low virtual machine latency is provided.
32+
* V8 will try to avoid as many JavaScript execution interruptions
33+
* as possible. Throughput may be throttled. This is the default mode.
34+
*/
35+
const PERFORMANCE_ANIMATION = 1;
36+
/**
37+
* Idle performance mode: The embedder is idle. V8 can complete deferred work in this mode.
38+
*/
39+
const PERFORMANCE_IDLE = 2;
40+
/**
41+
* Load performance mode: In this mode high throughput is provided. V8 may turn off latency optimizations.
42+
*/
43+
const PERFORMANCE_LOAD = 3;
44+
}

tests/001-verify_extension_entities.phpt

+7
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ final class V8\IndexFilter
260260
const INCLUDE_INDICES = 0
261261
const SKIP_INDICES = 1
262262

263+
final class V8\RAILMode
264+
const PERFORMANCE_RESPONSE = 0
265+
const PERFORMANCE_ANIMATION = 1
266+
const PERFORMANCE_IDLE = 2
267+
const PERFORMANCE_LOAD = 3
268+
263269
class V8\Exceptions\Exception
264270
extends Exception
265271
implements Throwable
@@ -345,6 +351,7 @@ class V8\Isolate
345351
public function throwException(V8\Context $context, V8\Value $value, Throwable $e)
346352
public function idleNotificationDeadline($deadline_in_seconds): bool
347353
public function lowMemoryNotification()
354+
public function setRAILMode(int $rail_mode)
348355
public function terminateExecution()
349356
public function isExecutionTerminating(): bool
350357
public function cancelTerminateExecution()

tests/Isolate.phpt

+39-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,41 @@ $helper->line();
2525
$helper->method_export($isolate, 'getHeapStatistics');
2626

2727
$isolate->lowMemoryNotification();
28+
29+
$helper->line();
30+
31+
try {
32+
$isolate->memoryPressureNotification(-2);
33+
} catch (\V8\Exceptions\ValueException $e) {
34+
$helper->exception_export($e);
35+
}
36+
2837
$isolate->memoryPressureNotification(\V8\Isolate::MEMORY_PRESSURE_LEVEL_NONE);
2938
$isolate->memoryPressureNotification(\V8\Isolate::MEMORY_PRESSURE_LEVEL_MODERATE);
3039
$isolate->memoryPressureNotification(\V8\Isolate::MEMORY_PRESSURE_LEVEL_CRITICAL);
40+
try {
41+
$isolate->memoryPressureNotification(42);
42+
} catch (\V8\Exceptions\ValueException $e) {
43+
$helper->exception_export($e);
44+
}
45+
46+
$helper->line();
47+
48+
try {
49+
$isolate->setRAILMode(-2);
50+
} catch (\V8\Exceptions\ValueException $e) {
51+
$helper->exception_export($e);
52+
}
53+
$isolate->setRAILMode(\V8\RAILMode::PERFORMANCE_RESPONSE);
54+
$isolate->setRAILMode(\V8\RAILMode::PERFORMANCE_ANIMATION);
55+
$isolate->setRAILMode(\V8\RAILMode::PERFORMANCE_IDLE);
56+
$isolate->setRAILMode(\V8\RAILMode::PERFORMANCE_LOAD);
57+
try {
58+
$isolate->setRAILMode(42);
59+
} catch (\V8\Exceptions\ValueException $e) {
60+
$helper->exception_export($e);
61+
}
62+
3163

3264
$isolate = null;
3365

@@ -47,7 +79,7 @@ V8\Isolate::MEMORY_PRESSURE_LEVEL_MODERATE = 1
4779
V8\Isolate::MEMORY_PRESSURE_LEVEL_CRITICAL = 2
4880

4981
V8\Isolate->getHeapStatistics():
50-
object(V8\HeapStatistics)#27 (9) {
82+
object(V8\HeapStatistics)#28 (9) {
5183
["total_heap_size":"V8\HeapStatistics":private]=>
5284
float(%f)
5385
["total_heap_size_executable":"V8\HeapStatistics":private]=>
@@ -67,3 +99,9 @@ V8\Isolate->getHeapStatistics():
6799
["does_zap_garbage":"V8\HeapStatistics":private]=>
68100
bool(false)
69101
}
102+
103+
V8\Exceptions\ValueException: Invalid memory pressure level given. See V8\Isolate MEMORY_PRESSURE_LEVEL_* class constants for available levels.
104+
V8\Exceptions\ValueException: Invalid memory pressure level given. See V8\Isolate MEMORY_PRESSURE_LEVEL_* class constants for available levels.
105+
106+
V8\Exceptions\ValueException: Invalid RAIL mode given. See V8\RAILMode class constants for available values.
107+
V8\Exceptions\ValueException: Invalid RAIL mode given. See V8\RAILMode class constants for available values.

0 commit comments

Comments
 (0)