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

Commit b4f92e0

Browse files
authored
Merge pull request #90 from pinepain/upgrades-and-new-features
Upgrades and new features
2 parents 74935e7 + 29944f9 commit b4f92e0

17 files changed

+338
-67
lines changed

.travis.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ services:
66

77
env:
88
global:
9-
- V8=6.5
9+
- V8=6.5.144
1010
matrix:
11-
- TAG=xenial-v8-65-php-71
12-
- TAG=xenial-v8-65-php-71 TEST_PHP_ARGS=-m
13-
- TAG=xenial-v8-65-php-72
14-
- TAG=xenial-v8-65-php-72 TEST_PHP_ARGS=-m
11+
- PHP=7.2
12+
- PHP=7.2 TEST_PHP_ARGS=-m
13+
- PHP=7.1
14+
- PHP=7.1 TEST_PHP_ARGS=-m
1515

1616
before_install:
1717
# docker on travis doesn't allow anything before FROM, let's fix that
18-
- sed -i -e "s/\${TAG}/$TAG/g" Dockerfile && sed -i -e '1,2d' Dockerfile
19-
- docker build -t ${TRAVIS_REPO_SLUG}:${V8}-${PHP} --build-arg TAG=${TAG} .
18+
- sed -i -e "s/:latest/:${V8}/g" Dockerfile
19+
- docker build -t ${TRAVIS_REPO_SLUG} --build-arg PHP=${PHP} .
2020

2121
script:
22-
- docker run -e TEST_PHP_ARGS ${TRAVIS_REPO_SLUG}:${V8}-${PHP} bash -c "make test | tee result.txt; ./test-report.sh"
22+
- docker run
23+
-e TEST_PHP_ARGS
24+
-v `pwd`:/root/php-v8
25+
${TRAVIS_REPO_SLUG}
26+
bash -c "phpize && ./configure && make test | tee result.txt; ./test-report.sh"

Dockerfile

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
ARG TAG=xenial-v8-65-php-72
1+
FROM pinepain/libv8:latest
22

3-
FROM pinepain/php-v8-docker:${TAG}
3+
ARG PHP=7.2
4+
5+
ENV DEBIAN_FRONTEND noninteractive
6+
ENV TERM=xterm-256color
7+
ENV LC_ALL=C.UTF-8
8+
ENV NO_INTERACTION=1
9+
ENV REPORT_EXIT_STATUS=1
10+
11+
RUN echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" > /etc/apt/sources.list.d/ondrej-php-xenial.list && \
12+
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C && \
13+
apt-get update && \
14+
apt-get install -y valgrind && \
15+
apt-get install -y php${PHP} php${PHP}-cli php${PHP}-dev php${PHP}-fpm && \
16+
rm -rf /var/lib/apt/lists/* && \
17+
echo 'variables_order = "EGPCS"' >> `php --ini | grep "Loaded Configuration File" | awk '{print $4}'` && \
18+
php -i && \
19+
php-config || true && \
20+
mkdir /root/php-v8
421

5-
COPY . /root/php-v8
622
COPY ./scripts/provision/.bashrc /root/.bashrc
723

824
WORKDIR /root/php-v8
925

10-
ENV NO_INTERACTION=1
26+
#COPY . /root/php-v8
27+
#RUN phpize && ./configure && make
1128

12-
RUN php -i && php-config || true
13-
RUN phpize && ./configure && make

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ on top of php-v8 extension and makes embedding JavaScript in PHP easier.
100100
### Requirements
101101

102102
#### V8
103-
You will need a recent v8 Google JavaScript engine version installed. At this time v8 >= 6.5.116 required.
103+
You will need a recent v8 Google JavaScript engine version installed. At this time v8 >= 6.5.144 required.
104104

105105
#### PHP
106106
This extension is PHP7-only. It works and tested with both PHP 7.0 and PHP 7.1.
@@ -167,6 +167,15 @@ $ sudo make install
167167
- [pinepain/libv8-experimental](https://launchpad.net/~pinepain/+archive/ubuntu/libv8-experimental) normally contains
168168
`libv8` version that used in current `master` branch.
169169

170+
### Docker
171+
172+
First, let's build docker image `docker build -t pinepain/php-v8 .` that we'll use later for development. By default,
173+
it contains PHP 7.2, though you can change that by passing `--build-arg PHP=MAJOR.MINOR` where MAJOR.MINOR version
174+
present in [ondrej/php](https://launchpad.net/~ondrej/+archive/ubuntu/php) PPA.
175+
176+
To start playing with php-v8 in docker, run ```docker run -e TEST_PHP_ARGS -v `pwd`:/root/php-v8 -it pinepain/php-v8 bash``.
177+
Now you can build php-v8 as usual with `phpize && ./configure && make`. Don't forget to run `make test`!
178+
170179
### Docs
171180

172181
To start writing docs you will need to get [GitBook](https://gitbook.com), just run `npm install gitbook-cli -g` and

config.m4

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ if test "$PHP_V8" != "no"; then
1919
SEARCH_PATH="/usr/local /usr"
2020
SEARCH_FOR="include/v8.h"
2121

22-
V8_MIN_API_VERSION_STR=6.5.116
22+
V8_MIN_API_VERSION_STR=6.5.144
2323

2424
DESIRED_V8_VERSION=`echo "${V8_MIN_API_VERSION_STR}" | $AWK 'BEGIN { FS = "."; } { printf "%s.%s", [$]1, [$]2;}'`
2525

2626
# Path where v8 from packages we recommend are installed, it's /opt/libv8-MAJOR.MINOR on Ubuntu
27-
# and /usr/local/opt/[email protected] on macOS
28-
PRIORITY_SEARCH_PATH="/opt/libv8-${DESIRED_V8_VERSION} /usr/local/opt/v8@${DESIRED_V8_VERSION}"
29-
SEARCH_PATH="${PRIORITY_SEARCH_PATH} /usr/local /usr"
27+
# and /usr/local/opt/[email protected] on macOS. For Docker image it's just /opt/libv8
28+
PRIORITY_SEARCH_PATH="/opt/libv8-${DESIRED_V8_VERSION} /usr/local/opt/v8@${DESIRED_V8_VERSION} /opt/libv8"
29+
SEARCH_PATH="${PRIORITY_SEARCH_PATH} ${SEARCH_PATH}"
3030

3131
if test -r $PHP_V8/$SEARCH_FOR; then
3232
V8_ROOT_DIR=$PHP_V8

docs/development/release-libv8.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@ To track v8 changes you can use these links:
2323
1. Copy fresh `libv8-X.Y` build packages from `libv8-experimental` (default target for all libv8 builds we trigger)
2424
to it `libv8-X.Y` PPA. Do not rebuild, just copy binaries.
2525
2. **Wait for packages copied and published!**
26-
3. If there was a minor version bump in `libv8` (not patch), create new dockerfiles for it in
27-
[php-v8-docker](https://github.com/pinepain/php-v8-docker) and set `ARG V8=X.Y` to proper version.
28-
4. Go to [php-v8-docker Docker Hub](https://hub.docker.com/r/pinepain/php-v8-docker) and remove old Docker tags from
29-
been rebuild on push as we don't need them anymore and replace them with a new one (we still need to keep old tags
30-
for CI purpose and just in case things won't go smooth and we have to rollback).
31-
5. Rebuild/publish docker images to include new `libv8` version.
32-
6. You may want to set proper default image `TAG` in `php-v8` `Dockerfile` by updating it first line `ARG TAG=xenial-v8-64-php-72`.
33-
7. Make sure you have proper `php-v8` version set in `packaging/Dockerfile` under `V8` constant.
26+
3. Build [pinepain/libv8](https://github.com/pinepain/dockerfiles/tree/master/libv8) docker image, tag it with the
27+
relevant v8 full version and push to Docker Hub.
28+
4. You may want to set proper `V8` version in `php-v8` by updating it in `.travis.yml`.
29+
5. Make sure you have proper `php-v8` version set in `packaging/Dockerfile` under `V8` constant.
3430

3531
## After docker images rebuilt/published
3632

src/php_v8_heap_statistics.cc

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ void php_v8_heap_statistics_create_from_heap_statistics(zval *return_value, v8::
3737
zend_update_property_double(this_ce, return_value, ZEND_STRL("peak_malloced_memory"), hs->peak_malloced_memory());
3838

3939
zend_update_property_bool(this_ce, return_value, ZEND_STRL("does_zap_garbage"), static_cast<zend_long>(hs->does_zap_garbage()));
40+
41+
zend_update_property_double(this_ce, return_value, ZEND_STRL("number_of_native_contexts"), hs->number_of_native_contexts());
42+
zend_update_property_double(this_ce, return_value, ZEND_STRL("number_of_detached_contexts"), hs->number_of_detached_contexts());
4043
}
4144

4245
static PHP_METHOD(HeapStatistics, __construct) {
@@ -51,10 +54,14 @@ static PHP_METHOD(HeapStatistics, __construct) {
5154

5255
zend_bool does_zap_garbage = '\0';
5356

54-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|" "dddd" "dddd" "b",
57+
double number_of_native_contexts = 0;
58+
double number_of_detached_contexts = 0;
59+
60+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|" "dddd" "dddd" "b" "dd",
5561
&total_heap_size, &total_heap_size_executable, &total_physical_size, &total_available_size,
5662
&used_heap_size, &heap_size_limit, &malloced_memory, &peak_malloced_memory,
57-
&does_zap_garbage) == FAILURE) {
63+
&does_zap_garbage,
64+
&number_of_native_contexts, &number_of_detached_contexts) == FAILURE) {
5865
return;
5966
}
6067

@@ -68,6 +75,9 @@ static PHP_METHOD(HeapStatistics, __construct) {
6875
zend_update_property_double(this_ce, getThis(), ZEND_STRL("peak_malloced_memory"), peak_malloced_memory);
6976

7077
zend_update_property_bool(this_ce, getThis(), ZEND_STRL("does_zap_garbage"), does_zap_garbage);
78+
79+
zend_update_property_double(this_ce, getThis(), ZEND_STRL("number_of_native_contexts"), number_of_native_contexts);
80+
zend_update_property_double(this_ce, getThis(), ZEND_STRL("number_of_detached_contexts"), number_of_detached_contexts);
7181
}
7282

7383
static PHP_METHOD(HeapStatistics, getTotalHeapSize) {
@@ -160,6 +170,26 @@ static PHP_METHOD(HeapStatistics, doesZapGarbage) {
160170
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("does_zap_garbage"), 0, &rv), 1, 0);
161171
}
162172

173+
static PHP_METHOD(HeapStatistics, getNumberOfNativeContexts) {
174+
zval rv;
175+
176+
if (zend_parse_parameters_none() == FAILURE) {
177+
return;
178+
}
179+
180+
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("number_of_native_contexts"), 0, &rv), 1, 0);
181+
}
182+
183+
static PHP_METHOD(HeapStatistics, getNumberOfDetachedContexts) {
184+
zval rv;
185+
186+
if (zend_parse_parameters_none() == FAILURE) {
187+
return;
188+
}
189+
190+
RETVAL_ZVAL(zend_read_property(this_ce, getThis(), ZEND_STRL("number_of_detached_contexts"), 0, &rv), 1, 0);
191+
}
192+
163193

164194
PHP_V8_ZEND_BEGIN_ARG_WITH_CONSTRUCTOR_INFO_EX(arginfo___construct, 0)
165195
ZEND_ARG_TYPE_INFO(0, total_heap_size, IS_DOUBLE, 0)
@@ -202,6 +232,12 @@ ZEND_END_ARG_INFO()
202232
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_doesZapGarbage, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0)
203233
ZEND_END_ARG_INFO()
204234

235+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getNumberOfNativeContexts, ZEND_RETURN_VALUE, 0, IS_DOUBLE, 0)
236+
ZEND_END_ARG_INFO()
237+
238+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getNumberOfDetachedContexts, ZEND_RETURN_VALUE, 0, IS_DOUBLE, 0)
239+
ZEND_END_ARG_INFO()
240+
205241

206242
static const zend_function_entry php_v8_heap_statistics_methods[] = {
207243
PHP_V8_ME(HeapStatistics, __construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
@@ -215,6 +251,8 @@ static const zend_function_entry php_v8_heap_statistics_methods[] = {
215251
PHP_V8_ME(HeapStatistics, getMallocedMemory, ZEND_ACC_PUBLIC)
216252
PHP_V8_ME(HeapStatistics, getPeakMallocedMemory, ZEND_ACC_PUBLIC)
217253
PHP_V8_ME(HeapStatistics, doesZapGarbage, ZEND_ACC_PUBLIC)
254+
PHP_V8_ME(HeapStatistics, getNumberOfNativeContexts, ZEND_ACC_PUBLIC)
255+
PHP_V8_ME(HeapStatistics, getNumberOfDetachedContexts, ZEND_ACC_PUBLIC)
218256

219257
PHP_FE_END
220258
};
@@ -236,5 +274,8 @@ PHP_MINIT_FUNCTION (php_v8_heap_statistics) {
236274

237275
zend_declare_property_bool(this_ce, ZEND_STRL("does_zap_garbage"), false, ZEND_ACC_PRIVATE);
238276

277+
zend_declare_property_double(this_ce, ZEND_STRL("number_of_native_contexts"), 0, ZEND_ACC_PRIVATE);
278+
zend_declare_property_double(this_ce, ZEND_STRL("number_of_detached_contexts"), 0, ZEND_ACC_PRIVATE);
279+
239280
return SUCCESS;
240281
}

src/php_v8_script_compiler.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,39 @@ static PHP_METHOD(ScriptCompiler, compileFunctionInContext)
268268
php_v8_get_or_create_value(return_value, local_function, php_v8_context->php_v8_isolate);
269269
}
270270

271+
static PHP_METHOD(ScriptCompiler, createCodeCache)
272+
{
273+
zval rv;
274+
275+
zval *php_v8_unbound_script_zv;
276+
zval *php_v8_source_string_zv;
277+
278+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "oo", &php_v8_unbound_script_zv, &php_v8_source_string_zv) == FAILURE) {
279+
return;
280+
}
281+
282+
php_v8_init();
283+
284+
PHP_V8_FETCH_UNBOUND_SCRIPT_WITH_CHECK(php_v8_unbound_script_zv, php_v8_unbound_script);
285+
PHP_V8_VALUE_FETCH_WITH_CHECK(php_v8_source_string_zv, php_v8_source_string);
286+
287+
PHP_V8_DATA_ISOLATES_CHECK(php_v8_unbound_script, php_v8_source_string);
288+
289+
PHP_V8_ENTER_STORED_ISOLATE(php_v8_unbound_script);
290+
291+
v8::Local<v8::UnboundScript> local_unbound_script = php_v8_unbound_script_get_local(php_v8_unbound_script);
292+
v8::Local<v8::String> local_source_string = php_v8_value_get_local_as<v8::String>(php_v8_source_string);
293+
294+
v8::ScriptCompiler::CachedData* cached_data = v8::ScriptCompiler::CreateCodeCache(local_unbound_script, local_source_string);
295+
296+
if (!cached_data) {
297+
PHP_V8_THROW_VALUE_EXCEPTION("Failed to create code cache");
298+
return;
299+
}
300+
301+
php_v8_create_cached_data(return_value, cached_data);
302+
}
303+
271304

272305
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_getCachedDataVersionTag, ZEND_RETURN_VALUE, 0, IS_DOUBLE, 0)
273306
ZEND_END_ARG_INFO()
@@ -291,12 +324,18 @@ PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_compileFunctionInContext,
291324
ZEND_ARG_TYPE_INFO(0, context_extensions, IS_ARRAY, 0)
292325
ZEND_END_ARG_INFO()
293326

327+
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_createCodeCache, ZEND_RETURN_VALUE, 2, V8\\ScriptCompiler\\CachedData, 0)
328+
ZEND_ARG_OBJ_INFO(0, unbound_script, V8\\UnboundScript, 0)
329+
ZEND_ARG_OBJ_INFO(0, source_string, V8\\StringValue, 0)
330+
ZEND_END_ARG_INFO()
331+
294332

295333
static const zend_function_entry php_v8_script_compiler_methods[] = {
296334
PHP_V8_ME(ScriptCompiler, getCachedDataVersionTag, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
297335
PHP_V8_ME(ScriptCompiler, compileUnboundScript, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
298336
PHP_V8_ME(ScriptCompiler, compile, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
299337
PHP_V8_ME(ScriptCompiler, compileFunctionInContext, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
338+
PHP_V8_ME(ScriptCompiler, createCodeCache, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
300339

301340
PHP_FE_END
302341
};
@@ -315,6 +354,7 @@ PHP_MINIT_FUNCTION(php_v8_script_compiler)
315354
zend_declare_class_constant_long(this_ce, ZEND_STRL("OPTION_PRODUCE_CODE_CACHE"), static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kProduceCodeCache));
316355
zend_declare_class_constant_long(this_ce, ZEND_STRL("OPTION_PRODUCE_FULL_CODE_CACHE"), static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kProduceFullCodeCache));
317356
zend_declare_class_constant_long(this_ce, ZEND_STRL("OPTION_CONSUME_CODE_CACHE"), static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kConsumeCodeCache));
357+
zend_declare_class_constant_long(this_ce, ZEND_STRL("OPTION_EAGER_COMPILE"), static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kEagerCompile));
318358

319359
return SUCCESS;
320360
}

src/php_v8_script_compiler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ extern "C" {
2727
extern zend_class_entry *php_v8_script_compiler_class_entry;
2828

2929
#define PHP_V8_CHECK_COMPILER_OPTIONS_RANGE(options, message) \
30-
if (options < static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kNoCompileOptions) \
31-
|| options > static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kConsumeCodeCache)) { \
30+
if ((options) < static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kNoCompileOptions) \
31+
|| (options) > static_cast<zend_long>(v8::ScriptCompiler::CompileOptions::kEagerCompile)) { \
3232
PHP_V8_THROW_VALUE_EXCEPTION(message); \
3333
return; \
3434
}

stubs/src/HeapStatistics.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class HeapStatistics
6060
* @var bool
6161
*/
6262
private $does_zap_garbage;
63+
/**
64+
* @var float
65+
*/
66+
private $number_of_native_contexts;
67+
/**
68+
* @var float
69+
*/
70+
private $number_of_detached_contexts;
6371

6472
/**
6573
* @param float $total_heap_size
@@ -69,7 +77,10 @@ class HeapStatistics
6977
* @param float $used_heap_size
7078
* @param float $heap_size_limit
7179
* @param float $malloced_memory
80+
* @param float $peak_malloced_memory
7281
* @param bool $does_zap_garbage
82+
* @param float $number_of_native_contexts
83+
* @param float $number_of_detached_contexts
7384
*/
7485
public function __construct(
7586
float $total_heap_size,
@@ -80,17 +91,21 @@ public function __construct(
8091
float $heap_size_limit,
8192
float $malloced_memory,
8293
float $peak_malloced_memory,
83-
bool $does_zap_garbage
94+
bool $does_zap_garbage,
95+
float $number_of_native_contexts,
96+
float $number_of_detached_contexts
8497
) {
85-
$this->total_heap_size = $total_heap_size;
86-
$this->total_heap_size_executable = $total_heap_size_executable;
87-
$this->total_physical_size = $total_physical_size;
88-
$this->total_available_size = $total_available_size;
89-
$this->used_heap_size = $used_heap_size;
90-
$this->heap_size_limit = $heap_size_limit;
91-
$this->malloced_memory = $malloced_memory;
92-
$this->peak_malloced_memory = $peak_malloced_memory;
93-
$this->does_zap_garbage = $does_zap_garbage;
98+
$this->total_heap_size = $total_heap_size;
99+
$this->total_heap_size_executable = $total_heap_size_executable;
100+
$this->total_physical_size = $total_physical_size;
101+
$this->total_available_size = $total_available_size;
102+
$this->used_heap_size = $used_heap_size;
103+
$this->heap_size_limit = $heap_size_limit;
104+
$this->malloced_memory = $malloced_memory;
105+
$this->peak_malloced_memory = $peak_malloced_memory;
106+
$this->does_zap_garbage = $does_zap_garbage;
107+
$this->number_of_native_contexts = $number_of_native_contexts;
108+
$this->number_of_detached_contexts = $number_of_detached_contexts;
94109
}
95110

96111
/**
@@ -166,4 +181,22 @@ public function doesZapGarbage(): bool
166181
{
167182
return $this->does_zap_garbage;
168183
}
184+
185+
/**
186+
* The total number of native contexts object on the heap
187+
* @return float
188+
*/
189+
public function getNumberOfNativeContexts(): float
190+
{
191+
return $this->number_of_native_contexts;
192+
}
193+
194+
/**
195+
* The total number of native contexts that were detached but were not garbage collected yet
196+
* @return float
197+
*/
198+
public function getNumberOfDetachedContexts(): float
199+
{
200+
return $this->number_of_detached_contexts;
201+
}
169202
}

0 commit comments

Comments
 (0)