Browse Source

api doc and test for duk_push_current_function

pull/1/head
Sami Vaarala 11 years ago
parent
commit
b7d118045b
  1. 46
      api-testcases/test-push-current-function.c
  2. 17
      website/api/duk_push_current_function.txt

46
api-testcases/test-push-current-function.c

@ -0,0 +1,46 @@
/*===
no running function
type=1
duk_is_object: 0
basic case
my_func, top=1
type=6
duk_is_object: 1
final top: 2
rc=0, result='undefined'
===*/
int my_func(duk_context *ctx) {
printf("my_func, top=%d\n", duk_get_top(ctx));
duk_push_current_function(ctx);
printf("type=%d\n", duk_get_type(ctx, -1));
printf("duk_is_object: %d\n", duk_is_object(ctx, -1));
/* FIXME: get the underlying function and compare against 'my_func' */
printf("final top: %d\n", duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
int rc;
/* first test what happens when there is no running function */
printf("no running function\n");
duk_push_current_function(ctx);
printf("type=%d\n", duk_get_type(ctx, -1));
printf("duk_is_object: %d\n", duk_is_object(ctx, -1));
duk_pop(ctx);
/* then test the basic case */
printf("basic case\n");
duk_push_c_function(ctx, my_func, 1 /*nargs*/);
duk_push_int(ctx, 123);
rc = duk_pcall(ctx, 1, DUK_INVALID_INDEX);
printf("rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1));
duk_pop(ctx);
}

17
website/api/duk_push_current_function.txt

@ -0,0 +1,17 @@
=proto
void duk_push_current_function(duk_context *ctx);
=stack
[ ... ] -> [ ... func! ]
=summary
<p>Push the currently running function to the stack. The value pushed is
an Ecmascript Function object.</p>
<p>This function allows a C function to gain access to its function object.
Since multiple function objects can internally point to the same C function,
a function object is a convenient place for function parameterization and
can also act as an internal state stash.</p>
=example
duk_push_current_function(ctx);
Loading…
Cancel
Save