Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions README.Linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@ 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.

Compile latest v8
Snapshots
---------

V8 has (optional) support for so-called snapshots which speed up startup
performance drastically. Hence they are generally recommended for use.

There are two flavours of snapshots: internal & external.

Internal snapshots are built right into the V8 library (libv8.so file),
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/...`.

Compile latest V8
-----------------

```
Expand All @@ -25,11 +41,15 @@ fetch v8
cd v8

# (optional) If you'd like to build a certain version:
git checkout 3.32.6
git checkout 4.9.385.28
gclient sync

# Build (disable snapshots for V8 > 4.4.9.1)
make native library=shared snapshot=off -j8
# use libicu of operating system
export GYP_DEFINES="use_system_icu=1"

# Build (with internal snapshots)
export GYPFLAGS="-Dv8_use_external_startup_data=0"
make native library=shared snapshot=on -j8

# Install to /usr
sudo mkdir -p /usr/lib /usr/include
Expand All @@ -40,16 +60,9 @@ echo -e "create /usr/lib/libv8_libplatform.a\naddlib out/native/obj.target/tools

Then add `extension=v8js.so` to your php.ini file. If you have a separate configuration for CLI, add it there also.

* If the V8 library is newer than 4.4.9.1 you need to pass `snapshot=off` to
`make`, otherwise the V8 library will not be usable
(see V8 [Issue 4192](https://code.google.com/p/v8/issues/detail?id=4192))
* 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.
* If you do that with a v8 library of 4.2 branch or newer, then you need
to fix the RUNPATH header in the v8js.so library so the libicui18n.so
is found. By default it is set to `$ORIGIN/lib.target/`, however the files
lie side by side. Use `chrpath -r '$ORIGIN' libv8.so` to fix.

`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
Expand All @@ -62,7 +75,7 @@ Compile php-v8js itself

```
cd /tmp
git clone https://github.com/preillyme/v8js.git
git clone https://github.com/phpv8/v8js.git
cd v8js
phpize
./configure
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ class V8Js
/* Methods */

/**
* Initializes and starts V8 engine and Returns new V8Js object with it's own V8 context.
* Initializes and starts V8 engine and returns new V8Js object with it's own V8 context.
* Snapshots are supported by V8 4.3.7 and higher.
* @param string $object_name
* @param array $variables
* @param array $extensions
* @param bool $report_uncaught_exceptions
* @param string $snapshot_blob
*/
public function __construct($object_name = "PHP", array $variables = NULL, array $extensions = NULL, $report_uncaught_exceptions = TRUE)
public function __construct($object_name = "PHP", array $variables = [], array $extensions = [], $report_uncaught_exceptions = TRUE, $snapshot_blob = NULL)
{}

/**
Expand Down Expand Up @@ -174,6 +176,16 @@ class V8Js
*/
public static function getExtensions()
{}

/**
* Creates a custom V8 heap snapshot with the provided JavaScript source embedded.
* Snapshots are supported by V8 4.3.7 and higher. For older versions of V8 this
* extension doesn't provide this method.
* @param string $embed_source
* @return string|false
*/
public static function createSnapshot($embed_source)
{}
}

final class V8JsScriptException extends Exception
Expand Down
98 changes: 92 additions & 6 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ int main ()
AC_MSG_ERROR([could not determine libv8 version])
fi

AC_LANG_RESTORE
LIBS=$old_LIBS
LDFLAGS=$old_LDFLAGS
CPPFLAGS=$old_CPPFLAGS

if test "$V8_API_VERSION" -ge 3029036 ; then
dnl building for v8 3.29.36 or later, which requires us to
dnl initialize and provide a platform; hence we need to
Expand Down Expand Up @@ -169,10 +164,101 @@ int main ()
AC_MSG_ERROR([Please provide $static_link_extra_file next to the libv8.so, see README.md for details])
fi

LDFLAGS="$LDFLAGS $static_link_dir/$static_link_extra_file"
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"
fi

if test "$V8_API_VERSION" -ge 4004010 ; then
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 <v8.h>
#include <libplatform/libplatform.h>
#include <stdlib.h>
#include <string.h>

#if PHP_V8_API_VERSION >= 4004010
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); }
};
#endif

int main ()
{
v8::Platform *v8_platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(v8_platform);
v8::V8::Initialize();

#if PHP_V8_API_VERSION >= 4004044
static ArrayBufferAllocator array_buffer_allocator;
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &array_buffer_allocator;

v8::Isolate::New(create_params);
#else /* PHP_V8_API_VERSION < 4004044 */
v8::Isolate::New();
#endif
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
])
fi

AC_LANG_RESTORE
LIBS=$old_LIBS
LDFLAGS="$old_LDFLAGS $LDFLAGS_libplatform"
CPPFLAGS=$old_CPPFLAGS


PHP_NEW_EXTENSION(v8js, [ \
v8js_array_access.cc \
v8js.cc \
Expand Down
32 changes: 32 additions & 0 deletions tests/create_snapshot_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Test V8Js::createSnapshot() : Basic snapshot creation & re-use
--SKIPIF--
<?php
require_once(dirname(__FILE__) . '/skipif.inc');

if (!method_exists('V8Js', 'createSnapshot')) {
die('SKIP V8Js::createSnapshot not supported');
}
?>
--FILE--
<?php
$doublifySource = <<<EOJS
function doublify(x) {
return 2 * x;
}
EOJS;

$snap = V8Js::createSnapshot($doublifySource);

if (strlen($snap) > 0) {
var_dump("snapshot successfully created");
}

$v8 = new V8Js('PHP', array(), array(), true, $snap);
$v8->executeString('var_dump(doublify(23));');
?>
===EOF===
--EXPECT--
string(29) "snapshot successfully created"
int(46)
===EOF===
Loading