diff --git a/README.Linux.md b/README.Linux.md index 462523a7..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,18 +29,55 @@ 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) +--------------------------------------------- ``` # 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,33 +93,30 @@ 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" # 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 -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' /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 @@ -92,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 diff --git a/README.MacOS.md b/README.MacOS.md index a0f35d9e..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. @@ -28,7 +27,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 diff --git a/config.m4 b/config.m4 index 694fca9d..6010d812 100644 --- a/config.m4 +++ b/config.m4 @@ -81,21 +81,41 @@ 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 @@ -130,122 +150,36 @@ int main () AC_MSG_ERROR([could not determine libv8 version]) fi - 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]) + 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 - 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 + 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 - if test -z "$static_link_dir"; then + if test "$blob_found" -ne 1; 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. - 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]) - fi - - 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 ]) + 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/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=== 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,