本文整理汇总了C++中duk_call函数的典型用法代码示例。如果您正苦于以下问题:C++ duk_call函数的具体用法?C++ duk_call怎么用?C++ duk_call使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了duk_call函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_arguments_object
/* Arguments object still works after dump/load, relies on e.g. _Formals. */
static duk_ret_t test_arguments_object(duk_context *ctx) {
duk_eval_string(ctx,
"(function () {\n"
" var f = function test(x,y) {\n"
" print(typeof arguments, Object.prototype.toString.call(arguments));\n"
" print(arguments[0], arguments[1]);\n"
" arguments[0] = 123;\n"
" print(x, y);\n"
" };\n"
" return f;\n"
"})()");
duk_dup_top(ctx);
duk_push_string(ctx, "foo");
duk_push_string(ctx, "bar");
duk_call(ctx, 2);
duk_pop(ctx);
duk_dump_function(ctx);
duk_load_function(ctx);
duk_push_string(ctx, "foo");
duk_push_string(ctx, "bar");
duk_call(ctx, 2);
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例2: test_uncovered
/* It's not an error for the underlying plain buffer to be too small to
* cover the slice. This is allowed because it may happen for dynamic
* and external buffers at run time anyway. In any case, no memory
* unsafe behavior happens.
*/
static duk_ret_t test_uncovered(duk_context *ctx, void *udata) {
(void) udata;
duk_push_fixed_buffer(ctx, 256);
duk_push_buffer_object(ctx, -1, 7, 512, DUK_BUFOBJ_UINT32ARRAY);
duk_eval_string(ctx, "dumpBufferInfo");
duk_dup(ctx, -2);
duk_call(ctx, 1);
duk_pop(ctx);
duk_eval_string(ctx,
"(function (v) {\n"
" for (var i = 0; i < v.length; i++) { v[i] = 123; }\n"
" for (var i = 0; i < v.length; i++) { var ignore = v[i]; }\n"
"})");
duk_dup(ctx, -2);
duk_call(ctx, 1);
duk_pop(ctx);
duk_pop_n(ctx, 2);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例3: test_strict
/* Strictness status is preserved. */
static duk_ret_t test_strict(duk_context *ctx) {
duk_compile_string(ctx, DUK_COMPILE_FUNCTION,
"function () {\n"
" var strict = (function () { return !this; })();\n"
" print('strict: ' + strict);\n"
"}");
duk_dump_function(ctx);
duk_load_function(ctx);
duk_call(ctx, 0);
duk_pop(ctx);
duk_compile_string(ctx, DUK_COMPILE_FUNCTION,
"function () {\n"
" 'use strict';\n"
" var strict = (function () { return !this; })();\n"
" print('strict: ' + strict);\n"
"}");
duk_dump_function(ctx);
duk_load_function(ctx);
duk_call(ctx, 0);
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例4: test_eval_code
/* Test serialization of Eval (instead of function) code:
*
* - Bindings established via Eval code are configurable; bindings
* established from program and function code are not. (E5 Section
* 10.5 step 2).
*
* Here we bytecode dump an eval function that is then loaded and executed
* in the global scope.
*/
static duk_ret_t test_eval_code(duk_context *ctx) {
/* Demonstrate behavior without dump/load. */
duk_compile_string(ctx, DUK_COMPILE_EVAL,
"var testEval1 = 123;"
);
duk_call(ctx, 0);
duk_pop(ctx);
duk_eval_string_noresult(ctx,
"print(JSON.stringify(Object.getOwnPropertyDescriptor(this, 'testEval1')));"
);
/* Same with dump/load. */
duk_compile_string(ctx, DUK_COMPILE_EVAL,
"var testEval2 = 234;"
);
duk_dump_function(ctx);
duk_load_function(ctx);
duk_call(ctx, 0);
duk_pop(ctx);
duk_eval_string_noresult(ctx,
"print(JSON.stringify(Object.getOwnPropertyDescriptor(this, 'testEval2')));"
);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例5: test_varmap
/* _Varmap is preserved if function needs it. */
static duk_ret_t test_varmap(duk_context *ctx) {
/* Get access to _Varmap by creating a function that provides
* an 'eval service' in a function scope.
*/
duk_compile_string(ctx, DUK_COMPILE_FUNCTION,
"function (code) {\n"
" var foo = 123;\n"
" eval(code);\n"
"}");
duk_dump_function(ctx);
duk_load_function(ctx);
duk_dup(ctx, -1);
duk_push_string(ctx, "print('hello world');");
duk_call(ctx, 1);
duk_pop(ctx);
/* Eval code will use GETVAR to read 'foo', and _Varmap is
* needed for that.
*/
duk_dup(ctx, -1);
duk_push_string(ctx, "print(foo);");
duk_call(ctx, 1);
duk_pop(ctx);
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例6: test_name_binding_funcexpr
/* Name binding for function expressions is preserved, it is important
* for recursive functions.
*/
static duk_ret_t test_name_binding_funcexpr(duk_context *ctx) {
duk_eval_string(ctx,
"(function () {\n"
" var f = function test() { print('i am a ' + typeof test); };\n"
" return f;\n"
"})()");
duk_dup_top(ctx);
duk_call(ctx, 0); /* undumped */
duk_pop(ctx);
duk_dump_function(ctx);
duk_load_function(ctx);
duk_call(ctx, 0); /* dump/load */
duk_pop(ctx);
duk_eval_string(ctx,
"(function () {\n"
" var f = function test(n) { print(n); if (n > 0) { test(n - 1); } };\n"
" return f;\n"
"})()");
duk_dup_top(ctx);
duk_push_int(ctx, 5);
duk_call(ctx, 1); /* undumped */
duk_pop(ctx);
duk_dump_function(ctx);
duk_load_function(ctx);
duk_push_int(ctx, 7);
duk_call(ctx, 1); /* dump/load */
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例7: duk__eval_module_source
static duk_int_t duk__eval_module_source(duk_context *ctx, void *udata) {
#else
static duk_int_t duk__eval_module_source(duk_context *ctx) {
#endif
/*
* Stack: [ ... module source ]
*/
#if DUK_VERSION >= 19999
(void) udata;
#endif
/* Wrap the module code in a function expression. This is the simplest
* way to implement CommonJS closure semantics and matches the behavior of
* e.g. Node.js.
*/
duk_push_string(ctx, "(function(exports,require,module,__filename,__dirname){");
duk_dup(ctx, -2); /* source */
duk_push_string(ctx, "})");
duk_concat(ctx, 3);
/* [ ... module source func_src ] */
(void) duk_get_prop_string(ctx, -3, "filename");
duk_compile(ctx, DUK_COMPILE_EVAL);
duk_call(ctx, 0);
/* [ ... module source func ] */
/* Set name for the wrapper function. */
duk_push_string(ctx, "name");
duk_push_string(ctx, "main");
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);
/* call the function wrapper */
(void) duk_get_prop_string(ctx, -3, "exports"); /* exports */
(void) duk_get_prop_string(ctx, -4, "require"); /* require */
duk_dup(ctx, -5); /* module */
(void) duk_get_prop_string(ctx, -6, "filename"); /* __filename */
duk_push_undefined(ctx); /* __dirname */
duk_call(ctx, 5);
/* [ ... module source result(ignore) ] */
/* module.loaded = true */
duk_push_true(ctx);
duk_put_prop_string(ctx, -4, "loaded");
/* [ ... module source retval ] */
duk_pop_2(ctx);
/* [ ... module ] */
return 1;
}
示例8: havefile1_helper_2
static duk_ret_t havefile1_helper_2(duk_context *ctx) {
duk_push_string(ctx,
"(function () {\n"
" var fn = function haveFileName(v) { v(); return 123; };\n"
" return fn;\n"
"})()");
duk_push_string(ctx, "dummy_filename.js");
duk_compile(ctx, DUK_COMPILE_EVAL);
duk_call(ctx, 0);
duk_push_c_function(ctx, target_func_hack, 0);
duk_call(ctx, 1);
return 0;
}
示例9: test
void test(duk_context *ctx) {
duk_idx_t i, funcidx, argcount;
duk_ret_t rc;
/* test C function arg count variants */
duk_push_c_function(ctx, my_int_adder, 0); /* [0] = c func with 0 args */
duk_push_c_function(ctx, my_int_adder, 1); /* [1] = c func with 1 arg */
duk_push_c_function(ctx, my_int_adder, 2); /* [2] = c func with 2 args */
duk_push_c_function(ctx, my_int_adder, DUK_VARARGS); /* [3] = c func with varargs */
for (funcidx = 0; funcidx < 4; funcidx++) {
for (argcount = 0; argcount < 5; argcount++) {
duk_dup(ctx, funcidx);
for (i = 0; i < argcount; i++) {
duk_push_int(ctx, i + 1); /* 1, 2, 3, ... */
}
/* [ ... func <args> ] */
duk_call(ctx, argcount);
printf("funcidx=%ld, argcount=%ld -> result=%ld\n",
(long) funcidx, (long) argcount, (long) duk_to_int(ctx, -1));
duk_pop(ctx);
}
}
/* test C function return value 0 and negative */
duk_set_top(ctx, 0);
duk_push_c_function(ctx, my_zero_ret, 0);
duk_call(ctx, 0);
printf("top after calling my_zero_ret: %ld, retval='%s'\n",
(long) duk_get_top(ctx), duk_to_string(ctx, -1));
duk_pop(ctx);
duk_set_top(ctx, 0);
duk_push_c_function(ctx, my_neg_ret, 0);
rc = duk_pcall(ctx, 0);
printf("top after calling my_neg_ret: %ld, rc=%d, retval='%s'\n",
(long) duk_get_top(ctx), (int) rc, duk_to_string(ctx, -1));
duk_pop(ctx);
duk_set_top(ctx, 0);
duk_push_c_function(ctx, my_type_error_ret, 0);
rc = duk_pcall(ctx, 0);
printf("top after calling my_type_error_ret: %ld, rc=%d, retval='%s'\n",
(long) duk_get_top(ctx), (int) rc, duk_to_string(ctx, -1));
duk_pop(ctx);
}
示例10: test_mandel
/* Dump/load mandelbrot. No inner functions but a bit more code. */
static duk_ret_t test_mandel(duk_context *ctx) {
unsigned char *p;
duk_size_t i, sz;
printf("Mandelbrot source length: %ld\n", (long) strlen(MANDELBROT));
/* Compiled as a function; force source filename. */
duk_push_string(ctx, MANDELBROT);
duk_push_string(ctx, "mandel.js");
duk_compile(ctx, DUK_COMPILE_FUNCTION);
duk_dump_function(ctx);
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);
duk_call(ctx, 0);
printf("call result: %s\n", duk_safe_to_string(ctx, -1));
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
示例11: duk__console_log_helper
static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) {
duk_idx_t i, n;
n = duk_get_top(ctx);
duk_get_global_string(ctx, "console");
duk_get_prop_string(ctx, -1, "format");
for (i = 0; i < n; i++) {
if (duk_check_type_mask(ctx, i, DUK_TYPE_MASK_OBJECT)) {
/* Slow path formatting. */
duk_dup(ctx, -1); /* console.format */
duk_dup(ctx, i);
duk_call(ctx, 1);
duk_replace(ctx, i); /* arg[i] = console.format(arg[i]); */
}
}
duk_pop_2(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, n);
if (error_name) {
duk_push_error_object(ctx, DUK_ERR_ERROR, "%s", duk_require_string(ctx, -1));
duk_push_string(ctx, "name");
duk_push_string(ctx, error_name);
duk_def_prop(ctx, -3, DUK_DEFPROP_FORCE | DUK_DEFPROP_HAVE_VALUE); /* to get e.g. 'Trace: 1 2 3' */
duk_get_prop_string(ctx, -1, "stack");
}
printf("%s\n", duk_to_string(ctx, -1));
return 0;
}
示例12: test_modsearch_module
static duk_ret_t test_modsearch_module(duk_context *ignored_ctx, void *udata) {
duk_context *ctx;
(void) udata;
ctx = duk_create_heap_default();
if (!ctx) {
printf("Failed to create heap\n");
return 0;
}
/* Dummy print() binding. */
duk_push_c_function(ctx, my_print, 1);
duk_put_global_string(ctx, "print");
/* Register Duktape.modSearch. */
duk_eval_string(ctx, "(function (fun) { Duktape.modSearch = fun; })");
duk_push_c_function(ctx, my_modsearch, 4 /*nargs*/);
duk_call(ctx, 1);
duk_pop(ctx);
/* Require test. */
duk_eval_string_noresult(ctx, "var mod = require('my_module'); print(mod.FLAG_FOO); mod.func1();");
printf("final top: %ld\n", (long) duk_get_top(ctx));
duk_destroy_heap(ctx);
return 0;
}
示例13: _gum_duk_create_subclass
void
_gum_duk_create_subclass (duk_context * ctx,
const gchar * parent,
const gchar * name,
duk_c_function constructor,
gint constructor_nargs,
duk_c_function finalize)
{
duk_push_global_object (ctx);
duk_get_prop_string (ctx, -1, "Object");
duk_get_prop_string (ctx, -1, "create");
duk_get_prop_string (ctx, -3, parent);
duk_get_prop_string (ctx, -1, "prototype");
duk_dup (ctx, -3);
duk_dup (ctx, -2);
duk_call (ctx, 1);
if (constructor != NULL)
duk_push_c_function (ctx, constructor, constructor_nargs);
else
duk_push_object (ctx);
duk_dup (ctx, -2);
if (finalize != NULL)
{
duk_push_c_function (ctx, finalize, 2);
duk_set_finalizer (ctx, -2);
}
duk_put_prop_string (ctx, -2, "prototype");
duk_put_prop_string (ctx, -7, name);
duk_pop_n (ctx, 6);
}
示例14: duv_resolve
// Assumes nargs are the top of the stack. Rest comes from request
// Return value is not left on the stack.
void duv_resolve(uv_req_t* req, int nargs) {
duk_context *ctx = req->data;
duv_push_handle(ctx, req);
// stack: args... obj
duk_get_prop_string(ctx, -1, "\xff""uv-callback");
// stack: args... obj callback
duk_del_prop_string(ctx, -2, "\xff""uv-callback");
// stack: args... obj callback
if (!duk_is_function(ctx, -1)) {
// stack: args... obj callback
duk_pop_n(ctx, 2 + nargs);
return;
}
duk_remove(ctx, -2);
// stack: args... callback
duk_insert(ctx, -(nargs + 1));
// stack: callback args...
duk_call(ctx, nargs);
// stack: result
duk_pop(ctx);
// Remove the request from the GC roots
duv_remove_handle(ctx, req);
}
示例15: test_1
static duk_ret_t test_1(duk_context *ctx, void *udata) {
(void) udata;
duk_eval_string(ctx, "(function (x) { print(Duktape.enc('jx', x)); })");
duk_push_object(ctx);
/* Ordinary property */
duk_push_int(ctx, 1);
duk_put_prop_string(ctx, -2, "foo"); /* obj.foo = 1 */
/* Internal property \xFF\xFFabc, technically enumerable (based on
* property attributes) but because of internal property special
* behavior, does not enumerate.
*/
duk_push_int(ctx, 2);
duk_put_prop_string(ctx, -2, "\xff\xff" "abc"); /* obj[\xff\xffabc] = 2, internal property */
/* Another property with invalid UTF-8 data but doesn't begin with
* \xFF => gets enumerated and JX prints out an approximate key.
*/
duk_push_int(ctx, 3);
duk_put_prop_string(ctx, -2, " \xff" "bar"); /* obj[ \xffbar] = 3, invalid utf-8 but not an internal property */
duk_call(ctx, 1);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}