v7 icon indicating copy to clipboard operation
v7 copied to clipboard

Problem running a basic test program

Open gardhr opened this issue 8 years ago • 2 comments

I couldn't get the example shown in the docs to run properly (after fixing several syntax errors!), so I put together this simple test program:

#include "v7.h"

static v7_val_t
    js_get_1024(struct v7* v7, v7_val_t this_obj, v7_val_t args)
{
    (void)this_obj;
    (void)args;
    return v7_mk_number(v7, 1024);
}

int
    main(void)
{
    struct v7*
        v7 = v7_create();
    v7_set_method(v7, v7_get_global(v7), "get_1024", &js_get_1024);
    v7_val_t
        result = 0;
    enum v7_err
        status = v7_exec(v7, "print('get_1024: ' + get_1024())", &result);
    if(status != V7_OK)
        fprintf(stderr, "error: %d\n", (int)status);
    else
        printf("result: %f\n", (float)v7_get_double(v7, result));
    v7_destroy(v7);
    return (int)status;
}

The output of the program is:

get_1024: undefined result: -nan

Program was compiled with GCC 6.2.1:

gcc -Wno-incompatible-pointer-types -lm -o test v7.c test.c

Any ideas as to what's going on?

gardhr avatar Apr 26 '17 16:04 gardhr

Ah, it's the docs. Had I not turned off warnings I would have spotted the problem right away too: I was passing a pointer to a function with the wrong signature! This example runs just fine:

#include "v7.h"

static enum v7_err
    js_get_1024(struct v7 *v7, v7_val_t *res)
{
    *res = v7_mk_number(v7, 1024);
    return V7_OK;
}

int
    main(void)
{
    struct v7*
        v7 = v7_create();
    v7_set_method(v7, v7_get_global(v7), "get_1024", js_get_1024);
    v7_val_t
        result = V7_INTERNAL_ERROR;
    enum v7_err
        status = v7_exec(v7, "print('get_1024: ' + get_1024())", &result);
    if(status != V7_OK)
        fprintf(stderr, "error: %d\n", (int)status);
    else
        printf("result: %s\n", (enum v7_err)result == V7_OK ? "success" : "failure");
    v7_destroy(v7);
    return (int)status;
}

Output:

get_1024: 1024 result: success

gardhr avatar Apr 26 '17 19:04 gardhr

I've put in a pull request with corrections to the examples.

gardhr avatar Apr 26 '17 20:04 gardhr