|
27 | 27 | #include "php_v8_a.h"
|
28 | 28 | #include "php_v8.h"
|
29 | 29 |
|
| 30 | +#include <float.h> |
| 31 | + |
30 | 32 |
|
31 | 33 | zend_class_entry *php_v8_isolate_class_entry;
|
32 | 34 | #define this_ce php_v8_isolate_class_entry
|
@@ -130,6 +132,8 @@ static HashTable * php_v8_isolate_gc(zval *object, zval **table, int *n) {
|
130 | 132 | static void php_v8_isolate_free(zend_object *object) {
|
131 | 133 | php_v8_isolate_t *php_v8_isolate = php_v8_isolate_fetch_object(object);
|
132 | 134 |
|
| 135 | + php_v8_isolate_limits_free(php_v8_isolate); |
| 136 | + |
133 | 137 | if (php_v8_isolate->weak_function_templates) {
|
134 | 138 | php_v8_isolate_clean_weak<v8::FunctionTemplate>(php_v8_isolate->weak_function_templates);
|
135 | 139 | delete php_v8_isolate->weak_function_templates;
|
@@ -189,6 +193,8 @@ static zend_object *php_v8_isolate_ctor(zend_class_entry *ce) {
|
189 | 193 |
|
190 | 194 | php_v8_isolate->std.handlers = &php_v8_isolate_object_handlers;
|
191 | 195 |
|
| 196 | + php_v8_isolate_limits_ctor(php_v8_isolate); |
| 197 | + |
192 | 198 | return &php_v8_isolate->std;
|
193 | 199 | }
|
194 | 200 |
|
@@ -240,6 +246,106 @@ static PHP_METHOD(V8Isolate, __construct) {
|
240 | 246 | php_v8_isolate->isolate->SetFatalErrorHandler(php_v8_fatal_error_handler);
|
241 | 247 | }
|
242 | 248 |
|
| 249 | +static PHP_METHOD(V8Isolate, SetTimeLimit) { |
| 250 | + double time_limit_in_seconds; |
| 251 | + |
| 252 | + if (zend_parse_parameters(ZEND_NUM_ARGS(), "d", &time_limit_in_seconds) == FAILURE) { |
| 253 | + return; |
| 254 | + } |
| 255 | + |
| 256 | + PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate); |
| 257 | + |
| 258 | + if (time_limit_in_seconds < 0) { |
| 259 | + PHP_V8_THROW_EXCEPTION("Time limit should be a non-negative float"); |
| 260 | + return; |
| 261 | + } |
| 262 | + |
| 263 | + php_v8_isolate_limits_set_time_limit(php_v8_isolate, time_limit_in_seconds); |
| 264 | + |
| 265 | + zend_update_property_double(this_ce, getThis(), ZEND_STRL("time_limit"), time_limit_in_seconds); |
| 266 | + zend_update_property_bool(this_ce, getThis(), ZEND_STRL("time_limit_hit"), 0); |
| 267 | +} |
| 268 | + |
| 269 | +static PHP_METHOD(V8Isolate, GetTimeLimit) { |
| 270 | + zval rv; |
| 271 | + |
| 272 | + zval *prop = NULL; |
| 273 | + if (zend_parse_parameters_none() == FAILURE) { |
| 274 | + return; |
| 275 | + } |
| 276 | + |
| 277 | + PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate); |
| 278 | + |
| 279 | + prop = zend_read_property(this_ce, getThis(), ZEND_STRL("time_limit"), 0, &rv); |
| 280 | + |
| 281 | + RETVAL_ZVAL(prop, 1, 0); |
| 282 | +} |
| 283 | + |
| 284 | +static PHP_METHOD(V8Isolate, IsTimeLimitHit) { |
| 285 | + zval rv; |
| 286 | + |
| 287 | + zval *prop = NULL; |
| 288 | + if (zend_parse_parameters_none() == FAILURE) { |
| 289 | + return; |
| 290 | + } |
| 291 | + |
| 292 | + PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate); |
| 293 | + |
| 294 | + prop = zend_read_property(this_ce, getThis(), ZEND_STRL("time_limit_hit"), 0, &rv); |
| 295 | + |
| 296 | + RETVAL_ZVAL(prop, 1, 0); |
| 297 | +} |
| 298 | + |
| 299 | +static PHP_METHOD(V8Isolate, SetMemoryLimit) { |
| 300 | + long memory_limit_in_bytes; |
| 301 | + |
| 302 | + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &memory_limit_in_bytes) == FAILURE) { |
| 303 | + return; |
| 304 | + } |
| 305 | + |
| 306 | + PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate); |
| 307 | + |
| 308 | + if (memory_limit_in_bytes < 0) { |
| 309 | + PHP_V8_THROW_EXCEPTION("Memory limit should be a non-negative numeric value"); |
| 310 | + return; |
| 311 | + } |
| 312 | + |
| 313 | + php_v8_isolate_limits_set_memory_limit(php_v8_isolate, static_cast<size_t>(memory_limit_in_bytes)); |
| 314 | + |
| 315 | + zend_update_property_long(this_ce, getThis(), ZEND_STRL("memory_limit"), memory_limit_in_bytes); |
| 316 | + zend_update_property_bool(this_ce, getThis(), ZEND_STRL("memory_limit_hit"), 0); |
| 317 | +} |
| 318 | + |
| 319 | +static PHP_METHOD(V8Isolate, GetMemoryLimit) { |
| 320 | + zval rv; |
| 321 | + |
| 322 | + zval *prop = NULL; |
| 323 | + if (zend_parse_parameters_none() == FAILURE) { |
| 324 | + return; |
| 325 | + } |
| 326 | + |
| 327 | + PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate); |
| 328 | + |
| 329 | + prop = zend_read_property(this_ce, getThis(), ZEND_STRL("memory_limit"), 0, &rv); |
| 330 | + |
| 331 | + RETVAL_ZVAL(prop, 1, 0); |
| 332 | +} |
| 333 | + |
| 334 | +static PHP_METHOD(V8Isolate, IsMemoryLimitHit) { |
| 335 | + zval rv; |
| 336 | + |
| 337 | + zval *prop = NULL; |
| 338 | + if (zend_parse_parameters_none() == FAILURE) { |
| 339 | + return; |
| 340 | + } |
| 341 | + |
| 342 | + PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate); |
| 343 | + |
| 344 | + prop = zend_read_property(this_ce, getThis(), ZEND_STRL("memory_limit_hit"), 0, &rv); |
| 345 | + |
| 346 | + RETVAL_ZVAL(prop, 1, 0); |
| 347 | +} |
| 348 | + |
243 | 349 | static PHP_METHOD(V8Isolate, GetSnapshot) {
|
244 | 350 | zval rv;
|
245 | 351 |
|
@@ -434,6 +540,26 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_isolate___construct, ZEND_SEND_BY_VAL, ZEND_RE
|
434 | 540 | ZEND_ARG_OBJ_INFO(0, snapshot, v8\\StartupData, 1)
|
435 | 541 | ZEND_END_ARG_INFO()
|
436 | 542 |
|
| 543 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_isolate_SetTimeLimit, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1) |
| 544 | + ZEND_ARG_TYPE_INFO(0, time_limit_in_seconds, IS_DOUBLE, 0) |
| 545 | +ZEND_END_ARG_INFO() |
| 546 | + |
| 547 | +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_GetTimeLimit, ZEND_RETURN_VALUE, 0, IS_DOUBLE, NULL, 0) |
| 548 | +ZEND_END_ARG_INFO() |
| 549 | + |
| 550 | +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_IsTimeLimitHit, ZEND_RETURN_VALUE, 0, _IS_BOOL, NULL, 0) |
| 551 | +ZEND_END_ARG_INFO() |
| 552 | + |
| 553 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_isolate_SetMemoryLimit, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1) |
| 554 | + ZEND_ARG_TYPE_INFO(0, memory_limit_in_bytes, IS_LONG, 0) |
| 555 | +ZEND_END_ARG_INFO() |
| 556 | + |
| 557 | +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_GetMemoryLimit, ZEND_RETURN_VALUE, 0, IS_LONG, NULL, 0) |
| 558 | +ZEND_END_ARG_INFO() |
| 559 | + |
| 560 | +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_IsMemoryLimitHit, ZEND_RETURN_VALUE, 0, _IS_BOOL, NULL, 0) |
| 561 | +ZEND_END_ARG_INFO() |
| 562 | + |
437 | 563 | ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_GetSnapshot, ZEND_RETURN_VALUE, 0, IS_OBJECT, "v8\\StartupData", 1)
|
438 | 564 | ZEND_END_ARG_INFO()
|
439 | 565 |
|
@@ -485,6 +611,14 @@ ZEND_END_ARG_INFO()
|
485 | 611 | static const zend_function_entry php_v8_isolate_methods[] = {
|
486 | 612 | PHP_ME(V8Isolate, __construct, arginfo_v8_isolate___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
|
487 | 613 |
|
| 614 | + PHP_ME(V8Isolate, SetTimeLimit, arginfo_v8_isolate_SetTimeLimit, ZEND_ACC_PUBLIC) |
| 615 | + PHP_ME(V8Isolate, GetTimeLimit, arginfo_v8_isolate_GetTimeLimit, ZEND_ACC_PUBLIC) |
| 616 | + PHP_ME(V8Isolate, IsTimeLimitHit, arginfo_v8_isolate_IsTimeLimitHit, ZEND_ACC_PUBLIC) |
| 617 | + |
| 618 | + PHP_ME(V8Isolate, SetMemoryLimit, arginfo_v8_isolate_SetMemoryLimit, ZEND_ACC_PUBLIC) |
| 619 | + PHP_ME(V8Isolate, GetMemoryLimit, arginfo_v8_isolate_GetMemoryLimit, ZEND_ACC_PUBLIC) |
| 620 | + PHP_ME(V8Isolate, IsMemoryLimitHit, arginfo_v8_isolate_IsMemoryLimitHit, ZEND_ACC_PUBLIC) |
| 621 | + |
488 | 622 | PHP_ME(V8Isolate, GetSnapshot, arginfo_v8_isolate_GetSnapshot, ZEND_ACC_PUBLIC)
|
489 | 623 |
|
490 | 624 | PHP_ME(V8Isolate, GetHeapStatistics, arginfo_v8_isolate_GetHeapStatistics, ZEND_ACC_PUBLIC)
|
@@ -517,6 +651,12 @@ PHP_MINIT_FUNCTION (php_v8_isolate) {
|
517 | 651 |
|
518 | 652 | zend_declare_property_null(this_ce, ZEND_STRL("snapshot"), ZEND_ACC_PRIVATE);
|
519 | 653 |
|
| 654 | + zend_declare_property_double(this_ce, ZEND_STRL("time_limit"), 0.0, ZEND_ACC_PRIVATE); |
| 655 | + zend_declare_property_bool(this_ce, ZEND_STRL("time_limit_hit"), 0, ZEND_ACC_PRIVATE); |
| 656 | + |
| 657 | + zend_declare_property_long(this_ce, ZEND_STRL("memory_limit"), 0, ZEND_ACC_PRIVATE); |
| 658 | + zend_declare_property_bool(this_ce, ZEND_STRL("memory_limit_hit"), 0, ZEND_ACC_PRIVATE); |
| 659 | + |
520 | 660 | memcpy(&php_v8_isolate_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
521 | 661 |
|
522 | 662 | php_v8_isolate_object_handlers.offset = XtOffsetOf(php_v8_isolate_t, std);
|
|
0 commit comments