mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
11 years ago
5 changed files with 36 additions and 43 deletions
@ -1,6 +1,37 @@ |
|||
#include <stdio.h> |
|||
/*
|
|||
* Very simple example program |
|||
*/ |
|||
|
|||
#include "duktape.h" |
|||
|
|||
int adder(duk_context *ctx) { |
|||
int i; |
|||
int n = duk_get_top(ctx); /* #args */ |
|||
double res = 0.0; |
|||
|
|||
for (i = 0; i < n; i++) { |
|||
res += duk_to_number(ctx, i); |
|||
} |
|||
|
|||
duk_push_number(ctx, res); |
|||
return 1; /* one return value */ |
|||
} |
|||
|
|||
int main(int argc, char *argv[]) { |
|||
/* FIXME */ |
|||
duk_context *ctx = duk_create_heap_default(); |
|||
|
|||
duk_eval_string(ctx, "print('Hello world!');"); |
|||
|
|||
duk_push_global_object(ctx); |
|||
duk_push_c_function(ctx, adder, DUK_VARARGS); |
|||
duk_put_prop_string(ctx, -2, "adder"); |
|||
duk_pop(ctx); /* pop global */ |
|||
|
|||
duk_eval_string(ctx, "print('2+3=' + adder(2, 3));"); |
|||
duk_pop(ctx); /* pop eval result */ |
|||
|
|||
duk_destroy_heap(ctx); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
|
@ -1,37 +0,0 @@ |
|||
/*
|
|||
* Very simple example program |
|||
*/ |
|||
|
|||
#include "duktape.h" |
|||
|
|||
int adder(duk_context *ctx) { |
|||
int i; |
|||
int n = duk_get_top(ctx); /* #args */ |
|||
double res = 0.0; |
|||
|
|||
for (i = 0; i < n; i++) { |
|||
res += duk_to_number(ctx, i); |
|||
} |
|||
|
|||
duk_push_number(ctx, res); |
|||
return 1; /* one return value */ |
|||
} |
|||
|
|||
int main(int argc, char *argv[]) { |
|||
duk_context *ctx = duk_create_heap_default(); |
|||
|
|||
duk_eval_string(ctx, "print('Hello world!');"); |
|||
|
|||
duk_push_global_object(ctx); |
|||
duk_push_c_function(ctx, adder, DUK_VARARGS); |
|||
duk_put_prop_string(ctx, -2, "adder"); |
|||
duk_pop(ctx); /* pop global */ |
|||
|
|||
duk_eval_string(ctx, "print('2+3=' + adder(2, 3));"); |
|||
duk_pop(ctx); /* pop eval result */ |
|||
|
|||
duk_destroy_heap(ctx); |
|||
|
|||
return 0; |
|||
} |
|||
|
Loading…
Reference in new issue