Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem running a basic test program #579

Open
gardhr opened this issue Apr 26, 2017 · 2 comments
Open

Problem running a basic test program #579

gardhr opened this issue Apr 26, 2017 · 2 comments

Comments

@gardhr
Copy link

gardhr commented Apr 26, 2017

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
Copy link
Author

gardhr commented Apr 26, 2017

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
Copy link
Author

gardhr commented Apr 26, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant