|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Zend Engine | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 2.00 of the Zend license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.zend.com/license/2_00.txt. | |
| 11 | + | If you did not receive a copy of the Zend license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | [email protected] so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "zend.h" |
| 18 | +#include "zend_list.h" |
| 19 | +#include "zend_API.h" |
| 20 | +#include "zend_exceptions.h" |
| 21 | +#include "zend_context_managers_arginfo.h" |
| 22 | + |
| 23 | +ZEND_API zend_class_entry *zend_ce_context_manager = NULL; |
| 24 | +ZEND_API zend_class_entry *zend_ce_resource_context_manager = NULL; |
| 25 | + |
| 26 | +ZEND_MINIT_FUNCTION(context_managers) |
| 27 | +{ |
| 28 | + zend_ce_context_manager = register_class_ContextManager(); |
| 29 | + zend_ce_resource_context_manager = register_class_ResourceContextManager(zend_ce_context_manager); |
| 30 | + |
| 31 | + return SUCCESS; |
| 32 | +} |
| 33 | + |
| 34 | +void zend_resource_context_manager_init(zend_object *obj, zend_resource *res) |
| 35 | +{ |
| 36 | + zval zres; |
| 37 | + ZVAL_RES(&zres, res); |
| 38 | + |
| 39 | + // TODO: reuse zend_update_property_num_checked() or similar |
| 40 | + zend_update_property(obj->ce, obj, "resource", strlen("resource"), &zres); |
| 41 | +} |
| 42 | + |
| 43 | +ZEND_METHOD(ResourceContextManager, __construct) |
| 44 | +{ |
| 45 | + zval *zres; |
| 46 | + zend_object *this = Z_OBJ_P(getThis()); |
| 47 | + |
| 48 | + ZEND_PARSE_PARAMETERS_START(1, 1); |
| 49 | + Z_PARAM_RESOURCE(zres); |
| 50 | + ZEND_PARSE_PARAMETERS_END(); |
| 51 | + |
| 52 | + zend_resource_context_manager_init(this, Z_RES_P(zres)); |
| 53 | +} |
| 54 | + |
| 55 | +ZEND_METHOD(ResourceContextManager, enterContext) |
| 56 | +{ |
| 57 | + zend_object *this = Z_OBJ_P(getThis()); |
| 58 | + |
| 59 | + ZEND_PARSE_PARAMETERS_NONE(); |
| 60 | + |
| 61 | + zval rv; |
| 62 | + zval *zres = zend_read_property(this->ce, this, "resource", strlen("resource"), false, &rv); |
| 63 | + |
| 64 | + if (Z_TYPE_P(zres) != IS_RESOURCE) { |
| 65 | + zend_throw_error(NULL, "%s object is not initialized", ZSTR_VAL(this->ce->name)); |
| 66 | + RETURN_THROWS(); |
| 67 | + } |
| 68 | + |
| 69 | + RETURN_COPY_DEREF(zres); |
| 70 | +} |
| 71 | + |
| 72 | +ZEND_METHOD(ResourceContextManager, exitContext) |
| 73 | +{ |
| 74 | + zend_object *this = Z_OBJ_P(getThis()); |
| 75 | + zend_object *exception; |
| 76 | + |
| 77 | + ZEND_PARSE_PARAMETERS_START(0, 1); |
| 78 | + Z_PARAM_OPTIONAL |
| 79 | + Z_PARAM_OBJ_OF_CLASS_OR_NULL(exception, zend_ce_throwable); |
| 80 | + ZEND_PARSE_PARAMETERS_END(); |
| 81 | + |
| 82 | + zval rv; |
| 83 | + zval *zres = zend_read_property(this->ce, this, "resource", strlen("resource"), false, &rv); |
| 84 | + |
| 85 | + if (Z_TYPE_P(zres) != IS_RESOURCE) { |
| 86 | + zend_throw_error(NULL, "%s object is not initialized", ZSTR_VAL(this->ce->name)); |
| 87 | + RETURN_THROWS(); |
| 88 | + } |
| 89 | + |
| 90 | + zend_resource *res = Z_RES_P(zres); |
| 91 | + |
| 92 | + if (res->type == -1) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + zend_resource_dtor(res); |
| 97 | + |
| 98 | + /* Do not swallow exception */ |
| 99 | + RETURN_FALSE; |
| 100 | +} |
0 commit comments