
# Copyright (C) Dmitry Volyntsev
# Copyright (C) NGINX, Inc.


NJS_QUICKJS_LIB=
NJS_HAVE_QUICKJS=NO
NJS_QUICKJS_DEFAULT_INCS="src $NJS_BUILD_DIR"

if [ $NJS_TRY_QUICKJS = YES ]; then
    njs_found=no

    njs_feature="QuickJS library -lquickjs.lto"
    njs_feature_name=NJS_HAVE_QUICKJS
    njs_feature_run=yes
    njs_feature_incs="$NJS_QUICKJS_DEFAULT_INCS"
    njs_feature_libs="-lquickjs.lto -lm -ldl -lpthread"
    njs_feature_test="#include <quickjs_compat.h>

                      int main() {
                          JSRuntime *rt;

                          rt = JS_NewRuntime();
                          JS_FreeRuntime(rt);
                          return 0;
                     }"
    . auto/feature

    if [ $njs_found = no ]; then
        njs_feature="QuickJS library -lquickjs"
        njs_feature_libs="-lquickjs -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $njs_found = no ]; then
        njs_feature="QuickJS library -I/usr/include/quickjs/ -L/usr/lib/quickjs/ -lquickjs.lto"
        njs_feature_incs="$NJS_QUICKJS_DEFAULT_INCS /usr/include/quickjs/"
        njs_feature_libs="-L/usr/lib/quickjs/ -lquickjs.lto -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $njs_found = no ]; then
        njs_feature="QuickJS library -I/usr/include/quickjs/ -L/usr/lib/quickjs/ -lquickjs"
        njs_feature_incs="$NJS_QUICKJS_DEFAULT_INCS /usr/include/quickjs/"
        njs_feature_libs="-L/usr/lib/quickjs/ -lquickjs -lm -ldl -lpthread"

        . auto/feature
    fi

    if [ $njs_found = no ]; then
        if command -v pkg-config >/dev/null 2>&1 && pkg-config --exists quickjs-ng; then

            # pkg-config

            njs_feature="QuickJS-NG library via pkg-config"

            njs_feature_incs="$NJS_QUICKJS_DEFAULT_INCS"
            njs_feature_incs="$njs_feature_incs $(pkg-config quickjs-ng --cflags-only-I | sed -e 's/-I//g')"
            njs_feature_libs=$(pkg-config quickjs-ng --libs)

            . auto/feature
        fi
    fi

    if [ $njs_found = no ]; then
        njs_feature="QuickJS-NG library -lqjs"
        njs_feature_incs="$NJS_QUICKJS_DEFAULT_INCS"
        njs_feature_libs="-lqjs -lm -ldl -lpthread"

        . auto/feature
    fi


    if [ $njs_found = yes ]; then

        njs_feature="QuickJS JS_GetClassID()"
        njs_feature_name=NJS_HAVE_QUICKJS_GET_CLASS_ID
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
                              (void) JS_GetClassID(JS_UNDEFINED);
                              return 0;
                         }"

        . auto/feature

        if [ $njs_found = no ]; then
            echo
            echo $0: error: QuickJS library found, but JS_GetClassID\(\) is missing.
            echo
            exit 1;
        fi

        njs_feature="QuickJS JS_NewTypedArray()"
        njs_feature_name=NJS_HAVE_QUICKJS_NEW_TYPED_ARRAY
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
                              JSValue   ta, argv;
                              JSRuntime *rt;
                              JSContext *ctx;

                              rt = JS_NewRuntime();
                              ctx = JS_NewContext(rt);
                              argv = JS_NewInt64(ctx, 1);
                              ta = JS_NewTypedArray(ctx, 1, &argv,
                                                    JS_TYPED_ARRAY_UINT8);
                              JS_FreeValue(ctx, ta);
                              JS_FreeContext(ctx);
                              JS_FreeRuntime(rt);
                              return 0;
                         }"

        . auto/feature

        njs_feature="QuickJS JS_IsSameValue()"
        njs_feature_name=NJS_HAVE_QUICKJS_IS_SAME_VALUE
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
                              JSRuntime *rt;
                              JSContext *ctx;

                              rt = JS_NewRuntime();
                              ctx = JS_NewContext(rt);
                              (void) JS_IsSameValue(ctx, JS_UNDEFINED, JS_UNDEFINED);
                              JS_FreeContext(ctx);
                              JS_FreeRuntime(rt);
                              return 0;
                         }"

        . auto/feature

        njs_feature="QuickJS JS_IsArray()"
        njs_feature_name=NJS_HAVE_QUICKJS_IS_ARRAY_SINGLE_ARG
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
                              JSRuntime *rt;
                              JSContext *ctx;

                              rt = JS_NewRuntime();
                              ctx = JS_NewContext(rt);
                              (void) JS_IsArray(JS_UNDEFINED);
                              JS_FreeContext(ctx);
                              JS_FreeRuntime(rt);
                              return 0;
                         }"

        . auto/feature

        njs_feature="QuickJS JS_IsError()"
        njs_feature_name=NJS_HAVE_QUICKJS_IS_ERROR_SINGLE_ARG
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
                              JSRuntime *rt;
                              JSContext *ctx;

                              rt = JS_NewRuntime();
                              ctx = JS_NewContext(rt);
                              (void) JS_IsError(JS_UNDEFINED);
                              JS_FreeContext(ctx);
                              JS_FreeRuntime(rt);
                              return 0;
                         }"

        . auto/feature

        njs_feature="QuickJS JS_AddIntrinsicBigInt()"
        njs_feature_name=NJS_HAVE_QUICKJS_ADD_INTRINSIC_BIG_INT
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
                              JSRuntime *rt;
                              JSContext *ctx;

                              rt = JS_NewRuntime();
                              ctx = JS_NewContextRaw(rt);
                              JS_AddIntrinsicBigInt(ctx);
                              JS_FreeContext(ctx);
                              JS_FreeRuntime(rt);
                              return 0;
                         }"

        . auto/feature

        njs_feature="QuickJS JS_NewArrayBuffer() with max_len"
        njs_feature_name=NJS_HAVE_QUICKJS_ARRAY_BUFFER_MAX_LEN
        njs_feature_run=no
        njs_feature_test="#include <quickjs_compat.h>

                          static void *
                          realloc_func(JSRuntime *rt, void *opaque, void *ptr,
                                       size_t size)
                          {
                              return NULL;
                          }

                          int main() {
                              JSRuntime *rt;
                              JSContext *ctx;

                              rt = JS_NewRuntime();
                              ctx = JS_NewContext(rt);
                              (void) JS_NewArrayBuffer(ctx, NULL, 0, 0,
                                                       realloc_func, NULL, 0);
                              JS_FreeContext(ctx);
                              JS_FreeRuntime(rt);
                              return 0;
                         }"

        . auto/feature

        njs_feature="QuickJS version"
        njs_feature_name=NJS_QUICKJS_VERSION
        njs_feature_run=value
        njs_feature_test="#include <quickjs_compat.h>

                          int main() {
#if defined(QJS_VERSION_MAJOR)
                              printf(\"\\\"%s\\\"\", JS_GetVersion());
#else
                              printf(\"\\\"Unknown\\\"\");
#endif
                              return 0;
                         }"

        . auto/feature

        NJS_HAVE_QUICKJS=YES
        NJS_QUICKJS_LIB="$njs_feature_libs"
        NJS_QUICKJS_INCS=`echo "$njs_feature_incs" | sed -e "s|^$NJS_QUICKJS_DEFAULT_INCS||"`
        NJS_LIB_INCS="$NJS_LIB_INCS $NJS_QUICKJS_INCS"
        NJS_LIB_AUX_LIBS="$NJS_LIB_AUX_LIBS $njs_feature_libs"
    fi

    if [ $NJS_QUICKJS = YES -a $NJS_HAVE_QUICKJS = NO ]; then
        echo
        echo $0: error: no QuickJS library found.
        echo
        exit 1;
    fi

fi
