Skip to content

Commit

Permalink
use php7 wrapper in jsonnet.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeke committed Mar 29, 2018
1 parent eb68c5b commit 892acb7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 55 deletions.
79 changes: 33 additions & 46 deletions jsonnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@
+----------------------------------------------------------------------+
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
#include "ext/standard/url.h"
#include "ext/standard/php_string.h"
#include "ext/json/php_json.h"
#include "Zend/zend_exceptions.h"
#include "php_jsonnet.h"

#include "libjsonnet.h"


Expand Down Expand Up @@ -103,7 +90,7 @@ PHP_MINIT_FUNCTION(jsonnet)

zend_class_entry jsonnet;
INIT_CLASS_ENTRY(jsonnet, JSONNET_RES_NAME, jsonnet_methods);
jsonnet_ce = zend_register_internal_class_ex(&jsonnet, NULL, NULL TSRMLS_CC);
jsonnet_ce = JSONNET_ZEND_REGISTER_CLASS(&jsonnet);
jsonnet_ce->ce_flags = ZEND_ACC_IMPLICIT_PUBLIC;

return SUCCESS;
Expand Down Expand Up @@ -142,12 +129,12 @@ PHP_MINFO_FUNCTION(jsonnet)

PHP_FUNCTION(jsonnet_get_version)
{
RETURN_STRINGL(JSONNET_PHP_VERSION, strlen(JSONNET_PHP_VERSION), 1);
JSONNET_RETURN_STRINGL(JSONNET_PHP_VERSION, strlen(JSONNET_PHP_VERSION));
}

PHP_FUNCTION(jsonnet_get_author)
{
RETURN_STRINGL(JSONNET_PHP_AUTHOR, strlen(JSONNET_PHP_AUTHOR), 1);
JSONNET_RETURN_STRINGL(JSONNET_PHP_AUTHOR, strlen(JSONNET_PHP_AUTHOR));
}

PHP_METHOD(JSONNET_RES_NAME, __construct)
Expand Down Expand Up @@ -181,35 +168,35 @@ PHP_METHOD(JSONNET_RES_NAME, evaluateFile)

if (error)
{
MAKE_STD_ZVAL(err);
ZVAL_STRING(err,output,1);
JSONNET_MAKE_ZVAL(err);
JSONNET_ZVAL_STRING(err,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

zend_throw_exception(php_com_exception_class_entry, Z_STRVAL_P(err), CODE_ERROR TSRMLS_CC);
zval_ptr_dtor(&err);
JSONNET_ZVAL_PTR_DTOR(err);
RETURN_FALSE;
}

MAKE_STD_ZVAL(result);
ZVAL_STRING(result,output,1);
JSONNET_MAKE_ZVAL(result);
JSONNET_ZVAL_STRING(result,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

MAKE_STD_ZVAL(resultZval);
JSONNET_MAKE_ZVAL(resultZval);
php_json_decode(resultZval, Z_STRVAL_P(result), Z_STRLEN_P(result), 1, 512 TSRMLS_CC);

if(Z_TYPE_P(resultZval) == IS_NULL)
{
zval_ptr_dtor(&result);
zval_ptr_dtor(&resultZval);
JSONNET_ZVAL_PTR_DTOR(result);
JSONNET_ZVAL_PTR_DTOR(resultZval);
zend_throw_exception(php_com_exception_class_entry, "JsonNet::evaluateFile #error", CODE_ERROR TSRMLS_CC);
return;
}

zval_ptr_dtor(&result);
JSONNET_ZVAL_PTR_DTOR(result);
RETURN_ZVAL(resultZval,1,1);
}

Expand All @@ -236,35 +223,35 @@ PHP_METHOD(JSONNET_RES_NAME, evaluateSnippet)

if (error)
{
MAKE_STD_ZVAL(err);
ZVAL_STRING(err,output,1);
JSONNET_MAKE_ZVAL(err);
JSONNET_ZVAL_STRING(err,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

zend_throw_exception(php_com_exception_class_entry, Z_STRVAL_P(err), CODE_ERROR TSRMLS_CC);
zval_ptr_dtor(&err);
JSONNET_ZVAL_PTR_DTOR(err);
RETURN_FALSE;
}

MAKE_STD_ZVAL(result);
ZVAL_STRING(result,output,1);
JSONNET_MAKE_ZVAL(result);
JSONNET_ZVAL_STRING(result,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

MAKE_STD_ZVAL(resultZval);
JSONNET_MAKE_ZVAL(resultZval);
php_json_decode(resultZval, Z_STRVAL_P(result), Z_STRLEN_P(result), 1, 512 TSRMLS_CC);

if(Z_TYPE_P(resultZval) == IS_NULL)
{
zval_ptr_dtor(&result);
zval_ptr_dtor(&resultZval);
JSONNET_ZVAL_PTR_DTOR(result);
JSONNET_ZVAL_PTR_DTOR(resultZval);
zend_throw_exception(php_com_exception_class_entry, "JsonNet::evaluateSnippet #error", CODE_ERROR TSRMLS_CC);
return;
}

zval_ptr_dtor(&result);
JSONNET_ZVAL_PTR_DTOR(result);
RETURN_ZVAL(resultZval,1,1);
}

Expand Down Expand Up @@ -292,26 +279,26 @@ PHP_METHOD(JSONNET_RES_NAME, fmtFile)

if (error)
{
MAKE_STD_ZVAL(err);
ZVAL_STRING(err,output,1);
JSONNET_MAKE_ZVAL(err);
JSONNET_ZVAL_STRING(err,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

zend_throw_exception(php_com_exception_class_entry, Z_STRVAL_P(err), CODE_ERROR TSRMLS_CC);
zval_ptr_dtor(&err);
JSONNET_ZVAL_PTR_DTOR(err);
RETURN_FALSE;
}

MAKE_STD_ZVAL(result);
ZVAL_STRING(result,output,1);
JSONNET_MAKE_ZVAL(result);
JSONNET_ZVAL_STRING(result,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

if(Z_TYPE_P(result) == IS_NULL)
{
zval_ptr_dtor(&result);
JSONNET_ZVAL_PTR_DTOR(result);
zend_throw_exception(php_com_exception_class_entry, "JsonNet::fmtFile #error", CODE_ERROR TSRMLS_CC);
return;
}
Expand Down Expand Up @@ -343,26 +330,26 @@ PHP_METHOD(JSONNET_RES_NAME, fmtSnippet)

if (error)
{
MAKE_STD_ZVAL(err);
ZVAL_STRING(err,output,1);
JSONNET_MAKE_ZVAL(err);
JSONNET_ZVAL_STRING(err,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

zend_throw_exception(php_com_exception_class_entry, Z_STRVAL_P(err), CODE_ERROR TSRMLS_CC);
zval_ptr_dtor(&err);
JSONNET_ZVAL_PTR_DTOR(err);
RETURN_FALSE;
}

MAKE_STD_ZVAL(result);
ZVAL_STRING(result,output,1);
JSONNET_MAKE_ZVAL(result);
JSONNET_ZVAL_STRING(result,output);

jsonnet_realloc(vm, output, 0);
jsonnet_destroy(vm);

if(Z_TYPE_P(result) == IS_NULL)
{
zval_ptr_dtor(&result);
JSONNET_ZVAL_PTR_DTOR(result);
zend_throw_exception(php_com_exception_class_entry, "JsonNet::fmtSnippet #error", CODE_ERROR TSRMLS_CC);
return;
}
Expand Down
15 changes: 6 additions & 9 deletions php_jsonnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#ifndef PHP_JSONNET_H
#define PHP_JSONNET_H

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/Jsonnet.h"

extern zend_module_entry jsonnet_module_entry;
#define phpext_jsonnet_ptr &jsonnet_module_entry

Expand All @@ -17,15 +23,6 @@ extern zend_module_entry jsonnet_module_entry;
#include "TSRM.h"
#endif

#define JSONNET_RES_NAME "JsonNet"
#define JSONNET_PHP_VERSION "v1.2.0"
#define JSONNET_PHP_AUTHOR "Chitao.Gao [ [email protected] ]"

#define SL_S(s) s, sizeof(s) - 1

#define CODE_SUCCESS 1000
#define CODE_ERROR 900

PHP_MINIT_FUNCTION(jsonnet);
PHP_MSHUTDOWN_FUNCTION(jsonnet);
PHP_RINIT_FUNCTION(jsonnet);
Expand Down

0 comments on commit 892acb7

Please sign in to comment.