]> git.proxmox.com Git - ceph.git/blame - ceph/src/civetweb/src/third_party/duktape-1.8.0/examples/guide/primecheck.c
import 12.2.13 release
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.8.0 / examples / guide / primecheck.c
CommitLineData
7c673cae
FG
1/* primecheck.c */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include "duktape.h"
6
7static duk_ret_t native_prime_check(duk_context *ctx) {
8 int val = duk_require_int(ctx, 0);
9 int lim = duk_require_int(ctx, 1);
10 int i;
11
12 for (i = 2; i <= lim; i++) {
13 if (val % i == 0) {
14 duk_push_false(ctx);
15 return 1;
16 }
17 }
18
19 duk_push_true(ctx);
20 return 1;
21}
22
23int main(int argc, const char *argv[]) {
24 duk_context *ctx = NULL;
25
26 ctx = duk_create_heap_default();
27 if (!ctx) {
28 printf("Failed to create a Duktape heap.\n");
29 exit(1);
30 }
31
32 duk_push_global_object(ctx);
33 duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/);
34 duk_put_prop_string(ctx, -2, "primeCheckNative");
35
36 if (duk_peval_file(ctx, "prime.js") != 0) {
37 printf("Error: %s\n", duk_safe_to_string(ctx, -1));
38 goto finished;
39 }
40 duk_pop(ctx); /* ignore result */
41
42 duk_get_prop_string(ctx, -1, "primeTest");
43 if (duk_pcall(ctx, 0) != 0) {
44 printf("Error: %s\n", duk_safe_to_string(ctx, -1));
45 }
46 duk_pop(ctx); /* ignore result */
47
48 finished:
49 duk_destroy_heap(ctx);
50
51 exit(0);
52}