]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* uppercase.c */ |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include "duktape.h" | |
5 | ||
6 | static int dummy_upper_case(duk_context *ctx) { | |
7 | size_t sz; | |
8 | const char *val = duk_require_lstring(ctx, 0, &sz); | |
9 | size_t i; | |
10 | ||
11 | /* We're going to need 'sz' additional entries on the stack. */ | |
12 | duk_require_stack(ctx, sz); | |
13 | ||
14 | for (i = 0; i < sz; i++) { | |
15 | char ch = val[i]; | |
16 | if (ch >= 'a' && ch <= 'z') { | |
17 | ch = ch - 'a' + 'A'; | |
18 | } | |
19 | duk_push_lstring(ctx, (const char *) &ch, 1); | |
20 | } | |
21 | ||
22 | duk_concat(ctx, sz); | |
23 | return 1; | |
24 | } | |
25 | ||
26 | int main(int argc, char *argv[]) { | |
27 | duk_context *ctx; | |
28 | ||
29 | if (argc < 2) { exit(1); } | |
30 | ||
31 | ctx = duk_create_heap_default(); | |
32 | if (!ctx) { exit(1); } | |
33 | ||
34 | duk_push_c_function(ctx, dummy_upper_case, 1); | |
35 | duk_push_string(ctx, argv[1]); | |
36 | duk_call(ctx, 1); | |
37 | printf("%s -> %s\n", argv[1], duk_to_string(ctx, -1)); | |
38 | duk_pop(ctx); | |
39 | ||
40 | duk_destroy_heap(ctx); | |
41 | return 0; | |
42 | } |