Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Fix Template::Set() #26

Merged
merged 3 commits into from
Apr 28, 2017
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
1 change: 1 addition & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ if test "$PHP_V8" != "no"; then
src/php_v8_data.cc \
src/php_v8_value.cc \
src/php_v8_primitive.cc \
src/php_v8_undefined.cc \
src/php_v8_null.cc \
src/php_v8_boolean.cc \
src/php_v8_name.cc \
Expand Down
4 changes: 4 additions & 0 deletions php_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ ZEND_END_MODULE_GLOBALS(v8)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, NULL, allow_null)
#endif

#if PHP_VERSION_ID < 70100
#define zend_get_executed_scope() EG(scope)
#endif


/* Always refer to the globals in your function as PHP_V8_G(variable).
You are encouraged to rename these macros something shorter, see
Expand Down
26 changes: 7 additions & 19 deletions src/php_v8_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ bool php_v8_function_unpack_args(zval *arguments_zv, int arg_position, v8::Isola

char *exception_message;

#if PHP_VERSION_ID >= 70100
zend_string *ce_name = zend_get_executed_scope()->name;
#else
zend_string *ce_name = EG(scope)->name;
#endif

ZEND_HASH_FOREACH_VAL(myht, pzval) {
if (Z_TYPE_P(pzval) != IS_OBJECT) {
zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() should be array of \\V8\\Value objects, %s given at %d offset",
"Argument %d passed to %s::%s() must be an array of \\V8\\Value objects, %s given at %d offset",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(),
zend_zval_type_name(pzval), i);

Expand All @@ -66,7 +62,7 @@ bool php_v8_function_unpack_args(zval *arguments_zv, int arg_position, v8::Isola

if (!instanceof_function(Z_OBJCE_P(pzval), php_v8_value_class_entry)) {
zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() should be array of \\V8\\Value objects, instance of %s given at %d offset",
"Argument %d passed to %s::%s() must be an array of \\V8\\Value objects, instance of %s given at %d offset",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(),
ZSTR_VAL(Z_OBJCE_P(pzval)->name), i);

Expand Down Expand Up @@ -136,16 +132,12 @@ bool php_v8_function_unpack_string_args(zval* arguments_zv, int arg_position, v8

char *exception_message;

#if PHP_VERSION_ID >= 70100
zend_string *ce_name = zend_get_executed_scope()->name;
#else
zend_string *ce_name = EG(scope)->name;
#endif
zend_string *ce_name = zend_get_executed_scope()->name;

ZEND_HASH_FOREACH_VAL(myht, pzval) {
if (Z_TYPE_P(pzval) != IS_OBJECT) {
zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() should be array of \\V8\\StringValue objects, %s given at %d offset",
"Argument %d passed to %s::%s() must be an array of \\V8\\StringValue objects, %s given at %d offset",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(),
zend_zval_type_name(pzval), i);

Expand All @@ -155,7 +147,7 @@ bool php_v8_function_unpack_string_args(zval* arguments_zv, int arg_position, v8

if (!instanceof_function(Z_OBJCE_P(pzval), php_v8_string_class_entry)) {
zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() should be array of \\V8\\StringValue, instance of %s given at %d offset",
"Argument %d passed to %s::%s() must be an array of \\V8\\StringValue, instance of %s given at %d offset",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(),
ZSTR_VAL(Z_OBJCE_P(pzval)->name), i);

Expand Down Expand Up @@ -225,16 +217,12 @@ bool php_v8_function_unpack_object_args(zval* arguments_zv, int arg_position, v8

char *exception_message;

#if PHP_VERSION_ID >= 70100
zend_string *ce_name = zend_get_executed_scope()->name;
#else
zend_string *ce_name = EG(scope)->name;
#endif

ZEND_HASH_FOREACH_VAL(myht, pzval) {
if (Z_TYPE_P(pzval) != IS_OBJECT) {
zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() should be array of \\V8\\ObjectValue objects, %s given at %d offset",
"Argument %d passed to %s::%s() must be an array of \\V8\\ObjectValue objects, %s given at %d offset",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(),
zend_zval_type_name(pzval), i);

Expand All @@ -244,7 +232,7 @@ bool php_v8_function_unpack_object_args(zval* arguments_zv, int arg_position, v8

if (!instanceof_function(Z_OBJCE_P(pzval), php_v8_object_class_entry)) {
zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() should be array of \\V8\\ObjectValue, instance of %s given at %d offset",
"Argument %d passed to %s::%s() must be an array of \\V8\\ObjectValue, instance of %s given at %d offset",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(),
ZSTR_VAL(Z_OBJCE_P(pzval)->name), i);

Expand Down
1 change: 1 addition & 0 deletions src/php_v8_name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ PHP_MINIT_FUNCTION(php_v8_name)
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "NameValue", php_v8_name_methods);
this_ce = zend_register_internal_class_ex(&ce, php_v8_primitive_class_entry);
this_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;

return SUCCESS;
}
1 change: 1 addition & 0 deletions src/php_v8_primitive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ PHP_MINIT_FUNCTION(php_v8_primitive)
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "PrimitiveValue", php_v8_primitive_methods);
this_ce = zend_register_internal_class_ex(&ce, php_v8_value_class_entry);
this_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;

return SUCCESS;
}
14 changes: 13 additions & 1 deletion src/php_v8_template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,19 @@ void php_v8_template_Set(v8::Isolate *isolate, v8::Local<T> local_template, N* p

PHP_V8_DATA_ISOLATES_CHECK(php_v8_template, php_v8_value_to_set);

local_template->Set(local_name, php_v8_value_get_local(php_v8_value_to_set), static_cast<v8::PropertyAttribute>(attributes));
v8::Local<v8::Value> local_value = php_v8_value_get_local(php_v8_value_to_set);

if (local_value->IsObject()) {
int arg_position = 3;
zend_string *ce_name = zend_get_executed_scope()->name;

zend_throw_error(zend_ce_type_error,
"Argument %d passed to %s::%s() must be an instance of \\V8\\PrimitiveValue or \\V8\\Template, instance of %s given",
arg_position, ZSTR_VAL(ce_name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(php_v8_value_zv)->name));
return;
}

local_template->Set(local_name, local_value, static_cast<v8::PropertyAttribute>(attributes));
} else if (instanceof_function(Z_OBJCE_P(php_v8_value_zv), php_v8_object_template_class_entry)) {
// set object template
PHP_V8_FETCH_OBJECT_TEMPLATE_WITH_CHECK(php_v8_value_zv, php_v8_object_template_to_set);
Expand Down
56 changes: 56 additions & 0 deletions src/php_v8_undefined.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of the pinepain/php-v8 PHP extension.
*
* Copyright (c) 2015-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php_v8_undefined.h"
#include "php_v8_primitive.h"
#include "php_v8_value.h"
#include "php_v8.h"

zend_class_entry *php_v8_undefined_class_entry;
#define this_ce php_v8_undefined_class_entry


static PHP_METHOD(V8Undefined, __construct) {
zval *php_v8_isolate_zv;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
return;
}

PHP_V8_VALUE_CONSTRUCT(getThis(), php_v8_isolate_zv, php_v8_isolate, php_v8_value);

php_v8_value->persistent->Reset(isolate, v8::Undefined(isolate));
}


ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_undefined___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0)
ZEND_END_ARG_INFO()


static const zend_function_entry php_v8_undefined_methods[] = {
PHP_ME(V8Undefined, __construct, arginfo_v8_undefined___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_FE_END
};


PHP_MINIT_FUNCTION(php_v8_undefined) {
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "UndefinedValue", php_v8_undefined_methods);
this_ce = zend_register_internal_class_ex(&ce, php_v8_primitive_class_entry);

return SUCCESS;
}
29 changes: 29 additions & 0 deletions src/php_v8_undefined.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This file is part of the pinepain/php-v8 PHP extension.
*
* Copyright (c) 2015-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/

#ifndef PHP_V8_UNDEFINED_H
#define PHP_V8_UNDEFINED_H

extern "C" {
#include "php.h"

#ifdef ZTS
#include "TSRM.h"
#endif
}

extern zend_class_entry* php_v8_undefined_class_entry;


PHP_MINIT_FUNCTION(php_v8_undefined);

#endif //PHP_V8_UNDEFINED_H
27 changes: 14 additions & 13 deletions src/php_v8_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "php_v8_uint32.h"
#include "php_v8_integer.h"
#include "php_v8_number.h"
#include "php_v8_undefined.h"
/* end of type listing */

#include "php_v8_data.h"
Expand Down Expand Up @@ -231,7 +232,7 @@ zend_class_entry *php_v8_get_class_entry_from_value(v8::Local<v8::Value> value)
// working with scalars

if (value->IsUndefined()) {
return php_v8_value_class_entry;
return php_v8_undefined_class_entry;
}

if (value->IsNull()) {
Expand Down Expand Up @@ -313,17 +314,15 @@ php_v8_value_t *php_v8_get_or_create_value(zval *return_value, v8::Local<v8::Val
}


static PHP_METHOD (V8Value, __construct) {
zval *php_v8_isolate_zv;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
return;
}

PHP_V8_VALUE_CONSTRUCT(getThis(), php_v8_isolate_zv, php_v8_isolate, php_v8_value);

php_v8_value->persistent->Reset(isolate, v8::Undefined(isolate));
}
//static PHP_METHOD (V8Value, __construct) {
// zval *php_v8_isolate_zv;
//
// if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
// return;
// }
//
// PHP_V8_THROW_EXCEPTION("V8\\Value::__construct() should not be called. Use specific values instead.")
//}

static PHP_METHOD(V8Value, GetIsolate) {
zval rv;
Expand Down Expand Up @@ -1062,7 +1061,8 @@ ZEND_END_ARG_INFO()


static const zend_function_entry php_v8_value_methods[] = {
PHP_ME(V8Value, __construct, arginfo_v8_value___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
// PHP_ME(V8Value, __construct, arginfo_v8_value___construct, ZEND_ACC_PRIVATE | ZEND_ACC_CTOR)

PHP_ME(V8Value, GetIsolate, arginfo_v8_value_GetIsolate, ZEND_ACC_PUBLIC)

PHP_ME(V8Value, IsUndefined, arginfo_v8_value_IsUndefined, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -1146,6 +1146,7 @@ PHP_MINIT_FUNCTION (php_v8_value) {
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "Value", php_v8_value_methods);
this_ce = zend_register_internal_class_ex(&ce, php_v8_data_class_entry);
this_ce->create_object = php_v8_value_ctor;
this_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;

zend_declare_property_null(this_ce, ZEND_STRL("isolate"), ZEND_ACC_PRIVATE);

Expand Down
1 change: 0 additions & 1 deletion stubs/src/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class ArrayObject extends ObjectValue
*/
public function __construct(Context $context, int $length = 0)
{
parent::__construct($context);
}

/**
Expand Down
1 change: 0 additions & 1 deletion stubs/src/BooleanObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class BooleanObject extends ObjectValue
*/
public function __construct(Context $context, bool $value)
{
parent::__construct($context);
}

/**
Expand Down
1 change: 0 additions & 1 deletion stubs/src/BooleanValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class BooleanValue extends PrimitiveValue
*/
public function __construct(Isolate $isolate, bool $value)
{
parent::__construct($isolate);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion stubs/src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function TypeError(Context $context, StringValue $message): Value
* @param Context $context
* @param \V8\StringValue $message
*
* @return Value
* @return Value | ObjectValue
*/
public static function Error(Context $context, StringValue $message): Value
{
Expand Down
2 changes: 1 addition & 1 deletion stubs/src/NameValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* A superclass for symbols and strings.
*/
class NameValue extends PrimitiveValue
abstract class NameValue extends PrimitiveValue
{
/**
* Returns the identity hash for this object. The current implementation
Expand Down
6 changes: 5 additions & 1 deletion stubs/src/NullValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
namespace V8;


class NullValue extends Value
class NullValue extends PrimitiveValue
{
public function __construct(Isolate $isolate)
{
}

/**
* @return null
*/
Expand Down
1 change: 0 additions & 1 deletion stubs/src/NumberValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class NumberValue extends PrimitiveValue
*/
public function __construct(Isolate $isolate, float $value)
{
parent::__construct($isolate);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion stubs/src/PrimitiveValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
/**
* The superclass of primitive values. See ECMA-262 4.3.2.
*/
class PrimitiveValue extends Value
abstract class PrimitiveValue extends Value
{
}
1 change: 0 additions & 1 deletion stubs/src/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class StringValue extends NameValue
*/
public function __construct(Isolate $isolate, $data = '')
{
parent::__construct($isolate);
}

/**
Expand Down
1 change: 0 additions & 1 deletion stubs/src/SymbolValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class SymbolValue extends NameValue
*/
public function __construct(Isolate $isolate, StringValue $name = null)
{
parent::__construct($isolate);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions stubs/src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public function GetIsolate(): Isolate
/**
* Adds a property to each instance created by this template.
*
* @param NameValue $name
* @param \V8\Data $value
* @param int $attributes One of \v8\PropertyAttribute constants
* @param NameValue $name
* @param PrimitiveValue|Template $value
* @param int $attributes One of \v8\PropertyAttribute constants
*
* @return void
*/
public function Set(NameValue $name, Data $value, int $attributes = PropertyAttribute::None)
public function Set(NameValue $name, /*Data*/ $value, int $attributes = PropertyAttribute::None)
{
}

Expand Down
Loading