From 15cdc3e7df0f7b76f5f9f670d8d422f3de403965 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 11 Aug 2016 19:06:49 +0200 Subject: [PATCH 1/7] Backport test of issue #246 to PHP 5 The reference handling issue applied only to PHP 7, nevertheless run the test against PHP 5 version as well. --- tests/issue_246_001.phpt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/issue_246_001.phpt diff --git a/tests/issue_246_001.phpt b/tests/issue_246_001.phpt new file mode 100644 index 00000000..e2b12ae7 --- /dev/null +++ b/tests/issue_246_001.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test V8::executeString() : Handle Z_TYPE == IS_REFERENCE (issue #246) +--SKIPIF-- + +--FILE-- + 'ipsum']; +array_walk_recursive($array, function (&$item) {}); +$v8->some_array = $array; +$v8->executeString('var_dump(PHP.some_array.lorem);'); +?> +===EOF=== +--EXPECT-- +string(5) "ipsum" +===EOF=== From ea8ac667625644a8e9453de7c69b0e97aecc0af6 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Mon, 5 Dec 2016 16:18:16 +0000 Subject: [PATCH 2/7] Use linker to detect libv8_libplatform & libv8_libbase Conflicts: config.m4 Backport from php7 branch. --- config.m4 | 78 ++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/config.m4 b/config.m4 index 694fca9d..f56a8237 100644 --- a/config.m4 +++ b/config.m4 @@ -81,21 +81,43 @@ if test "$PHP_V8JS" != "no"; then old_LDFLAGS=$LDFLAGS old_CPPFLAGS=$CPPFLAGS - case $host_os in - darwin* ) - # MacOS does not support --rpath - LDFLAGS="-L$V8_DIR/$PHP_LIBDIR" - ;; - * ) - LDFLAGS="-Wl,--rpath=$V8_DIR/$PHP_LIBDIR -L$V8_DIR/$PHP_LIBDIR" - ;; - esac - - LIBS=-lv8 - CPPFLAGS="-I$V8_DIR/include -std=$ac_cv_v8_cstd" AC_LANG_SAVE AC_LANG_CPLUSPLUS + CPPFLAGS="$CPPFLAGS -I$V8_DIR/include -std=$ac_cv_v8_cstd" + LDFLAGS="$LDFLAGS -L$V8_DIR/$PHP_LIBDIR" + + AC_DEFUN([V8_CHECK_LINK], [ + AC_MSG_CHECKING([for libv8_libplatform]) + save_LIBS="$LIBS" + LIBS="$LIBS $1 -lv8_libplatform -lv8" + AC_LINK_IFELSE([AC_LANG_PROGRAM([ + namespace v8 { + namespace platform { + void* CreateDefaultPlatform(int thread_pool_size = 0); + } + } + ], [ v8::platform::CreateDefaultPlatform(); ])], [ + dnl libv8_libplatform.so found + AC_MSG_RESULT(found) + V8JS_SHARED_LIBADD="$1 -lv8_libplatform $V8JS_SHARED_LIBADD" + $2 + ], [ $3 ]) + LIBS="$save_LIBS" + ]) + + V8_CHECK_LINK([], [], [ + V8_CHECK_LINK([-lv8_libbase], [], [ AC_MSG_ERROR([could not find libv8_libplatform library]) ]) + ]) + + + dnl + dnl Check for V8 version + dnl (basic support for library linking assumed to be achieved above) + dnl + + + LIBS="$LIBS $V8JS_SHARED_LIBADD" AC_CACHE_CHECK(for V8 version, ac_cv_v8_version, [ AC_TRY_RUN([#include #include @@ -132,40 +154,8 @@ int main () PHP_ADD_INCLUDE($V8_DIR) - case $host_os in - darwin* ) - static_link_extra="libv8_libplatform.a libv8_libbase.a" - ;; - * ) - static_link_extra="libv8_libplatform.a" - ;; - esac - - LDFLAGS_libplatform="" - for static_link_extra_file in $static_link_extra; do - AC_MSG_CHECKING([for $static_link_extra_file]) - - if test -r $V8_DIR/lib64/$static_link_extra_file; then - static_link_dir=$V8_DIR/lib64 - AC_MSG_RESULT(found in $V8_DIR/lib64) - fi - - if test -r $V8_DIR/lib/$static_link_extra_file; then - static_link_dir=$V8_DIR/lib - AC_MSG_RESULT(found in $V8_DIR/lib) - fi - - if test -z "$static_link_dir"; then - AC_MSG_RESULT([not found]) - AC_MSG_ERROR([Please provide $static_link_extra_file next to the libv8.so, see README.md for details]) - fi - - LDFLAGS_libplatform="$LDFLAGS_libplatform $static_link_dir/$static_link_extra_file" - done - # modify flags for (possibly) succeeding V8 startup check CPPFLAGS="$CPPFLAGS -I$V8_DIR" - LIBS="$LIBS $LDFLAGS_libplatform" dnl building for v8 4.4.10 or later, which requires us to dnl provide startup data, if V8 wasn't compiled with snapshot=off. From 5c07ed29740c24118f0bd2f0f43031eacfe867e7 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Mon, 5 Dec 2016 16:28:59 +0000 Subject: [PATCH 3/7] Don't try to guess if blob.bin files are needed Search for them, if they exist, provide them to V8. Otherwise try to go without them. Conflicts: config.m4 Backport from php7 branch. --- config.m4 | 98 ++++++++++++------------------------------------------ v8js_v8.cc | 2 +- 2 files changed, 22 insertions(+), 78 deletions(-) diff --git a/config.m4 b/config.m4 index f56a8237..6010d812 100644 --- a/config.m4 +++ b/config.m4 @@ -115,8 +115,6 @@ if test "$PHP_V8JS" != "no"; then dnl Check for V8 version dnl (basic support for library linking assumed to be achieved above) dnl - - LIBS="$LIBS $V8JS_SHARED_LIBADD" AC_CACHE_CHECK(for V8 version, ac_cv_v8_version, [ AC_TRY_RUN([#include @@ -152,90 +150,36 @@ int main () AC_MSG_ERROR([could not determine libv8 version]) fi - PHP_ADD_INCLUDE($V8_DIR) - # modify flags for (possibly) succeeding V8 startup check - CPPFLAGS="$CPPFLAGS -I$V8_DIR" - - dnl building for v8 4.4.10 or later, which requires us to - dnl provide startup data, if V8 wasn't compiled with snapshot=off. - AC_MSG_CHECKING([whether V8 requires startup data]) - AC_TRY_RUN([ - #include - #include - #include - #include - -class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { -public: - virtual void* Allocate(size_t length) { - void* data = AllocateUninitialized(length); - return data == NULL ? data : memset(data, 0, length); - } - virtual void* AllocateUninitialized(size_t length) { return malloc(length); } - virtual void Free(void* data, size_t) { free(data); } -}; - - int main () - { - v8::Platform *v8_platform = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(v8_platform); - v8::V8::Initialize(); - - static ArrayBufferAllocator array_buffer_allocator; - v8::Isolate::CreateParams create_params; - create_params.array_buffer_allocator = &array_buffer_allocator; - - v8::Isolate::New(create_params); - return 0; - } - ], [ - AC_MSG_RESULT([no]) - ], [ - AC_MSG_RESULT([yes]) - AC_DEFINE([PHP_V8_USE_EXTERNAL_STARTUP_DATA], [1], [Whether V8 requires (and can be provided with custom versions of) external startup data]) - - SEARCH_PATH="$V8_DIR/lib $V8_DIR/share/v8" - - AC_MSG_CHECKING([for natives_blob.bin]) - SEARCH_FOR="natives_blob.bin" - - for i in $SEARCH_PATH ; do - if test -r $i/$SEARCH_FOR; then - AC_MSG_RESULT([found ($i/$SEARCH_FOR)]) - AC_DEFINE_UNQUOTED([PHP_V8_NATIVES_BLOB_PATH], "$i/$SEARCH_FOR", [Full path to natives_blob.bin file]) - native_blob_found=1 - fi - done - - if test -z "$native_blob_found"; then - AC_MSG_RESULT([not found]) - AC_MSG_ERROR([Please provide V8 native blob as needed]) + dnl + dnl Scan for blob.bin files (that might be needed) + dnl + AC_DEFUN([V8_SEARCH_BLOB], [ + AC_MSG_CHECKING([for $1]) + blob_found=0 + + for i in "$V8_DIR/$PHP_LIBDIR" "$V8_DIR/share/v8"; do + if test -r "$i/$1"; then + AC_MSG_RESULT([found ($i/$1)]) + AC_DEFINE_UNQUOTED([$2], "$i/$1", [Full path to $1 file]) + blob_found=1 fi + done - AC_MSG_CHECKING([for snapshot_blob.bin]) - SEARCH_FOR="snapshot_blob.bin" - - for i in $SEARCH_PATH ; do - if test -r $i/$SEARCH_FOR; then - AC_MSG_RESULT([found ($i/$SEARCH_FOR)]) - AC_DEFINE_UNQUOTED([PHP_V8_SNAPSHOT_BLOB_PATH], "$i/$SEARCH_FOR", [Full path to snapshot_blob.bin file]) - snapshot_blob_found=1 - fi - done - - if test -z "$snapshot_blob_found"; then - AC_MSG_RESULT([not found]) - AC_MSG_ERROR([Please provide V8 snapshot blob as needed]) - fi + if test "$blob_found" -ne 1; then + AC_MSG_RESULT([not found]) + fi ]) + V8_SEARCH_BLOB([natives_blob.bin], [PHP_V8_NATIVES_BLOB_PATH]) + V8_SEARCH_BLOB([snapshot_blob.bin], [PHP_V8_SNAPSHOT_BLOB_PATH]) + AC_LANG_RESTORE LIBS=$old_LIBS - LDFLAGS="$old_LDFLAGS $LDFLAGS_libplatform" + LDFLAGS="$old_LDFLAGS" CPPFLAGS=$old_CPPFLAGS - + PHP_ADD_INCLUDE($V8_DIR) PHP_NEW_EXTENSION(v8js, [ \ v8js_array_access.cc \ v8js.cc \ diff --git a/v8js_v8.cc b/v8js_v8.cc index 8e120150..1a3d0817 100644 --- a/v8js_v8.cc +++ b/v8js_v8.cc @@ -54,7 +54,7 @@ void v8js_v8_init(TSRMLS_D) /* {{{ */ } #endif -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA +#if defined(PHP_V8_NATIVES_BLOB_PATH) && defined(PHP_V8_SNAPSHOT_BLOB_PATH) /* V8 doesn't work without startup data, load it. */ v8::V8::InitializeExternalStartupData( PHP_V8_NATIVES_BLOB_PATH, From dd06941728e2648c9c95023e087d24af79ed49aa Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 12 Aug 2016 23:30:46 +0200 Subject: [PATCH 4/7] Don't suggest use_system_icu=1, closes #238 --- README.Linux.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.Linux.md b/README.Linux.md index 462523a7..e6e6001a 100644 --- a/README.Linux.md +++ b/README.Linux.md @@ -32,10 +32,10 @@ Compile latest V8 ``` # Install `build-essential` if you haven't already: -sudo apt-get install build-essential +sudo apt install build-essential -# Install `libicu-dev` if you haven't already: -sudo apt-get install libicu-dev +# Install `chrpath` for fixing libv8.so's RUNPATH header, if you haven't already: +sudo apt install chrpath cd /tmp @@ -51,9 +51,6 @@ cd v8 git checkout 4.9.385.28 gclient sync -# use libicu of operating system -export GYP_DEFINES="use_system_icu=1" - # Build (with internal snapshots) export GYPFLAGS="-Dv8_use_external_startup_data=0" @@ -67,6 +64,9 @@ sudo mkdir -p /usr/lib /usr/include sudo cp out/native/lib.target/lib*.so /usr/lib/ sudo cp -R include/* /usr/include +# Fix libv8.so's RUNPATH header +sudo chrpath -r '$ORIGIN' /usr/lib/libv8.so + # Install libv8_libplatform.a (V8 >= 5.2.51) echo -e "create /usr/lib/libv8_libplatform.a\naddlib out/native/obj.target/src/libv8_libplatform.a\nsave\nend" | sudo ar -M From 7bf7613301a7c9a215b561f09be9a723541226d2 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Tue, 1 Nov 2016 21:12:52 +0100 Subject: [PATCH 5/7] mention what -j8 means, refs #271 --- README.Linux.md | 1 + README.MacOS.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.Linux.md b/README.Linux.md index e6e6001a..54ef331b 100644 --- a/README.Linux.md +++ b/README.Linux.md @@ -57,6 +57,7 @@ export GYPFLAGS="-Dv8_use_external_startup_data=0" # Force gyp to use system-wide ld.gold export GYPFLAGS="${GYPFLAGS} -Dlinux_use_bundled_gold=0" +# Compile V8 (using up to 8 CPU cores, requires a lot of RAM, adapt as needed) make native library=shared snapshot=on -j8 # Install to /usr diff --git a/README.MacOS.md b/README.MacOS.md index a0f35d9e..076e7f19 100644 --- a/README.MacOS.md +++ b/README.MacOS.md @@ -28,7 +28,7 @@ cd v8 git checkout 3.32.6 gclient sync -# Build +# Compile V8 (using up to 8 CPU cores, requires a lot of RAM, adapt as needed) make native library=shared -j8 # Install to /usr From ad9c5a25515f3d1f3964aef758254348e230af44 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Mon, 5 Dec 2016 17:46:17 +0100 Subject: [PATCH 6/7] README.Linux: add build instructions for V8 5.6, closes #269 --- README.Linux.md | 68 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/README.Linux.md b/README.Linux.md index 54ef331b..7a9d6b9d 100644 --- a/README.Linux.md +++ b/README.Linux.md @@ -10,6 +10,11 @@ years ago, since Node.js requires such an old version. This means that you usually need to compile v8 on your own before you can start to compile & install v8js itself. +It is recommended to install the V8 version for V8Js off your system's +load path, so it doesn't interfere with the V8 library shipped with your +system's distribution. + + Snapshots --------- @@ -24,10 +29,47 @@ so there's no need to handle them specially. Besides there are external snapshots (which are enabled unless configured otherwise). If V8 is compiled with these, then V8Js needs to provide two "binary blobs" to V8, named `natives_blob.bin` and `snapshot_blob.bin`. -In that case copy those two files to `/usr/share/v8/...`. +In that case copy those two files to the same directory `libv8.so` was +installed to. -Compile latest V8 ------------------ + +Compile V8 5.6 and newer (using GN) +----------------------------------- + +``` +# Install required dependencies +sudo apt-get install build-essential git python libglib2.0-dev + +cd /tmp + +# Install depot_tools first (needed for source checkout) +git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git +export PATH=`pwd`/depot_tools:"$PATH" + +# Download v8 +fetch v8 +cd v8 + +# (optional) If you'd like to build a certain version: +git checkout 5.6.326.12 +gclient sync + +# Setup GN +tools/dev/v8gen.py -vv x64.release +echo is_component_build = true >> out.gn/x64.release/args.gn + +# Build +ninja -C out.gn/x64.release/ + +# Install to /opt/v8/ +sudo mkdir -p /opt/v8/{lib,include} +sudo cp out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin /opt/v8/lib/ +sudo cp -R include/* /opt/v8/include/ +``` + + +Compile V8 versions 5.5 and older (using Gyp) +--------------------------------------------- ``` @@ -60,25 +102,21 @@ export GYPFLAGS="${GYPFLAGS} -Dlinux_use_bundled_gold=0" # Compile V8 (using up to 8 CPU cores, requires a lot of RAM, adapt as needed) make native library=shared snapshot=on -j8 -# Install to /usr -sudo mkdir -p /usr/lib /usr/include -sudo cp out/native/lib.target/lib*.so /usr/lib/ -sudo cp -R include/* /usr/include +# Install to /opt/v8 +sudo mkdir -p /opt/v8/{lib,include} +sudo cp out/native/lib.target/lib*.so /opt/v8/lib/ +sudo cp -R include/* /opt/v8/include # Fix libv8.so's RUNPATH header -sudo chrpath -r '$ORIGIN' /usr/lib/libv8.so +sudo chrpath -r '$ORIGIN' /opt/v8/lib/libv8.so # Install libv8_libplatform.a (V8 >= 5.2.51) -echo -e "create /usr/lib/libv8_libplatform.a\naddlib out/native/obj.target/src/libv8_libplatform.a\nsave\nend" | sudo ar -M +echo -e "create /opt/v8/lib/libv8_libplatform.a\naddlib out/native/obj.target/src/libv8_libplatform.a\nsave\nend" | sudo ar -M # ... same for V8 < 5.2.51, libv8_libplatform.a is built in tools/gyp directory -echo -e "create /usr/lib/libv8_libplatform.a\naddlib out/native/obj.target/tools/gyp/libv8_libplatform.a\nsave\nend" | sudo ar -M +echo -e "create /opt/v8/lib/libv8_libplatform.a\naddlib out/native/obj.target/tools/gyp/libv8_libplatform.a\nsave\nend" | sudo ar -M ``` -* If you don't want to overwrite the system copy of v8, replace `/usr` in - the above commands with some other path like `/opt/v8` and then add - `--with-v8js=/opt/v8` to the php-v8js `./configure` command below. - `libv8_libplatform.a` should not be copied directly since it's a thin archive, i.e. it contains only pointers to the build objects, which otherwise must not be deleted. The simple mri-script converts the @@ -93,7 +131,7 @@ cd /tmp git clone https://github.com/phpv8/v8js.git cd v8js phpize -./configure +./configure --with-v8js=/opt/v8 make make test sudo make install From c3f7ad66e459c033043fe4ee677c6d2bd1610b97 Mon Sep 17 00:00:00 2001 From: Matt Dennewitz Date: Thu, 2 Mar 2017 10:36:06 -0600 Subject: [PATCH 7/7] Update README.MacOS.md `php56-v8js` now a part of `homebrew-php` --- README.MacOS.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.MacOS.md b/README.MacOS.md index 076e7f19..c468f4d1 100644 --- a/README.MacOS.md +++ b/README.MacOS.md @@ -3,8 +3,7 @@ V8Js on MacOS Installation of V8Js on MacOS is pretty much straight forward. -If you have [brew](https://brew.sh) around, just `brew install -php56-v8js` (or `php54-v8js` / `php55-v8js` depending on your PHP +If you have [brew](https://brew.sh) around, just `brew install homebrew/php/php56-v8js` (or `php54-v8js` / `php55-v8js` depending on your PHP version) and you should be done. This will install a recent version of V8 along with this extension.