|
| 1 | +/* |
| 2 | + * This file is part of the pinepain/php-v8 PHP extension. |
| 3 | + * |
| 4 | + * Copyright (c) 2015-2017 Bogdan Padalko <pinepain@gmail.com> |
| 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_cached_data.h" |
| 18 | +#include "php_v8_string.h" |
| 19 | +#include "php_v8.h" |
| 20 | + |
| 21 | + |
| 22 | +zend_class_entry * php_v8_cached_data_class_entry; |
| 23 | +#define this_ce php_v8_cached_data_class_entry |
| 24 | + |
| 25 | +static zend_object_handlers php_v8_cached_data_object_handlers; |
| 26 | + |
| 27 | +php_v8_cached_data_t * php_v8_cached_data_fetch_object(zend_object *obj) { |
| 28 | + return (php_v8_cached_data_t *)((char *)obj - XtOffsetOf(php_v8_cached_data_t, std)); |
| 29 | +} |
| 30 | + |
| 31 | +php_v8_cached_data_t * php_v8_create_cached_data(zval *return_value, const v8::ScriptCompiler::CachedData *cached_data) { |
| 32 | + |
| 33 | + object_init_ex(return_value, this_ce); |
| 34 | + PHP_V8_FETCH_CACHED_DATA_INTO(return_value, php_v8_cached_data); |
| 35 | + |
| 36 | + int length = cached_data->length; |
| 37 | + uint8_t* data = new uint8_t[length]; |
| 38 | + memcpy(data, cached_data->data, static_cast<size_t>(length)); |
| 39 | + |
| 40 | + php_v8_cached_data->cached_data = new v8::ScriptCompiler::CachedData(data, length, v8::ScriptCompiler::CachedData::BufferPolicy::BufferOwned); |
| 41 | + |
| 42 | + return php_v8_cached_data; |
| 43 | +} |
| 44 | + |
| 45 | +static void php_v8_cached_data_free(zend_object *object) |
| 46 | +{ |
| 47 | + php_v8_cached_data_t *php_v8_cached_data = php_v8_cached_data_fetch_object(object); |
| 48 | + |
| 49 | + if (php_v8_cached_data->cached_data) { |
| 50 | + delete php_v8_cached_data->cached_data; |
| 51 | + } |
| 52 | + |
| 53 | + zend_object_std_dtor(&php_v8_cached_data->std); |
| 54 | +} |
| 55 | + |
| 56 | +static zend_object * php_v8_cached_data_ctor(zend_class_entry *ce) |
| 57 | +{ |
| 58 | + php_v8_cached_data_t *php_v8_cached_data; |
| 59 | + |
| 60 | + php_v8_cached_data = (php_v8_cached_data_t *) ecalloc(1, sizeof(php_v8_cached_data_t) + zend_object_properties_size(ce)); |
| 61 | + |
| 62 | + zend_object_std_init(&php_v8_cached_data->std, ce); |
| 63 | + object_properties_init(&php_v8_cached_data->std, ce); |
| 64 | + |
| 65 | + php_v8_cached_data->std.handlers = &php_v8_cached_data_object_handlers; |
| 66 | + |
| 67 | + return &php_v8_cached_data->std; |
| 68 | +} |
| 69 | + |
| 70 | +static PHP_METHOD(V8CachedData, __construct) |
| 71 | +{ |
| 72 | + zend_string *string = NULL; |
| 73 | + |
| 74 | + if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &string) == FAILURE) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + PHP_V8_CHECK_CACHE_DATA_STRING_RANGE(string, "CachedData data string is too long"); |
| 79 | + |
| 80 | + PHP_V8_FETCH_CACHED_DATA_INTO(getThis(), php_v8_cached_data); |
| 81 | + |
| 82 | + int length = static_cast<int>(MAYBE_ZSTR_LEN(string)); |
| 83 | + uint8_t* data = new uint8_t[length]; |
| 84 | + memcpy(data, MAYBE_ZSTR_VAL(string), static_cast<size_t>(length)); |
| 85 | + |
| 86 | + php_v8_cached_data->cached_data = new v8::ScriptCompiler::CachedData(data, length, v8::ScriptCompiler::CachedData::BufferPolicy::BufferOwned); |
| 87 | +} |
| 88 | + |
| 89 | +static PHP_METHOD(V8CachedData, GetData) |
| 90 | +{ |
| 91 | + if (zend_parse_parameters_none() == FAILURE) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + PHP_V8_FETCH_CACHED_DATA_WITH_CHECK(getThis(), php_v8_cached_data); |
| 96 | + |
| 97 | + RETVAL_STRINGL(reinterpret_cast<const char*>(php_v8_cached_data->cached_data->data), php_v8_cached_data->cached_data->length); |
| 98 | +} |
| 99 | + |
| 100 | +static PHP_METHOD(V8CachedData, IsRejected) |
| 101 | +{ |
| 102 | + if (zend_parse_parameters_none() == FAILURE) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + PHP_V8_FETCH_CACHED_DATA_WITH_CHECK(getThis(), php_v8_cached_data); |
| 107 | + |
| 108 | + RETVAL_BOOL(php_v8_cached_data->cached_data->rejected); |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_v8_cached_data___construct, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1) |
| 113 | + ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0) |
| 114 | +ZEND_END_ARG_INFO() |
| 115 | + |
| 116 | +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_cached_data_GetData, ZEND_RETURN_VALUE, 0, IS_STRING, 0) |
| 117 | +ZEND_END_ARG_INFO() |
| 118 | + |
| 119 | +PHP_V8_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8_cached_data_IsRejected, ZEND_RETURN_VALUE, 0, _IS_BOOL, 0) |
| 120 | +ZEND_END_ARG_INFO() |
| 121 | + |
| 122 | + |
| 123 | +static const zend_function_entry php_v8_cached_data_methods[] = { |
| 124 | + PHP_ME(V8CachedData, __construct, arginfo_v8_cached_data___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 125 | + PHP_ME(V8CachedData, GetData, arginfo_v8_cached_data_GetData, ZEND_ACC_PUBLIC) |
| 126 | + PHP_ME(V8CachedData, IsRejected, arginfo_v8_cached_data_IsRejected, ZEND_ACC_PUBLIC) |
| 127 | + |
| 128 | + PHP_FE_END |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +PHP_MINIT_FUNCTION(php_v8_cached_data) |
| 133 | +{ |
| 134 | + zend_class_entry ce; |
| 135 | + |
| 136 | + INIT_NS_CLASS_ENTRY(ce, "V8\\ScriptCompiler", "CachedData", php_v8_cached_data_methods); |
| 137 | + this_ce = zend_register_internal_class(&ce); |
| 138 | + this_ce->create_object = php_v8_cached_data_ctor; |
| 139 | + |
| 140 | + memcpy(&php_v8_cached_data_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 141 | + |
| 142 | + php_v8_cached_data_object_handlers.offset = XtOffsetOf(php_v8_cached_data_t, std); |
| 143 | + php_v8_cached_data_object_handlers.free_obj = php_v8_cached_data_free; |
| 144 | + |
| 145 | + return SUCCESS; |
| 146 | +} |
0 commit comments