From 0945749e7cf1d3d6f23cc3c7f976dcb0f55915e3 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Thu, 7 Jan 2016 13:44:40 +0100 Subject: [PATCH 01/24] add basic test on issue #183 --- tests/issue_183_basic.phpt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/issue_183_basic.phpt diff --git a/tests/issue_183_basic.phpt b/tests/issue_183_basic.phpt new file mode 100644 index 0000000..3a0cca5 --- /dev/null +++ b/tests/issue_183_basic.phpt @@ -0,0 +1,27 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes +--SKIPIF-- + +--FILE-- +executeString($JS); + +?> +===EOF=== +--EXPECT-- +Hello World +===EOF=== From 3508f0c8e7dd005e6cb8e645755cb30853eb9941 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 8 Jan 2016 15:42:43 +0100 Subject: [PATCH 02/24] export public methods of classes derived from \V8Js, closes #183 --- php_v8js_macros.h | 7 +++++ v8js_class.cc | 63 +++++++++++++++++++++++++++++++++++++++++++ v8js_object_export.cc | 7 +---- v8js_object_export.h | 2 ++ 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 9a2b9e8..f7a0836 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -85,6 +85,13 @@ extern "C" { #define V8JS_FLAG_FORCE_ARRAY (1<<1) #define V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS (1<<2) + +/* These are not defined by Zend */ +#define ZEND_WAKEUP_FUNC_NAME "__wakeup" +#define ZEND_SLEEP_FUNC_NAME "__sleep" +#define ZEND_SET_STATE_FUNC_NAME "__set_state" + + /* Convert zval into V8 value */ v8::Handle zval_to_v8js(zval *, v8::Isolate * TSRMLS_DC); diff --git a/v8js_class.cc b/v8js_class.cc index f538a82..27f2457 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -30,6 +30,7 @@ extern "C" { #include "v8js_v8.h" #include "v8js_exceptions.h" #include "v8js_v8object_class.h" +#include "v8js_object_export.h" #include "v8js_timer.h" #include @@ -319,6 +320,10 @@ static void v8js_fatal_error_handler(const char *location, const char *message) } /* }}} */ +#define IS_MAGIC_FUNC(mname) \ + ((key_len == sizeof(mname)) && \ + !strncasecmp(key, mname, key_len - 1)) + /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions]]]) __construct for V8Js */ static PHP_METHOD(V8Js, __construct) @@ -480,7 +485,65 @@ static PHP_METHOD(V8Js, __construct) } } + /* Add pointer to zend object */ + php_obj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), v8::External::New(isolate, getThis())); + /* Export public methods */ + zend_function *method_ptr; + char *key = NULL; + uint key_len; + + zend_hash_internal_pointer_reset_ex(&c->std.ce->function_table, &pos); + for (;; zend_hash_move_forward_ex(&c->std.ce->function_table, &pos)) { + if (zend_hash_get_current_key_ex(&c->std.ce->function_table, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING || + zend_hash_get_current_data_ex(&c->std.ce->function_table, (void **) &method_ptr, &pos) == FAILURE + ) { + break; + } + + if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) { + /* Allow only public methods */ + continue; + } + + if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) { + /* no __construct, __destruct(), or __clone() functions */ + continue; + } + + /* hide (do not export) other PHP magic functions */ + if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME) || + IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) { + continue; + } + + v8::Local method_name = V8JS_STR(method_ptr->common.function_name); + v8::Local ft; + + try { + ft = v8::Local::New + (isolate, c->method_tmpls.at(method_ptr)); + } + catch (const std::out_of_range &) { + ft = v8::FunctionTemplate::New(isolate, v8js_php_callback, + v8::External::New((isolate), method_ptr)); + // @fixme add/check Signature v8::Signature::New((isolate), tmpl)); + v8js_tmpl_t *persistent_ft = &c->method_tmpls[method_ptr]; + persistent_ft->Reset(isolate, ft); + } + + + php_obj->ForceSet(method_name, ft->GetFunction()); + } } /* }}} */ diff --git a/v8js_object_export.cc b/v8js_object_export.cc index 04befee..949888f 100644 --- a/v8js_object_export.cc +++ b/v8js_object_export.cc @@ -169,7 +169,7 @@ failure: /* }}} */ /* Callback for PHP methods and functions */ -static void v8js_php_callback(const v8::FunctionCallbackInfo& info) /* {{{ */ +void v8js_php_callback(const v8::FunctionCallbackInfo& info) /* {{{ */ { v8::Isolate *isolate = info.GetIsolate(); v8::Local self = info.Holder(); @@ -291,11 +291,6 @@ static void v8js_weak_closure_callback(const v8::WeakCallbackDataweak_closures.erase(persist_tpl_); }; -/* These are not defined by Zend */ -#define ZEND_WAKEUP_FUNC_NAME "__wakeup" -#define ZEND_SLEEP_FUNC_NAME "__sleep" -#define ZEND_SET_STATE_FUNC_NAME "__set_state" - #define IS_MAGIC_FUNC(mname) \ ((key_len == sizeof(mname)) && \ !strncasecmp(key, mname, key_len - 1)) diff --git a/v8js_object_export.h b/v8js_object_export.h index 72c48a7..ea697b4 100644 --- a/v8js_object_export.h +++ b/v8js_object_export.h @@ -30,4 +30,6 @@ v8::Local v8js_named_property_callback(v8::Local property property_op_t callback_type, v8::Local set_value = v8::Local()); +void v8js_php_callback(const v8::FunctionCallbackInfo& info); + #endif /* V8JS_OBJECT_EXPORT_H */ From e0f990bfa1ad7c2aa799cfecd88ad4c5813b678c Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 8 Jan 2016 15:47:24 +0100 Subject: [PATCH 03/24] Add test with private/protected methods, refs #183 --- tests/issue_183_001.phpt | 30 ++++++++++++++++++++++++++++++ tests/issue_183_002.phpt | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/issue_183_001.phpt create mode 100644 tests/issue_183_002.phpt diff --git a/tests/issue_183_001.phpt b/tests/issue_183_001.phpt new file mode 100644 index 0000000..8469940 --- /dev/null +++ b/tests/issue_183_001.phpt @@ -0,0 +1,30 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes (protected) +--SKIPIF-- + +--FILE-- +executeString($JS); + +?> +===EOF=== +--EXPECTF-- +Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: PHP.hello is not a function' in %s +Stack trace: +#0 %s: V8Js->executeString('PHP.hello();') +#1 {main} + thrown in %s on line 16 diff --git a/tests/issue_183_002.phpt b/tests/issue_183_002.phpt new file mode 100644 index 0000000..fa8704b --- /dev/null +++ b/tests/issue_183_002.phpt @@ -0,0 +1,30 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes (private) +--SKIPIF-- + +--FILE-- +executeString($JS); + +?> +===EOF=== +--EXPECTF-- +Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: PHP.hello is not a function' in %s +Stack trace: +#0 %s: V8Js->executeString('PHP.hello();') +#1 {main} + thrown in %s on line 16 From 1e86e2c9f79ea46f6990f1cef62137a247e902f2 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 9 Jan 2016 19:02:58 +0100 Subject: [PATCH 04/24] Don't export methods of V8Js object to V8 --- tests/issue_183_003.phpt | 57 ++++++++++++++++++++++++++++++++++++++++ v8js_class.cc | 18 ++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/issue_183_003.phpt diff --git a/tests/issue_183_003.phpt b/tests/issue_183_003.phpt new file mode 100644 index 0000000..1a96c3f --- /dev/null +++ b/tests/issue_183_003.phpt @@ -0,0 +1,57 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes (V8Js methods) +--SKIPIF-- + +--FILE-- +executeString($JS); + +?> +===EOF=== +--EXPECTF-- +string(8) "function" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(9) "undefined" +string(6) "caught" +===EOF=== diff --git a/v8js_class.cc b/v8js_class.cc index 27f2457..5b121ae 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -46,6 +46,9 @@ static zend_class_entry *php_ce_v8js; static zend_object_handlers v8js_object_handlers; /* }}} */ +/* Forward declare v8js_methods, actually "static" but not possible in C++ */ +extern const zend_function_entry v8js_methods[]; + typedef struct _v8js_script { char *name; v8js_ctx *ctx; @@ -526,6 +529,19 @@ static PHP_METHOD(V8Js, __construct) continue; } + const zend_function_entry *fe; + for (fe = v8js_methods; fe->fname; fe ++) { + if (fe->fname == method_ptr->common.function_name) { + break; + } + } + + if(fe->fname) { + /* Method belongs to \V8Js class itself, never export to V8, even if + * it is overriden in a derived class. */ + continue; + } + v8::Local method_name = V8JS_STR(method_ptr->common.function_name); v8::Local ft; @@ -1119,7 +1135,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1) ZEND_END_ARG_INFO() -static const zend_function_entry v8js_methods[] = { /* {{{ */ +const zend_function_entry v8js_methods[] = { /* {{{ */ PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) PHP_ME(V8Js, __wakeup, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) From d438624a3d30db4bb8019a91dc0cf30276a9df3b Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 9 Jan 2016 19:11:30 +0100 Subject: [PATCH 05/24] Don't export V8Js methods even if overwritten --- tests/issue_183_004.phpt | 44 ++++++++++++++++++++++++++++++++++++++++ v8js_class.cc | 4 ++-- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/issue_183_004.phpt diff --git a/tests/issue_183_004.phpt b/tests/issue_183_004.phpt new file mode 100644 index 0000000..e4d2f17 --- /dev/null +++ b/tests/issue_183_004.phpt @@ -0,0 +1,44 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes (overridden V8Js methods) +--SKIPIF-- + +--FILE-- +executeString($JS); + +?> +===EOF=== +--EXPECTF-- +string(13) "executeString" +string(8) "function" +string(9) "undefined" +string(6) "caught" +===EOF=== diff --git a/v8js_class.cc b/v8js_class.cc index 5b121ae..a472209 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -531,7 +531,7 @@ static PHP_METHOD(V8Js, __construct) const zend_function_entry *fe; for (fe = v8js_methods; fe->fname; fe ++) { - if (fe->fname == method_ptr->common.function_name) { + if (strcmp(fe->fname, method_ptr->common.function_name) == 0) { break; } } @@ -1186,7 +1186,7 @@ static void v8js_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRM /* Global PHP JS object */ v8::Local object_name_js = v8::Local::New(isolate, c->object_name); v8::Local jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject(); - + /* Delete value from PHP JS object */ jsobj->Delete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member))); From 4a8a8b812fc6dbb2d5249200a25f47e079461b0f Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 9 Jan 2016 19:15:12 +0100 Subject: [PATCH 06/24] Add test that V8Js::__sleep and V8Js::__wakeup are final --- tests/issue_183_005.phpt | 19 +++++++++++++++++++ tests/issue_183_006.phpt | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/issue_183_005.phpt create mode 100644 tests/issue_183_006.phpt diff --git a/tests/issue_183_005.phpt b/tests/issue_183_005.phpt new file mode 100644 index 0000000..2cff423 --- /dev/null +++ b/tests/issue_183_005.phpt @@ -0,0 +1,19 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes (__sleep) +--SKIPIF-- + +--FILE-- + +===EOF=== +--EXPECTF-- +Fatal error: Cannot override final method V8Js::__sleep() in %s diff --git a/tests/issue_183_006.phpt b/tests/issue_183_006.phpt new file mode 100644 index 0000000..395e905 --- /dev/null +++ b/tests/issue_183_006.phpt @@ -0,0 +1,19 @@ +--TEST-- +Test V8::executeString() : Method access on derived classes (__wakeup) +--SKIPIF-- + +--FILE-- + +===EOF=== +--EXPECTF-- +Fatal error: Cannot override final method V8Js::__wakeup() in %s From 796ae85cb62e71a3e14918ebb7d351d174de10aa Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 9 Jan 2016 21:36:30 +0100 Subject: [PATCH 07/24] Adapt tests to different V8 versions --- tests/issue_183_001.phpt | 2 +- tests/issue_183_002.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/issue_183_001.phpt b/tests/issue_183_001.phpt index 8469940..2896e1b 100644 --- a/tests/issue_183_001.phpt +++ b/tests/issue_183_001.phpt @@ -23,7 +23,7 @@ $v8->executeString($JS); ?> ===EOF=== --EXPECTF-- -Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: PHP.hello is not a function' in %s +Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: %s is not a function' in %s Stack trace: #0 %s: V8Js->executeString('PHP.hello();') #1 {main} diff --git a/tests/issue_183_002.phpt b/tests/issue_183_002.phpt index fa8704b..f79d5e8 100644 --- a/tests/issue_183_002.phpt +++ b/tests/issue_183_002.phpt @@ -23,7 +23,7 @@ $v8->executeString($JS); ?> ===EOF=== --EXPECTF-- -Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: PHP.hello is not a function' in %s +Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: %s is not a function' in %s Stack trace: #0 %s: V8Js->executeString('PHP.hello();') #1 {main} From 3808f69deac8586cb2996f1d32fbc84dd1a35aea Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 9 Jan 2016 21:45:10 +0100 Subject: [PATCH 08/24] Adapt tests V8 3.24 also --- tests/issue_183_001.phpt | 2 +- tests/issue_183_002.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/issue_183_001.phpt b/tests/issue_183_001.phpt index 2896e1b..921fec6 100644 --- a/tests/issue_183_001.phpt +++ b/tests/issue_183_001.phpt @@ -23,7 +23,7 @@ $v8->executeString($JS); ?> ===EOF=== --EXPECTF-- -Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: %s is not a function' in %s +Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: %s' in %s Stack trace: #0 %s: V8Js->executeString('PHP.hello();') #1 {main} diff --git a/tests/issue_183_002.phpt b/tests/issue_183_002.phpt index f79d5e8..1745fdf 100644 --- a/tests/issue_183_002.phpt +++ b/tests/issue_183_002.phpt @@ -23,7 +23,7 @@ $v8->executeString($JS); ?> ===EOF=== --EXPECTF-- -Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: %s is not a function' in %s +Fatal error: Uncaught exception 'V8JsScriptException' with message 'V8Js::compileString():1: TypeError: %s' in %s Stack trace: #0 %s: V8Js->executeString('PHP.hello();') #1 {main} From b405a3469004b66dcd46240a4c4f4d43bbe04173 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 17:00:47 +0100 Subject: [PATCH 09/24] Allow to create snapshots and re-use them --- README.md | 13 ++++++++-- v8js_class.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++------- v8js_class.h | 3 +++ 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 18ce894..71d20b1 100644 --- a/README.md +++ b/README.md @@ -62,13 +62,14 @@ 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. * @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) {} /** @@ -174,6 +175,14 @@ class V8Js */ public static function getExtensions() {} + + /** + * Creates a custom V8 heap snapshot with the provided JavaScript source embedded. + * @param string $embed_source + * @return string|false + */ + public static function createSnapshot($embed_source) + {} } final class V8JsScriptException extends Exception diff --git a/v8js_class.cc b/v8js_class.cc index a472209..47e645d 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -207,6 +207,10 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ c->modules_stack.~vector(); c->modules_base.~vector(); + if (c->snapshot_blob.data) { + efree((void*)c->snapshot_blob.data); + } + efree(object); } /* }}} */ @@ -327,12 +331,12 @@ static void v8js_fatal_error_handler(const char *location, const char *message) ((key_len == sizeof(mname)) && \ !strncasecmp(key, mname, key_len - 1)) -/* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions]]]) +/* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions [, string snapshot_blob]]]]]) __construct for V8Js */ static PHP_METHOD(V8Js, __construct) { - char *object_name = NULL, *class_name = NULL; - int object_name_len = 0, free = 0; + char *object_name = NULL, *class_name = NULL, *snapshot_blob = NULL; + int object_name_len = 0, free = 0, snapshot_blob_len = 0; zend_uint class_name_len = 0; zend_bool report_uncaught = 1; zval *vars_arr = NULL, *exts_arr = NULL; @@ -346,7 +350,7 @@ static PHP_METHOD(V8Js, __construct) return; } - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saab", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saabs", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught, &snapshot_blob, &snapshot_blob_len) == FAILURE) { return; } @@ -360,9 +364,17 @@ static PHP_METHOD(V8Js, __construct) #if PHP_V8_API_VERSION >= 4004044 static ArrayBufferAllocator array_buffer_allocator; - static v8::Isolate::CreateParams create_params; - create_params.array_buffer_allocator = &array_buffer_allocator; - c->isolate = v8::Isolate::New(create_params); + new (&c->create_params) v8::Isolate::CreateParams(); + c->create_params.array_buffer_allocator = &array_buffer_allocator; + + new (&c->snapshot_blob) v8::StartupData(); + if (snapshot_blob && snapshot_blob_len) { + c->snapshot_blob.data = snapshot_blob; + c->snapshot_blob.raw_size = snapshot_blob_len; + c->create_params.snapshot_blob = &c->snapshot_blob; + } + + c->isolate = v8::Isolate::New(c->create_params); #else c->isolate = v8::Isolate::New(); #endif @@ -991,6 +1003,10 @@ static int v8js_register_extension(char *name, uint name_len, char *source, uint } /* }}} */ + + +/* ## Static methods ## */ + /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]]) */ static PHP_METHOD(V8Js, registerExtension) @@ -1015,8 +1031,6 @@ static PHP_METHOD(V8Js, registerExtension) } /* }}} */ -/* ## Static methods ## */ - /* {{{ proto array V8Js::getExtensions() */ static PHP_METHOD(V8Js, getExtensions) @@ -1063,6 +1077,37 @@ static PHP_METHOD(V8Js, getExtensions) } /* }}} */ +/* {{{ proto string|bool V8Js::createSnapshot(string embed_source) + */ +static PHP_METHOD(V8Js, createSnapshot) +{ + char *script; + int script_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script, &script_len) == FAILURE) { + return; + } + + if (!script_len) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty"); + RETURN_FALSE; + } + + /* Initialize V8, if not already done. */ + v8js_v8_init(TSRMLS_C); + + v8::StartupData snapshot_blob = v8::V8::CreateSnapshotDataBlob(script); + + if (!snapshot_blob.data) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 heap snapshot. Check $embed_source for errors."); + RETURN_FALSE; + } + + RETVAL_STRINGL(snapshot_blob.data, snapshot_blob.raw_size, 1); + delete[] snapshot_blob.data; +} +/* }}} */ + /* {{{ arginfo */ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0) ZEND_ARG_INFO(0, object_name) @@ -1126,6 +1171,10 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1) + ZEND_ARG_INFO(0, script) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1) ZEND_ARG_INFO(0, time_limit) ZEND_END_ARG_INFO() @@ -1149,6 +1198,7 @@ const zend_function_entry v8js_methods[] = { /* {{{ */ PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC) PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC) PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} diff --git a/v8js_class.h b/v8js_class.h index ab7b7aa..1c29b70 100644 --- a/v8js_class.h +++ b/v8js_class.h @@ -67,6 +67,9 @@ struct v8js_ctx { std::vector accessor_list; std::vector script_objects; char *tz; + + v8::Isolate::CreateParams create_params; + v8::StartupData snapshot_blob; #ifdef ZTS void ***zts_ctx; #endif From 39062b4248d2d11d2b24fa53f2b058e35d3fbe9e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 17:48:44 +0100 Subject: [PATCH 10/24] Test for snapshot support in V8 Looks like we have to test for internal functions unfortunately since the public V8 snapshot API is available no matter whether the library really supports it or not. --- config.m4 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ v8js_v8.cc | 7 +++++++ 2 files changed, 55 insertions(+) diff --git a/config.m4 b/config.m4 index 1e3c488..a192f5d 100644 --- a/config.m4 +++ b/config.m4 @@ -130,6 +130,54 @@ int main () AC_MSG_ERROR([could not determine libv8 version]) fi + AC_MSG_CHECKING([for v8::internal::ReadNatives]) + AC_TRY_LINK([ + namespace v8 { + namespace internal { + void ReadNatives(); + } + }], [v8::internal::ReadNatives();], [ + AC_MSG_RESULT([found (using snapshots)]) + 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" + + 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 + + ], [ + AC_MSG_RESULT([not found (snapshots disabled)]) + ]) + AC_LANG_RESTORE LIBS=$old_LIBS LDFLAGS=$old_LDFLAGS diff --git a/v8js_v8.cc b/v8js_v8.cc index 8b52063..0b1b4b3 100644 --- a/v8js_v8.cc +++ b/v8js_v8.cc @@ -54,6 +54,13 @@ void v8js_v8_init(TSRMLS_D) /* {{{ */ } #endif +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA + v8::V8::InitializeExternalStartupData( + PHP_V8_NATIVES_BLOB_PATH, + PHP_V8_SNAPSHOT_BLOB_PATH + ); +#endif + #if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036 v8js_process_globals.v8_platform = v8::platform::CreateDefaultPlatform(); v8::V8::InitializePlatform(v8js_process_globals.v8_platform); From 2c8ef2aa661fa8002ea4c825f180718b1d2804c5 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 17:54:00 +0100 Subject: [PATCH 11/24] conditionally compile in snapshot functionality --- v8js_class.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/v8js_class.cc b/v8js_class.cc index 47e645d..64f8c4a 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -207,9 +207,11 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ c->modules_stack.~vector(); c->modules_base.~vector(); +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA if (c->snapshot_blob.data) { efree((void*)c->snapshot_blob.data); } +#endif efree(object); } @@ -368,14 +370,16 @@ static PHP_METHOD(V8Js, __construct) c->create_params.array_buffer_allocator = &array_buffer_allocator; new (&c->snapshot_blob) v8::StartupData(); +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA if (snapshot_blob && snapshot_blob_len) { c->snapshot_blob.data = snapshot_blob; c->snapshot_blob.raw_size = snapshot_blob_len; c->create_params.snapshot_blob = &c->snapshot_blob; } +#endif /* PHP_V8_USE_EXTERNAL_STARTUP_DATA */ c->isolate = v8::Isolate::New(c->create_params); -#else +#else /* PHP_V8_API_VERSION < 4004044 */ c->isolate = v8::Isolate::New(); #endif @@ -1077,6 +1081,7 @@ static PHP_METHOD(V8Js, getExtensions) } /* }}} */ +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA /* {{{ proto string|bool V8Js::createSnapshot(string embed_source) */ static PHP_METHOD(V8Js, createSnapshot) @@ -1107,6 +1112,8 @@ static PHP_METHOD(V8Js, createSnapshot) delete[] snapshot_blob.data; } /* }}} */ +#endif /* PHP_V8_USE_EXTERNAL_STARTUP_DATA */ + /* {{{ arginfo */ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0) @@ -1171,9 +1178,11 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0) ZEND_END_ARG_INFO() +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1) ZEND_ARG_INFO(0, script) ZEND_END_ARG_INFO() +#endif ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1) ZEND_ARG_INFO(0, time_limit) @@ -1196,11 +1205,13 @@ const zend_function_entry v8js_methods[] = { /* {{{ */ PHP_ME(V8Js, clearPendingException, arginfo_v8js_clearpendingexception, ZEND_ACC_PUBLIC) PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC) PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC) - PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) - PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) - PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC) PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC) + PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA + PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) +#endif {NULL, NULL, NULL} }; /* }}} */ From 52ebdc28ed84d46d7d2d892b831a8d16b58cc991 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 19:52:46 +0100 Subject: [PATCH 12/24] Simply v8::Isolate::New to test if snapshot blob needed The test on internal symbols seems too fragile, e.g. with V8 version 4.5.90 it is false positive. --- config.m4 | 146 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 92 insertions(+), 54 deletions(-) diff --git a/config.m4 b/config.m4 index a192f5d..b35090a 100644 --- a/config.m4 +++ b/config.m4 @@ -130,59 +130,6 @@ int main () AC_MSG_ERROR([could not determine libv8 version]) fi - AC_MSG_CHECKING([for v8::internal::ReadNatives]) - AC_TRY_LINK([ - namespace v8 { - namespace internal { - void ReadNatives(); - } - }], [v8::internal::ReadNatives();], [ - AC_MSG_RESULT([found (using snapshots)]) - 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" - - 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 - - ], [ - AC_MSG_RESULT([not found (snapshots disabled)]) - ]) - - 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 @@ -217,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 + #include + #include + #include + +#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" + + 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 \ From 40f0f5e89f110d8550b76638a67576b88db21387 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 20:17:20 +0100 Subject: [PATCH 13/24] Provide startup data loader for V8 < 4.6.76 --- v8js_v8.cc | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/v8js_v8.cc b/v8js_v8.cc index 0b1b4b3..8091429 100644 --- a/v8js_v8.cc +++ b/v8js_v8.cc @@ -2,7 +2,7 @@ +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2015 The PHP Group | + | Copyright (c) 1997-2016 The PHP Group | +----------------------------------------------------------------------+ | http://www.opensource.org/licenses/mit-license.php MIT License | +----------------------------------------------------------------------+ @@ -34,6 +34,43 @@ extern "C" { #include "v8js_timer.h" #include "v8js_exceptions.h" +#if defined(PHP_V8_USE_EXTERNAL_STARTUP_DATA) && PHP_V8_API_VERSION < 4006076 +/* Old V8 version, requires startup data but has no + * (internal/API) means to let it be loaded. */ +static v8::StartupData natives_; +static v8::StartupData snapshot_; + +static void v8js_v8_load_startup_data(const char* blob_file, + v8::StartupData* startup_data, + void (*setter_fn)(v8::StartupData*)) { + startup_data->data = NULL; + startup_data->raw_size = 0; + + if (!blob_file) { + return; + } + + FILE* file = fopen(blob_file, "rb"); + if (!file) { + return; + } + + fseek(file, 0, SEEK_END); + startup_data->raw_size = static_cast(ftell(file)); + rewind(file); + + startup_data->data = new char[startup_data->raw_size]; + int read_size = static_cast(fread(const_cast(startup_data->data), + 1, startup_data->raw_size, file)); + fclose(file); + + if (startup_data->raw_size == read_size) { + (*setter_fn)(startup_data); + } +} +#endif + + void v8js_v8_init(TSRMLS_D) /* {{{ */ { /* Run only once; thread-local test first */ @@ -55,10 +92,16 @@ void v8js_v8_init(TSRMLS_D) /* {{{ */ #endif #ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA + /* V8 doesn't work without startup data, load it. */ +#if PHP_V8_API_VERSION >= 4006076 v8::V8::InitializeExternalStartupData( PHP_V8_NATIVES_BLOB_PATH, PHP_V8_SNAPSHOT_BLOB_PATH ); +#else + v8js_v8_load_startup_data(PHP_V8_NATIVES_BLOB_PATH, &natives_, v8::V8::SetNativesDataBlob); + v8js_v8_load_startup_data(PHP_V8_SNAPSHOT_BLOB_PATH, &snapshot_, v8::V8::SetSnapshotDataBlob); +#endif #endif #if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036 From 93d3f9591f8803254c766bda402c9b2602f6313b Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 20:17:56 +0100 Subject: [PATCH 14/24] Add CreateParams fields in v8js_ctx only if needed --- v8js_class.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/v8js_class.h b/v8js_class.h index 1c29b70..35db5e9 100644 --- a/v8js_class.h +++ b/v8js_class.h @@ -2,12 +2,13 @@ +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2013 The PHP Group | + | Copyright (c) 1997-2016 The PHP Group | +----------------------------------------------------------------------+ | http://www.opensource.org/licenses/mit-license.php MIT License | +----------------------------------------------------------------------+ | Author: Jani Taskinen | | Author: Patrick Reilly | + | Author: Stefan Siegl | +----------------------------------------------------------------------+ */ @@ -68,8 +69,13 @@ struct v8js_ctx { std::vector script_objects; char *tz; +#if PHP_V8_API_VERSION >= 4004044 v8::Isolate::CreateParams create_params; +#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA v8::StartupData snapshot_blob; +#endif +#endif + #ifdef ZTS void ***zts_ctx; #endif From a1cdd7537b49c771289f88872aaa49ad8b8f4d8c Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sun, 28 Feb 2016 20:29:53 +0100 Subject: [PATCH 15/24] Fix conditional compilation for snapshot=off --- v8js_class.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/v8js_class.cc b/v8js_class.cc index 64f8c4a..c72d456 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -207,7 +207,7 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ c->modules_stack.~vector(); c->modules_base.~vector(); -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA +#if defined(PHP_V8_USE_EXTERNAL_STARTUP_DATA) && PHP_V8_API_VERSION >= 4004044 if (c->snapshot_blob.data) { efree((void*)c->snapshot_blob.data); } @@ -369,8 +369,8 @@ static PHP_METHOD(V8Js, __construct) new (&c->create_params) v8::Isolate::CreateParams(); c->create_params.array_buffer_allocator = &array_buffer_allocator; - new (&c->snapshot_blob) v8::StartupData(); #ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA + new (&c->snapshot_blob) v8::StartupData(); if (snapshot_blob && snapshot_blob_len) { c->snapshot_blob.data = snapshot_blob; c->snapshot_blob.raw_size = snapshot_blob_len; From 4df6e80be9707f073187480eca27822116b20c7e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 14:12:19 +0100 Subject: [PATCH 16/24] Add testcase on V8Js::createSnapshot --- tests/create_snapshot_basic.phpt | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/create_snapshot_basic.phpt diff --git a/tests/create_snapshot_basic.phpt b/tests/create_snapshot_basic.phpt new file mode 100644 index 0000000..731426d --- /dev/null +++ b/tests/create_snapshot_basic.phpt @@ -0,0 +1,32 @@ +--TEST-- +Test V8Js::createSnapshot() : Basic snapshot creation & re-use +--SKIPIF-- + +--FILE-- + 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=== From 67269ddfc48899d1cb03948858cf31e4f58d5560 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 17:11:52 +0100 Subject: [PATCH 17/24] Adapt test expectations to V8 error message changes V8 5.1 uses slightly changed SyntaxError messages. This adapts our tests as needed (for support of V8 5.1 as well as older versions) --- tests/checkstring.phpt | 2 +- tests/checkstring_compile.phpt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/checkstring.phpt b/tests/checkstring.phpt index b5f2790..9d6ce0e 100644 --- a/tests/checkstring.phpt +++ b/tests/checkstring.phpt @@ -20,5 +20,5 @@ Deprecated: Function V8Js::checkString() is deprecated in %s on line %d bool(true) Deprecated: Function V8Js::checkString() is deprecated in %s on line %d -string(60) "V8Js::checkString():1: SyntaxError: Unexpected token ILLEGAL" +string(%d) "V8Js::checkString():1: SyntaxError: %s" ===EOF=== diff --git a/tests/checkstring_compile.phpt b/tests/checkstring_compile.phpt index 2c9e188..c571e06 100644 --- a/tests/checkstring_compile.phpt +++ b/tests/checkstring_compile.phpt @@ -17,5 +17,5 @@ try { ===EOF=== --EXPECTF-- resource(%d) of type (V8Js script) -string(62) "V8Js::compileString():1: SyntaxError: Unexpected token ILLEGAL" +string(%d) "V8Js::compileString():1: SyntaxError: %s" ===EOF=== From d0e78019aaca8134c5f98557c826c254edd5386a Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 14:22:48 +0100 Subject: [PATCH 18/24] Allow snapshot creation & use with V8 > 4.3.7 This does *not* seem to depend on whether V8 itself was compiled with support for snapshots or not. Therefore use PHP_V8_USE_EXTERNAL_STARTUP_DATA only to mark whether we need to provide external snapshot data to V8. --- v8js_class.cc | 20 +++++++++++--------- v8js_class.h | 4 +--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/v8js_class.cc b/v8js_class.cc index c72d456..810d414 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -207,7 +207,7 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ c->modules_stack.~vector(); c->modules_base.~vector(); -#if defined(PHP_V8_USE_EXTERNAL_STARTUP_DATA) && PHP_V8_API_VERSION >= 4004044 +#if PHP_V8_API_VERSION >= 4003007 if (c->snapshot_blob.data) { efree((void*)c->snapshot_blob.data); } @@ -364,22 +364,23 @@ static PHP_METHOD(V8Js, __construct) c->pending_exception = NULL; c->in_execution = 0; +#if PHP_V8_API_VERSION >= 4003007 + new (&c->create_params) v8::Isolate::CreateParams(); + #if PHP_V8_API_VERSION >= 4004044 static ArrayBufferAllocator array_buffer_allocator; - new (&c->create_params) v8::Isolate::CreateParams(); c->create_params.array_buffer_allocator = &array_buffer_allocator; +#endif -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA new (&c->snapshot_blob) v8::StartupData(); if (snapshot_blob && snapshot_blob_len) { c->snapshot_blob.data = snapshot_blob; c->snapshot_blob.raw_size = snapshot_blob_len; c->create_params.snapshot_blob = &c->snapshot_blob; } -#endif /* PHP_V8_USE_EXTERNAL_STARTUP_DATA */ c->isolate = v8::Isolate::New(c->create_params); -#else /* PHP_V8_API_VERSION < 4004044 */ +#else /* PHP_V8_API_VERSION < 4003007 */ c->isolate = v8::Isolate::New(); #endif @@ -1081,7 +1082,7 @@ static PHP_METHOD(V8Js, getExtensions) } /* }}} */ -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA +#if PHP_V8_API_VERSION >= 4003007 /* {{{ proto string|bool V8Js::createSnapshot(string embed_source) */ static PHP_METHOD(V8Js, createSnapshot) @@ -1112,7 +1113,7 @@ static PHP_METHOD(V8Js, createSnapshot) delete[] snapshot_blob.data; } /* }}} */ -#endif /* PHP_V8_USE_EXTERNAL_STARTUP_DATA */ +#endif /* PHP_V8_API_VERSION >= 4003007 */ /* {{{ arginfo */ @@ -1178,7 +1179,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0) ZEND_END_ARG_INFO() -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA +#if PHP_V8_API_VERSION >= 4003007 ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1) ZEND_ARG_INFO(0, script) ZEND_END_ARG_INFO() @@ -1209,7 +1210,8 @@ const zend_function_entry v8js_methods[] = { /* {{{ */ PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC) PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA + +#if PHP_V8_API_VERSION >= 4003007 PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) #endif {NULL, NULL, NULL} diff --git a/v8js_class.h b/v8js_class.h index 35db5e9..d0fd967 100644 --- a/v8js_class.h +++ b/v8js_class.h @@ -69,12 +69,10 @@ struct v8js_ctx { std::vector script_objects; char *tz; -#if PHP_V8_API_VERSION >= 4004044 +#if PHP_V8_API_VERSION >= 4003007 v8::Isolate::CreateParams create_params; -#ifdef PHP_V8_USE_EXTERNAL_STARTUP_DATA v8::StartupData snapshot_blob; #endif -#endif #ifdef ZTS void ***zts_ctx; From 8e3e0c91b5b447360d07367656984827e79b0010 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 14:24:08 +0100 Subject: [PATCH 19/24] Add ZEND_ARG_INFO for V8Js::__construct 5th arg: snapshot_blob --- v8js_class.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/v8js_class.cc b/v8js_class.cc index 810d414..39f830e 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -1122,6 +1122,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0) ZEND_ARG_INFO(0, variables) ZEND_ARG_INFO(0, extensions) ZEND_ARG_INFO(0, report_uncaught_exceptions) + ZEND_ARG_INFO(0, snapshot_blob) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0) From 606eb5a60fa08c21d3504d5d321f8ce83ce118cb Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 14:27:46 +0100 Subject: [PATCH 20/24] Mention limitted snapshot availibility in README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 71d20b1..c4a54b6 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ class V8Js /** * 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 @@ -178,6 +179,8 @@ class V8Js /** * 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 */ From 930b450d992be5061d5e4466393b87993930d69a Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 16:54:06 +0100 Subject: [PATCH 21/24] refcount on snapshot_blob zval --- v8js_class.cc | 26 +++++++++++++++++--------- v8js_class.h | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/v8js_class.cc b/v8js_class.cc index 39f830e..f10725f 100644 --- a/v8js_class.cc +++ b/v8js_class.cc @@ -208,8 +208,8 @@ static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */ c->modules_base.~vector(); #if PHP_V8_API_VERSION >= 4003007 - if (c->snapshot_blob.data) { - efree((void*)c->snapshot_blob.data); + if (c->zval_snapshot_blob) { + zval_ptr_dtor(&c->zval_snapshot_blob); } #endif @@ -337,13 +337,14 @@ static void v8js_fatal_error_handler(const char *location, const char *message) __construct for V8Js */ static PHP_METHOD(V8Js, __construct) { - char *object_name = NULL, *class_name = NULL, *snapshot_blob = NULL; - int object_name_len = 0, free = 0, snapshot_blob_len = 0; + char *object_name = NULL, *class_name = NULL; + int object_name_len = 0, free = 0; zend_uint class_name_len = 0; zend_bool report_uncaught = 1; zval *vars_arr = NULL, *exts_arr = NULL; const char **exts = NULL; int exts_count = 0; + zval *snapshot_blob = NULL; v8js_ctx *c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC); @@ -352,7 +353,7 @@ static PHP_METHOD(V8Js, __construct) return; } - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saabs", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught, &snapshot_blob, &snapshot_blob_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saabz", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught, &snapshot_blob) == FAILURE){ return; } @@ -373,10 +374,17 @@ static PHP_METHOD(V8Js, __construct) #endif new (&c->snapshot_blob) v8::StartupData(); - if (snapshot_blob && snapshot_blob_len) { - c->snapshot_blob.data = snapshot_blob; - c->snapshot_blob.raw_size = snapshot_blob_len; - c->create_params.snapshot_blob = &c->snapshot_blob; + if (snapshot_blob) { + if (Z_TYPE_P(snapshot_blob) == IS_STRING) { + c->zval_snapshot_blob = snapshot_blob; + Z_ADDREF_P(c->zval_snapshot_blob); + + c->snapshot_blob.data = Z_STRVAL_P(snapshot_blob); + c->snapshot_blob.raw_size = Z_STRLEN_P(snapshot_blob); + c->create_params.snapshot_blob = &c->snapshot_blob; + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument snapshot_blob expected to be of string type"); + } } c->isolate = v8::Isolate::New(c->create_params); diff --git a/v8js_class.h b/v8js_class.h index d0fd967..0e91243 100644 --- a/v8js_class.h +++ b/v8js_class.h @@ -71,6 +71,7 @@ struct v8js_ctx { #if PHP_V8_API_VERSION >= 4003007 v8::Isolate::CreateParams create_params; + zval *zval_snapshot_blob; v8::StartupData snapshot_blob; #endif From de0b8e2aa809ff92bff4685bc479cf4b8721d774 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 16:58:55 +0100 Subject: [PATCH 22/24] Check V8_DIR/share/v8 for snapshot images as well --- config.m4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.m4 b/config.m4 index b35090a..f721165 100644 --- a/config.m4 +++ b/config.m4 @@ -217,7 +217,7 @@ public: 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" + SEARCH_PATH="$V8_DIR/lib $V8_DIR/share/v8" AC_MSG_CHECKING([for natives_blob.bin]) SEARCH_FOR="natives_blob.bin" From f49d3b79cab57e5d87c6e332863cb4cb872bb5ac Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 17:09:34 +0100 Subject: [PATCH 23/24] Update README.Linux.md for snapshot usage --- README.Linux.md | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/README.Linux.md b/README.Linux.md index e85c261..9f9afc9 100644 --- a/README.Linux.md +++ b/README.Linux.md @@ -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 ----------------- ``` @@ -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 @@ -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 @@ -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 From 770c9b2fca66b034ea27fb0372add2c56d82e15c Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Sat, 5 Mar 2016 21:23:27 +0100 Subject: [PATCH 24/24] Bump version to 0.5.0 --- package.xml | 64 ++++++++++++++++++++++++++++++++++------------- php_v8js_macros.h | 2 +- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/package.xml b/package.xml index bb57324..53a6dd0 100644 --- a/package.xml +++ b/package.xml @@ -16,11 +16,11 @@ stesie@php.net yes - 2016-01-08 - + 2016-03-05 + - 0.4.1 - 0.4.1 + 0.5.0 + 0.5.0 stable @@ -28,8 +28,11 @@ The MIT License (MIT) -- Fix PHP.var_dump output on regexp objects (for consistency over all V8 versions) -- Fix "this" on V8Object method invocations +- Export public methods on classes derived from \V8Js to V8 +- Support V8 compiled with external snapshots +- Allow to create custom snapshots of V8 heaps +- Allow to create V8 contexts based on custom snapshots generated earlier +- Support V8 5.1 well @@ -61,8 +64,8 @@ - - + + @@ -86,6 +89,7 @@ + @@ -124,6 +128,13 @@ + + + + + + + @@ -181,36 +192,36 @@ - + - - + + - + - - + + - - + + - + @@ -488,5 +499,24 @@ - Fix "this" on V8Object method invocations + + + 0.5.0 + 0.5.0 + + + stable + stable + + 2016-03-05 + The MIT License (MIT) + +- Export public methods on classes derived from \V8Js to V8 +- Support V8 compiled with external snapshots +- Allow to create custom snapshots of V8 heaps +- Allow to create V8 contexts based on custom snapshots generated earlier +- Support V8 5.1 well + + diff --git a/php_v8js_macros.h b/php_v8js_macros.h index 68431d6..d0a4760 100644 --- a/php_v8js_macros.h +++ b/php_v8js_macros.h @@ -53,7 +53,7 @@ extern "C" { #endif /* V8Js Version */ -#define PHP_V8JS_VERSION "0.4.1" +#define PHP_V8JS_VERSION "0.5.0" /* Hidden field name used to link JS wrappers with underlying PHP object */ #define PHPJS_OBJECT_KEY "phpjs::object"