0
0
mirror of https://github.com/phpv8/v8js.git synced 2024-11-09 16:28:41 +00:00

Replaced deprecated v8 api calls with newer equivalents

This commit is contained in:
Stefan Siegl 2013-07-08 00:58:19 +02:00
parent 6eeb6fe9d6
commit ffa42cdfaf
5 changed files with 71 additions and 58 deletions

View File

@ -50,7 +50,7 @@ extern "C" {
#define V8JS_BOOL(v) v8::Boolean::New(v) #define V8JS_BOOL(v) v8::Boolean::New(v)
#define V8JS_NULL v8::Null() #define V8JS_NULL v8::Null()
#define V8JS_MN(name) v8js_method_##name #define V8JS_MN(name) v8js_method_##name
#define V8JS_METHOD(name) v8::Handle<v8::Value> V8JS_MN(name)(const v8::Arguments& args) #define V8JS_METHOD(name) void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
#define V8JS_THROW(type, message, message_len) v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len))) #define V8JS_THROW(type, message, message_len) v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))
#define V8JS_GLOBAL v8::Context::GetCurrent()->Global() #define V8JS_GLOBAL v8::Context::GetCurrent()->Global()

View File

@ -389,7 +389,7 @@ void php_v8js_create_v8(zval *res, v8::Handle<v8::Value> value, int flags, v8::I
c = (php_v8js_object *) zend_object_store_get_object(res TSRMLS_CC); c = (php_v8js_object *) zend_object_store_get_object(res TSRMLS_CC);
c->v8obj = v8::Persistent<v8::Value>::New(value); c->v8obj = v8::Persistent<v8::Value>::New(isolate, value);
c->flags = flags; c->flags = flags;
c->isolate = isolate; c->isolate = isolate;
} }
@ -587,14 +587,14 @@ static PHP_METHOD(V8Js, __construct)
/* Create global template for global object */ /* Create global template for global object */
// Now we are using multiple isolates this needs to be created for every context // Now we are using multiple isolates this needs to be created for every context
c->global_template = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New()); c->global_template = v8::Persistent<v8::FunctionTemplate>::New(c->isolate, v8::FunctionTemplate::New());
c->global_template->SetClassName(V8JS_SYM("V8Js")); c->global_template->SetClassName(V8JS_SYM("V8Js"));
/* Register builtin methods */ /* Register builtin methods */
php_v8js_register_methods(c->global_template->InstanceTemplate(), c); php_v8js_register_methods(c->global_template->InstanceTemplate(), c);
/* Create context */ /* Create context */
c->context = v8::Persistent<v8::Context>::New(v8::Context::New(c->isolate, &extension_conf, c->global_template->InstanceTemplate())); c->context = v8::Persistent<v8::Context>::New(c->isolate, v8::Context::New(c->isolate, &extension_conf, c->global_template->InstanceTemplate()));
c->context->SetAlignedPointerInEmbedderData(1, c); c->context->SetAlignedPointerInEmbedderData(1, c);
if (exts) { if (exts) {
@ -632,7 +632,7 @@ static PHP_METHOD(V8Js, __construct)
} }
/* Set name for the PHP JS object */ /* Set name for the PHP JS object */
c->object_name = v8::Persistent<v8::String>::New((object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP")); c->object_name = v8::Persistent<v8::String>::New(c->isolate, (object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP"));
/* Add the PHP object into global object */ /* Add the PHP object into global object */
V8JS_GLOBAL->Set(c->object_name, php_obj_t->InstanceTemplate()->NewInstance(), v8::ReadOnly); V8JS_GLOBAL->Set(c->object_name, php_obj_t->InstanceTemplate()->NewInstance(), v8::ReadOnly);

View File

@ -34,24 +34,24 @@ extern "C" {
#include <v8.h> #include <v8.h>
/* Callback for PHP methods and functions */ /* Callback for PHP methods and functions */
static v8::Handle<v8::Value> php_v8js_php_callback(const v8::Arguments &args) /* {{{ */ static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{ {
v8::Handle<v8::Value> return_value; v8::Handle<v8::Value> return_value;
zval *value = reinterpret_cast<zval *>(args.This()->GetAlignedPointerFromInternalField(0)); zval *value = reinterpret_cast<zval *>(info.This()->GetAlignedPointerFromInternalField(0));
v8::Isolate *isolate = reinterpret_cast<v8::Isolate *>(args.This()->GetAlignedPointerFromInternalField(1)); v8::Isolate *isolate = reinterpret_cast<v8::Isolate *>(info.This()->GetAlignedPointerFromInternalField(1));
zend_function *method_ptr; zend_function *method_ptr;
zend_fcall_info fci; zend_fcall_info fci;
zend_fcall_info_cache fcc; zend_fcall_info_cache fcc;
zval fname, *retval_ptr = NULL, **argv = NULL; zval fname, *retval_ptr = NULL, **argv = NULL;
TSRMLS_FETCH(); TSRMLS_FETCH();
zend_class_entry *ce = Z_OBJCE_P(value); zend_class_entry *ce = Z_OBJCE_P(value);
zend_uint argc = args.Length(), min_num_args = 0, max_num_args = 0; zend_uint argc = info.Length(), min_num_args = 0, max_num_args = 0;
char *error; char *error;
int error_len, i, flags = V8JS_FLAG_NONE; int error_len, i, flags = V8JS_FLAG_NONE;
/* Set method_ptr from v8::External or fetch the closure invoker */ /* Set method_ptr from v8::External or fetch the closure invoker */
if (!args.Data().IsEmpty() && args.Data()->IsExternal()) { if (!info.Data().IsEmpty() && info.Data()->IsExternal()) {
method_ptr = static_cast<zend_function *>(v8::External::Cast(*args.Data())->Value()); method_ptr = static_cast<zend_function *>(v8::External::Cast(*info.Data())->Value());
} else { } else {
method_ptr = zend_get_closure_invoke_method(value TSRMLS_CC); method_ptr = zend_get_closure_invoke_method(value TSRMLS_CC);
} }
@ -90,7 +90,8 @@ static v8::Handle<v8::Value> php_v8js_php_callback(const v8::Arguments &args) /*
efree(method_ptr); efree(method_ptr);
} }
efree(error); efree(error);
return return_value; info.GetReturnValue().Set(return_value);
return;
} }
/* Convert parameters passed from V8 */ /* Convert parameters passed from V8 */
@ -100,7 +101,7 @@ static v8::Handle<v8::Value> php_v8js_php_callback(const v8::Arguments &args) /*
argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0); argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
MAKE_STD_ZVAL(argv[i]); MAKE_STD_ZVAL(argv[i]);
if (v8js_to_zval(args[i], argv[i], flags, isolate TSRMLS_CC) == FAILURE) { if (v8js_to_zval(info[i], argv[i], flags, isolate TSRMLS_CC) == FAILURE) {
fci.param_count++; fci.param_count++;
error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name); error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name);
return_value = V8JS_THROW(Error, error, error_len); return_value = V8JS_THROW(Error, error, error_len);
@ -141,7 +142,7 @@ failure:
return_value = V8JS_NULL; return_value = V8JS_NULL;
} }
return return_value; info.GetReturnValue().Set(return_value);
} }
/* }}} */ /* }}} */
@ -167,24 +168,24 @@ static int _php_v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
} }
/* }}} */ /* }}} */
static v8::Handle<v8::Value> php_v8js_property_caller(const v8::Arguments &args) /* {{{ */ static void php_v8js_property_caller(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
{ {
v8::Local<v8::Object> self = args.Holder(); v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::String> cname = args.Callee()->GetName()->ToString(); v8::Local<v8::String> cname = info.Callee()->GetName()->ToString();
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
v8::Local<v8::String> cb_func = v8::Local<v8::String>::Cast(args.Data()); v8::Local<v8::String> cb_func = v8::Local<v8::String>::Cast(info.Data());
value = self->GetHiddenValue(cb_func); value = self->GetHiddenValue(cb_func);
if (!value.IsEmpty() && value->IsFunction()) if (!value.IsEmpty() && value->IsFunction())
{ {
int argc = args.Length(), i = 0; int argc = info.Length(), i = 0;
v8::Local<v8::Value> *argv = new v8::Local<v8::Value>[argc]; v8::Local<v8::Value> *argv = new v8::Local<v8::Value>[argc];
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(value); v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(value);
if (cb_func->Equals(V8JS_SYM(ZEND_INVOKE_FUNC_NAME))) { if (cb_func->Equals(V8JS_SYM(ZEND_INVOKE_FUNC_NAME))) {
for (; i < argc; ++i) { for (; i < argc; ++i) {
argv[i] = args[i]; argv[i] = info[i];
} }
value = cb->Call(self, argc, argv); value = cb->Call(self, argc, argv);
} }
@ -192,25 +193,28 @@ static v8::Handle<v8::Value> php_v8js_property_caller(const v8::Arguments &args)
{ {
v8::Local<v8::Array> argsarr = v8::Array::New(argc); v8::Local<v8::Array> argsarr = v8::Array::New(argc);
for (; i < argc; ++i) { for (; i < argc; ++i) {
argsarr->Set(i, args[i]); argsarr->Set(i, info[i]);
} }
v8::Local<v8::Value> argsv[2] = { cname, argsarr }; v8::Local<v8::Value> argsv[2] = { cname, argsarr };
value = cb->Call(self, 2, argsv); value = cb->Call(self, 2, argsv);
} }
} }
if (args.IsConstructCall()) { if (info.IsConstructCall()) {
if (!value.IsEmpty() && !value->IsNull()) { if (!value.IsEmpty() && !value->IsNull()) {
return value; info.GetReturnValue().Set(value);
return;
} }
return self;
info.GetReturnValue().Set(self);
return;
} }
return value; info.GetReturnValue().Set(value);
} }
/* }}} */ /* }}} */
static v8::Handle<v8::Value> php_v8js_property_getter(v8::Local<v8::String> property, const v8::AccessorInfo &info) /* {{{ */ static void php_v8js_property_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
{ {
v8::Local<v8::Object> self = info.Holder(); v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
@ -220,7 +224,8 @@ static v8::Handle<v8::Value> php_v8js_property_getter(v8::Local<v8::String> prop
value = self->GetRealNamedProperty(property); value = self->GetRealNamedProperty(property);
if (!value.IsEmpty()) { if (!value.IsEmpty()) {
return value; info.GetReturnValue().Set(value);
return;
} }
/* If __get() is set for PHP object, call it */ /* If __get() is set for PHP object, call it */
@ -236,21 +241,23 @@ static v8::Handle<v8::Value> php_v8js_property_getter(v8::Local<v8::String> prop
v8::Local<v8::FunctionTemplate> cb_t = v8::FunctionTemplate::New(php_v8js_property_caller, V8JS_SYM(ZEND_CALL_FUNC_NAME)); v8::Local<v8::FunctionTemplate> cb_t = v8::FunctionTemplate::New(php_v8js_property_caller, V8JS_SYM(ZEND_CALL_FUNC_NAME));
cb = cb_t->GetFunction(); cb = cb_t->GetFunction();
cb->SetName(property); cb->SetName(property);
return cb; info.GetReturnValue().Set(cb);
return;
} }
return value; info.GetReturnValue().Set(value);
} }
/* }}} */ /* }}} */
static v8::Handle<v8::Integer> php_v8js_property_query(v8::Local<v8::String> property, const v8::AccessorInfo &info) /* {{{ */ static void php_v8js_property_query(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
{ {
v8::Local<v8::Object> self = info.Holder(); v8::Local<v8::Object> self = info.Holder();
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
/* Return early if property is set in JS object */ /* Return early if property is set in JS object */
if (self->HasRealNamedProperty(property)) { if (self->HasRealNamedProperty(property)) {
return V8JS_INT(v8::ReadOnly); info.GetReturnValue().Set(V8JS_INT(v8::ReadOnly));
return;
} }
value = self->GetHiddenValue(V8JS_SYM(ZEND_ISSET_FUNC_NAME)); value = self->GetHiddenValue(V8JS_SYM(ZEND_ISSET_FUNC_NAME));
@ -260,7 +267,7 @@ static v8::Handle<v8::Integer> php_v8js_property_query(v8::Local<v8::String> pro
value = cb->Call(self, 1, argv); value = cb->Call(self, 1, argv);
} }
return (!value.IsEmpty() && value->IsTrue()) ? V8JS_INT(v8::ReadOnly) : v8::Local<v8::Integer>(); info.GetReturnValue().Set((!value.IsEmpty() && value->IsTrue()) ? V8JS_INT(v8::ReadOnly) : v8::Local<v8::Integer>());
} }
/* }}} */ /* }}} */

View File

@ -36,15 +36,13 @@ void php_v8js_commonjs_normalise_identifier(char *base, char *identifier, char *
V8JS_METHOD(exit) /* {{{ */ V8JS_METHOD(exit) /* {{{ */
{ {
v8::V8::TerminateExecution(); v8::V8::TerminateExecution();
return v8::Undefined();
} }
/* }}} */ /* }}} */
/* global.sleep - sleep for passed seconds */ /* global.sleep - sleep for passed seconds */
V8JS_METHOD(sleep) /* {{{ */ V8JS_METHOD(sleep) /* {{{ */
{ {
php_sleep(args[0]->Int32Value()); php_sleep(info[0]->Int32Value());
return v8::Undefined();
} }
/* }}} */ /* }}} */
@ -54,12 +52,12 @@ V8JS_METHOD(print) /* {{{ */
int ret = 0; int ret = 0;
TSRMLS_FETCH(); TSRMLS_FETCH();
for (int i = 0; i < args.Length(); i++) { for (int i = 0; i < info.Length(); i++) {
v8::String::Utf8Value str(args[i]); v8::String::Utf8Value str(info[i]);
const char *cstr = ToCString(str); const char *cstr = ToCString(str);
ret = PHPWRITE(cstr, strlen(cstr)); ret = PHPWRITE(cstr, strlen(cstr));
} }
return V8JS_INT(ret); info.GetReturnValue().Set(V8JS_INT(ret));
} }
/* }}} */ /* }}} */
@ -161,26 +159,27 @@ V8JS_METHOD(var_dump) /* {{{ */
int i; int i;
TSRMLS_FETCH(); TSRMLS_FETCH();
for (int i = 0; i < args.Length(); i++) { for (int i = 0; i < info.Length(); i++) {
_php_v8js_dumper(args[i], 1 TSRMLS_CC); _php_v8js_dumper(info[i], 1 TSRMLS_CC);
} }
return V8JS_NULL; info.GetReturnValue().Set(V8JS_NULL);
} }
/* }}} */ /* }}} */
V8JS_METHOD(require) V8JS_METHOD(require)
{ {
// Get the extension context // Get the extension context
v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(args.Data()); v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(info.Data());
php_v8js_ctx *c = static_cast<php_v8js_ctx*>(data->Value()); php_v8js_ctx *c = static_cast<php_v8js_ctx*>(data->Value());
// Check that we have a module loader // Check that we have a module loader
if (c->module_loader == NULL) { if (c->module_loader == NULL) {
return v8::ThrowException(v8::String::New("No module loader")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("No module loader")));
return;
} }
v8::String::Utf8Value module_id_v8(args[0]); v8::String::Utf8Value module_id_v8(info[0]);
// Make sure to duplicate the module name string so it doesn't get freed by the V8 garbage collector // Make sure to duplicate the module name string so it doesn't get freed by the V8 garbage collector
char *module_id = estrdup(ToCString(module_id_v8)); char *module_id = estrdup(ToCString(module_id_v8));
@ -207,7 +206,8 @@ V8JS_METHOD(require)
if (!strcmp(*it, normalised_module_id)) { if (!strcmp(*it, normalised_module_id)) {
efree(normalised_path); efree(normalised_path);
return v8::ThrowException(v8::String::New("Module cyclic dependency")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module cyclic dependency")));
return;
} }
} }
@ -215,7 +215,8 @@ V8JS_METHOD(require)
if (V8JSG(modules_loaded).count(normalised_module_id) > 0) { if (V8JSG(modules_loaded).count(normalised_module_id) > 0) {
efree(normalised_path); efree(normalised_path);
return V8JSG(modules_loaded)[normalised_module_id]; info.GetReturnValue().Set(V8JSG(modules_loaded)[normalised_module_id]);
return;
} }
// Callback to PHP to load the module code // Callback to PHP to load the module code
@ -230,7 +231,8 @@ V8JS_METHOD(require)
if (FAILURE == call_user_function(EG(function_table), NULL, c->module_loader, &module_code, 1, params TSRMLS_CC)) { if (FAILURE == call_user_function(EG(function_table), NULL, c->module_loader, &module_code, 1, params TSRMLS_CC)) {
efree(normalised_path); efree(normalised_path);
return v8::ThrowException(v8::String::New("Module loader callback failed")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module loader callback failed")));
return;
} }
// Check if an exception was thrown // Check if an exception was thrown
@ -239,7 +241,8 @@ V8JS_METHOD(require)
// Clear the PHP exception and throw it in V8 instead // Clear the PHP exception and throw it in V8 instead
zend_clear_exception(TSRMLS_CC); zend_clear_exception(TSRMLS_CC);
return v8::ThrowException(v8::String::New("Module loader callback exception")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module loader callback exception")));
return;
} }
// Convert the return value to string // Convert the return value to string
@ -251,7 +254,8 @@ V8JS_METHOD(require)
if (!strlen(Z_STRVAL(module_code))) { if (!strlen(Z_STRVAL(module_code))) {
efree(normalised_path); efree(normalised_path);
return v8::ThrowException(v8::String::New("Module loader callback did not return code")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module loader callback did not return code")));
return;
} }
// Create a template for the global object and set the built-in global functions // Create a template for the global object and set the built-in global functions
@ -272,7 +276,7 @@ V8JS_METHOD(require)
global->Set(v8::String::New("module"), module); global->Set(v8::String::New("module"), module);
// Each module gets its own context so different modules do not affect each other // Each module gets its own context so different modules do not affect each other
v8::Persistent<v8::Context> context = v8::Persistent<v8::Context>::New(v8::Context::New(c->isolate, NULL, global)); v8::Persistent<v8::Context> context = v8::Persistent<v8::Context>::New(c->isolate, v8::Context::New(c->isolate, NULL, global));
// Catch JS exceptions // Catch JS exceptions
v8::TryCatch try_catch; v8::TryCatch try_catch;
@ -295,7 +299,8 @@ V8JS_METHOD(require)
// The script will be empty if there are compile errors // The script will be empty if there are compile errors
if (script.IsEmpty()) { if (script.IsEmpty()) {
efree(normalised_path); efree(normalised_path);
return v8::ThrowException(v8::String::New("Module script compile failed")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module script compile failed")));
return;
} }
// Add this module and path to the stack // Add this module and path to the stack
@ -314,13 +319,15 @@ V8JS_METHOD(require)
// Script possibly terminated, return immediately // Script possibly terminated, return immediately
if (!try_catch.CanContinue()) { if (!try_catch.CanContinue()) {
return v8::ThrowException(v8::String::New("Module script compile failed")); info.GetReturnValue().Set(v8::ThrowException(v8::String::New("Module script compile failed")));
return;
} }
// Handle runtime JS exceptions // Handle runtime JS exceptions
if (try_catch.HasCaught()) { if (try_catch.HasCaught()) {
// Rethrow the exception back to JS // Rethrow the exception back to JS
return try_catch.ReThrow(); info.GetReturnValue().Set(try_catch.ReThrow());
return;
} }
// Cache the module so it doesn't need to be compiled and run again // Cache the module so it doesn't need to be compiled and run again
@ -333,7 +340,7 @@ V8JS_METHOD(require)
V8JSG(modules_loaded)[normalised_module_id] = handle_scope.Close(exports); V8JSG(modules_loaded)[normalised_module_id] = handle_scope.Close(exports);
} }
return V8JSG(modules_loaded)[normalised_module_id]; info.GetReturnValue().Set(V8JSG(modules_loaded)[normalised_module_id]);
} }
void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global, php_v8js_ctx *c) /* {{{ */ void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global, php_v8js_ctx *c) /* {{{ */

View File

@ -38,7 +38,7 @@ struct php_v8js_accessor_ctx
v8::Isolate *isolate; v8::Isolate *isolate;
}; };
static v8::Handle<v8::Value> php_v8js_fetch_php_variable(v8::Local<v8::String> name, const v8::AccessorInfo &info) /* {{{ */ static void php_v8js_fetch_php_variable(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
{ {
v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(info.Data()); v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(info.Data());
php_v8js_accessor_ctx *ctx = static_cast<php_v8js_accessor_ctx *>(data->Value()); php_v8js_accessor_ctx *ctx = static_cast<php_v8js_accessor_ctx *>(data->Value());
@ -49,10 +49,9 @@ static v8::Handle<v8::Value> php_v8js_fetch_php_variable(v8::Local<v8::String> n
zend_is_auto_global(ctx->variable_name_string, ctx->variable_name_string_len TSRMLS_CC); zend_is_auto_global(ctx->variable_name_string, ctx->variable_name_string_len TSRMLS_CC);
if (zend_hash_find(&EG(symbol_table), ctx->variable_name_string, ctx->variable_name_string_len + 1, (void **) &variable) == SUCCESS) { if (zend_hash_find(&EG(symbol_table), ctx->variable_name_string, ctx->variable_name_string_len + 1, (void **) &variable) == SUCCESS) {
return zval_to_v8js(*variable, ctx->isolate TSRMLS_CC); info.GetReturnValue().Set(zval_to_v8js(*variable, ctx->isolate TSRMLS_CC));
return;
} }
return v8::Undefined();
} }
/* }}} */ /* }}} */