git.delta.rocks / jrsonnet / refs/commits / 494be6551f3b

difftreelog

source

bindings/c/libjsonnet_test_file.c1.9 KiBsourcehistory
1/*2Copyright 2015 Google Inc. All rights reserved.3Licensed under the Apache License, Version 2.0 (the "License");4you may not use this file except in compliance with the License.5You may obtain a copy of the License at6    http://www.apache.org/licenses/LICENSE-2.07Unless required by applicable law or agreed to in writing, software8distributed under the License is distributed on an "AS IS" BASIS,9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10See the License for the specific language governing permissions and11limitations under the License.12*/1314#include <stdlib.h>15#include <stdio.h>1617#include "libjsonnet.h"1819typedef struct JsonnetJsonValue* val_t;20typedef struct JsonnetVm* vm_t;2122typedef struct native_ctx_t {23    vm_t vm24} native_ctx_t;2526val_t native_add(void* nctx, const struct JsonnetJsonValue* const* argv, int* success) {27    native_ctx_t* ctx = nctx;28    double a;29    double b;30    jsonnet_json_extract_number(ctx->vm, argv[0], &a);31    jsonnet_json_extract_number(ctx->vm, argv[1], &b);32    *success = 1;33    return jsonnet_json_make_number(ctx->vm, a + b);34}3536int main(int argc, const char **argv)37{38    int error;39    char *output;40    struct JsonnetVm *vm;41    if (argc != 2) {42        fprintf(stderr, "libjsonnet_test_file <file>\n");43        return EXIT_FAILURE;44    }45    vm = jsonnet_make();4647    native_ctx_t* native_ctx = malloc(sizeof(native_ctx_t));48    native_ctx->vm = vm;49    const char* params[3] = {"a", "b", NULL};50    jsonnet_native_callback(vm, "nativeAdd", native_add, native_ctx, params);5152    output = jsonnet_evaluate_file(vm, argv[1], &error);53    if (error) {54        fprintf(stderr, "%s", output);55        jsonnet_realloc(vm, output, 0);56        jsonnet_destroy(vm);57        return EXIT_FAILURE;58    }59    printf("%s", output);60    jsonnet_realloc(vm, output, 0);61    free(native_ctx);62    jsonnet_destroy(vm);63    return EXIT_SUCCESS;64}