Browse Source

Extras update for file API removal

pull/788/head
Sami Vaarala 9 years ago
parent
commit
20d97886bf
  1. 6
      extras/duk-v1-compat/Makefile
  2. 4
      extras/duk-v1-compat/README.rst
  3. 104
      extras/duk-v1-compat/duk_v1_compat.c
  4. 13
      extras/duk-v1-compat/duk_v1_compat.h
  5. 104
      extras/duk-v1-compat/test.c
  6. 3
      extras/duk-v1-compat/test_compile1.js
  7. 3
      extras/duk-v1-compat/test_compile2.js
  8. 4
      extras/duk-v1-compat/test_eval1.js
  9. 4
      extras/duk-v1-compat/test_eval2.js

6
extras/duk-v1-compat/Makefile

@ -0,0 +1,6 @@
# For manual testing; say 'make' in extras/duk-v1-compat and run ./test.
.PHONY: test
test:
gcc -o $@ -I../../src/ -I. ../../src/duktape.c duk_v1_compat.c test.c -lm
./test

4
extras/duk-v1-compat/README.rst

@ -19,3 +19,7 @@ Provides helpers for migrating from Duktape 1.x to 2.x:
The helpers don't restore full 1.x compatibility because some API calls such
as ``duk_safe_call()`` have changed in an incompatible manner.
The old APIs are documented in:
* http://duktape.org/1.5.0/api.html

104
extras/duk-v1-compat/duk_v1_compat.c

@ -2,6 +2,10 @@
#include "duktape.h"
#include "duk_v1_compat.h"
/*
* duk_dump_context_{stdout,stderr}()
*/
void duk_dump_context_stdout(duk_context *ctx) {
duk_push_context_dump(ctx);
fprintf(stdout, "%s\n", duk_safe_to_string(ctx, -1));
@ -13,3 +17,103 @@ void duk_dump_context_stderr(duk_context *ctx) {
fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
}
/*
* duk_push_string_file() and duk_push_string_file_raw()
*/
const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
FILE *f = NULL;
char *buf;
long sz; /* ANSI C typing */
if (!path) {
goto fail;
}
f = fopen(path, "rb");
if (!f) {
goto fail;
}
if (fseek(f, 0, SEEK_END) < 0) {
goto fail;
}
sz = ftell(f);
if (sz < 0) {
goto fail;
}
if (fseek(f, 0, SEEK_SET) < 0) {
goto fail;
}
buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz);
if ((size_t) fread(buf, 1, (size_t) sz, f) != (size_t) sz) {
goto fail;
}
(void) fclose(f); /* ignore fclose() error */
return duk_to_string(ctx, -1);
fail:
if (f) {
(void) fclose(f); /* ignore fclose() error */
}
if (flags & DUK_STRING_PUSH_SAFE) {
duk_push_undefined(ctx);
} else {
duk_error(ctx, DUK_ERR_TYPE_ERROR, "read file error");
}
return NULL;
}
/*
* duk_eval_file(), duk_compile_file(), and their variants
*/
void duk_eval_file(duk_context *ctx, const char *path) {
duk_push_string_file_raw(ctx, path, 0);
duk_push_string(ctx, path);
duk_compile(ctx, DUK_COMPILE_EVAL);
duk_push_global_object(ctx); /* 'this' binding */
duk_call_method(ctx, 0);
}
void duk_eval_file_noresult(duk_context *ctx, const char *path) {
duk_eval_file(ctx, path);
duk_pop(ctx);
}
duk_int_t duk_peval_file(duk_context *ctx, const char *path) {
duk_int_t rc;
duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE);
duk_push_string(ctx, path);
rc = duk_pcompile(ctx, DUK_COMPILE_EVAL);
if (rc != 0) {
return rc;
}
duk_push_global_object(ctx); /* 'this' binding */
rc = duk_pcall_method(ctx, 0);
return rc;
}
duk_int_t duk_peval_file_noresult(duk_context *ctx, const char *path) {
duk_int_t rc;
rc = duk_peval_file(ctx, path);
duk_pop(ctx);
return rc;
}
void duk_compile_file(duk_context *ctx, duk_uint_t flags, const char *path) {
duk_push_string_file_raw(ctx, path, 0);
duk_push_string(ctx, path);
duk_compile(ctx, flags);
}
duk_int_t duk_pcompile_file(duk_context *ctx, duk_uint_t flags, const char *path) {
duk_int_t rc;
duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE);
duk_push_string(ctx, path);
rc = duk_pcompile(ctx, flags);
return rc;
}

13
extras/duk-v1-compat/duk_v1_compat.h

@ -3,7 +3,20 @@
#include "duktape.h"
/* Flags for duk_push_string_file_raw() */
#define DUK_STRING_PUSH_SAFE (1 << 0) /* no error if file does not exist */
extern void duk_dump_context_stdout(duk_context *ctx);
extern void duk_dump_context_stderr(duk_context *ctx);
extern const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags);
extern void duk_eval_file(duk_context *ctx, const char *path);
extern void duk_eval_file_noresult(duk_context *ctx, const char *path);
extern duk_int_t duk_peval_file(duk_context *ctx, const char *path);
extern duk_int_t duk_peval_file_noresult(duk_context *ctx, const char *path);
extern void duk_compile_file(duk_context *ctx, duk_uint_t flags, const char *path);
extern duk_int_t duk_pcompile_file(duk_context *ctx, duk_uint_t flags, const char *path);
#define duk_push_string_file(ctx,path) \
duk_push_string_file_raw((ctx), (path), 0)
#endif /* DUK_V1_COMPAT_INCLUDED */

104
extras/duk-v1-compat/test.c

@ -0,0 +1,104 @@
#include <stdio.h>
#include "duktape.h"
#include "duk_v1_compat.h"
static duk_ret_t my_print(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_to_string(ctx, -1));
fflush(stdout);
return 0;
}
int main(int argc, char *argv[]) {
duk_context *ctx;
int i;
duk_int_t rc;
ctx = duk_create_heap_default();
if (!ctx) {
return 1;
}
/* Minimal print() provider. */
duk_push_c_function(ctx, my_print, DUK_VARARGS);
duk_put_global_string(ctx, "print");
printf("top after init: %ld\n", (long) duk_get_top(ctx));
for (i = 1; i < argc; i++) {
printf("Evaling: %s\n", argv[i]);
(void) duk_peval_string(ctx, argv[i]);
printf("--> %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
}
/* Test duk_dump_context_{stdout,stderr}() */
duk_push_string(ctx, "dump to stdout");
duk_dump_context_stdout(ctx);
duk_pop(ctx);
duk_push_string(ctx, "dump to stderr");
duk_dump_context_stderr(ctx);
duk_pop(ctx);
/* Test duk_eval_file() and related. */
printf("top before duk_eval_file(): %ld\n", (long) duk_get_top(ctx));
duk_eval_file(ctx, "test_eval1.js");
printf("top after duk_eval_file(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("top before duk_eval_file_noresult(): %ld\n", (long) duk_get_top(ctx));
duk_eval_file_noresult(ctx, "test_eval1.js");
printf("top after duk_eval_file_noresult(): %ld\n", (long) duk_get_top(ctx));
printf("top before duk_peval_file(): %ld\n", (long) duk_get_top(ctx));
rc = duk_peval_file(ctx, "test_eval1.js");
printf("top after duk_peval_file(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %ld, %s\n", (long) rc, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("top before duk_peval_file(): %ld\n", (long) duk_get_top(ctx));
rc = duk_peval_file(ctx, "test_eval2.js");
printf("top after duk_peval_file(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %ld, %s\n", (long) rc, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("top before duk_peval_file_noresult(): %ld\n", (long) duk_get_top(ctx));
rc = duk_peval_file_noresult(ctx, "test_eval1.js");
printf("top after duk_peval_file_noresult(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %ld\n", (long) rc);
printf("top before duk_peval_file_noresult(): %ld\n", (long) duk_get_top(ctx));
rc = duk_peval_file_noresult(ctx, "test_eval2.js");
printf("top after duk_peval_file_noresult(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %ld\n", (long) rc);
/* Test duk_compile_file() and related. */
printf("top before duk_compile_file(): %ld\n", (long) duk_get_top(ctx));
duk_compile_file(ctx, 0, "test_compile1.js");
printf("top after duk_compile_file(): %ld\n", (long) duk_get_top(ctx));
duk_call(ctx, 0);
duk_pop(ctx);
printf("top before duk_pcompile_file(): %ld\n", (long) duk_get_top(ctx));
rc = duk_pcompile_file(ctx, 0, "test_compile1.js");
printf("top after duk_pcompile_file(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %ld: %s\n", (long) rc, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("top before duk_pcompile_file(): %ld\n", (long) duk_get_top(ctx));
rc = duk_pcompile_file(ctx, 0, "test_compile2.js");
printf("top after duk_pcompile_file(): %ld\n", (long) duk_get_top(ctx));
printf(" --> %ld: %s\n", (long) rc, duk_safe_to_string(ctx, -1));
duk_pop(ctx);
printf("Done\n");
duk_destroy_heap(ctx);
return 0;
}

3
extras/duk-v1-compat/test_compile1.js

@ -0,0 +1,3 @@
// File to compile, no error
print('Hello from test_compile1.js');
print(new Error('test error for traceback (shows filename)').stack);

3
extras/duk-v1-compat/test_compile2.js

@ -0,0 +1,3 @@
// File to compile, syntax error
print('Hello from test_compile2.js');
= y +;

4
extras/duk-v1-compat/test_eval1.js

@ -0,0 +1,4 @@
// File to eval, no error
print('Hello from test_eval1.js');
print(new Error('test error for traceback (shows filename)').stack);
123;

4
extras/duk-v1-compat/test_eval2.js

@ -0,0 +1,4 @@
// File to eval, throws error
print('Hello from test_eval2.js');
print(new Error('test error for traceback (shows filename)').stack);
throw new Error('aiee');
Loading…
Cancel
Save