/* {{{ proto string json_encode(mixed data [, int options[, int depth]]) Returns the JSON representation of a value */ static PHP_FUNCTION(json_encode) { zval *parameter; php_json_encoder encoder; smart_str buf = {0}; zend_long options = 0; zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_ZVAL_DEREF(parameter) Z_PARAM_OPTIONAL Z_PARAM_LONG(options) Z_PARAM_LONG(depth) ZEND_PARSE_PARAMETERS_END(); php_json_encode_init(&encoder); encoder.max_depth = (int)depth; encoder.error_code = PHP_JSON_ERROR_NONE; php_json_encode_zval(&buf, parameter, (int)options, &encoder); JSON_G(error_code) = encoder.error_code; if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { smart_str_free(&buf); RETURN_FALSE; } smart_str_0(&buf); /* copy? */ if (buf.s) { RETURN_NEW_STR(buf.s); } RETURN_EMPTY_STRING(); }
/* {{{ bool SQLite::sqliteCreateFunction(string name, mixed callback [, int argcount, int flags]) Registers a UDF with the sqlite db handle */ static PHP_METHOD(SQLite, sqliteCreateFunction) { struct pdo_sqlite_func *func; zval *callback; char *func_name; size_t func_name_len; zend_long argc = -1; zend_long flags = 0; zend_string *cbname = NULL; pdo_dbh_t *dbh; pdo_sqlite_db_handle *H; int ret; ZEND_PARSE_PARAMETERS_START(2, 4) Z_PARAM_STRING(func_name, func_name_len) Z_PARAM_ZVAL_DEREF(callback) Z_PARAM_OPTIONAL Z_PARAM_LONG(argc) Z_PARAM_LONG(flags) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); dbh = Z_PDO_DBH_P(getThis()); PDO_CONSTRUCT_CHECK; if (!zend_is_callable(callback, 0, &cbname)) { php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname)); zend_string_release(cbname); RETURN_FALSE; } zend_string_release(cbname); H = (pdo_sqlite_db_handle *)dbh->driver_data; func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func)); ret = sqlite3_create_function(H->db, func_name, argc, flags | SQLITE_UTF8, func, php_sqlite3_func_callback, NULL, NULL); if (ret == SQLITE_OK) { func->funcname = estrdup(func_name); ZVAL_COPY(&func->func, callback); func->argc = argc; func->next = H->funcs; H->funcs = func; RETURN_TRUE; } efree(func); RETURN_FALSE; }
/* {{{ proto mixed json_decode(string json [, bool assoc [, long depth]]) Decodes the JSON representation into a PHP value */ static PHP_FUNCTION(json_decode) { char *str; size_t str_len; zend_bool assoc = 0; /* return JS objects as PHP objects by default */ zend_bool assoc_null = 1; zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; zend_long options = 0; ZEND_PARSE_PARAMETERS_START(1, 4) Z_PARAM_STRING(str, str_len) Z_PARAM_OPTIONAL Z_PARAM_BOOL_EX(assoc, assoc_null, 1, 0) Z_PARAM_LONG(depth) Z_PARAM_LONG(options) ZEND_PARSE_PARAMETERS_END(); if (!(options & PHP_JSON_THROW_ON_ERROR)) { JSON_G(error_code) = PHP_JSON_ERROR_NONE; } if (!str_len) { if (!(options & PHP_JSON_THROW_ON_ERROR)) { JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX; } else { zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX); } RETURN_NULL(); } if (depth <= 0) { php_error_docref(NULL, E_WARNING, "Depth must be greater than zero"); RETURN_NULL(); } if (depth > INT_MAX) { php_error_docref(NULL, E_WARNING, "Depth must be lower than %d", INT_MAX); RETURN_NULL(); } /* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */ if (!assoc_null) { if (assoc) { options |= PHP_JSON_OBJECT_AS_ARRAY; } else { options &= ~PHP_JSON_OBJECT_AS_ARRAY; } } php_json_decode_ex(return_value, str, str_len, options, depth); }
static PHP_METHOD(SQLite, sqliteCreateAggregate) { struct pdo_sqlite_func *func; zval *step_callback, *fini_callback; char *func_name; size_t func_name_len; zend_long argc = -1; pdo_dbh_t *dbh; pdo_sqlite_db_handle *H; int ret; ZEND_PARSE_PARAMETERS_START(3, 4) Z_PARAM_STRING(func_name, func_name_len) Z_PARAM_ZVAL(step_callback) Z_PARAM_ZVAL(fini_callback) Z_PARAM_OPTIONAL Z_PARAM_LONG(argc) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); dbh = Z_PDO_DBH_P(getThis()); PDO_CONSTRUCT_CHECK; if (!zend_is_callable(step_callback, 0, NULL)) { zend_string *cbname = zend_get_callable_name(step_callback); php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname)); zend_string_release_ex(cbname, 0); RETURN_FALSE; } if (!zend_is_callable(fini_callback, 0, NULL)) { zend_string *cbname = zend_get_callable_name(fini_callback); php_error_docref(NULL, E_WARNING, "function '%s' is not callable", ZSTR_VAL(cbname)); zend_string_release_ex(cbname, 0); RETURN_FALSE; } H = (pdo_sqlite_db_handle *)dbh->driver_data; func = (struct pdo_sqlite_func*)ecalloc(1, sizeof(*func)); ret = sqlite3_create_function(H->db, func_name, argc, SQLITE_UTF8, func, NULL, php_sqlite3_func_step_callback, php_sqlite3_func_final_callback); if (ret == SQLITE_OK) { func->funcname = estrdup(func_name); ZVAL_COPY(&func->step, step_callback); ZVAL_COPY(&func->fini, fini_callback); func->argc = argc; func->next = H->funcs; H->funcs = func; RETURN_TRUE; } efree(func); RETURN_FALSE; }
PHP_METHOD(swoole_atomic_long, __construct) { zend_long value = 0; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_LONG(value) ZEND_PARSE_PARAMETERS_END(); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &value) == FAILURE) { RETURN_FALSE; } #endif sw_atomic_long_t *atomic = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(sw_atomic_long_t)); if (atomic == NULL) { zend_throw_exception(swoole_exception_class_entry_ptr, "global memory allocation failure.", SW_ERROR_MALLOC_FAIL TSRMLS_CC); RETURN_FALSE; } *atomic = (sw_atomic_long_t) value; swoole_set_object(getThis(), (void*) atomic); RETURN_TRUE; }
/** * Watch the connectivity state of the channel until it changed * @param long The previous connectivity state of the channel * @param Timeval The deadline this function should wait until * @return bool If the connectivity state changes from last_state * before deadline */ PHP_METHOD(Channel, watchConnectivityState) { wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); zend_long last_state; zval *deadline_obj; /* "lO" == 1 long 1 object */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "lO", &last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "watchConnectivityState expects 1 long 1 timeval", 1); return; } #else ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(last_state) Z_PARAM_OBJECT_OF_CLASS(deadline_obj, grpc_ce_timeval) ZEND_PARSE_PARAMETERS_END(); #endif wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); grpc_channel_watch_connectivity_state( channel->wrapped, (grpc_connectivity_state)last_state, deadline->wrapped, completion_queue, NULL); grpc_event event = grpc_completion_queue_pluck( completion_queue, NULL, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); RETURN_BOOL(event.success); }
/** public static function ION\EventAbstract::setPriority(int $prio) */ CLASS_METHOD(ION_EventAbstract, setPriority) { ion_php_event * php_event = ION_THIS_OBJECT(ion_php_event); zend_long prio = 0; ION_PHP_EVENT_CHECK(php_event->event); ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(prio) ZEND_PARSE_PARAMETERS_END_EX(PION_ZPP_THROW); }
PHP_METHOD(swoole_atomic_long, cmpset) { zend_long cmp_value, set_value; sw_atomic_long_t *atomic = swoole_get_object(getThis()); #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(cmp_value) Z_PARAM_LONG(set_value) ZEND_PARSE_PARAMETERS_END(); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &cmp_value, &set_value) == FAILURE) { RETURN_FALSE; } #endif RETURN_BOOL(sw_atomic_cmp_set(atomic, (sw_atomic_long_t) cmp_value, (sw_atomic_long_t) set_value)); }
// extern void TF_SetAttrInt(TF_OperationDescription* desc, const char* attr_name, // int64_t value); static PHP_METHOD(TensorFlow_OperationDescription, setAttrInt) { zend_string *name; zend_long value; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(name) Z_PARAM_LONG(value) ZEND_PARSE_PARAMETERS_END(); // this t_tf_operation_description_object* intern = TF_OPERATION_DESCRIPTION_P_ZV(getThis()); t_tf_operation_description* node = intern->ptr; TF_SetAttrInt(node->src, name->val, value); }
PHP_METHOD(swoole_atomic_long, set) { sw_atomic_long_t *atomic = swoole_get_object(getThis()); zend_long set_value; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(set_value) ZEND_PARSE_PARAMETERS_END(); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &set_value) == FAILURE) { RETURN_FALSE; } #endif *atomic = (sw_atomic_long_t) set_value; }
PHP_METHOD(swoole_atomic, sub) { zend_long sub_value = 1; sw_atomic_t *atomic = swoole_get_object(getThis()); #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_LONG(sub_value) ZEND_PARSE_PARAMETERS_END(); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sub_value) == FAILURE) { RETURN_FALSE; } #endif RETURN_LONG(sw_atomic_sub_fetch(atomic, (uint32_t ) sub_value)); }
/** * Constructs a new instance of the Timeval class * @param long $usec The number of microseconds in the interval */ PHP_METHOD(Timeval, __construct) { wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); zend_long microseconds; /* "l" == 1 long */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", µseconds) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "Timeval expects a long", 1); return; } #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(microseconds) ZEND_PARSE_PARAMETERS_END(); #endif gpr_timespec time = gpr_time_from_micros(microseconds, GPR_TIMESPAN); memcpy(&timeval->wrapped, &time, sizeof(gpr_timespec)); }
// extern void TF_SetAttrType(TF_OperationDescription* desc, const char* attr_name, // TF_DataType value); static PHP_METHOD(TensorFlow_OperationDescription, setAttrType) { zend_string *name; zend_long dtype; ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(name) Z_PARAM_LONG(dtype) ZEND_PARSE_PARAMETERS_END(); if (!valid_dtype(dtype)) { zend_throw_exception(spl_ce_InvalidArgumentException, "dtype must be from 1 to 20", 0); return; } // this t_tf_operation_description_object* intern = TF_OPERATION_DESCRIPTION_P_ZV(getThis()); t_tf_operation_description* node = intern->ptr; TF_SetAttrType(node->src, name->val, dtype); }
PHP_METHOD(swoole_atomic, wakeup) { zend_long n = 1; sw_atomic_t *atomic = swoole_get_object(getThis()); #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_LONG(n) ZEND_PARSE_PARAMETERS_END(); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &n) == FAILURE) { RETURN_FALSE; } #endif #ifdef HAVE_FUTEX SW_CHECK_RETURN(swoole_futex_wakeup(atomic, (int ) n)); #else *atomic = 1; #endif }
static PHP_METHOD(swoole_coroutine_util, fread) { coro_check(TSRMLS_C); zval *handle; zend_long length = 0; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_RESOURCE(handle) Z_PARAM_OPTIONAL Z_PARAM_LONG(length) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &handle, &length) == FAILURE) { return; } #endif int async; int fd = swoole_convert_to_fd_ex(handle, &async TSRMLS_CC); if (fd < 0) { RETURN_FALSE; } if (async) { co_socket_read(fd, length, INTERNAL_FUNCTION_PARAM_PASSTHRU); return; } struct stat file_stat; if (fstat(fd, &file_stat) < 0) { SwooleG.error = errno; RETURN_FALSE; } off_t _seek = lseek(fd, 0, SEEK_CUR); if (_seek < 0) { SwooleG.error = errno; RETURN_FALSE; } if (length <= 0 || file_stat.st_size - _seek < length) { length = file_stat.st_size - _seek; } swAio_event ev; bzero(&ev, sizeof(swAio_event)); ev.nbytes = length + 1; ev.buf = emalloc(ev.nbytes); if (!ev.buf) { RETURN_FALSE; } php_context *context = emalloc(sizeof(php_context)); ((char *) ev.buf)[length] = 0; ev.flags = 0; ev.type = SW_AIO_READ; ev.object = context; ev.handler = swAio_handler_read; ev.callback = aio_onReadCompleted; ev.fd = fd; ev.offset = _seek; if (!SwooleAIO.init) { php_swoole_check_reactor(); swAio_init(); } swTrace("fd=%d, offset=%jd, length=%ld", fd, (intmax_t) ev.offset, ev.nbytes); int ret = swAio_dispatch(&ev); if (ret < 0) { efree(context); RETURN_FALSE; } context->state = SW_CORO_CONTEXT_RUNNING; coro_save(context); coro_yield(); }
static PHP_METHOD(swoole_coroutine_util, writeFile) { coro_check(TSRMLS_C); char *filename = NULL; size_t l_filename = 0; char *data = NULL; size_t l_data = 0; zend_long flags = 0; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STRING(filename, l_filename) Z_PARAM_STRING(data, l_data) Z_PARAM_OPTIONAL Z_PARAM_LONG(flags) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &filename, &l_filename, &data, &l_data, &flags) == FAILURE) { return; } #endif swAio_event ev; bzero(&ev, sizeof(swAio_event)); ev.nbytes = l_data; ev.buf = data; php_context *context = emalloc(sizeof(php_context)); ev.type = SW_AIO_WRITE_FILE; ev.object = context; ev.handler = swAio_handler_write_file; ev.callback = aio_onWriteFileCompleted; ev.req = estrndup(filename, l_filename); ev.flags = O_CREAT | O_WRONLY; if (flags & PHP_FILE_APPEND) { ev.flags |= O_APPEND; } else { ev.flags |= O_TRUNC; } if (!SwooleAIO.init) { php_swoole_check_reactor(); swAio_init(); } swTrace("writeFile(%s, %ld)", filename, ev.nbytes); int ret = swAio_dispatch(&ev); if (ret < 0) { efree(context); RETURN_FALSE; } context->state = SW_CORO_CONTEXT_RUNNING; coro_save(context); coro_yield(); }
static PHP_METHOD(swoole_coroutine_util, fwrite) { coro_check(TSRMLS_C); zval *handle; char *str; zend_size_t l_str; zend_long length = 0; #ifdef FAST_ZPP ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_RESOURCE(handle) Z_PARAM_STRING(str, l_str) Z_PARAM_OPTIONAL Z_PARAM_LONG(length) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); #else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &handle, &str, &l_str, &length) == FAILURE) { return; } #endif int async; int fd = swoole_convert_to_fd_ex(handle, &async TSRMLS_CC); if (fd < 0) { RETURN_FALSE; } if (async) { co_socket_write(fd, str, (length < 0 && length < l_str) ? length : l_str, INTERNAL_FUNCTION_PARAM_PASSTHRU); return; } off_t _seek = lseek(fd, 0, SEEK_CUR); if (_seek < 0) { SwooleG.error = errno; RETURN_FALSE; } if (length <= 0 || length > l_str) { length = l_str; } swAio_event ev; bzero(&ev, sizeof(swAio_event)); ev.nbytes = length; ev.buf = estrndup(str, length); if (!ev.buf) { RETURN_FALSE; } php_context *context = emalloc(sizeof(php_context)); ev.flags = 0; ev.type = SW_AIO_WRITE; ev.object = context; ev.handler = swAio_handler_write; ev.callback = aio_onWriteCompleted; ev.fd = fd; ev.offset = _seek; php_swoole_check_aio(); swTrace("fd=%d, offset=%jd, length=%ld", fd, (intmax_t) ev.offset, ev.nbytes); int ret = swAio_dispatch(&ev); if (ret < 0) { efree(context); RETURN_FALSE; } context->state = SW_CORO_CONTEXT_RUNNING; coro_save(context); coro_yield(); }
static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent) { char *host; size_t host_len; zend_long port = -1; zval *zerrno = NULL, *zerrstr = NULL; double timeout = (double)FG(default_socket_timeout); #ifndef PHP_WIN32 time_t conv; #else long conv; #endif struct timeval tv; char *hashkey = NULL; php_stream *stream = NULL; int err; char *hostname = NULL; size_t hostname_len; zend_string *errstr = NULL; RETVAL_FALSE; ZEND_PARSE_PARAMETERS_START(1, 5) Z_PARAM_STRING(host, host_len) Z_PARAM_OPTIONAL Z_PARAM_LONG(port) Z_PARAM_ZVAL(zerrno) Z_PARAM_ZVAL(zerrstr) Z_PARAM_DOUBLE(timeout) ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); if (persistent) { spprintf(&hashkey, 0, "pfsockopen__%s:" ZEND_LONG_FMT, host, port); } if (port > 0) { hostname_len = spprintf(&hostname, 0, "%s:" ZEND_LONG_FMT, host, port); } else { hostname_len = host_len; hostname = host; } /* prepare the timeout value for use */ #ifndef PHP_WIN32 conv = (time_t) (timeout * 1000000.0); tv.tv_sec = conv / 1000000; #else conv = (long) (timeout * 1000000.0); tv.tv_sec = conv / 1000000; #endif tv.tv_usec = conv % 1000000; stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, hashkey, &tv, NULL, &errstr, &err); if (port > 0) { efree(hostname); } if (stream == NULL) { php_error_docref(NULL, E_WARNING, "unable to connect to %s:" ZEND_LONG_FMT " (%s)", host, port, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr)); } if (hashkey) { efree(hashkey); } if (stream == NULL) { if (zerrno) { ZEND_TRY_ASSIGN_LONG(zerrno, err); } if (errstr) { if (zerrstr) { ZEND_TRY_ASSIGN_STR(zerrstr, errstr); } else { zend_string_release(errstr); } } RETURN_FALSE; } if (zerrno) { ZEND_TRY_ASSIGN_LONG(zerrno, 0); } if (zerrstr) { ZEND_TRY_ASSIGN_EMPTY_STRING(zerrstr); } if (errstr) { zend_string_release_ex(errstr, 0); } php_stream_to_zval(stream, return_value); }