diff --git a/jsonnet.c b/jsonnet.c index fa23be0..83c50d4 100644 --- a/jsonnet.c +++ b/jsonnet.c @@ -27,7 +27,6 @@ #include "ext/json/php_json.h" #include "Zend/zend_exceptions.h" #include "php_jsonnet.h" -#include #include "libjsonnet.h" @@ -50,8 +49,10 @@ const zend_function_entry jsonnet_methods[] = PHP_ME(JSONNET_RES_NAME, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) PHP_ME(JSONNET_RES_NAME, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR) - PHP_ME(JSONNET_RES_NAME, evaluateFile, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) - PHP_ME(JSONNET_RES_NAME, evaluateSnippet, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(JSONNET_RES_NAME, evaluateFile, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(JSONNET_RES_NAME, evaluateSnippet, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(JSONNET_RES_NAME, fmtFile, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(JSONNET_RES_NAME, fmtSnippet, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) { NULL, NULL, NULL @@ -204,7 +205,7 @@ PHP_METHOD(JSONNET_RES_NAME, evaluateFile) { zval_ptr_dtor(&result); zval_ptr_dtor(&resultZval); - zend_throw_exception(php_com_exception_class_entry, "JsonNet #error", CODE_ERROR TSRMLS_CC); + zend_throw_exception(php_com_exception_class_entry, "JsonNet::evaluateFile #error", CODE_ERROR TSRMLS_CC); return; } @@ -259,7 +260,7 @@ PHP_METHOD(JSONNET_RES_NAME, evaluateSnippet) { zval_ptr_dtor(&result); zval_ptr_dtor(&resultZval); - zend_throw_exception(php_com_exception_class_entry, "JsonNet #error", CODE_ERROR TSRMLS_CC); + zend_throw_exception(php_com_exception_class_entry, "JsonNet::evaluateSnippet #error", CODE_ERROR TSRMLS_CC); return; } @@ -270,11 +271,104 @@ PHP_METHOD(JSONNET_RES_NAME, evaluateSnippet) zend_throw_exception(php_com_exception_class_entry, "JsonNet::evaluateSnippet('string'), string is null", CODE_ERROR TSRMLS_CC); } -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ +PHP_METHOD(JSONNET_RES_NAME, fmtFile) +{ + int argc = ZEND_NUM_ARGS(); + int _file_path_len, error; + + char *output, *_file_path = NULL; + struct JsonnetVm *vm; + + zval *err, *result; + + if (zend_parse_parameters(argc TSRMLS_CC, "s", &_file_path, &_file_path_len) == FAILURE) + return; + + if (argc > 0 && _file_path_len > 0) + { + + vm = jsonnet_make(); + output = jsonnet_fmt_file(vm, _file_path, &error); + + if (error) + { + MAKE_STD_ZVAL(err); + ZVAL_STRING(err,output,1); + + 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); + RETURN_FALSE; + } + + MAKE_STD_ZVAL(result); + ZVAL_STRING(result,output,1); + + jsonnet_realloc(vm, output, 0); + jsonnet_destroy(vm); + + if(Z_TYPE_P(result) == IS_NULL) + { + zval_ptr_dtor(&result); + zend_throw_exception(php_com_exception_class_entry, "JsonNet::fmtFile #error", CODE_ERROR TSRMLS_CC); + return; + } + + RETURN_ZVAL(result,1,1); + } + + zend_throw_exception(php_com_exception_class_entry, "JsonNet::fmtFile('filePath'), filePath is null", CODE_ERROR TSRMLS_CC); +} + +PHP_METHOD(JSONNET_RES_NAME, fmtSnippet) +{ + int argc = ZEND_NUM_ARGS(); + int _snippet_string_len, error; + + char *output, *_snippet_string = NULL; + struct JsonnetVm *vm; + + zval *err, *result; + + if (zend_parse_parameters(argc TSRMLS_CC, "s", &_snippet_string, &_snippet_string_len) == FAILURE) + return; + + if (argc > 0 && _snippet_string_len > 0) + { + + vm = jsonnet_make(); + output = jsonnet_fmt_snippet(vm, "snippet", _snippet_string, &error); + + if (error) + { + MAKE_STD_ZVAL(err); + ZVAL_STRING(err,output,1); + + 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); + RETURN_FALSE; + } + + MAKE_STD_ZVAL(result); + ZVAL_STRING(result,output,1); + + jsonnet_realloc(vm, output, 0); + jsonnet_destroy(vm); + + if(Z_TYPE_P(result) == IS_NULL) + { + zval_ptr_dtor(&result); + zend_throw_exception(php_com_exception_class_entry, "JsonNet::fmtSnippet #error", CODE_ERROR TSRMLS_CC); + return; + } + + RETURN_ZVAL(result,1,1); + } + + zend_throw_exception(php_com_exception_class_entry, "JsonNet::fmtSnippet('string'), string is null", CODE_ERROR TSRMLS_CC); +} diff --git a/libjsonnet/Makefile b/libjsonnet/Makefile index aaecd9b..cfad23a 100644 --- a/libjsonnet/Makefile +++ b/libjsonnet/Makefile @@ -16,7 +16,7 @@ # User-servicable parts: ################################################################################ -# C/C++ compiler -- clang also works +# C/C++ compiler; to use Clang, build with `make CC=clang CXX=clang++` CXX ?= g++ CC ?= gcc @@ -29,9 +29,10 @@ OD ?= od OPT ?= -O3 -CXXFLAGS ?= -g $(OPT) -Wall -Wextra -Woverloaded-virtual -pedantic -std=c++0x -fPIC -Iinclude +CXXFLAGS ?= -g $(OPT) -Wall -Wextra -Woverloaded-virtual -pedantic -std=c++0x -fPIC -Iinclude -Ithird_party/md5 CFLAGS ?= -g $(OPT) -Wall -Wextra -pedantic -std=c99 -fPIC -Iinclude -EMCXXFLAGS = $(CXXFLAGS) --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 +MAKEDEPENDFLAGS ?= -Iinclude -Ithird_party/md5 +EMCXXFLAGS = $(CXXFLAGS) -Os --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 -s OUTLINING_LIMIT=10000 EMCFLAGS = $(CFLAGS) --memory-init-file 0 -s DISABLE_EXCEPTION_CATCHING=0 LDFLAGS ?= @@ -47,9 +48,11 @@ LIB_SRC = \ core/lexer.cpp \ core/libjsonnet.cpp \ core/parser.cpp \ + core/pass.cpp \ core/static_analysis.cpp \ core/string_utils.cpp \ - core/vm.cpp + core/vm.cpp \ + third_party/md5/md5.cpp LIB_OBJ = $(LIB_SRC:.cpp=.o) @@ -81,7 +84,8 @@ ALL_HEADERS = \ core/vm.h \ core/std.jsonnet.h \ include/libjsonnet.h \ - include/libjsonnet++.h + include/libjsonnet++.h \ + third_party/md5/md5.h LIB_JSONNET_PATH = /usr/local/lib @@ -89,15 +93,14 @@ default: jsonnet all: $(ALL) -TEST_SNIPPET = "std.assertEqual(({ x: 1, y: self.x } { x: 2 }).y, 2)" test: jsonnet libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file - ./jsonnet -e $(TEST_SNIPPET) - LD_LIBRARY_PATH=. ./libjsonnet_test_snippet $(TEST_SNIPPET) - LD_LIBRARY_PATH=. ./libjsonnet_test_file "test_suite/object.jsonnet" - cd examples ; ./check.sh - cd examples/terraform ; ./check.sh - cd test_suite ; ./run_tests.sh - cd test_suite ; ./run_fmt_tests.sh + ./tests.sh + +reformat: + clang-format -i -style=file **/*.cpp **/*.h + +test-formatting: + test "`clang-format -style=file -output-replacements-xml **/*.cpp **/*.h | grep -c " Makefile.depend + rm -f Makefile.depend + for FILE in $(LIB_SRC) $(MAKEDEPEND_SRCS) ; do $(CXX) -MM $(CXXFLAGS) $$FILE -MT $$(dirname $$FILE)/$$(basename $$FILE .cpp).o >> Makefile.depend ; done core/desugarer.cpp: core/std.jsonnet.h @@ -124,10 +128,11 @@ libjsonnet.so: $(LIB_OBJ) cp -rf include/libjsonnet.h $(LIB_JSONNET_PATH) cp -rf libjsonnet.so $(LIB_JSONNET_PATH) + libjsonnet++.so: $(LIB_CPP_OBJ) $(CXX) $(LDFLAGS) $(LIB_CPP_OBJ) $(SHARED_LDFLAGS) -o $@ -# Javascript build of C binding +# JavaScript build of C binding JS_EXPORTED_FUNCTIONS = 'EXPORTED_FUNCTIONS=["_jsonnet_make", "_jsonnet_evaluate_snippet", "_jsonnet_realloc", "_jsonnet_destroy"]' libjsonnet.js: $(LIB_SRC) $(ALL_HEADERS) @@ -163,6 +168,8 @@ core/%.jsonnet.h: stdlib/%.jsonnet echo >> $@ clean: - rm -vf */*~ *~ .*~ */.*.swp .*.swp $(ALL) *.o core/*.jsonnet.h Make.depend + rm -vf */*~ *~ .*~ */.*.swp .*.swp $(ALL) *.o core/*.jsonnet.h Makefile.depend -include Makefile.depend + +.PHONY: default all depend clean reformat test test-formatting diff --git a/libjsonnet/cmd/jsonnet.cpp b/libjsonnet/cmd/jsonnet.cpp index 3449d40..9fc22ef 100644 --- a/libjsonnet/cmd/jsonnet.cpp +++ b/libjsonnet/cmd/jsonnet.cpp @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include #include #include -#include #include #include @@ -26,7 +26,7 @@ limitations under the License. #include extern "C" { - #include +#include } std::string next_arg(unsigned &i, const std::vector &args) @@ -43,7 +43,7 @@ std::string next_arg(unsigned &i, const std::vector &args) std::vector simplify_args(int argc, const char **argv) { std::vector r; - for (int i=1 ; i simplify_args(int argc, const char **argv) } // Check if it is of the form -abc and convert to -a -b -c if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') { - for (unsigned j=1 ; j] {