From 21453601a5cb05f8ee2ce8a023edbd2e6afb4969 Mon Sep 17 00:00:00 2001 From: Filip Halaxa Date: Sun, 30 Apr 2023 17:43:38 +0200 Subject: [PATCH] Ext reads streams --- ext/jsonmachine/jsonmachine.c | 39 ++++++++------------- ext/jsonmachine/jsonmachine.stub.php | 2 +- ext/jsonmachine/jsonmachine_arginfo.h | 7 ++-- test/JsonMachineTest/ExtJsonmachineTest.php | 8 ++--- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/ext/jsonmachine/jsonmachine.c b/ext/jsonmachine/jsonmachine.c index db3f5cb..80a0a9f 100644 --- a/ext/jsonmachine/jsonmachine.c +++ b/ext/jsonmachine/jsonmachine.c @@ -18,40 +18,29 @@ PHP_FUNCTION(jsonmachine_next_token) { - unsigned short int boundary[256] = {0}; - zend_string *jsonChunk; - bool finish = 0; - zend_array tokens[2]; + zval *resource; + php_stream *stream; + ssize_t bytes_read; + char buffer[1024]; - // (string $bytes, $finish = false) - ZEND_PARSE_PARAMETERS_START(1, 2) - Z_PARAM_STR(jsonChunk) - Z_PARAM_OPTIONAL - Z_PARAM_BOOL(finish) + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_RESOURCE(resource) ZEND_PARSE_PARAMETERS_END(); - // Treat UTF-8 BOM bytes as whitespace - boundary[0xEF] = 1; - boundary[0xBB] = 1; - boundary[0xBF] = 1; + php_stream_from_zval_no_verify(stream, resource); - boundary[' '] = 1; - boundary['\n'] = 1; - boundary['\r'] = 1; - boundary['\t'] = 1; + if (stream == NULL) { + php_error_docref(NULL, E_WARNING, "Invalid stream resource"); + RETURN_NULL(); + } - boundary['{'] = 2; - boundary['}'] = 2; - boundary['['] = 2; - boundary[']'] = 2; - boundary[':'] = 2; - boundary[','] = 2; + bytes_read = php_stream_read(stream, buffer, sizeof(buffer) - 1); + buffer[bytes_read] = '\0'; - RETURN_ARR(tokens); + RETURN_STRING(buffer); } - /* {{{ void test1() */ PHP_FUNCTION(test1) { diff --git a/ext/jsonmachine/jsonmachine.stub.php b/ext/jsonmachine/jsonmachine.stub.php index 24427c2..aadbbd3 100644 --- a/ext/jsonmachine/jsonmachine.stub.php +++ b/ext/jsonmachine/jsonmachine.stub.php @@ -6,4 +6,4 @@ function test1(): void {} function test2(string $str = ""): string {} -function jsonmachine_next_token(string $jsonChunk, bool $finish = false): array {} +function jsonmachine_next_token(resource $jsonChunk): string {} diff --git a/ext/jsonmachine/jsonmachine_arginfo.h b/ext/jsonmachine/jsonmachine_arginfo.h index 5555748..dcb4556 100644 --- a/ext/jsonmachine/jsonmachine_arginfo.h +++ b/ext/jsonmachine/jsonmachine_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 58d3cb761235950c70cafa30f73e24dad6845530 */ + * Stub hash: 5ab3feae5bd01fa0681248e24d3b4c2cd7c66e96 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test1, 0, 0, IS_VOID, 0) ZEND_END_ARG_INFO() @@ -8,9 +8,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test2, 0, 0, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, str, IS_STRING, 0, "\"\"") ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_jsonmachine_next_token, 0, 1, IS_ARRAY, 0) - ZEND_ARG_TYPE_INFO(0, jsonChunk, IS_STRING, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, finish, _IS_BOOL, 0, "false") +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_jsonmachine_next_token, 0, 1, IS_STRING, 0) + ZEND_ARG_TYPE_INFO(0, resource, IS_RESOURCE, 0) ZEND_END_ARG_INFO() diff --git a/test/JsonMachineTest/ExtJsonmachineTest.php b/test/JsonMachineTest/ExtJsonmachineTest.php index ec4cc6c..680b340 100644 --- a/test/JsonMachineTest/ExtJsonmachineTest.php +++ b/test/JsonMachineTest/ExtJsonmachineTest.php @@ -6,11 +6,11 @@ use PHPUnit\Framework\TestCase; class ExtJsonmachineTest extends TestCase { - public function xtestExtensionLoaded() + public function testExtensionLoaded() { $this->assertTrue(function_exists('jsonmachine_next_token')); - jsonmachine_next_token('{}'); - jsonmachine_next_token('{}', true); - jsonmachine_next_token('{}', false); + var_dump(jsonmachine_next_token(fopen('data://text/plain,XXX', 'r'))); +// jsonmachine_next_token('{}', true); +// jsonmachine_next_token('{}', false); } }