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

Commit 2d4d7da

Browse files
committed
Add ReturnValue::Get(), refactor retval internals, improve context checking
1 parent 9ece87f commit 2d4d7da

10 files changed

+430
-272
lines changed

src/php_v8_callback_info.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ static PHP_METHOD(V8CallbackInfo, GetReturnValue) {
190190
RETVAL_ZVAL(&php_v8_callback_info->php_v8_return_value->this_ptr, 1, 0);
191191
}
192192

193+
static PHP_METHOD(V8CallbackInfo, InContext) {
194+
if (zend_parse_parameters_none() == FAILURE) {
195+
return;
196+
}
197+
198+
PHP_V8_CALLBACK_INFO_FETCH_WITH_CHECK(getThis(), php_v8_callback_info);
199+
200+
RETURN_BOOL(PHP_V8_V8_CALLBACK_INFO_IN_CONTEXT(php_v8_callback_info));
201+
}
202+
193203

194204
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_callback_info_GetIsolate, ZEND_RETURN_VALUE, 0, IS_OBJECT, PHP_V8_NS "\\Isolate", 0)
195205
ZEND_END_ARG_INFO()
@@ -206,13 +216,17 @@ ZEND_END_ARG_INFO()
206216
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_callback_info_GetReturnValue, ZEND_RETURN_VALUE, 0, IS_OBJECT, PHP_V8_NS "\\ReturnValue", 0)
207217
ZEND_END_ARG_INFO()
208218

219+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_callback_info_InContext, ZEND_RETURN_VALUE, 0, _IS_BOOL, NULL, 0)
220+
ZEND_END_ARG_INFO()
221+
209222

210223
static const zend_function_entry php_v8_callback_info_methods[] = {
211224
PHP_ME(V8CallbackInfo, This, arginfo_v8_callback_info_This, ZEND_ACC_PUBLIC)
212225
PHP_ME(V8CallbackInfo, Holder, arginfo_v8_callback_info_Holder, ZEND_ACC_PUBLIC)
213226
PHP_ME(V8CallbackInfo, GetIsolate, arginfo_v8_callback_info_GetIsolate, ZEND_ACC_PUBLIC)
214227
PHP_ME(V8CallbackInfo, GetContext, arginfo_v8_callback_info_GetContext, ZEND_ACC_PUBLIC)
215228
PHP_ME(V8CallbackInfo, GetReturnValue, arginfo_v8_callback_info_GetReturnValue, ZEND_ACC_PUBLIC)
229+
PHP_ME(V8CallbackInfo, InContext, arginfo_v8_callback_info_InContext, ZEND_ACC_PUBLIC)
216230
PHP_FE_END
217231
};
218232

src/php_v8_callback_info.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ extern void php_v8_callback_info_invalidate(php_v8_callback_info_t *php_v8_callb
4949
PHP_V8_CALLBACK_INFO_FETCH_INTO(pzval, into); \
5050
PHP_V8_CHECK_EMPTY_CALLBACK_INFO_HANDLER(into);
5151

52+
#define PHP_V8_V8_CALLBACK_INFO_IN_CONTEXT(value) ((value)->php_v8_return_value != NULL && PHP_V8_RETURN_VALUE_IN_CONTEXT((value)->php_v8_return_value))
5253

53-
// TODO: suggest better naming
5454
#define PHP_V8_V8_CALLBACK_INFO_CHECK_IN_CONTEXT(value) \
55-
if ((value)->php_v8_return_value == NULL || PHP_V8_RETVAL_ACCEPTS_INVALID == (value)->php_v8_return_value->accepts) { \
55+
if (!PHP_V8_V8_CALLBACK_INFO_IN_CONTEXT(value)) { \
5656
PHP_V8_THROW_EXCEPTION("Attempt to use callback info object out of callback context"); \
5757
return; \
5858
}

src/php_v8_callbacks.cc

Lines changed: 32 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -258,130 +258,32 @@ void php_v8_bucket_gc(php_v8_callbacks_bucket_t *bucket, zval **gc_data, int * g
258258
*n = *gc_data_count;
259259
}
260260

261-
void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<void> retval, php_v8_return_value_t *return_value) {
262-
if (!return_value->type) {
263-
return;
264-
}
265-
266-
switch (return_value->type) {
267-
default:
268-
// should never get here, just in case new types will be added in future
269-
PHP_V8_THROW_EXCEPTION("Failed to set returned value: unsupported type");
270-
return;
271-
break;
272-
}
261+
static inline void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<void> *rv, php_v8_return_value_t *php_v8_return_value) {
262+
php_v8_return_value->accepts = PHP_V8_RETVAL_ACCEPTS_VOID;
263+
php_v8_return_value->rv_void = rv;
273264
}
274265

275-
void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Value> retval, php_v8_return_value_t *return_value) {
276-
if (!return_value->type) {
277-
return;
278-
}
279-
280-
switch (return_value->type) {
281-
case PHP_V8_RETVAL_UNDEFINED:
282-
retval.SetUndefined();
283-
break;
284-
case PHP_V8_RETVAL_NULL:
285-
retval.SetNull();
286-
break;
287-
case PHP_V8_RETVAL_EMPTY_STRING:
288-
retval.SetEmptyString();
289-
break;
290-
case PHP_V8_RETVAL_BOOL:
291-
retval.Set(return_value->value.set_bool);
292-
break;
293-
case PHP_V8_RETVAL_INT32:
294-
retval.Set(return_value->value.set_int32);
295-
break;
296-
case PHP_V8_RETVAL_UINT32:
297-
retval.Set(return_value->value.set_uint32);
298-
break;
299-
case PHP_V8_RETVAL_LONG:
300-
retval.Set(static_cast<double>(return_value->value.set_long));
301-
302-
break;
303-
case PHP_V8_RETVAL_DOUBLE:
304-
retval.Set(return_value->value.set_double);
305-
break;
306-
case PHP_V8_RETVAL_V8_VALUE:
307-
retval.Set(php_v8_value_get_value_local(retval.GetIsolate(),
308-
PHP_V8_VALUE_FETCH(&return_value->value.php_v8_value_zv)));
309-
break;
310-
default:
311-
// should never get here, just in case new types will be added in future
312-
313-
// TODO: maybe value exception?
314-
PHP_V8_THROW_EXCEPTION("Failed to set returned value: unsupported type");
315-
return;
316-
break;
317-
}
266+
static inline void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Value> *rv, php_v8_return_value_t *php_v8_return_value) {
267+
php_v8_return_value->accepts = PHP_V8_RETVAL_ACCEPTS_ANY;
268+
php_v8_return_value->rv_any = rv;
318269
}
319270

320-
void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Array> retval, php_v8_return_value_t *return_value) {
321-
if (!return_value->type) {
322-
return;
323-
}
324-
325-
switch (return_value->type) {
326-
case PHP_V8_RETVAL_V8_VALUE:
327-
retval.Set(php_v8_value_get_array_local(retval.GetIsolate(),
328-
PHP_V8_VALUE_FETCH(&return_value->value.php_v8_value_zv)));
329-
break;
330-
default:
331-
// should never get here, just in case new types will be added in future
332-
PHP_V8_THROW_EXCEPTION("Failed to set returned value: unsupported type");
333-
return;
334-
break;
335-
}
271+
static inline void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Integer> *rv, php_v8_return_value_t *php_v8_return_value) {
272+
php_v8_return_value->accepts = PHP_V8_RETVAL_ACCEPTS_INTEGER;
273+
php_v8_return_value->rv_integer = rv;
336274
}
337275

338-
void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Integer> retval, php_v8_return_value_t *return_value) {
339-
if (!return_value->type) {
340-
return;
341-
}
342-
343-
switch (return_value->type) {
344-
case PHP_V8_RETVAL_INT32:
345-
retval.Set(return_value->value.set_int32);
346-
break;
347-
case PHP_V8_RETVAL_UINT32:
348-
retval.Set(return_value->value.set_uint32);
349-
break;
350-
case PHP_V8_RETVAL_V8_VALUE:
351-
retval.Set(php_v8_value_get_integer_local(retval.GetIsolate(),
352-
PHP_V8_VALUE_FETCH(&return_value->value.php_v8_value_zv)));
353-
break;
354-
default:
355-
// should never get here, just in case new types will be added in future
356-
PHP_V8_THROW_EXCEPTION("Failed to set returned value: unsupported type");
357-
return;
358-
break;
359-
}
276+
static inline void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Boolean> *rv, php_v8_return_value_t *php_v8_return_value) {
277+
php_v8_return_value->accepts = PHP_V8_RETVAL_ACCEPTS_BOOLEAN;
278+
php_v8_return_value->rv_boolean = rv;
360279
}
361280

362-
void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Boolean> retval, php_v8_return_value_t *return_value) {
363-
if (!return_value->type) {
364-
return;
365-
}
366-
367-
switch (return_value->type) {
368-
case PHP_V8_RETVAL_BOOL:
369-
retval.Set(return_value->value.set_bool);
370-
break;
371-
case PHP_V8_RETVAL_V8_VALUE:
372-
retval.Set(php_v8_value_get_boolean_local(retval.GetIsolate(),
373-
PHP_V8_VALUE_FETCH(&return_value->value.php_v8_value_zv)));
374-
break;
375-
default:
376-
// should never get here, just in case new types will be added in future
377-
PHP_V8_THROW_EXCEPTION("Failed to set returned value: unsupported type");
378-
return;
379-
break;
380-
}
281+
static inline void php_v8_callback_set_retval_from_callback_info(v8::ReturnValue<v8::Array> *rv, php_v8_return_value_t *php_v8_return_value) {
282+
php_v8_return_value->accepts = PHP_V8_RETVAL_ACCEPTS_ARRAY;
283+
php_v8_return_value->rv_array = rv;
381284
}
382285

383286

384-
385287
void php_v8_callback_call_from_bucket_with_zargs(size_t index, v8::Local<v8::Value> data, zval *args, zval *retval) {
386288
php_v8_callbacks_bucket_t *bucket;
387289

@@ -416,8 +318,8 @@ void php_v8_callback_call_from_bucket_with_zargs(size_t index, v8::Local<v8::Val
416318
zend_fcall_info_args_clear(&fci, 1);
417319
}
418320

419-
template<class T>
420-
void php_v8_callback_call_from_bucket_with_zargs(size_t index, const T &info, zval *args) {
321+
template<class T, class M>
322+
void php_v8_callback_call_from_bucket_with_zargs(size_t index, const T &info, M rv, zval *args) {
421323
zval callback_info;
422324
php_v8_callback_info_t *php_v8_callback_info;
423325
// Wrap callback info
@@ -429,15 +331,14 @@ void php_v8_callback_call_from_bucket_with_zargs(size_t index, const T &info, zv
429331

430332
add_next_index_zval(args, &callback_info);
431333

432-
php_v8_callback_call_from_bucket_with_zargs(index, info.Data(), args, NULL);
334+
php_v8_callback_set_retval_from_callback_info(&rv, php_v8_callback_info->php_v8_return_value);
433335

434-
php_v8_callback_set_retval_from_callback_info(info.GetReturnValue(), php_v8_callback_info->php_v8_return_value);
336+
php_v8_callback_call_from_bucket_with_zargs(index, info.Data(), args, NULL);
435337

436338
php_v8_callback_info_invalidate(php_v8_callback_info);
437339
}
438340

439341

440-
441342
void php_v8_callback_function(const v8::FunctionCallbackInfo<v8::Value> &info) {
442343
PHP_V8_DECLARE_ISOLATE_LOCAL_ALIAS(info.GetIsolate());
443344

@@ -446,7 +347,7 @@ void php_v8_callback_function(const v8::FunctionCallbackInfo<v8::Value> &info) {
446347
/* Build the parameter array */
447348
array_init_size(&args, 1);
448349

449-
php_v8_callback_call_from_bucket_with_zargs(0, info, &args);
350+
php_v8_callback_call_from_bucket_with_zargs(0, info, info.GetReturnValue(), &args);
450351

451352
zval_ptr_dtor(&args);
452353
}
@@ -463,7 +364,7 @@ void php_v8_callback_accessor_name_getter(v8::Local<v8::Name> property, const v8
463364
php_v8_get_or_create_value(&property_name, property, isolate);
464365
add_index_zval(&args, 0, &property_name);
465366

466-
php_v8_callback_call_from_bucket_with_zargs(0, info, &args);
367+
php_v8_callback_call_from_bucket_with_zargs(0, info, info.GetReturnValue(), &args);
467368

468369
zval_ptr_dtor(&args);
469370
}
@@ -484,7 +385,7 @@ void php_v8_callback_accessor_name_setter(v8::Local<v8::Name> property, v8::Loca
484385
add_index_zval(&args, 0, &property_name);
485386
add_index_zval(&args, 1, &property_value);
486387

487-
php_v8_callback_call_from_bucket_with_zargs(1, info, &args);
388+
php_v8_callback_call_from_bucket_with_zargs(1, info, info.GetReturnValue(), &args);
488389

489390
zval_ptr_dtor(&args);
490391
}
@@ -502,7 +403,7 @@ void php_v8_callback_generic_named_property_getter(v8::Local<v8::Name> property,
502403
php_v8_get_or_create_value(&property_name, property, isolate);
503404
add_index_zval(&args, 0, &property_name);
504405

505-
php_v8_callback_call_from_bucket_with_zargs(0, info, &args);
406+
php_v8_callback_call_from_bucket_with_zargs(0, info, info.GetReturnValue(), &args);
506407

507408
zval_ptr_dtor(&args);
508409
}
@@ -523,7 +424,7 @@ void php_v8_callback_generic_named_property_setter(v8::Local<v8::Name> property,
523424
add_index_zval(&args, 0, &property_name);
524425
add_index_zval(&args, 1, &property_value);
525426

526-
php_v8_callback_call_from_bucket_with_zargs(1, info, &args);
427+
php_v8_callback_call_from_bucket_with_zargs(1, info, info.GetReturnValue(), &args);
527428

528429
zval_ptr_dtor(&args);
529430
}
@@ -540,7 +441,7 @@ void php_v8_callback_generic_named_property_query(v8::Local<v8::Name> property,
540441
php_v8_get_or_create_value(&property_name, property, isolate);
541442
add_index_zval(&args, 0, &property_name);
542443

543-
php_v8_callback_call_from_bucket_with_zargs(2, info, &args);
444+
php_v8_callback_call_from_bucket_with_zargs(2, info, info.GetReturnValue(), &args);
544445

545446
zval_ptr_dtor(&args);
546447
}
@@ -557,7 +458,7 @@ void php_v8_callback_generic_named_property_deleter(v8::Local<v8::Name> property
557458
php_v8_get_or_create_value(&property_name, property, isolate);
558459
add_index_zval(&args, 0, &property_name);
559460

560-
php_v8_callback_call_from_bucket_with_zargs(3, info, &args);
461+
php_v8_callback_call_from_bucket_with_zargs(3, info, info.GetReturnValue(), &args);
561462

562463
zval_ptr_dtor(&args);
563464
}
@@ -570,7 +471,7 @@ void php_v8_callback_generic_named_property_enumerator(const v8::PropertyCallbac
570471
/* Build the parameter array */
571472
array_init_size(&args, 1);
572473

573-
php_v8_callback_call_from_bucket_with_zargs(4, info, &args);
474+
php_v8_callback_call_from_bucket_with_zargs(4, info, info.GetReturnValue(), &args);
574475

575476
zval_ptr_dtor(&args);
576477
}
@@ -589,7 +490,7 @@ void php_v8_callback_indexed_property_getter(uint32_t index, const v8::PropertyC
589490
ZVAL_LONG(&property_name, index);
590491
add_index_zval(&args, 0, &property_name);
591492

592-
php_v8_callback_call_from_bucket_with_zargs(0, info, &args);
493+
php_v8_callback_call_from_bucket_with_zargs(0, info, info.GetReturnValue(), &args);
593494

594495
zval_ptr_dtor(&args);
595496
}
@@ -610,7 +511,7 @@ void php_v8_callback_indexed_property_setter(uint32_t index, v8::Local<v8::Value
610511
add_index_zval(&args, 0, &property_name);
611512
add_index_zval(&args, 1, &property_value);
612513

613-
php_v8_callback_call_from_bucket_with_zargs(1, info, &args);
514+
php_v8_callback_call_from_bucket_with_zargs(1, info, info.GetReturnValue(), &args);
614515

615516
zval_ptr_dtor(&args);
616517
}
@@ -627,7 +528,7 @@ void php_v8_callback_indexed_property_query(uint32_t index, const v8::PropertyCa
627528
ZVAL_LONG(&property_name, index);
628529
add_index_zval(&args, 0, &property_name);
629530

630-
php_v8_callback_call_from_bucket_with_zargs(2, info, &args);
531+
php_v8_callback_call_from_bucket_with_zargs(2, info, info.GetReturnValue(), &args);
631532

632533
zval_ptr_dtor(&args);
633534
}
@@ -644,7 +545,7 @@ void php_v8_callback_indexed_property_deleter(uint32_t index, const v8::Property
644545
ZVAL_LONG(&property_name, index);
645546
add_index_zval(&args, 0, &property_name);
646547

647-
php_v8_callback_call_from_bucket_with_zargs(3, info, &args);
548+
php_v8_callback_call_from_bucket_with_zargs(3, info, info.GetReturnValue(), &args);
648549

649550
zval_ptr_dtor(&args);
650551
}
@@ -657,7 +558,7 @@ void php_v8_callback_indexed_property_enumerator(const v8::PropertyCallbackInfo<
657558
/* Build the parameter array */
658559
array_init_size(&args, 1);
659560

660-
php_v8_callback_call_from_bucket_with_zargs(4, info, &args);
561+
php_v8_callback_call_from_bucket_with_zargs(4, info, info.GetReturnValue(), &args);
661562

662563
zval_ptr_dtor(&args);
663564
}

0 commit comments

Comments
 (0)