本文整理汇总了C++中duk_safe_call函数的典型用法代码示例。如果您正苦于以下问题:C++ duk_safe_call函数的具体用法?C++ duk_safe_call怎么用?C++ duk_safe_call使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了duk_safe_call函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sjs_vm_eval_code
DUK_EXTERNAL int sjs_vm_eval_code(const sjs_vm_t* vm, const char* filename, const char* code, size_t len, FILE* foutput, FILE* ferror) {
int r;
assert(vm);
duk_context* ctx = vm->ctx;
duk_push_pointer(ctx, (void *) code);
duk_push_uint(ctx, len);
duk_push_string(ctx, filename);
r = duk_safe_call(ctx, sjs__compile_execute, 3 /*nargs*/, 1 /*nret*/);
if (r != DUK_EXEC_SUCCESS) {
if (ferror) {
duk_safe_call(ctx, sjs__get_error_stack, 1 /*nargs*/, 1 /*nrets*/);
fprintf(ferror, "%s\n", duk_safe_to_string(ctx, -1));
fflush(ferror);
}
} else {
if (foutput) {
fprintf(foutput, "= %s\n", duk_safe_to_string(ctx, -1));
fflush(foutput);
}
}
duk_pop(ctx);
return r;
}
示例2: test
void test(duk_context *ctx) {
int rc;
rc = duk_safe_call(ctx, test_1, 0, 1);
printf("rc=%d, result=%s\n", rc, duk_to_string(ctx, -1));
duk_pop(ctx);
rc = duk_safe_call(ctx, test_2, 0, 1);
printf("rc=%d, result=%s\n", rc, duk_to_string(ctx, -1));
duk_pop(ctx);
}
示例3: test
void test(duk_context *ctx) {
int rc;
rc = duk_safe_call(ctx, test_1, 0, 1, DUK_INVALID_INDEX);
printf("rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1));
duk_pop(ctx);
rc = duk_safe_call(ctx, test_2, 0, 1, DUK_INVALID_INDEX);
printf("rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1));
duk_pop(ctx);
}
示例4: test_2a
static duk_ret_t test_2a(duk_context *ctx, void *udata) {
(void) udata;
/* Test first with nothing on stack index -3. */
duk_safe_call(ctx, test__null, NULL, 0, 1); printf("%s\n", duk_safe_to_string(ctx, 0));
duk_pop(ctx);
duk_set_top(ctx, 0); duk_push_undefined(ctx); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_null(ctx); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_true(ctx); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_false(ctx); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_number(ctx, 123.0); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_string(ctx, "foo\x00" "bar"); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_fixed_buffer(ctx, 16); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_pointer(ctx, NULL); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_pointer(ctx, (void *) 0xdeadbeefUL); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_object(ctx); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_array(ctx); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_c_function(ctx, dummy_func, 0); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_c_lightfunc(ctx, dummy_func, 0, 0, 0); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_eval_string(ctx, "(function dummy(){})"); test__require_calls(ctx);
duk_set_top(ctx, 0); duk_push_thread(ctx); test__require_calls(ctx);
printf("done\n");
return 0;
}
示例5: test_error_augment
static duk_ret_t test_error_augment(duk_context *ctx, void *udata) {
duk_int_t ret;
(void) udata;
prep(ctx);
/* This case is a bit tricky. There used to be a problem where the
* error augmentation process itself failed when checking whether or
* not the throw value inherited from Error. This has now been fixed
* and the error value no longer gets augmented and is thrown correctly.
*
* The TEST_SAFE_CALL() wrapper uses duk_safe_to_string() to coerce
* the throw result. Since the object doesn't have a toString()
* function, this coercion will fail and generate a prototype loop
* error!
*
* So, use a separate duk_safe_call() wrapping here to ensure we treat
* the final result value carefully. We print out 'foo' to be sure
* the correct value was thrown.
*/
duk_dup(ctx, 0);
ret = duk_safe_call(ctx, augment_raw, NULL, 0 /*nargs*/, 1 /*nrets*/);
printf("ret=%d\n", (int) ret);
duk_get_prop_string(ctx, -1, "foo");
printf("throw value .foo=%d\n", duk_get_int(ctx, -1));
duk_pop_2(ctx);
return 0;
}
示例6: test_basic
static duk_ret_t test_basic(duk_context *ctx, void *udata) {
duk_idx_t i, n;
duk_int_t rc;
(void) udata;
duk_push_undefined(ctx);
duk_push_null(ctx);
duk_push_true(ctx);
duk_push_false(ctx);
duk_push_string(ctx, "foo");
duk_push_int(ctx, 123);
duk_push_object(ctx);
duk_push_pointer(ctx, (void *) NULL);
duk_push_pointer(ctx, (void *) 0xdeadbeefUL);
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i <= n; i++) {
rc = duk_safe_call(ctx, safe_helper, (void *) i, 0, 1);
if (rc != DUK_EXEC_SUCCESS) {
printf("index %ld: %s\n", (long) i, duk_safe_to_string(ctx, -1));
}
duk_pop(ctx);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例7: test
void test(duk_context *ctx) {
duk_context *new_ctx;
new_ctx = duk_create_heap(NULL, NULL, NULL, NULL, my_fatal_handler);
duk_safe_call(new_ctx, my_func, NULL, 0, 1);
printf("duk_safe_call() returned, should not happen\n");
}
示例8: handle_eval
static int handle_eval(duk_context *ctx, const char *code) {
int rc;
int retval = -1;
duk_push_pointer(ctx, (void *) code);
duk_push_uint(ctx, (duk_uint_t) strlen(code));
duk_push_string(ctx, "eval");
interactive_mode = 0; /* global */
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
if (rc != DUK_EXEC_SUCCESS) {
print_pop_error(ctx, stderr);
} else {
duk_pop(ctx);
retval = 0;
}
return retval;
}
示例9: print_pop_error
/* in interactive mode, write to stdout */
print_pop_error(ctx, stdout);
retval = -1; /* an error 'taints' the execution */
} else {
duk_pop(ctx);
}
}
done:
if (buffer) {
free(buffer);
buffer = NULL;
}
return retval;
}
#else /* NO_READLINE */
static int handle_interactive(duk_context *ctx) {
const char *prompt = "duk> ";
char *buffer = NULL;
int retval = 0;
int rc;
duk_eval_string(ctx, GREET_CODE(""));
duk_pop(ctx);
/*
* Note: using readline leads to valgrind-reported leaks inside
* readline itself. Execute code from an input file (and not
* through stdin) for clean valgrind runs.
*/
rl_initialize();
for (;;) {
if (buffer) {
free(buffer);
buffer = NULL;
}
buffer = readline(prompt);
if (!buffer) {
break;
}
if (buffer && buffer[0] != (char) 0) {
add_history(buffer);
}
duk_push_lstring(ctx, buffer, strlen(buffer));
duk_push_string(ctx, "input");
if (buffer) {
free(buffer);
buffer = NULL;
}
interactive_mode = 1; /* global */
rc = duk_safe_call(ctx, wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/);
if (rc != DUK_EXEC_SUCCESS) {
/* in interactive mode, write to stdout */
print_pop_error(ctx, stdout);
retval = -1; /* an error 'taints' the execution */
} else {
duk_pop(ctx);
}
}
if (buffer) {
free(buffer);
buffer = NULL;
}
return retval;
}
示例10: duk_module_node_peval_main
/* Load a module as the 'main' module. */
duk_ret_t duk_module_node_peval_main(duk_context *ctx, const char *path) {
/*
* Stack: [ ... source ]
*/
duk__push_module_object(ctx, path, 1 /*main*/);
/* [ ... source module ] */
duk_dup(ctx, 0);
/* [ ... source module source ] */
#if DUK_VERSION >= 19999
return duk_safe_call(ctx, duk__eval_module_source, NULL, 2, 1);
#else
return duk_safe_call(ctx, duk__eval_module_source, 2, 1);
#endif
}
示例11: sjs_vm_eval_code
DUK_EXTERNAL int sjs_vm_eval_code(const sjs_vm_t* vm,
const char* filename,
const char* code,
size_t len,
FILE* foutput,
FILE* ferror,
bool use_strict) {
int r;
assert(vm);
duk_context* ctx = vm->ctx;
duk_push_boolean(ctx, use_strict);
duk_push_pointer(ctx, (void *) code);
duk_push_uint(ctx, len);
duk_push_string(ctx, filename);
r = duk_safe_call(ctx, sjs__compile_execute, 4 /*nargs*/, 1 /*nret*/);
if (r != DUK_EXEC_SUCCESS) {
if (ferror) {
duk_safe_call(ctx, sjs__get_error_stack, 1 /*nargs*/, 1 /*nrets*/);
fprintf(ferror, "%s\n", duk_safe_to_string(ctx, -1));
fflush(ferror);
}
} else {
if (foutput) {
/* TODO: make this optional with a parameter? */
/* beautify output */
duk_eval_string(ctx, "(function (v) {\n"
" try {\n"
" return Duktape.enc('jx', v, null, 4);\n"
" } catch (e) {\n"
" return String(v);\n"
" }\n"
"})");
duk_insert(ctx, -2);
duk_call(ctx, 1);
fprintf(foutput, "= %s\n", duk_safe_to_string(ctx, -1));
fflush(foutput);
}
}
duk_pop(ctx);
return r;
}
示例12: handle_fh
static int handle_fh(duk_context *ctx, FILE *f, const char *filename, const char *bytecode_filename) {
char *buf = NULL;
int len;
size_t got;
int rc;
int retval = -1;
if (fseek(f, 0, SEEK_END) < 0) {
goto error;
}
len = (int) ftell(f);
if (fseek(f, 0, SEEK_SET) < 0) {
goto error;
}
buf = (char *) malloc(len);
if (!buf) {
goto error;
}
got = fread((void *) buf, (size_t) 1, (size_t) len, f);
duk_push_string(ctx, bytecode_filename);
duk_push_pointer(ctx, (void *) buf);
duk_push_uint(ctx, (duk_uint_t) got);
duk_push_string(ctx, filename);
interactive_mode = 0; /* global */
rc = duk_safe_call(ctx, wrapped_compile_execute, 4 /*nargs*/, 1 /*nret*/);
#if defined(DUK_CMDLINE_AJSHEAP)
ajsheap_clear_exec_timeout();
#endif
free(buf);
buf = NULL;
if (rc != DUK_EXEC_SUCCESS) {
print_pop_error(ctx, stderr);
goto error;
} else {
duk_pop(ctx);
retval = 0;
}
/* fall thru */
cleanup:
if (buf) {
free(buf);
}
return retval;
error:
fprintf(stderr, "error in executing file %s\n", filename);
fflush(stderr);
goto cleanup;
}
示例13: test_basic
static duk_ret_t test_basic(duk_context *ctx, void *udata) {
duk_idx_t i, n;
duk_int_t rc;
(void) udata;
duk_push_undefined(ctx);
duk_push_null(ctx);
duk_push_true(ctx);
duk_push_false(ctx);
duk_push_string(ctx, "");
duk_push_string(ctx, "foo");
duk_push_int(ctx, 123);
duk_push_object(ctx);
duk_push_fixed_buffer(ctx, 0);
duk_push_fixed_buffer(ctx, 1024);
duk_push_dynamic_buffer(ctx, 0);
duk_push_dynamic_buffer(ctx, 2048);
duk_eval_string(ctx, "(function () { return new ArrayBuffer(16); })()");
duk_eval_string(ctx, "(function () { return new Uint32Array(16); })()");
duk_eval_string(ctx, "(function () { return new DataView(new ArrayBuffer(16)); })()");
duk_eval_string(ctx, "(function () { return new Uint32Array(16).subarray(3, 6); })()");
duk_eval_string(ctx, "(function () { return new Buffer('ABCDEFGH'); })()");
duk_eval_string(ctx, "(function () { return new Buffer('ABCDEFGH').slice(3, 6); })()");
n = duk_get_top(ctx);
printf("top: %ld\n", (long) n);
for (i = 0; i <= n; i++) {
rc = duk_safe_call(ctx, safe_helper1, (void *) i, 0, 1);
if (rc != DUK_EXEC_SUCCESS) {
printf("index %ld: %s\n", (long) i, duk_safe_to_string(ctx, -1));
}
duk_pop(ctx);
rc = duk_safe_call(ctx, safe_helper2, (void *) i, 0, 1);
if (rc != DUK_EXEC_SUCCESS) {
printf("index %ld: %s\n", (long) i, duk_safe_to_string(ctx, -1));
}
duk_pop(ctx);
}
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例14: duk_hobject_run_finalizer
DUK_INTERNAL void duk_hobject_run_finalizer(duk_hthread *thr, duk_hobject *obj) {
duk_context *ctx = (duk_context *) thr;
duk_ret_t rc;
#if defined(DUK_USE_ASSERTIONS)
duk_idx_t entry_top;
#endif
DUK_DDD(DUK_DDDPRINT("running object finalizer for object: %p", (void *) obj));
DUK_ASSERT(thr != NULL);
DUK_ASSERT(ctx != NULL);
DUK_ASSERT(obj != NULL);
DUK_ASSERT_VALSTACK_SPACE(thr, 1);
#if defined(DUK_USE_ASSERTIONS)
entry_top = duk_get_top(ctx);
#endif
/*
* Get and call the finalizer. All of this must be wrapped
* in a protected call, because even getting the finalizer
* may trigger an error (getter may throw one, for instance).
*/
DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj));
if (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) obj)) {
DUK_D(DUK_DPRINT("object already finalized, avoid running finalizer twice: %!O", obj));
return;
}
DUK_HEAPHDR_SET_FINALIZED((duk_heaphdr *) obj); /* ensure never re-entered until rescue cycle complete */
if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(obj)) {
/* This shouldn't happen; call sites should avoid looking up
* _Finalizer "through" a Proxy, but ignore if we come here
* with a Proxy to avoid finalizer re-entry.
*/
DUK_D(DUK_DPRINT("object is a proxy, skip finalizer call"));
return;
}
/* XXX: use a NULL error handler for the finalizer call? */
DUK_DDD(DUK_DDDPRINT("-> finalizer found, calling wrapped finalize helper"));
duk_push_hobject(ctx, obj); /* this also increases refcount by one */
rc = duk_safe_call(ctx, duk__finalize_helper, NULL /*udata*/, 0 /*nargs*/, 1 /*nrets*/); /* -> [... obj retval/error] */
DUK_ASSERT_TOP(ctx, entry_top + 2); /* duk_safe_call discipline */
if (rc != DUK_EXEC_SUCCESS) {
/* Note: we ask for one return value from duk_safe_call to get this
* error debugging here.
*/
DUK_D(DUK_DPRINT("wrapped finalizer call failed for object %p (ignored); error: %!T",
(void *) obj, (duk_tval *) duk_get_tval(ctx, -1)));
}
duk_pop_2(ctx); /* -> [...] */
DUK_ASSERT_TOP(ctx, entry_top);
}
示例15: print_error
/* Print error to stderr and pop error. */
static void print_error(duk_context *ctx, FILE *f) {
/* Print error objects with a stack trace specially.
* Note that getting the stack trace may throw an error
* so this also needs to be safe call wrapped.
*/
(void) duk_safe_call(ctx, get_stack_raw, 1 /*nargs*/, 1 /*nrets*/);
fprintf(f, "%s\n", duk_safe_to_string(ctx, -1));
fflush(f);
duk_pop(ctx);
}