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

Commit c2ffd55

Browse files
committed
Add support for integer->strings for GetOwnPropertyNames and GetPropertyNames, closes #96
1 parent 6dfe11f commit c2ffd55

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/php_v8_object.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,17 @@ static PHP_METHOD(Object, getPropertyNames) {
614614
zend_long mode = static_cast<zend_long>(v8::KeyCollectionMode::kOwnOnly);
615615
zend_long property_filter = static_cast<zend_long>(v8::PropertyFilter::ALL_PROPERTIES);
616616
zend_long index_filter = static_cast<zend_long>(v8::IndexFilter::kIncludeIndices);
617+
zend_bool convert_to_strings = '\0';
617618

618-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|lll", &context_zv, &mode, &property_filter, &index_filter) == FAILURE) {
619+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|lllb",
620+
&context_zv, &mode, &property_filter, &index_filter, &convert_to_strings) == FAILURE) {
619621
return;
620622
}
621623

622624
mode = mode ? mode & PHP_V8_KEY_COLLECTION_MODE_FLAGS : mode;
623625
property_filter = property_filter ? property_filter & PHP_V8_PROPERTY_FILTER_FLAGS : property_filter;
624626
index_filter = index_filter ? index_filter & PHP_V8_INDEX_FILTER_FLAGS : index_filter;
627+
v8::KeyConversionMode key_conversion = convert_to_strings ? v8::KeyConversionMode::kConvertToString : v8::KeyConversionMode::kKeepNumbers;
625628

626629
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
627630
PHP_V8_CONTEXT_FETCH_WITH_CHECK(context_zv, php_v8_context);
@@ -639,7 +642,8 @@ static PHP_METHOD(Object, getPropertyNames) {
639642
v8::MaybeLocal<v8::Array> maybe_local_array = local_object->GetPropertyNames(context,
640643
static_cast<v8::KeyCollectionMode>(mode),
641644
static_cast<v8::PropertyFilter >(property_filter),
642-
static_cast<v8::IndexFilter>(index_filter));
645+
static_cast<v8::IndexFilter>(index_filter),
646+
key_conversion);
643647

644648
PHP_V8_MAYBE_CATCH(php_v8_context, try_catch);
645649
PHP_V8_THROW_EXCEPTION_WHEN_EMPTY(maybe_local_array, "Failed to get property names")
@@ -652,11 +656,13 @@ static PHP_METHOD(Object, getPropertyNames) {
652656
static PHP_METHOD(Object, getOwnPropertyNames) {
653657
zval *context_zv;
654658
zend_long filter = static_cast<zend_long>(v8::PropertyFilter::ALL_PROPERTIES);
659+
zend_bool convert_to_strings = '\0';
655660

656-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|l", &context_zv, &filter) == FAILURE) {
661+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o|lb", &context_zv, &filter, &convert_to_strings) == FAILURE) {
657662
return;
658663
}
659664
filter = filter ? filter & PHP_V8_PROPERTY_FILTER_FLAGS : filter;
665+
v8::KeyConversionMode key_conversion = convert_to_strings ? v8::KeyConversionMode::kConvertToString : v8::KeyConversionMode::kKeepNumbers;
660666

661667
PHP_V8_VALUE_FETCH_WITH_CHECK(getThis(), php_v8_value);
662668
PHP_V8_CONTEXT_FETCH_WITH_CHECK(context_zv, php_v8_context);
@@ -671,7 +677,7 @@ static PHP_METHOD(Object, getOwnPropertyNames) {
671677
PHP_V8_TRY_CATCH(isolate);
672678
PHP_V8_INIT_ISOLATE_LIMITS_ON_OBJECT_VALUE(php_v8_value);
673679

674-
v8::MaybeLocal<v8::Array> maybe_local_array = local_object->GetOwnPropertyNames(context, static_cast<v8::PropertyFilter >(filter));
680+
v8::MaybeLocal<v8::Array> maybe_local_array = local_object->GetOwnPropertyNames(context, static_cast<v8::PropertyFilter >(filter), key_conversion);
675681

676682
PHP_V8_MAYBE_CATCH(php_v8_context, try_catch);
677683
PHP_V8_THROW_EXCEPTION_WHEN_EMPTY(maybe_local_array, "Failed to get own property names")
@@ -1372,11 +1378,13 @@ PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_getPropertyNames, ZEND_RET
13721378
ZEND_ARG_TYPE_INFO(0, mode, IS_LONG, 0)
13731379
ZEND_ARG_TYPE_INFO(0, property_filter, IS_LONG, 0)
13741380
ZEND_ARG_TYPE_INFO(0, index_filter, IS_LONG, 0)
1381+
ZEND_ARG_TYPE_INFO(0, convert_to_strings, _IS_BOOL, 0)
13751382
ZEND_END_ARG_INFO()
13761383

13771384
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_getOwnPropertyNames, ZEND_RETURN_VALUE, 1, V8\\ArrayObject, 0)
13781385
ZEND_ARG_OBJ_INFO(0, context, V8\\Context, 0)
13791386
ZEND_ARG_TYPE_INFO(0, filter, IS_LONG, 0)
1387+
ZEND_ARG_TYPE_INFO(0, convert_to_strings, _IS_BOOL, 0)
13801388
ZEND_END_ARG_INFO()
13811389

13821390
PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_getPrototype, ZEND_RETURN_VALUE, 0, V8\\Value, 0)

stubs/src/ObjectValue.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,19 @@ public function setNativeDataProperty(
202202
* be enumerated by a for-in statement over this object.
203203
*
204204
* @param Context $context
205-
* @param int $mode One of KeyCollectionMode options
205+
* @param int $mode One of KeyCollectionMode options
206206
* @param int $property_filter One or multiple PropertyFilter options
207-
* @param int $index_filter One or multiple IndexFilter options
207+
* @param int $index_filter One or multiple IndexFilter options
208+
* @param bool $convert_to_strings Convert integer indices to strings
208209
*
209210
* @return ArrayObject
210211
*/
211212
public function getPropertyNames(
212213
Context $context,
213214
int $mode = KeyCollectionMode::kOwnOnly,
214215
int $property_filter = PropertyFilter::ALL_PROPERTIES,
215-
int $index_filter = IndexFilter::kIncludeIndices
216+
int $index_filter = IndexFilter::kIncludeIndices,
217+
bool $convert_to_strings = false
216218
): ArrayObject {
217219
}
218220

@@ -223,11 +225,15 @@ public function getPropertyNames(
223225
*
224226
* @param Context $context
225227
* @param int $filter One or multiple PropertyFilter options
228+
* @param bool $convert_to_strings Will convert integer indices to strings
226229
*
227230
* @return ArrayObject
228231
*/
229-
public function getOwnPropertyNames(Context $context, int $filter = PropertyFilter::ALL_PROPERTIES): ArrayObject
230-
{
232+
public function getOwnPropertyNames(
233+
Context $context,
234+
int $filter = PropertyFilter::ALL_PROPERTIES,
235+
bool $convert_to_strings = false
236+
): ArrayObject {
231237
}
232238

233239
/**

tests/001-verify_extension_entities.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ class V8\ObjectValue
698698
public function setAccessor(V8\Context $context, V8\NameValue $name, callable $getter, ?callable $setter, int $settings, int $attributes): bool
699699
public function setAccessorProperty(V8\NameValue $name, V8\FunctionObject $getter, V8\FunctionObject $setter, int $attributes, int $settings)
700700
public function setNativeDataProperty(V8\Context $context, V8\NameValue $name, callable $getter, ?callable $setter, int $attributes): bool
701-
public function getPropertyNames(V8\Context $context, int $mode, int $property_filter, int $index_filter): V8\ArrayObject
702-
public function getOwnPropertyNames(V8\Context $context, int $filter): V8\ArrayObject
701+
public function getPropertyNames(V8\Context $context, int $mode, int $property_filter, int $index_filter, bool $convert_to_strings): V8\ArrayObject
702+
public function getOwnPropertyNames(V8\Context $context, int $filter, bool $convert_to_strings): V8\ArrayObject
703703
public function getPrototype(): V8\Value
704704
public function setPrototype(V8\Context $context, V8\Value $prototype): bool
705705
public function findInstanceInPrototypeChain(V8\FunctionTemplate $tmpl): V8\ObjectValue

0 commit comments

Comments
 (0)