1 module duk_extras.v1_compat; 2 3 import core.stdc.stdio; 4 import core.stdc..string; 5 import std.conv; 6 import std.exception; 7 import std.file; 8 9 import duktape; 10 11 enum DUK_STRING_PUSH_SAFE = (1 << 0); /* no error if file does not exist */ 12 13 /* 14 * Push operations 15 */ 16 17 const(char) *duk_push_string_file_raw(duk_context *ctx, const(char) *path, duk_uint_t flags) { 18 if (path != null && strlen(path) > 0) { 19 char[] content = cast(char[])read(path.to!string).ifThrown(null); 20 21 if (content.length > 0) { 22 auto buf = cast(char *)duk_push_fixed_buffer(ctx, 23 cast(duk_size_t)content.length); 24 25 memcpy(buf, content.ptr, content.length); 26 27 return duk_buffer_to_string(ctx, -1); 28 } 29 } 30 31 if (flags & DUK_STRING_PUSH_SAFE) { 32 duk_push_undefined(ctx); 33 } else { 34 duk_error(ctx, DUK_ERR_TYPE_ERROR, "read file error"); 35 } 36 37 return null; 38 } 39 40 const(char) *duk_push_string_file(duk_context *ctx, const(char) *path) { 41 return duk_push_string_file_raw(ctx, path, 0); 42 } 43 44 /* file */ 45 void duk_eval_file(duk_context *ctx, const(char) *path) { 46 duk_push_string_file_raw(ctx, path, 0); 47 duk_push_string(ctx, path); 48 duk_compile(ctx, DUK_COMPILE_EVAL); 49 duk_push_global_object(ctx); /* 'this' binding */ 50 duk_call_method(ctx, 0);} 51 52 void duk_eval_file_noresult(duk_context *ctx, const(char) *path) { 53 duk_eval_file(ctx, path); 54 duk_pop(ctx); 55 } 56 57 duk_int_t duk_peval_file(duk_context *ctx, const(char) *path) { 58 duk_int_t rc; 59 60 duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE); 61 duk_push_string(ctx, path); 62 rc = duk_pcompile(ctx, DUK_COMPILE_EVAL); 63 if (rc != 0) { 64 return rc; 65 } 66 duk_push_global_object(ctx); /* 'this' binding */ 67 rc = duk_pcall_method(ctx, 0); 68 return rc; 69 } 70 71 duk_int_t duk_peval_file_noresult(duk_context *ctx, const(char) *path) { 72 duk_int_t rc; 73 74 rc = duk_peval_file(ctx, path); 75 duk_pop(ctx); 76 return rc; 77 } 78 79 void duk_compile_file(duk_context *ctx, duk_uint_t flags, const(char) *path) { 80 duk_push_string_file_raw(ctx, path, 0); 81 duk_push_string(ctx, path); 82 duk_compile(ctx, flags); 83 } 84 85 duk_int_t duk_pcompile_file(duk_context *ctx, duk_uint_t flags, const(char) *path) { 86 duk_int_t rc; 87 88 duk_push_string_file_raw(ctx, path, DUK_STRING_PUSH_SAFE); 89 duk_push_string(ctx, path); 90 rc = duk_pcompile(ctx, flags); 91 return rc; 92 } 93 94 /* 95 * duk_dump_context_{stdout,stderr}() 96 */ 97 98 void duk_dump_context_stdout(duk_context *ctx) { 99 duk_push_context_dump(ctx); 100 fprintf(stdout, "%s\n", duk_safe_to_string(ctx, -1)); 101 duk_pop(ctx); 102 } 103 104 void duk_dump_context_stderr(duk_context *ctx) { 105 duk_push_context_dump(ctx); 106 fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1)); 107 duk_pop(ctx); 108 } 109 110 /* 111 * duk_to_defaultvalue() 112 */ 113 114 void duk_to_defaultvalue(duk_context *ctx, duk_idx_t idx, duk_int_t hint) { 115 duk_require_type_mask(ctx, idx, DUK_TYPE_MASK_OBJECT | 116 DUK_TYPE_MASK_BUFFER | 117 DUK_TYPE_MASK_LIGHTFUNC); 118 duk_to_primitive(ctx, idx, hint); 119 }