duk_v1_compat.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdio.h>
  2. #include "duktape.h"
  3. #include "duk_v1_compat.h"
  4. /*
  5. * duk_dump_context_{stdout,stderr}()
  6. */
  7. void duk_dump_context_stdout(duk_context *ctx) {
  8. duk_push_context_dump(ctx);
  9. fprintf(stdout, "%s\n", duk_safe_to_string(ctx, -1));
  10. duk_pop(ctx);
  11. }
  12. void duk_dump_context_stderr(duk_context *ctx) {
  13. duk_push_context_dump(ctx);
  14. fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1));
  15. duk_pop(ctx);
  16. }
  17. /*
  18. * duk_push_string_file() and duk_push_string_file_raw()
  19. */
  20. const char *duk_push_string_file_raw(duk_context *ctx, const char *path, duk_uint_t flags) {
  21. FILE *f = NULL;
  22. char *buf;
  23. long sz; /* ANSI C typing */
  24. if (!path) {
  25. goto fail;
  26. }
  27. f = fopen(path, "rb");
  28. if (!f) {
  29. goto fail;
  30. }
  31. if (fseek(f, 0, SEEK_END) < 0) {
  32. goto fail;
  33. }
  34. sz = ftell(f);
  35. if (sz < 0) {
  36. goto fail;
  37. }
  38. if (fseek(f, 0, SEEK_SET) < 0) {
  39. goto fail;
  40. }
  41. buf = (char *) duk_push_fixed_buffer(ctx, (duk_size_t) sz);
  42. if ((size_t) fread(buf, 1, (size_t) sz, f) != (size_t) sz) {
  43. duk_pop(ctx);
  44. goto fail;
  45. }
  46. (void) fclose(f); /* ignore fclose() error */
  47. return duk_buffer_to_string(ctx, -1);
  48. fail:
  49. if (f) {
  50. (void) fclose(f); /* ignore fclose() error */
  51. }
  52. if (flags & DUK_STRING_PUSH_SAFE) {
  53. duk_push_undefined(ctx);
  54. } else {
  55. duk_error(ctx, DUK_ERR_TYPE_ERROR, "read file error");
  56. }
  57. return NULL;
  58. }
  59. /*
  60. * duk_eval_file(), duk_compile_file(), and their variants
  61. */
  62. void duk_eval_file(duk_context *ctx, const char *path) {
  63. duk_push_string_file_raw(ctx, path, 0);
  64. duk_push_string(ctx, path);
  65. duk_compile(ctx, DUK_COMPILE_EVAL);
  66. duk_push_global_object(ctx); /* 'this' binding */
  67. duk_call_method(ctx, 0);
  68. }
  69. void duk_eval_file_noresult(duk_context *ctx, const char *path) {
  70. duk_eval_file(ctx, path);
  71. duk_pop(ctx);
  72. }
  73. duk_int_t duk_peval_file(duk_context *ctx, const char *path) {
  74. duk_int_t rc;
  75. duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE);
  76. duk_push_string(ctx, path);
  77. rc = duk_pcompile(ctx, DUK_COMPILE_EVAL);
  78. if (rc != 0) {
  79. return rc;
  80. }
  81. duk_push_global_object(ctx); /* 'this' binding */
  82. rc = duk_pcall_method(ctx, 0);
  83. return rc;
  84. }
  85. duk_int_t duk_peval_file_noresult(duk_context *ctx, const char *path) {
  86. duk_int_t rc;
  87. rc = duk_peval_file(ctx, path);
  88. duk_pop(ctx);
  89. return rc;
  90. }
  91. void duk_compile_file(duk_context *ctx, duk_uint_t flags, const char *path) {
  92. duk_push_string_file_raw(ctx, path, 0);
  93. duk_push_string(ctx, path);
  94. duk_compile(ctx, flags);
  95. }
  96. duk_int_t duk_pcompile_file(duk_context *ctx, duk_uint_t flags, const char *path) {
  97. duk_int_t rc;
  98. duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE);
  99. duk_push_string(ctx, path);
  100. rc = duk_pcompile(ctx, flags);
  101. return rc;
  102. }
  103. /*
  104. * duk_to_defaultvalue()
  105. */
  106. void duk_to_defaultvalue(duk_context *ctx, duk_idx_t idx, duk_int_t hint) {
  107. duk_require_type_mask(ctx, idx, DUK_TYPE_MASK_OBJECT |
  108. DUK_TYPE_MASK_BUFFER |
  109. DUK_TYPE_MASK_LIGHTFUNC);
  110. duk_to_primitive(ctx, idx, hint);
  111. }