mirror of https://github.com/svaarala/duktape.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
67 lines
1.5 KiB
11 years ago
|
/*===
|
||
|
*** test_1
|
||
|
duk_require_c_function == my_func: 1
|
||
|
final top: 1
|
||
|
rc=0, result='undefined'
|
||
|
*** test_2
|
||
|
rc=1, result='TypeError: incorrect type, expected c function'
|
||
|
*** test_3
|
||
|
rc=1, result='TypeError: incorrect type, expected c function'
|
||
|
===*/
|
||
|
|
||
|
int my_func(duk_context *ctx) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int test_1(duk_context *ctx) {
|
||
|
duk_c_function funcptr;
|
||
|
|
||
|
duk_set_top(ctx, 0);
|
||
|
duk_push_c_function(ctx, my_func, 1 /*nargs*/);
|
||
|
funcptr = duk_require_c_function(ctx, -1);
|
||
|
printf("duk_require_c_function == my_func: %d\n", (funcptr == my_func ? 1 : 0));
|
||
|
|
||
|
printf("final top: %d\n", duk_get_top(ctx));
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int test_2(duk_context *ctx) {
|
||
|
duk_c_function funcptr;
|
||
|
|
||
|
duk_set_top(ctx, 0);
|
||
|
duk_push_c_function(ctx, my_func, 1 /*nargs*/);
|
||
|
funcptr = duk_require_c_function(ctx, 3);
|
||
|
printf("index 3 -> NULL: %d\n", (funcptr == NULL ? 1 : 0));
|
||
|
|
||
|
printf("final top: %d\n", duk_get_top(ctx));
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int test_3(duk_context *ctx) {
|
||
|
duk_c_function funcptr;
|
||
|
|
||
|
duk_set_top(ctx, 0);
|
||
|
duk_push_c_function(ctx, my_func, 1 /*nargs*/);
|
||
|
funcptr = duk_require_c_function(ctx, DUK_INVALID_INDEX);
|
||
|
printf("index DUK_INVALID_INDEX -> NULL: %d\n", (funcptr == NULL ? 1 : 0));
|
||
|
|
||
|
printf("final top: %d\n", duk_get_top(ctx));
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
#define TEST(func) do { \
|
||
|
printf("*** %s\n", #func); \
|
||
|
rc = duk_safe_call(ctx, (func), 0, 1, DUK_INVALID_INDEX); \
|
||
|
printf("rc=%d, result='%s'\n", rc, duk_to_string(ctx, -1)); \
|
||
|
duk_pop(ctx); \
|
||
|
} while(0)
|
||
|
|
||
|
void test(duk_context *ctx) {
|
||
|
int rc;
|
||
|
|
||
|
TEST(test_1);
|
||
|
TEST(test_2);
|
||
|
TEST(test_3);
|
||
|
}
|
||
|
|