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

Commit 3d54ad9

Browse files
authored
Merge pull request #7 from pinepain/new_methods
Add Isolate::IsInUse() method
2 parents 53931b5 + d8345d8 commit 3d54ad9

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

Diff for: README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ intentionally with the purpose to provide more solid experience between native V
4040

4141
## Demo
4242

43-
Here is a [Hello World](https://developers.google.com/v8/get_started#hello-world)
44-
from V8 [Getting Started](https://developers.google.com/v8/intro) developers guide page implemented in PHP with php-v8:
43+
Here is a [Hello World][v8-hello-world]
44+
from V8 [Getting Started][v8-intro] developers guide page implemented in PHP with php-v8:
4545

4646
```php
4747
<?php
@@ -55,8 +55,8 @@ $result = $script->Run($context);
5555
echo $result->ToString($context)->Value(), PHP_EOL;
5656
```
5757

58-
which will output `Hello, World!`. See how it's shorter and readable from that C++ version? And it also doesn't limit
59-
you from V8 API utilizing to implement more amazing stuff.
58+
which will output `Hello, World!`. See how it's shorter and readable from [that C++ version]([v8-hello-world])?
59+
And it also doesn't limit you from V8 API utilizing to implement more amazing stuff.
6060

6161

6262
## Installation
@@ -103,11 +103,11 @@ You will need some fresh v8 Google JavaScript enging version installed. At this
103103
```
104104

105105

106-
### Installing from packages
106+
### Installing php-v8 from packages
107107

108108
- For Ubuntu there are [pinepain/php-v8](https://launchpad.net/~pinepain/+archive/ubuntu/php-v8) PPA.
109109

110-
To install fresh `php7.0` do:
110+
To install fresh `php-v8`:
111111

112112
```
113113
$ sudo add-apt-repository -y ppa:pinepain/php-v8
@@ -122,7 +122,7 @@ You will need some fresh v8 Google JavaScript enging version installed. At this
122122
$ brew install https://raw.githubusercontent.com/pinepain/php-v8/master/scripts/homebrew/php70-v8.rb
123123
```
124124

125-
### Building from sources
125+
### Building php-v8 from sources
126126

127127
```
128128
git clone https://github.com/pinepain/php-v8.git
@@ -152,5 +152,7 @@ Copyright (c) 2015-2016 Bogdan Padalko &lt;[email protected]&gt;
152152
[php-v8](https://github.com/pinepain/php-v8) PHP extension is licensed under the [MIT license](http://opensource.org/licenses/MIT).
153153

154154

155+
[v8-hello-world]: https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc
156+
[v8-intro]: https://developers.google.com/v8/intro
155157
[php70-v8.rb]: https://github.com/pinepain/php-v8/blob/master/scripts/homebrew/php70-v8.rb
156158
[php71-v8.rb]: https://github.com/pinepain/php-v8/blob/master/scripts/homebrew/php71-v8.rb

Diff for: src/php_v8_isolate.cc

+16-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ static PHP_METHOD(V8Isolate, SetCaptureStackTraceForUncaughtExceptions) {
489489
static_cast<v8::StackTrace::StackTraceOptions>(options & PHP_V8_STACK_TRACE_OPTIONS));
490490
}
491491

492-
493492
static PHP_METHOD(V8Isolate, IsDead) {
494493
if (zend_parse_parameters_none() == FAILURE) {
495494
return;
@@ -501,6 +500,17 @@ static PHP_METHOD(V8Isolate, IsDead) {
501500
RETURN_BOOL(isolate->IsDead());
502501
}
503502

503+
static PHP_METHOD(V8Isolate, IsInUse) {
504+
if (zend_parse_parameters_none() == FAILURE) {
505+
return;
506+
}
507+
508+
PHP_V8_ISOLATE_FETCH_WITH_CHECK(getThis(), php_v8_isolate);
509+
PHP_V8_DECLARE_ISOLATE(php_v8_isolate);
510+
511+
RETURN_BOOL(isolate->IsInUse());
512+
}
513+
504514

505515
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_isolate___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
506516
ZEND_ARG_OBJ_INFO(0, snapshot, V8\\StartupData, 1)
@@ -574,6 +584,10 @@ ZEND_END_ARG_INFO()
574584
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_IsDead, ZEND_RETURN_VALUE, 0, _IS_BOOL, NULL, 0)
575585
ZEND_END_ARG_INFO()
576586

587+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_isolate_IsInUse, ZEND_RETURN_VALUE, 0, _IS_BOOL, NULL, 0)
588+
ZEND_END_ARG_INFO()
589+
590+
577591
static const zend_function_entry php_v8_isolate_methods[] = {
578592
PHP_ME(V8Isolate, __construct, arginfo_v8_isolate___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
579593

@@ -603,6 +617,7 @@ static const zend_function_entry php_v8_isolate_methods[] = {
603617

604618
PHP_ME(V8Isolate, SetCaptureStackTraceForUncaughtExceptions, arginfo_v8_isolate_SetCaptureStackTraceForUncaughtExceptions, ZEND_ACC_PUBLIC)
605619
PHP_ME(V8Isolate, IsDead, arginfo_v8_isolate_IsDead, ZEND_ACC_PUBLIC)
620+
PHP_ME(V8Isolate, IsInUse, arginfo_v8_isolate_IsInUse, ZEND_ACC_PUBLIC)
606621

607622
PHP_FE_END
608623
};

Diff for: stubs/src/Isolate.php

+11
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,22 @@ public function LowMemoryNotification()
196196
/**
197197
* Check if V8 is dead and therefore unusable. This is the case after
198198
* fatal errors such as out-of-memory situations.
199+
*
200+
* @return bool
199201
*/
200202
public function IsDead() : bool
201203
{
202204
}
203205

206+
/**
207+
* Check if this isolate is in use.
208+
* True if at least one thread Enter'ed this isolate.
209+
*
210+
* @return bool
211+
*/
212+
public function IsInUse() : bool
213+
{
214+
}
204215

205216
/**
206217
* Tells V8 to capture current stack trace when uncaught exception occurs

Diff for: tests/.testsuite.php

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public function inline($message, $value)
3939
echo $message, ': ', $value, PHP_EOL;
4040
}
4141

42+
public function inline_dump($message, $value)
43+
{
44+
echo $message, ': ';
45+
$this->dump($value);
46+
}
47+
4248
public function space($new_lines_number = 2)
4349
{
4450
echo str_repeat(PHP_EOL, max(1, $new_lines_number));

Diff for: tests/V8Isolate.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object(V8\Isolate)#2 (5) {
4646
V8\Exceptions\GenericException: Not in context!
4747

4848
V8\Isolate->GetHeapStatistics():
49-
object(V8\HeapStatistics)#28 (9) {
49+
object(V8\HeapStatistics)#29 (9) {
5050
["total_heap_size":"V8\HeapStatistics":private]=>
5151
float(%d)
5252
["total_heap_size_executable":"V8\HeapStatistics":private]=>

Diff for: tests/V8Isolate_IsInUse.phpt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
V8\Isolate::IsInUse()
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
/** @var \Phpv8Testsuite $helper */
9+
$helper = require '.testsuite.php';
10+
11+
require '.v8-helpers.php';
12+
$v8_helper = new PhpV8Helpers($helper);
13+
14+
15+
$isolate = new V8\Isolate();
16+
17+
$helper->inline_dump('Isolate in use', $isolate->IsInUse());
18+
19+
$context = new V8\Context($isolate);
20+
$fnc = new \V8\FunctionObject($context, function (\V8\CallbackInfo $info) use ($helper) {
21+
$helper->inline_dump('Isolate in use', $info->GetIsolate()->IsInUse());
22+
});
23+
24+
25+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate, 'test'), $fnc);
26+
27+
(new \V8\Script($context, new \V8\StringValue($isolate, 'test()')))->Run($context);
28+
29+
?>
30+
--EXPECT--
31+
Isolate in use: bool(false)
32+
Isolate in use: bool(true)

0 commit comments

Comments
 (0)