本文整理汇总了C++中duk_safe_to_string函数的典型用法代码示例。如果您正苦于以下问题:C++ duk_safe_to_string函数的具体用法?C++ duk_safe_to_string怎么用?C++ duk_safe_to_string使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了duk_safe_to_string函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: duk_android_log_print
static duk_ret_t duk_android_log_print(duk_context *ctx) {
int n = duk_get_top(ctx);
switch (n) {
case 1:
LOGD("ScriptEngine", "%s", duk_safe_to_string(ctx, 0));
break;
case 2:
LOGD("ScriptEngine", "%s %s", duk_safe_to_string(ctx, 0),
duk_safe_to_string(ctx, 1));
break;
case 3:
LOGD("ScriptEngine", "%s %s %s", duk_safe_to_string(ctx, 0),
duk_safe_to_string(ctx, 1), duk_safe_to_string(ctx, 2));
break;
case 4: {
LOGD("ScriptEngine", "%s %s %s %s", duk_safe_to_string(ctx, 0),
duk_safe_to_string(ctx, 1), duk_safe_to_string(ctx, 2),
duk_safe_to_string(ctx, 3));
}
break;
case 0: {
LOGD("ScriptEngine", "\n");
}
break;
default: {
LOGW("ScriptEngine",
"too many args %d to print, javascript print only support less than 4 args\n",
n);
}
break;
}
return 0;
}
示例2: main
int main(int argc, const char *argv[]) {
duk_context *ctx = NULL;
ctx = duk_create_heap_default();
if (!ctx) {
printf("Failed to create a Duktape heap.\n");
exit(1);
}
duk_push_global_object(ctx);
duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/);
duk_put_prop_string(ctx, -2, "primeCheckNative");
if (duk_peval_file(ctx, "prime.js") != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
goto finished;
}
duk_pop(ctx); /* ignore result */
duk_get_prop_string(ctx, -1, "primeTest");
if (duk_pcall(ctx, 0) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
}
duk_pop(ctx); /* ignore result */
finished:
duk_destroy_heap(ctx);
exit(0);
}
示例3: 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;
}
示例4: test_is_prototype_of
static duk_ret_t test_is_prototype_of(duk_context *ctx, void *udata) {
(void) udata;
prep(ctx);
/* obj0.isPrototypeOf(dummy) -> false, traverses prototype chain of dummy */
duk_eval_string(ctx, "Object.prototype.isPrototypeOf");
duk_dup(ctx, 0);
duk_push_object(ctx);
duk_call_method(ctx, 1);
printf("Object.prototype.isPrototypeOf result: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
/* obj0.isPrototypeOf(obj1) -> true, traverses prototype chain of obj1 */
duk_eval_string(ctx, "Object.prototype.isPrototypeOf");
duk_dup(ctx, 0);
duk_dup(ctx, 1);
duk_call_method(ctx, 1);
printf("Object.prototype.isPrototypeOf result: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
/* dummy.isPrototypeOf(obj0) -> traverses prototype chain of obj0 and throws */
duk_eval_string(ctx, "Object.prototype.isPrototypeOf");
duk_push_object(ctx);
duk_dup(ctx, 0);
duk_call_method(ctx, 1);
printf("Object.prototype.isPrototypeOf result: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
return 0;
}
示例5: main
int main(int argc, const char *argv[]) {
duk_context *ctx = NULL;
char line[4096];
size_t idx;
int ch;
(void) argc; (void) argv;
ctx = duk_create_heap_default();
if (!ctx) {
printf("Failed to create a Duktape heap.\n");
exit(1);
}
push_file_as_string(ctx, "process.js");
if (duk_peval(ctx) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
goto finished;
}
duk_pop(ctx); /* ignore result */
memset(line, 0, sizeof(line));
idx = 0;
for (;;) {
if (idx >= sizeof(line)) {
printf("Line too long\n");
exit(1);
}
ch = fgetc(stdin);
if (ch == 0x0a) {
line[idx++] = '\0';
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1 /*index*/, "processLine");
duk_push_string(ctx, line);
if (duk_pcall(ctx, 1 /*nargs*/) != 0) {
printf("Error: %s\n", duk_safe_to_string(ctx, -1));
} else {
printf("%s\n", duk_safe_to_string(ctx, -1));
}
duk_pop(ctx); /* pop result/error */
idx = 0;
} else if (ch == EOF) {
break;
} else {
line[idx++] = (char) ch;
}
}
finished:
duk_destroy_heap(ctx);
exit(0);
}
示例6: test
void test(duk_context *ctx) {
duk_int_t rc;
/* From API doc */
duk_push_string(ctx, "print('Hello world!'); 123;");
duk_eval(ctx);
printf("return value is: %lf\n", duk_get_number(ctx, -1));
duk_pop(ctx);
/* Function expression */
duk_push_string(ctx, "(function adder(x,y) { return x+y; })");
duk_eval(ctx); /* [ func ] */
duk_push_int(ctx, 123);
duk_push_int(ctx, 234);
duk_call(ctx, 2); /* [ func 123 234 ] -> [ result ] */
printf("adder(123, 234) -> %lf\n", duk_get_number(ctx, -1));
duk_pop(ctx);
/* Protected eval with success */
duk_push_string(ctx, "print('Hello world!'); 123;");
rc = duk_peval(ctx);
printf("return value is: %s (rc=%d)\n", duk_safe_to_string(ctx, -1), (int) rc);
duk_pop(ctx);
/* Protected eval with failure */
duk_push_string(ctx, "throw new Error('eval error');");
rc = duk_peval(ctx);
printf("return value is: %s (rc=%d)\n", duk_safe_to_string(ctx, -1), (int) rc);
duk_pop(ctx);
/* Protected eval with syntax error */
duk_push_string(ctx, "obj = {");
rc = duk_peval(ctx);
printf("return value is: %s (rc=%d)\n", duk_safe_to_string(ctx, -1), (int) rc);
duk_pop(ctx);
/* Plain eval with no result */
printf("top=%ld\n", (long) duk_get_top(ctx));
duk_push_string(ctx, "print('doing eval');");
duk_eval_noresult(ctx);
/* Protected eval with no result, no error */
printf("top=%ld\n", (long) duk_get_top(ctx));
duk_push_string(ctx, "print('doing peval');");
rc = duk_peval_noresult(ctx);
printf("rc=%d\n", (int) rc);
/* Protected eval with no result, syntax error */
printf("top=%ld\n", (long) duk_get_top(ctx));
duk_push_string(ctx, "print('doing peval'); obj = {");
rc = duk_peval_noresult(ctx);
printf("rc=%d\n", (int) rc);
printf("top: %ld\n", (long) duk_get_top(ctx));
}
示例7: LOGE
bool StyleContext::evalFunction(FunctionID id) {
// Get all functions (array) in context
if (!duk_get_global_string(m_ctx, FUNC_ID)) {
LOGE("EvalFilterFn - functions array not initialized");
duk_pop(m_ctx); // pop [undefined] sitting at stack top
return false;
}
// Get function at index `id` from functions array, put it at stack top
if (!duk_get_prop_index(m_ctx, -1, id)) {
LOGE("EvalFilterFn - function %d not set", id);
duk_pop(m_ctx); // pop "undefined" sitting at stack top
duk_pop(m_ctx); // pop functions (array) now sitting at stack top
return false;
}
// pop fns array
duk_remove(m_ctx, -2);
// call popped function (sitting at stack top), evaluated value is put on stack top
if (duk_pcall(m_ctx, 0) != 0) {
LOGE("EvalFilterFn: %s", duk_safe_to_string(m_ctx, -1));
duk_pop(m_ctx);
return false;
}
return true;
}
示例8: duk_push_array
bool StyleContext::setFunctions(const std::vector<std::string>& _functions) {
auto arr_idx = duk_push_array(m_ctx);
int id = 0;
bool ok = true;
for (auto& function : _functions) {
duk_push_string(m_ctx, function.c_str());
duk_push_string(m_ctx, "");
if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) {
duk_put_prop_index(m_ctx, arr_idx, id);
} else {
LOGW("Compile failed: %s\n%s\n---",
duk_safe_to_string(m_ctx, -1),
function.c_str());
duk_pop(m_ctx);
ok = false;
}
id++;
}
if (!duk_put_global_string(m_ctx, FUNC_ID)) {
LOGE("'fns' object not set");
}
DUMP("setFunctions\n");
return ok;
}
示例9: Eval_Cmd
/*
* Evaluate a string as Duktape code in the selected heap.
* Usage: eval token code
* Return value: the result of the evaluation coerced to string.
* Side effects: may change the Duktape interpreter heap.
*/
static int
Eval_Cmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
duk_context *ctx;
duk_int_t duk_result;
const char *js_code;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, USAGE_EVAL);
return TCL_ERROR;
}
ctx = parse_id(cdata, interp, objv[1], 0);
if (ctx == NULL) {
return TCL_ERROR;
}
js_code = Tcl_GetString(objv[2]);
duk_result = duk_peval_string(ctx, js_code);
Tcl_SetObjResult(interp,
Tcl_NewStringObj(
duk_safe_to_string(ctx, -1), -1));
duk_pop(ctx);
if (duk_result == 0) {
return TCL_OK;
} else {
return TCL_ERROR;
}
}
示例10: duk_push_context_dump
DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) {
duk_idx_t idx;
duk_idx_t top;
/* We don't duk_require_stack() here now, but rely on the caller having
* enough space.
*/
top = duk_get_top(ctx);
duk_push_array(ctx);
for (idx = 0; idx < top; idx++) {
duk_dup(ctx, idx);
duk_put_prop_index(ctx, -2, idx);
}
/* XXX: conversion errors should not propagate outwards.
* Perhaps values need to be coerced individually?
*/
duk_bi_json_stringify_helper(ctx,
duk_get_top_index(ctx), /*idx_value*/
DUK_INVALID_INDEX, /*idx_replacer*/
DUK_INVALID_INDEX, /*idx_space*/
DUK_JSON_FLAG_EXT_CUSTOM |
DUK_JSON_FLAG_ASCII_ONLY |
DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
duk_push_sprintf(ctx, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(ctx, -1));
duk_replace(ctx, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */
duk_pop(ctx);
DUK_ASSERT(duk_is_string(ctx, -1));
}
示例11: 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;
}
示例12: test_basic
/* Basic dump/load test which exercises all constant types and has an inner
* function. Hex dumping the bytecode provides an exact test case dependency
* to the dump format so that any accidental changes break the test.
*/
static duk_ret_t test_basic(duk_context *ctx) {
unsigned char *p;
duk_size_t i, sz;
/* Integer constants generate LDINT now so also use a fractional
* constant to exercise number constants.
*
* Compiled as a program; force source filename.
*/
duk_push_string(ctx, "print('hello', (function adder(x,y) { return x+y; })(1, 2), 3.14);");
duk_push_string(ctx, "fakeFilename.js");
duk_compile(ctx, 0);
duk_dump_function(ctx);
printf("dump result type: %d\n", (int) duk_get_type(ctx, -1));
fflush(stdout);
p = (unsigned char *) duk_get_buffer(ctx, -1, &sz);
for (i = 0; i < sz; i++) {
printf("%02x", (int) p[i]);
}
printf("\n");
fflush(stdout);
/* Load test function from dump and execute it. */
duk_load_function(ctx);
printf("load result type: %d\n", (int) duk_get_type(ctx, -1));
fflush(stdout);
duk_call(ctx, 0);
printf("call result type: %d\n", (int) duk_get_type(ctx, -1));
fflush(stdout);
printf("call result: %s\n", duk_safe_to_string(ctx, -1));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例13: duk_push_global_stash
Variant DuktapeScriptFunction::call(const std::vector<Variant> & args)
{
// Get function wrapper from stash
duk_push_global_stash(m_context);
duk_get_prop_index(m_context, -1, m_stashIndex);
// Push arguments
for (Variant var : args)
{
m_scriptBackend->pushToDukStack(var);
}
// Call function
duk_int_t error = duk_pcall(m_context, args.size());
// Check for error
if (error)
{
// Raise script exception
m_scriptBackend->m_scriptContext->scriptException(std::string(duk_safe_to_string(m_context, -1)));
duk_pop_2(m_context);
return Variant();
}
// Convert return value
Variant value = m_scriptBackend->fromDukStack(-1);
duk_pop_2(m_context);
return value;
}
示例14: test_internal_prototype_lost
/* Custom internal prototype is lost during a dump/load. */
static duk_ret_t test_internal_prototype_lost(duk_context *ctx) {
duk_eval_string(ctx,
"(function () {\n"
" var f = function test() {};\n"
" Object.setPrototypeOf(f, { myProperty: 'myValue' });\n"
" print(f.myProperty);\n"
" return f;\n"
"})()");
duk_dump_function(ctx);
duk_load_function(ctx);
duk_get_prop_string(ctx, -1, "myProperty");
printf("myProperty: %s\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
duk_get_prototype(ctx, -1);
duk_eval_string(ctx, "Function.prototype");
printf("f prototype is Function.prototype: %d\n", (int) duk_strict_equals(ctx, -1, -2));
duk_pop_2(ctx);
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例15: duk_push_array
void StyleContext::initFunctions(const Scene& _scene) {
if (_scene.id == m_sceneId) {
return;
}
m_sceneId = _scene.id;
auto arr_idx = duk_push_array(m_ctx);
int id = 0;
for (auto& function : _scene.functions()) {
//logMsg("compile '%s'\n", function.c_str());
duk_push_string(m_ctx, function.c_str());
duk_push_string(m_ctx, "");
if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) {
duk_put_prop_index(m_ctx, arr_idx, id);
} else {
logMsg("Error: compile failed: %s\n", duk_safe_to_string(m_ctx, -1));
duk_pop(m_ctx);
}
id++;
}
if (!duk_put_global_string(m_ctx, FUNC_ID)) {
logMsg("Error: 'fns' object not set\n");
}
DUMP("setScene - %d functions\n", id);
}