Skip to content

Commit

Permalink
update to 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeke committed Nov 28, 2017
1 parent a94554c commit f42ac7b
Show file tree
Hide file tree
Showing 37 changed files with 5,910 additions and 3,544 deletions.
120 changes: 107 additions & 13 deletions jsonnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "ext/json/php_json.h"
#include "Zend/zend_exceptions.h"
#include "php_jsonnet.h"
#include <stdlib.h>

#include "libjsonnet.h"

Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
39 changes: 23 additions & 16 deletions libjsonnet/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 ?=

Expand All @@ -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)

Expand Down Expand Up @@ -81,31 +84,32 @@ 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

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 "<replacement "`" == 0

MAKEDEPEND_SRCS = \
cmd/jsonnet.cpp \
core/libjsonnet_test_snippet.c \
core/libjsonnet_test_file.c

depend:
makedepend -f- $(LIB_SRC) $(MAKEDEPEND_SRCS) > 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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Loading

0 comments on commit f42ac7b

Please sign in to comment.