mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
11 years ago
2 changed files with 96 additions and 0 deletions
@ -0,0 +1,35 @@ |
|||
/*===
|
|||
program code |
|||
return value is: '123' |
|||
compile rc=0 |
|||
program code |
|||
return value is: '123' |
|||
compile rc=1 -> SyntaxError: invalid object literal (line 1) |
|||
top: 0 |
|||
===*/ |
|||
|
|||
void test(duk_context *ctx) { |
|||
int rc; |
|||
const char *src1 = "print('program code'); 123"; |
|||
const char *src2 = "print('program code'); 123; obj={"; |
|||
|
|||
/* Normal compile */ |
|||
duk_compile_userbuffer(ctx, 0, src1, strlen(src1)); |
|||
duk_call(ctx, 0); |
|||
printf("return value is: '%s'\n", duk_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
/* Protected compile, success */ |
|||
rc = duk_pcompile_userbuffer(ctx, 0, src1, strlen(src1)); |
|||
printf("compile rc=%d\n", rc); |
|||
duk_call(ctx, 0); |
|||
printf("return value is: '%s'\n", duk_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
/* Protected compile, syntax error */ |
|||
rc = duk_pcompile_userbuffer(ctx, 0, src2, strlen(src2)); |
|||
printf("compile rc=%d -> %s\n", rc, duk_safe_to_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
printf("top: %d\n", duk_get_top(ctx)); |
|||
} |
@ -0,0 +1,61 @@ |
|||
/*===
|
|||
Hello world! |
|||
return value is: 123.000000 |
|||
result is: 'TESTSTRING' |
|||
Hello world! |
|||
return value is: 123 (rc=0) |
|||
return value is: Error: eval error (rc=1) |
|||
return value is: SyntaxError: invalid object literal (line 1) (rc=1) |
|||
top=0 |
|||
Hello world! |
|||
top=0 |
|||
Hello world! |
|||
no result, rc=0 |
|||
top=0 |
|||
no result, rc=1 |
|||
top: 0 |
|||
===*/ |
|||
|
|||
void test(duk_context *ctx) { |
|||
int rc; |
|||
const char *src1 = "print('Hello world!'); 123;"; |
|||
const char *src2 = "'testString'.toUpperCase()"; |
|||
const char *src3 = "throw new Error('eval error');"; |
|||
const char *src4 = "throw new Error('eval error'); obj = {"; |
|||
const char *src5 = "print('Hello world!'); obj = {"; |
|||
|
|||
duk_eval_userbuffer(ctx, src1, strlen(src1)); |
|||
printf("return value is: %lf\n", duk_get_number(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
duk_eval_userbuffer(ctx, src2, strlen(src2)); |
|||
printf("result is: '%s'\n", duk_get_string(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
rc = duk_peval_userbuffer(ctx, src1, strlen(src1)); |
|||
printf("return value is: %s (rc=%d)\n", duk_safe_to_string(ctx, -1), rc); |
|||
duk_pop(ctx); |
|||
|
|||
rc = duk_peval_userbuffer(ctx, src3, strlen(src3)); |
|||
printf("return value is: %s (rc=%d)\n", duk_safe_to_string(ctx, -1), rc); |
|||
duk_pop(ctx); |
|||
|
|||
rc = duk_peval_userbuffer(ctx, src4, strlen(src4)); |
|||
printf("return value is: %s (rc=%d)\n", duk_safe_to_string(ctx, -1), rc); |
|||
duk_pop(ctx); |
|||
|
|||
/* noresult variants */ |
|||
|
|||
printf("top=%d\n", duk_get_top(ctx)); |
|||
duk_eval_userbuffer_noresult(ctx, src1, strlen(src1)); |
|||
|
|||
printf("top=%d\n", duk_get_top(ctx)); |
|||
rc = duk_peval_userbuffer_noresult(ctx, src1, strlen(src1)); |
|||
printf("no result, rc=%d\n", rc); |
|||
|
|||
printf("top=%d\n", duk_get_top(ctx)); |
|||
rc = duk_peval_userbuffer_noresult(ctx, src5, strlen(src5)); |
|||
printf("no result, rc=%d\n", rc); |
|||
|
|||
printf("top: %d\n", duk_get_top(ctx)); |
|||
} |
Loading…
Reference in new issue