comparison molko-js/src/js-clock.c @ 189:a5436e15898d

molko-js: start Javascript bindings, continue #2503 @4h - Added basic API for the libcore library, - Added a window example.
author David Demelier <markand@malikania.fr>
date Fri, 06 Nov 2020 15:15:01 +0100
parents
children dd77bfb38df2
comparison
equal deleted inserted replaced
188:cbce9143bf87 189:a5436e15898d
1 /*
2 * js-clock.c -- track elapsed time (Javascript bindings)
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stdlib.h>
21
22 #include <duktape.h>
23
24 #include <core/alloc.h>
25 #include <core/clock.h>
26
27 #include "js.h"
28 #include "js-window.h"
29
30 #define SYMBOL DUK_HIDDEN_SYMBOL("molko::clock")
31
32 static struct clock *
33 js_clock_this(duk_context *ctx)
34 {
35 struct clock *clock;
36
37 duk_push_this(ctx);
38 duk_get_prop_string(ctx, -1, SYMBOL);
39 clock = duk_to_pointer(ctx, -1);
40 duk_pop_n(ctx, 2);
41
42 if (!clock)
43 duk_error(ctx, DUK_ERR_TYPE_ERROR, "Not a Clock object");
44
45 return clock;
46 }
47
48 static duk_ret_t
49 js_clock_getElapsed(duk_context *ctx)
50 {
51 duk_push_uint(ctx, clock_elapsed(js_clock_this(ctx)));
52
53 return 1;
54 }
55
56 static duk_ret_t
57 js_clock_new(duk_context *ctx)
58 {
59 if (!duk_is_constructor_call(ctx))
60 duk_error(ctx, DUK_ERR_TYPE_ERROR, "Clock must be new-constructed");
61
62 duk_push_this(ctx);
63 duk_push_pointer(ctx, alloc_zero(1, sizeof (struct clock)));
64 duk_put_prop_string(ctx, -2, SYMBOL);
65 duk_push_string(ctx, "elapsed");
66 duk_push_c_function(ctx, js_clock_getElapsed, 0);
67 duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_GETTER);
68 duk_pop(ctx);
69
70 return 0;
71 }
72
73 static duk_ret_t
74 js_clock_start(duk_context *ctx)
75 {
76 clock_start(js_clock_this(ctx));
77
78 return 0;
79 }
80
81 static duk_ret_t
82 js_clock_finish(duk_context *ctx)
83 {
84 duk_get_prop_string(ctx, 0, SYMBOL);
85 free(duk_to_pointer(ctx, -1));
86 duk_pop(ctx);
87 duk_del_prop_string(ctx, 0, SYMBOL);
88
89 return 0;
90 }
91
92 static const duk_function_list_entry methods[] = {
93 { "start", js_clock_start, 0 },
94 { NULL, NULL, 0 }
95 };
96
97 void
98 js_clock_load(struct js *js)
99 {
100 assert(js);
101
102 duk_push_global_object(js->handle);
103 duk_get_prop_string(js->handle, -1, "Molko");
104 duk_push_c_function(js->handle, js_clock_new, 0);
105 duk_push_object(js->handle);
106 duk_put_function_list(js->handle, -1, methods);
107 duk_push_c_function(js->handle, js_clock_finish, 1);
108 duk_set_finalizer(js->handle, -2);
109 duk_put_prop_string(js->handle, -2, "prototype");
110 duk_put_prop_string(js->handle, -2, "Clock");
111 duk_pop_n(js->handle, 2);
112 }