diff --git a/.gitignore b/.gitignore index b7aeb08..c6ca53c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,24 @@ .jiffy.dev -*.app -*.beam -*.o -*.so -.eunit -deps erln8.config TEST-*.xml +.rebar3 +_* +.eunit +*.o +*.so +*.beam +*.plt +*.swp +*.swo +.erlang.cookie +ebin +log +erl_crash.dump +.rebar +_rel +_deps +_plugins +_tdeps +logs +_build +c_src/env.mk diff --git a/Makefile b/Makefile deleted file mode 100644 index 025486e..0000000 --- a/Makefile +++ /dev/null @@ -1,43 +0,0 @@ -REBAR?=./rebar - - -all: build - - -clean: - $(REBAR) clean - rm -rf logs - rm -rf .eunit - rm -f test/*.beam - - -distclean: clean - git clean -fxd - - -devmarker: - @touch .jiffy.dev - - -depends: devmarker - @if test ! -d ./deps/proper; then \ - $(REBAR) get-deps; \ - fi - - -build: depends - $(REBAR) compile - - -eunit: - $(REBAR) eunit skip_deps=true - - -check: build eunit - - -%.beam: %.erl - erlc -o test/ $< - - -.PHONY: all clean distclean depends build etap eunit check diff --git a/c_src/Makefile b/c_src/Makefile new file mode 100644 index 0000000..0c9f5f4 --- /dev/null +++ b/c_src/Makefile @@ -0,0 +1,66 @@ +?ERL= erl +BASEDIR=$(CURDIR) +PRIVDIR=$(BASEDIR)/../priv + +NIF_DIR=$(BASEDIR) +NIF_ENV=$(BASEDIR)/env.mk + +NIF_SOURCES := $(shell find $(NIF_DIR) -type f \( -name "*.c" -o -name "*.cc" \)) +NIF_OBJS=$(addsuffix .o, $(basename $(NIF_SOURCES))) +NIF_SO=$(PRIVDIR)/jiffy.so + + +UNAME_SYS := $(shell uname -s) +ifeq ($(UNAME_SYS), Darwin) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -arch x86_64 -I. -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -arch x86_64 -I. -Wall + LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress +else ifeq ($(UNAME_SYS), FreeBSD) + CC ?= cc + CFLAGS ?= -O3 -std=c99 -finline-functions -I. -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -I. -Wall +else ifeq ($(UNAME_SYS), Linux) + CC ?= g++ + CFLAGS ?= -O3 -std=c99 -finline-functions -I. -Wall -Wmissing-prototypes + CXXFLAGS ?= -O3 -finline-functions -I. -Wall + LDFLAGS ?= -lstdc++ +endif + +CFLAGS += -fPIC -I$(ERTS_INCLUDE_DIR) -I$(ERL_INTERFACE_INCLUDE_DIR) +CXXFLAGS += -fPIC -I$(ERTS_INCLUDE_DIR) -I$(ERL_INTERFACE_INCLUDE_DIR) +LDLIBS += -L$(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei +LDFLAGS += -shared + + + +all: $(NIF_ENV) $(NIF_SO) + +clean: + @rm -rf $(NIF_SO) $(NIF_OBJS) + +distclean: clean + @rm -rf $(NIF_ENV) + +$(NIF_SO): $(NIF_OBJS) + @mkdir -p $(PRIVDIR) + $(CC) $(NIF_OBJS) $(LDFLAGS) $(LDLIBS) -o $(NIF_SO) + +%.o: %.c + @$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ + +%.o: %.cc + @$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@ + +$(NIF_ENV): + @$(ERL) -eval "file:write_file(\"$(NIF_ENV)\", \ + io_lib:format( \ + \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \ + \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \ + \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \ + [code:root_dir(), erlang:system_info(version), \ + code:lib_dir(erl_interface, include), \ + code:lib_dir(erl_interface, lib)])), \ + halt()." + +-include $(NIF_ENV) diff --git a/c_src/decoder.c b/c_src/decoder.c index 34cf467..2a7352b 100644 --- a/c_src/decoder.c +++ b/c_src/decoder.c @@ -65,6 +65,20 @@ typedef struct { int st_top; } Decoder; +Decoder* dec_new(ErlNifEnv* env); +void dec_init(Decoder* d, ErlNifEnv* env, ERL_NIF_TERM arg, ErlNifBinary* bin); +ERL_NIF_TERM dec_error(Decoder* d, const char* atom); +char dec_curr(Decoder* d); +int dec_top(Decoder* d); +void dec_push(Decoder* d, char val); +void dec_pop(Decoder* d, char val); +int dec_string(Decoder* d, ERL_NIF_TERM* value); +int dec_number(Decoder* d, ERL_NIF_TERM* value); +ERL_NIF_TERM make_empty_object(ErlNifEnv* env, int ret_map); +int make_object(ErlNifEnv* env, ERL_NIF_TERM pairs, ERL_NIF_TERM* out, int ret_map); +ERL_NIF_TERM make_array(ErlNifEnv* env, ERL_NIF_TERM list); + + Decoder* dec_new(ErlNifEnv* env) { diff --git a/c_src/encoder.c b/c_src/encoder.c index 9c6f59d..17c4feb 100644 --- a/c_src/encoder.c +++ b/c_src/encoder.c @@ -67,6 +67,16 @@ static char* shifts[NUM_SHIFTS] = { }; +Encoder* enc_new(ErlNifEnv* env); +int enc_init(Encoder* e, ErlNifEnv* env); +ERL_NIF_TERM enc_error(Encoder* e, const char* msg); +ERL_NIF_TERM enc_obj_error(Encoder* e, const char* msg, ERL_NIF_TERM obj); +int enc_result(Encoder* e, ERL_NIF_TERM* value); +int enc_done(Encoder* e, ERL_NIF_TERM* value); +#if MAP_TYPE_PRESENT +int enc_map_to_ejson(ErlNifEnv* env, ERL_NIF_TERM map, ERL_NIF_TERM* out); +#endif + Encoder* enc_new(ErlNifEnv* env) { diff --git a/rebar b/rebar deleted file mode 100755 index 36ef011..0000000 Binary files a/rebar and /dev/null differ diff --git a/rebar.config b/rebar.config index 9d26448..c2f56ec 100644 --- a/rebar.config +++ b/rebar.config @@ -1,33 +1,3 @@ -{port_specs, [ - {"priv/jiffy.so", [ - "c_src/*.c", - "c_src/*.cc", - "c_src/double-conversion/*.cc" - ]} -]}. - -{port_env, [ - {"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)", - "CFLAGS", "$CFLAGS -Ic_src/ -g -Wall -Werror -O3 -fno-strict-aliasing"}, - {"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)", - "CXXFLAGS", "$CXXFLAGS -Ic_src/ -g -Wall -Werror -O3"}, - - {"(linux|solaris|freebsd|netbsd|openbsd|dragonfly|darwin|gnu)", - "LDFLAGS", "$LDFLAGS -lstdc++"}, - - %% OS X Leopard flags for 64-bit - {"darwin9.*-64$", "CXXFLAGS", "-m64"}, - {"darwin9.*-64$", "LDFLAGS", "-arch x86_64"}, - - %% OS X Snow Leopard flags for 32-bit - {"darwin10.*-32$", "CXXFLAGS", "-m32"}, - {"darwin10.*-32$", "LDFLAGS", "-arch i386"}, - - %% This will merge into basho/rebar/rebar.config eventually - {"win32", "CFLAGS", "/Wall /DWIN32 /D_WINDOWS /D_WIN32 /DWINDOWS"}, - {"win32", "CXXFLAGS", "-Ic_src/ -g -Wall -O3"} -]}. - {erl_opts, [ {platform_define, "R1(1|2|3|4|5|6)", 'JIFFY_NO_MAPS'} ]}. @@ -39,7 +9,17 @@ }} ]}. -{plugins, [ - rebar_gdb_plugin -]}. +{eunit_tests, [{dir, "test"}]}. + +{profiles, [{test, + [{deps, [ + {proper, + {git, "git://github.com/manopapad/proper.git", + {branch, "master"}}} + ]}]}]}. + +{pre_hooks, [{"(linux|darwin|solaris)", compile, "make -C c_src"}, + {"(freebsd|netbsd|openbsd)", compile, "gmake -C c_src"}]}. +{post_hooks, [{"(linux|darwin|solaris)", clean, "make -C c_src clean"}, + {"(freebsd|netbsd|openbsd)", compile, "gmake -C c_src clean"}]}. diff --git a/rebar.config.script b/rebar.config.script index 8a0049d..537aec3 100644 --- a/rebar.config.script +++ b/rebar.config.script @@ -1,37 +1,44 @@ -% This file is part of Jiffy released under the MIT license. -% See the LICENSE file for more information. +IsRebar3 = case application:get_key(rebar, vsn) of + {ok, VSN} -> + [VSN1 | _] = string:tokens(VSN, "-"), + [Maj, Min, Patch] = string:tokens(VSN1, "."), + (list_to_integer(Maj) >= 3); + undefined -> + lists:keymember(mix, 1, application:loaded_applications()) + end, -% Only include PropEr as a dependency when the JIFFY_DEV -% environment variable is defined. This allows downstream -% applications to avoid requiring PropEr. -% -% This script is based on the example provided with Rebar. +case IsRebar3 of + true -> + CONFIG; + false -> + io:format("la~n", []), -ErlOpts = [{d, 'JIFFY_DEV'}], + CONFIG1 = [{eunit_opts, [verbose]}, {plugins, [rebar_gdb_plugin]} | CONFIG], -Proper = [ - {proper, ".*", {git, "git://github.com/manopapad/proper.git", "master"}} -], + ErlOpts = [{d, 'JIFFY_DEV'}], + Proper = [{proper, ".*", {git, "git://github.com/manopapad/proper.git", "master"}}], -ConfigPath = filename:dirname(SCRIPT), -DevMarker = filename:join([ConfigPath, ".jiffy.dev"]), + ConfigPath = filename:dirname(SCRIPT), + DevMarker = filename:join([ConfigPath, ".jiffy.dev"]), -case filelib:is_file(DevMarker) of - true -> - % Don't override existing dependencies - Config0 = case lists:keyfind(deps, 1, CONFIG) of + case filelib:is_file(DevMarker) of + true -> + % Don't override existing dependencies + CONFIG2 = case lists:keyfind(deps, 1, CONFIG1) of + false -> + CONFIG1 ++ [{deps, Proper}]; + {deps, DepsList} -> + lists:keyreplace(deps, 1, CONFIG1, {deps, DepsList ++ Proper}) + end, + + case lists:keyfind(erl_opts, 1, CONFIG2) of + false -> + CONFIG2 ++ [{erl_opts, ErlOpts}]; + {erl_opts, Opts} -> + NewOpts = {erl_opts, Opts ++ ErlOpts}, + lists:keyreplace(erl_opts, 1, CONFIG2, NewOpts) + end; false -> - CONFIG ++ [{deps, Proper}]; - {deps, DepsList} -> - lists:keyreplace(deps, 1, CONFIG, {deps, DepsList ++ Proper}) - end, - Config1 = case lists:keyfind(erl_opts, 1, Config0) of - false -> - Config0 ++ [{erl_opts, ErlOpts}]; - {erl_opts, Opts} -> - NewOpts = {erl_opts, Opts ++ ErlOpts}, - lists:keyreplace(erl_opts, 1, Config0, NewOpts) - end; - false -> - CONFIG + CONFIG1 + end end. diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..57afcca --- /dev/null +++ b/rebar.lock @@ -0,0 +1 @@ +[]. diff --git a/src/jiffy.app.src b/src/jiffy.app.src index 6f49231..c136849 100644 --- a/src/jiffy.app.src +++ b/src/jiffy.app.src @@ -2,5 +2,9 @@ {description, "JSON Decoder/Encoder."}, {vsn, git}, {registered, []}, - {applications, [kernel, stdlib]} + {applications, [kernel, stdlib]}, + + {maintainers, ["Paul J. Davis"]}, + {licenses, ["BSD"]}, + {links, [{"Github", "https://github.com/davisp/jiffy"}]} ]}. diff --git a/test/jiffy_10_short_double_tests.erl b/test/jiffy_10_short_double_tests.erl index d66e1ea..3dfef50 100644 --- a/test/jiffy_10_short_double_tests.erl +++ b/test/jiffy_10_short_double_tests.erl @@ -8,7 +8,12 @@ -include("jiffy_util.hrl"). -filename() -> "../test/cases/short-doubles.txt". +filename() -> + FName = "test/cases/short-doubles.txt", + case filelib:is_file(FName) of + true -> FName; + false -> "../" ++ FName + end. short_double_test_() ->