本文整理汇总了C++中duk_insert函数的典型用法代码示例。如果您正苦于以下问题:C++ duk_insert函数的具体用法?C++ duk_insert怎么用?C++ duk_insert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了duk_insert函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CallConnectSignal
void CallConnectSignal(duk_context* ctx, void* signal)
{
int numArgs = duk_get_top(ctx);
duk_push_number(ctx, (size_t)signal);
duk_insert(ctx, 0);
duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1, "_ConnectSignal");
duk_remove(ctx, -2); // Global object
duk_insert(ctx, 0);
duk_pcall(ctx, numArgs + 1);
duk_pop(ctx);
}
示例2: duk__call_errhandler
static void duk__call_errhandler(duk_hthread *thr) {
int call_flags;
int rc;
DUK_ASSERT(thr != NULL);
DUK_ASSERT(thr->heap != NULL);
if (!thr->heap->lj.errhandler) {
DUK_DDDPRINT("no errhandler, return");
return;
}
/* FIXME: assert/require for valstack space */
/* [ ... errval ] */
DUK_DDDPRINT("errhandler is %p", (void *) thr->heap->lj.errhandler);
DUK_DDDPRINT("errhandler dump: %!O", (duk_heaphdr *) thr->heap->lj.errhandler);
duk_push_hobject((duk_context *) thr, thr->heap->lj.errhandler);
duk_insert((duk_context *) thr, -2); /* -> [ ... errhandler errval ] */
duk_push_undefined((duk_context *) thr);
duk_insert((duk_context *) thr, -2); /* -> [ ... errhandler undefined(= this) errval ] */
/* [ ... errhandler undefined errval ] */
/*
* DUK_CALL_FLAG_IGNORE_RECLIMIT causes duk_handle_call() to ignore C
* recursion depth limit (and won't increase it either). This is
* dangerous, but useful because it allows an errhandler to run even
* if the original error is caused by C recursion depth limit. Because
* errhandler is NULL in the errhandler call, the errhandler call
* can't cause the same situation to occur again.
*
* We ignore errors now: a success return and an error value both
* replace the original error value. (This would be easy to change.)
*/
call_flags = DUK_CALL_FLAG_PROTECTED |
DUK_CALL_FLAG_IGNORE_RECLIMIT; /* protected, ignore reclimit, not constructor */
rc = duk_handle_call(thr,
1, /* num args */
call_flags, /* call_flags */
NULL); /* errhandler */
DUK_UNREF(rc); /* no need to check now: both success and error are OK */
/* [ ... errval ] */
}
示例3: duk_bi_object_getprototype_shared
/* Shared helper to implement Object.getPrototypeOf and the ES6
* Object.prototype.__proto__ getter.
*
* https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
*/
duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx) {
duk_hobject *h;
/* magic: 0=getter call, 1=Object.getPrototypeOf */
if (duk_get_current_magic(ctx) == 0) {
duk_push_this_coercible_to_object(ctx);
duk_insert(ctx, 0);
}
h = duk_require_hobject(ctx, 0);
DUK_ASSERT(h != NULL);
/* XXX: should the API call handle this directly, i.e. attempt
* to duk_push_hobject(ctx, null) would push a null instead?
* (On the other hand 'undefined' would be just as logical, but
* not wanted here.)
*/
if (h->prototype) {
duk_push_hobject(ctx, h->prototype);
} else {
duk_push_null(ctx);
}
return 1;
}
示例4: 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;
}
示例5: duk_bi_function_prototype_call
DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx) {
duk_idx_t nargs;
/* Step 1 is not necessary because duk_call_method() will take
* care of it.
*/
/* vararg function, thisArg needs special handling */
nargs = duk_get_top(ctx); /* = 1 + arg count */
if (nargs == 0) {
duk_push_undefined(ctx);
nargs++;
}
DUK_ASSERT(nargs >= 1);
/* [ thisArg arg1 ... argN ] */
duk_push_this(ctx); /* 'func' in the algorithm */
duk_insert(ctx, 0);
/* [ func thisArg arg1 ... argN ] */
DUK_DDD(DUK_DDDPRINT("func=%!iT, thisArg=%!iT, argcount=%ld, top=%ld",
(duk_tval *) duk_get_tval(ctx, 0),
(duk_tval *) duk_get_tval(ctx, 1),
(long) (nargs - 1),
(long) duk_get_top(ctx)));
duk_call_method(ctx, nargs - 1);
return 1;
}
示例6: duk_bi_object_setprototype_shared
/* Shared helper to implement ES6 Object.setPrototypeOf and
* Object.prototype.__proto__ setter.
*
* https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
* https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.setprototypeof
*/
duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hobject *h_obj;
duk_hobject *h_new_proto;
duk_hobject *h_curr;
duk_ret_t ret_success = 1; /* retval for success path */
/* Preliminaries for __proto__ and setPrototypeOf (E6 19.1.2.18 steps 1-4);
* magic: 0=setter call, 1=Object.setPrototypeOf
*/
if (duk_get_current_magic(ctx) == 0) {
duk_push_this_check_object_coercible(ctx);
duk_insert(ctx, 0);
if (!duk_check_type_mask(ctx, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT)) {
return 0;
}
/* __proto__ setter returns 'undefined' on success unlike the
* setPrototypeOf() call which returns the target object.
*/
ret_success = 0;
} else {
duk_require_object_coercible(ctx, 0);
duk_require_type_mask(ctx, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT);
}
h_obj = duk_get_hobject(ctx, 0);
if (!h_obj) {
goto skip;
}
h_new_proto = duk_get_hobject(ctx, 1);
DUK_ASSERT(h_obj != NULL);
/* h_new_proto may be NULL */
/* [[SetPrototypeOf]] standard behavior, E6 9.1.2 */
/* NOTE: steps 7-8 seem to be a cut-paste bug in the E6 draft */
/* TODO: implement Proxy object support here */
if (h_new_proto == h_obj->prototype) {
goto skip;
}
if (!DUK_HOBJECT_HAS_EXTENSIBLE(h_obj)) {
goto fail_nonextensible;
}
for (h_curr = h_new_proto; h_curr != NULL; h_curr = h_curr->prototype) {
/* Loop prevention */
if (h_curr == h_obj) {
goto fail_loop;
}
}
DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h_obj, h_new_proto);
/* fall thru */
skip:
duk_set_top(ctx, 1);
return ret_success;
fail_nonextensible:
fail_loop:
return DUK_RET_TYPE_ERROR;
}
示例7: duk_bi_array_prototype_to_string
DUK_INTERNAL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx) {
(void) duk_push_this_coercible_to_object(ctx);
duk_get_prop_stridx(ctx, -1, DUK_STRIDX_JOIN);
/* [ ... this func ] */
if (!duk_is_callable(ctx, -1)) {
/* Fall back to the initial (original) Object.toString(). We don't
* currently have pointers to the built-in functions, only the top
* level global objects (like "Array") so this is now done in a bit
* of a hacky manner. It would be cleaner to push the (original)
* function and use duk_call_method().
*/
/* XXX: 'this' will be ToObject() coerced twice, which is incorrect
* but should have no visible side effects.
*/
DUK_DDD(DUK_DDDPRINT("this.join is not callable, fall back to (original) Object.toString"));
duk_set_top(ctx, 0);
return duk_bi_object_prototype_to_string(ctx); /* has access to 'this' binding */
}
/* [ ... this func ] */
duk_insert(ctx, -2);
/* [ ... func this ] */
DUK_DDD(DUK_DDDPRINT("calling: func=%!iT, this=%!iT",
(duk_tval *) duk_get_tval(ctx, -2),
(duk_tval *) duk_get_tval(ctx, -1)));
duk_call_method(ctx, 0);
return 1;
}
示例8: my_print
static duk_ret_t my_print(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, duk_get_top(ctx) - 1);
printf("%s\n", duk_safe_to_string(ctx, -1));
return 0;
}
示例9: duk_bi_object_getprototype_shared
/* Shared helper to implement Object.getPrototypeOf and the ES6
* Object.prototype.__proto__ getter.
*
* https://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-object.prototype.__proto__
*/
DUK_INTERNAL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx) {
duk_hobject *h;
duk_hobject *proto;
/* magic: 0=getter call, 1=Object.getPrototypeOf */
if (duk_get_current_magic(ctx) == 0) {
duk_push_this_coercible_to_object(ctx);
duk_insert(ctx, 0);
}
h = duk_require_hobject_or_lfunc(ctx, 0);
/* h is NULL for lightfunc */
/* XXX: should the API call handle this directly, i.e. attempt
* to duk_push_hobject(ctx, null) would push a null instead?
* (On the other hand 'undefined' would be just as logical, but
* not wanted here.)
*/
if (h == NULL) {
duk_push_hobject_bidx(ctx, DUK_BIDX_FUNCTION_PROTOTYPE);
} else {
proto = DUK_HOBJECT_GET_PROTOTYPE(h);
if (proto) {
duk_push_hobject(ctx, proto);
} else {
duk_push_null(ctx);
}
}
return 1;
}
示例10: duv_mod_compile
// Given a module and js code, compile the code and execute as CJS module
// return the result of the compiled code ran as a function.
static duk_ret_t duv_mod_compile(duk_context *ctx) {
// Check the args
const duv_schema_entry schema[] = {
{ "code", dschema_is_data },
{ NULL, NULL }
};
dschema_check(ctx, schema);
duk_to_string(ctx, 0);
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, "id");
// Wrap the code
duk_push_string(ctx, "function(){var module=this,exports=this.exports,require=this.require.bind(this);");
duk_dup(ctx, 0);
duk_push_string(ctx, "}");
duk_concat(ctx, 3);
duk_insert(ctx, -2);
// Compile to a function
duk_compile(ctx, DUK_COMPILE_FUNCTION);
duk_push_this(ctx);
duk_call_method(ctx, 0);
return 1;
}
示例11: duk_bi_string_prototype_concat
DUK_INTERNAL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx) {
/* duk_concat() coerces arguments with ToString() in correct order */
(void) duk_push_this_coercible_to_string(ctx);
duk_insert(ctx, 0); /* this is relatively expensive */
duk_concat(ctx, duk_get_top(ctx));
return 1;
}
示例12: duk__call_prop_prep_stack
/* Prepare value stack for a method call through an object property.
* May currently throw an error e.g. when getting the property.
*/
DUK_LOCAL void duk__call_prop_prep_stack(duk_context *ctx, duk_idx_t normalized_obj_idx, duk_idx_t nargs) {
DUK_ASSERT_CTX_VALID(ctx);
DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_idx=%ld, nargs=%ld, stacktop=%ld",
(long) normalized_obj_idx, (long) nargs, (long) duk_get_top(ctx)));
/* [... key arg1 ... argN] */
/* duplicate key */
duk_dup(ctx, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */
duk_get_prop(ctx, normalized_obj_idx);
DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(ctx, -1)));
/* [... key arg1 ... argN func] */
duk_replace(ctx, -nargs - 2);
/* [... func arg1 ... argN] */
duk_dup(ctx, normalized_obj_idx);
duk_insert(ctx, -nargs - 1);
/* [... func this arg1 ... argN] */
}
示例13: 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);
}
示例14: duv_setup_request
duk_ret_t duv_setup_request(duk_context *ctx, uv_req_t* req, int callback) {
// Create a new container object for the request with request methods
duk_push_object(ctx);
duk_push_heap_stash(ctx);
duk_get_prop_string(ctx, -1, "req-prototype");
duk_remove(ctx, -2);
duk_set_prototype(ctx, -2);
// Set buffer as uv-data internal property.
duk_insert(ctx, -2);
duk_put_prop_string(ctx, -2, "\xff""uv-data");
// Store the request type.
duk_push_int(ctx, req->type);
duk_put_prop_string(ctx, -2, "\xff""req-type");
// Store a reference to the lua callback
duk_dup(ctx, callback);
duk_put_prop_string(ctx, -2, "\xff""uv-callback");
// Store this object in the heap stack keyed by the request's pointer address.
// This will prevent it from being garbage collected and allow us to find
// it with nothing more than the request's address.
duv_store_handle(ctx, req);
// Store the context in the handle so it can use duktape APIs.
req->data = ctx;
// TODO: is this still on the stack?
return 1;
}
示例15: duk_call
DUK_EXTERNAL void duk_call(duk_context *ctx, duk_idx_t nargs) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_small_uint_t call_flags;
duk_idx_t idx_func;
DUK_ASSERT_CTX_VALID(ctx);
DUK_ASSERT(thr != NULL);
idx_func = duk_get_top(ctx) - nargs - 1;
if (idx_func < 0 || nargs < 0) {
/* note that we can't reliably pop anything here */
DUK_ERROR_TYPE_INVALID_ARGS(thr);
}
/* XXX: awkward; we assume there is space for this, overwrite
* directly instead?
*/
duk_push_undefined(ctx);
duk_insert(ctx, idx_func + 1);
call_flags = 0; /* not protected, respect reclimit, not constructor */
duk_handle_call_unprotected(thr, /* thread */
nargs, /* num_stack_args */
call_flags); /* call_flags */
}