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

Commit a3b349e

Browse files
authored
Merge pull request #26 from pinepain/fix-template-set
Fix Template::Set()
2 parents 545925c + 917e072 commit a3b349e

39 files changed

+511
-802
lines changed

Diff for: config.m4

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ if test "$PHP_V8" != "no"; then
175175
src/php_v8_data.cc \
176176
src/php_v8_value.cc \
177177
src/php_v8_primitive.cc \
178+
src/php_v8_undefined.cc \
178179
src/php_v8_null.cc \
179180
src/php_v8_boolean.cc \
180181
src/php_v8_name.cc \

Diff for: php_v8.h

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ ZEND_END_MODULE_GLOBALS(v8)
7171
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, NULL, allow_null)
7272
#endif
7373

74+
#if PHP_VERSION_ID < 70100
75+
#define zend_get_executed_scope() EG(scope)
76+
#endif
77+
7478

7579
/* Always refer to the globals in your function as PHP_V8_G(variable).
7680
You are encouraged to rename these macros something shorter, see

Diff for: src/php_v8_function.cc

+7-19
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,12 @@ bool php_v8_function_unpack_args(zval *arguments_zv, int arg_position, v8::Isola
4747

4848
char *exception_message;
4949

50-
#if PHP_VERSION_ID >= 70100
5150
zend_string *ce_name = zend_get_executed_scope()->name;
52-
#else
53-
zend_string *ce_name = EG(scope)->name;
54-
#endif
5551

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

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

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

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

137133
char *exception_message;
138134

139-
#if PHP_VERSION_ID >= 70100
140-
zend_string *ce_name = zend_get_executed_scope()->name;
141-
#else
142-
zend_string *ce_name = EG(scope)->name;
143-
#endif
135+
zend_string *ce_name = zend_get_executed_scope()->name;
144136

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

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

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

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

226218
char *exception_message;
227219

228-
#if PHP_VERSION_ID >= 70100
229220
zend_string *ce_name = zend_get_executed_scope()->name;
230-
#else
231-
zend_string *ce_name = EG(scope)->name;
232-
#endif
233221

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

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

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

Diff for: src/php_v8_name.cc

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ PHP_MINIT_FUNCTION(php_v8_name)
5858
zend_class_entry ce;
5959
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "NameValue", php_v8_name_methods);
6060
this_ce = zend_register_internal_class_ex(&ce, php_v8_primitive_class_entry);
61+
this_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
6162

6263
return SUCCESS;
6364
}

Diff for: src/php_v8_primitive.cc

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ PHP_MINIT_FUNCTION(php_v8_primitive)
3232
zend_class_entry ce;
3333
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "PrimitiveValue", php_v8_primitive_methods);
3434
this_ce = zend_register_internal_class_ex(&ce, php_v8_value_class_entry);
35+
this_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
3536

3637
return SUCCESS;
3738
}

Diff for: src/php_v8_template.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,19 @@ void php_v8_template_Set(v8::Isolate *isolate, v8::Local<T> local_template, N* p
140140

141141
PHP_V8_DATA_ISOLATES_CHECK(php_v8_template, php_v8_value_to_set);
142142

143-
local_template->Set(local_name, php_v8_value_get_local(php_v8_value_to_set), static_cast<v8::PropertyAttribute>(attributes));
143+
v8::Local<v8::Value> local_value = php_v8_value_get_local(php_v8_value_to_set);
144+
145+
if (local_value->IsObject()) {
146+
int arg_position = 3;
147+
zend_string *ce_name = zend_get_executed_scope()->name;
148+
149+
zend_throw_error(zend_ce_type_error,
150+
"Argument %d passed to %s::%s() must be an instance of \\V8\\PrimitiveValue or \\V8\\Template, instance of %s given",
151+
arg_position, ZSTR_VAL(ce_name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(php_v8_value_zv)->name));
152+
return;
153+
}
154+
155+
local_template->Set(local_name, local_value, static_cast<v8::PropertyAttribute>(attributes));
144156
} else if (instanceof_function(Z_OBJCE_P(php_v8_value_zv), php_v8_object_template_class_entry)) {
145157
// set object template
146158
PHP_V8_FETCH_OBJECT_TEMPLATE_WITH_CHECK(php_v8_value_zv, php_v8_object_template_to_set);

Diff for: src/php_v8_undefined.cc

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file is part of the pinepain/php-v8 PHP extension.
3+
*
4+
* Copyright (c) 2015-2017 Bogdan Padalko <[email protected]>
5+
*
6+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
7+
*
8+
* For the full copyright and license information, please view the
9+
* LICENSE file that was distributed with this source or visit
10+
* http://opensource.org/licenses/MIT
11+
*/
12+
13+
#ifdef HAVE_CONFIG_H
14+
#include "config.h"
15+
#endif
16+
17+
#include "php_v8_undefined.h"
18+
#include "php_v8_primitive.h"
19+
#include "php_v8_value.h"
20+
#include "php_v8.h"
21+
22+
zend_class_entry *php_v8_undefined_class_entry;
23+
#define this_ce php_v8_undefined_class_entry
24+
25+
26+
static PHP_METHOD(V8Undefined, __construct) {
27+
zval *php_v8_isolate_zv;
28+
29+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
30+
return;
31+
}
32+
33+
PHP_V8_VALUE_CONSTRUCT(getThis(), php_v8_isolate_zv, php_v8_isolate, php_v8_value);
34+
35+
php_v8_value->persistent->Reset(isolate, v8::Undefined(isolate));
36+
}
37+
38+
39+
ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_undefined___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
40+
ZEND_ARG_OBJ_INFO(0, isolate, V8\\Isolate, 0)
41+
ZEND_END_ARG_INFO()
42+
43+
44+
static const zend_function_entry php_v8_undefined_methods[] = {
45+
PHP_ME(V8Undefined, __construct, arginfo_v8_undefined___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
46+
PHP_FE_END
47+
};
48+
49+
50+
PHP_MINIT_FUNCTION(php_v8_undefined) {
51+
zend_class_entry ce;
52+
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "UndefinedValue", php_v8_undefined_methods);
53+
this_ce = zend_register_internal_class_ex(&ce, php_v8_primitive_class_entry);
54+
55+
return SUCCESS;
56+
}

Diff for: src/php_v8_undefined.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of the pinepain/php-v8 PHP extension.
3+
*
4+
* Copyright (c) 2015-2017 Bogdan Padalko <[email protected]>
5+
*
6+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
7+
*
8+
* For the full copyright and license information, please view the
9+
* LICENSE file that was distributed with this source or visit
10+
* http://opensource.org/licenses/MIT
11+
*/
12+
13+
#ifndef PHP_V8_UNDEFINED_H
14+
#define PHP_V8_UNDEFINED_H
15+
16+
extern "C" {
17+
#include "php.h"
18+
19+
#ifdef ZTS
20+
#include "TSRM.h"
21+
#endif
22+
}
23+
24+
extern zend_class_entry* php_v8_undefined_class_entry;
25+
26+
27+
PHP_MINIT_FUNCTION(php_v8_undefined);
28+
29+
#endif //PHP_V8_UNDEFINED_H

Diff for: src/php_v8_value.cc

+14-13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "php_v8_uint32.h"
3939
#include "php_v8_integer.h"
4040
#include "php_v8_number.h"
41+
#include "php_v8_undefined.h"
4142
/* end of type listing */
4243

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

233234
if (value->IsUndefined()) {
234-
return php_v8_value_class_entry;
235+
return php_v8_undefined_class_entry;
235236
}
236237

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

315316

316-
static PHP_METHOD (V8Value, __construct) {
317-
zval *php_v8_isolate_zv;
318-
319-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
320-
return;
321-
}
322-
323-
PHP_V8_VALUE_CONSTRUCT(getThis(), php_v8_isolate_zv, php_v8_isolate, php_v8_value);
324-
325-
php_v8_value->persistent->Reset(isolate, v8::Undefined(isolate));
326-
}
317+
//static PHP_METHOD (V8Value, __construct) {
318+
// zval *php_v8_isolate_zv;
319+
//
320+
// if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &php_v8_isolate_zv) == FAILURE) {
321+
// return;
322+
// }
323+
//
324+
// PHP_V8_THROW_EXCEPTION("V8\\Value::__construct() should not be called. Use specific values instead.")
325+
//}
327326

328327
static PHP_METHOD(V8Value, GetIsolate) {
329328
zval rv;
@@ -1062,7 +1061,8 @@ ZEND_END_ARG_INFO()
10621061

10631062

10641063
static const zend_function_entry php_v8_value_methods[] = {
1065-
PHP_ME(V8Value, __construct, arginfo_v8_value___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
1064+
// PHP_ME(V8Value, __construct, arginfo_v8_value___construct, ZEND_ACC_PRIVATE | ZEND_ACC_CTOR)
1065+
10661066
PHP_ME(V8Value, GetIsolate, arginfo_v8_value_GetIsolate, ZEND_ACC_PUBLIC)
10671067

10681068
PHP_ME(V8Value, IsUndefined, arginfo_v8_value_IsUndefined, ZEND_ACC_PUBLIC)
@@ -1146,6 +1146,7 @@ PHP_MINIT_FUNCTION (php_v8_value) {
11461146
INIT_NS_CLASS_ENTRY(ce, PHP_V8_NS, "Value", php_v8_value_methods);
11471147
this_ce = zend_register_internal_class_ex(&ce, php_v8_data_class_entry);
11481148
this_ce->create_object = php_v8_value_ctor;
1149+
this_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
11491150

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

Diff for: stubs/src/ArrayObject.php

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class ArrayObject extends ObjectValue
3030
*/
3131
public function __construct(Context $context, int $length = 0)
3232
{
33-
parent::__construct($context);
3433
}
3534

3635
/**

Diff for: stubs/src/BooleanObject.php

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class BooleanObject extends ObjectValue
2727
*/
2828
public function __construct(Context $context, bool $value)
2929
{
30-
parent::__construct($context);
3130
}
3231

3332
/**

Diff for: stubs/src/BooleanValue.php

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class BooleanValue extends PrimitiveValue
2727
*/
2828
public function __construct(Isolate $isolate, bool $value)
2929
{
30-
parent::__construct($isolate);
3130
}
3231

3332
/**

Diff for: stubs/src/Exception.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function TypeError(Context $context, StringValue $message): Value
6666
* @param Context $context
6767
* @param \V8\StringValue $message
6868
*
69-
* @return Value
69+
* @return Value | ObjectValue
7070
*/
7171
public static function Error(Context $context, StringValue $message): Value
7272
{

Diff for: stubs/src/NameValue.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* A superclass for symbols and strings.
2121
*/
22-
class NameValue extends PrimitiveValue
22+
abstract class NameValue extends PrimitiveValue
2323
{
2424
/**
2525
* Returns the identity hash for this object. The current implementation

Diff for: stubs/src/NullValue.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
namespace V8;
1717

1818

19-
class NullValue extends Value
19+
class NullValue extends PrimitiveValue
2020
{
21+
public function __construct(Isolate $isolate)
22+
{
23+
}
24+
2125
/**
2226
* @return null
2327
*/

Diff for: stubs/src/NumberValue.php

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class NumberValue extends PrimitiveValue
2626
*/
2727
public function __construct(Isolate $isolate, float $value)
2828
{
29-
parent::__construct($isolate);
3029
}
3130

3231
/**

Diff for: stubs/src/PrimitiveValue.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
/**
2020
* The superclass of primitive values. See ECMA-262 4.3.2.
2121
*/
22-
class PrimitiveValue extends Value
22+
abstract class PrimitiveValue extends Value
2323
{
2424
}

Diff for: stubs/src/StringValue.php

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class StringValue extends NameValue
2929
*/
3030
public function __construct(Isolate $isolate, $data = '')
3131
{
32-
parent::__construct($isolate);
3332
}
3433

3534
/**

Diff for: stubs/src/SymbolValue.php

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class SymbolValue extends NameValue
3131
*/
3232
public function __construct(Isolate $isolate, StringValue $name = null)
3333
{
34-
parent::__construct($isolate);
3534
}
3635

3736
/**

Diff for: stubs/src/Template.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public function GetIsolate(): Isolate
4242
/**
4343
* Adds a property to each instance created by this template.
4444
*
45-
* @param NameValue $name
46-
* @param \V8\Data $value
47-
* @param int $attributes One of \v8\PropertyAttribute constants
45+
* @param NameValue $name
46+
* @param PrimitiveValue|Template $value
47+
* @param int $attributes One of \v8\PropertyAttribute constants
48+
*
49+
* @return void
4850
*/
49-
public function Set(NameValue $name, Data $value, int $attributes = PropertyAttribute::None)
51+
public function Set(NameValue $name, /*Data*/ $value, int $attributes = PropertyAttribute::None)
5052
{
5153
}
5254

0 commit comments

Comments
 (0)