Skip to content

Instantly share code, notes, and snippets.

@chrisseaton
Last active May 22, 2019 20:33
Show Gist options
  • Select an option

  • Save chrisseaton/6793af738cb10ec8edd001e79ca61b8a to your computer and use it in GitHub Desktop.

Select an option

Save chrisseaton/6793af738cb10ec8edd001e79ca61b8a to your computer and use it in GitHub Desktop.

Revisions

  1. chrisseaton revised this gist May 22, 2019. 1 changed file with 19 additions and 7 deletions.
    26 changes: 19 additions & 7 deletions extendc.c
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,7 @@ int main(int argc, char **argv) {

    if (poly_create_context(thread, NULL, 0, &context) != poly_ok) {
    fprintf(stderr, "poly_create_context error\n");
    poly_tear_down_isolate(thread);
    return 1;
    goto exit_isolate;
    }

    char* language = "js";
    @@ -28,7 +27,10 @@ int main(int argc, char **argv) {
    } else {
    poly_value result = NULL;

    poly_open_handle_scope(thread);
    if (poly_open_handle_scope(thread) != poly_ok) {
    fprintf(stderr, "poly_open_handle_scope error\n");
    goto exit_context;
    }

    if (poly_context_eval(thread, context, language, "eval", argv[n], &result) != poly_ok) {
    fprintf(stderr, "poly_context_eval error\n");
    @@ -52,23 +54,33 @@ int main(int argc, char **argv) {
    goto exit_scope;
    }

    poly_close_handle_scope(thread);
    if (poly_close_handle_scope(thread) != poly_ok) {
    fprintf(stderr, "poly_close_handle_scope error\n");
    goto exit_context;
    }

    buffer[length] = '\0';
    printf("%s\n", buffer);
    }
    }

    poly_context_close(thread, context, true);
    poly_tear_down_isolate(thread);
    if (poly_context_close(thread, context, true) != poly_ok) {
    fprintf(stderr, "poly_context_close error\n");
    goto exit_isolate;
    }

    if (poly_tear_down_isolate(thread) != poly_ok) {
    fprintf(stderr, "poly_tear_down_isolate error\n");
    return 1;
    }

    return 0;

    exit_scope:
    poly_close_handle_scope(thread);

    exit_context:
    poly_context_close(thread, context, true);
    exit_isolate:
    poly_tear_down_isolate(thread);
    return 1;
    }
  2. chrisseaton revised this gist May 22, 2019. 1 changed file with 26 additions and 10 deletions.
    36 changes: 26 additions & 10 deletions extendc.c
    Original file line number Diff line number Diff line change
    @@ -4,18 +4,19 @@
    #include <polyglot_api.h>

    int main(int argc, char **argv) {
    graal_isolate_t *isolate = NULL;
    graal_isolatethread_t *thread = NULL;
    poly_isolate isolate = NULL;
    poly_thread thread = NULL;

    if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
    fprintf(stderr, "graal_create_isolate error\n");
    if (poly_create_isolate(NULL, &isolate, &thread) != poly_ok) {
    fprintf(stderr, "poly_create_isolate error\n");
    return 1;
    }

    poly_context context = NULL;

    if (poly_create_context(thread, NULL, 0, &context) != poly_ok) {
    fprintf(stderr, "poly_create_context error\n");
    poly_tear_down_isolate(thread);
    return 1;
    }

    @@ -26,6 +27,8 @@ int main(int argc, char **argv) {
    language = &argv[n][1];
    } else {
    poly_value result = NULL;

    poly_open_handle_scope(thread);

    if (poly_context_eval(thread, context, language, "eval", argv[n], &result) != poly_ok) {
    fprintf(stderr, "poly_context_eval error\n");
    @@ -34,25 +37,38 @@ int main(int argc, char **argv) {

    if (poly_get_last_error_info(thread, &error) != poly_ok) {
    fprintf(stderr, "poly_get_last_error_info error\n");
    return 1;
    goto exit_scope;
    }

    fprintf(stderr, "%s\n", error->error_message);

    return 1;
    goto exit_scope;
    }

    char buffer[1024];
    size_t length;

    if (poly_value_to_string_utf8(thread, result, buffer, sizeof(buffer), &length) != poly_ok) {
    fprintf(stderr, "poly_value_to_string_utf8 error\n");
    return 1;
    goto exit_scope;
    }


    poly_close_handle_scope(thread);

    buffer[length] = '\0';
    printf("%s\n", buffer);
    }
    }

    poly_context_close(thread, context, true);
    poly_tear_down_isolate(thread);

    return 0;
    }

    exit_scope:
    poly_close_handle_scope(thread);

    exit_context:
    poly_context_close(thread, context, true);
    poly_tear_down_isolate(thread);
    return 1;
    }
  3. chrisseaton revised this gist May 21, 2019. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions extendc.c
    Original file line number Diff line number Diff line change
    @@ -7,15 +7,15 @@ int main(int argc, char **argv) {
    graal_isolate_t *isolate = NULL;
    graal_isolatethread_t *thread = NULL;

    if (graal_create_isolate(NULL, &isolate) != 0 || (thread = graal_current_thread(isolate)) == NULL) {
    fprintf(stderr, "initialization error\n");
    if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
    fprintf(stderr, "graal_create_isolate error\n");
    return 1;
    }

    poly_context context = NULL;

    if (poly_create_context(thread, NULL, 0, &context) != poly_ok) {
    fprintf(stderr, "initialization error\n");
    fprintf(stderr, "poly_create_context error\n");
    return 1;
    }

    @@ -27,23 +27,30 @@ int main(int argc, char **argv) {
    } else {
    poly_value result = NULL;

    if (poly_context_eval(thread, context, language, "unicalc", argv[n], &result) != poly_ok) {
    fprintf(stderr, "eval error\n");
    if (poly_context_eval(thread, context, language, "eval", argv[n], &result) != poly_ok) {
    fprintf(stderr, "poly_context_eval error\n");

    const poly_extended_error_info *error;

    if (poly_get_last_error_info(thread, &error) != poly_ok) {
    fprintf(stderr, "poly_get_last_error_info error\n");
    return 1;
    }

    fprintf(stderr, "%s\n", error->error_message);

    return 1;
    }

    char buffer[1024];
    size_t length;

    if (poly_value_to_string_utf8(thread, result, buffer, sizeof(buffer), &length) != poly_ok) {
    fprintf(stderr, "to string error\n");
    fprintf(stderr, "poly_value_to_string_utf8 error\n");
    return 1;
    }

    buffer[length] = '\0';
    printf("%s\n", buffer);

    poly_destroy_handle(thread, result);
    }
    }

  4. chrisseaton renamed this gist Apr 25, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. chrisseaton created this gist Apr 24, 2018.
    51 changes: 51 additions & 0 deletions unicalc.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #include <stdlib.h>
    #include <stdio.h>

    #include <polyglot_api.h>

    int main(int argc, char **argv) {
    graal_isolate_t *isolate = NULL;
    graal_isolatethread_t *thread = NULL;

    if (graal_create_isolate(NULL, &isolate) != 0 || (thread = graal_current_thread(isolate)) == NULL) {
    fprintf(stderr, "initialization error\n");
    return 1;
    }

    poly_context context = NULL;

    if (poly_create_context(thread, NULL, 0, &context) != poly_ok) {
    fprintf(stderr, "initialization error\n");
    return 1;
    }

    char* language = "js";

    for (int n = 1; n < argc; n++) {
    if (argv[n][0] == '-') {
    language = &argv[n][1];
    } else {
    poly_value result = NULL;

    if (poly_context_eval(thread, context, language, "unicalc", argv[n], &result) != poly_ok) {
    fprintf(stderr, "eval error\n");
    return 1;
    }

    char buffer[1024];
    size_t length;

    if (poly_value_to_string_utf8(thread, result, buffer, sizeof(buffer), &length) != poly_ok) {
    fprintf(stderr, "to string error\n");
    return 1;
    }

    buffer[length] = '\0';
    printf("%s\n", buffer);

    poly_destroy_handle(thread, result);
    }
    }

    return 0;
    }