Skip to content

Commit eadac68

Browse files
committed
Merge remote-tracking branch 'upstream/dev' into dev
2 parents 8624c92 + a8192d8 commit eadac68

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

docs/gettingstarted/events.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,17 @@ int timer_main() {
4747
# Events
4848
In twr-wasm, certain APIs can trigger events. For example a timer can trigger a "timer complete" event, or an audio api my trigger a "file has finished playing" event. An event had an `id` and an associated callback. The `id` is an integer that identifies the event (`int event_id`). In order to receive an event call back:
4949
50-
1. Write your callback in C/C++. It must be C linkage, and you should be exported from your C code to JavaScript/TypeScript using the `export_name` clang `__attribute__` like this: `__attribute__((export_name("on_timer2")))`
51-
2. Register your callback. This will also allocate the event ID paired to this callback. For example: `int t2_eventid=twr_register_callback("on_timer2");`
50+
1. Write your callback in C/C++. It must be C linkage, and you should be exported from your C code to JavaScript/TypeScript using the `export_name` clang `__attribute__` like this: `__attribute__((export_name("on_timer2")))`. Replace `on_timer2` with your callback function name.
51+
2. Register your callback. This will also allocate the event ID paired to this callback. For example: `int t2_event_id=twr_register_callback("on_timer2");`
5252
3. Call an API that takes an event `id`. For example: `twr_timer_repeat(500, t2_eventid);`. This will call the `t2_event` callback every 500ms.
5353
54-
You can use the same event/callback with multiple APIs if you wish. When the event callback is called, the first argument will be the event `id` triggering the callback. There may then optional parameters. These are event specific.
54+
You can use the same event/callback with multiple APIs if you wish. When the event callback is called, the first argument will be the event `id` triggering the callback. There may then be optional parameters. These are event specific.
55+
56+
As in JavaScript, twr-wasm event callbacks only occur when your C/C++ code is not running.
5557
5658
# When using twrWasmModuleAsync
5759
58-
With a `twrWasmModuleAsync` module, various blocking APIs are available. For example: `twr_sleep`. When these functions are blocking (waiting), event callbacks are queued and not processed until wait unblocks.
60+
With a `twrWasmModuleAsync` module, various blocking APIs are available. For example: `twr_sleep`. When these functions are blocking (waiting), event callbacks are queued and not processed until your C functions return back to JavaScript.
5961
6062
With a `twrWasmModuleAsync` module, events are sent from the JavaScript main thread to the worker thread that is running the C/C++ code.
6163

examples/tests-timer/tests-timer.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ __attribute__((export_name("on_timer1")))
1010
void on_timer1(int event_id) {
1111
t1_count++;
1212
printf("timer callback 1 entered (event id=%d, count=%d)\n", event_id, t1_count);
13-
14-
if (t2_count!=5)
15-
printf("twr_timer_repeat FAIL!!\n");
16-
else
17-
printf("test run complete\n");
1813
}
1914

2015
// timer2 event callback (called multiple times)
@@ -25,34 +20,30 @@ void on_timer2(int event_id) {
2520

2621
if (t2_count==5) {
2722
twr_timer_cancel(t2_id);
28-
if (t1_count!=0)
23+
if (t1_count!=1)
2924
printf("twr_timer_single_shot FAIL!!\n");
25+
printf("timer test complete\n");
3026
}
3127
}
3228

3329
int tests_timer(int is_async) {
3430

3531
printf("starting timer tests.\n");
3632

33+
int t1_eventid=twr_register_callback("on_timer1");
34+
twr_timer_single_shot(2000, t1_eventid);
35+
3736
int t2_eventid=twr_register_callback("on_timer2");
3837
t2_id=twr_timer_repeat(500, t2_eventid);
39-
40-
int t1_eventid=twr_register_callback("on_timer1");
4138

4239
if (is_async) {
4340
printf("going to sleep...\n");
4441
twr_sleep(1000);
4542
printf("awake from sleep!\n");
46-
47-
if (t2_count!=2) {
48-
printf("FAIL!! t2_count is %d\n", t2_count);
4943
}
5044

51-
twr_timer_single_shot(2000, t1_eventid);
52-
}
53-
54-
else {
55-
twr_timer_single_shot(4000, t1_eventid);
45+
if (t2_count!=0 && t1_count!=0) {
46+
printf("FAIL!! t1_count is %d, t2 %d\n", t1_count, t2_count);
5647
}
5748

5849
return 0;

source/twr-ts/twreventqueue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ export class twrEventQueueReceive {
107107
const [eventID, args, index]=this.findEvent(filterEvent);
108108
// execute callbacks up to this filterEvent (so as to call them in order)
109109
// if filterEvent not found, index is undefined, which causes doCallbacks to execute all pendingEventIDs
110-
//this.doCallbacks(index); causes all kinds of problems. See notes in twrlibrary.ts
110+
// this call commented out so that the C events act like JavaScript events/callbacks (only called when main function finishes)
111+
// to consider: allow callbacks in sync blocking functions like sleep (that use await in their implementations)
112+
//this.doCallbacks(index);
111113
if (eventID && args) {
112114
return [eventID, args];
113115
}

source/twr-ts/twrlibrary.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@ import {twrEventQueueReceive} from "./twreventqueue.js"
77
/////////////////////////////////////////////////////////////////////
88

99
// TODO List
10-
// Working on: Allow callbacks while blocking call is waiting.
11-
// see commented out doCallbacks in WaitEvents
12-
// what if printf in callback and char waits for ret val?
13-
// that is what drawseq does
14-
// is animateframe being called before drawseq retval?
15-
// ie, recursive waitEvent
16-
// update doc, can't block in a callback?
17-
// finish test_timer
18-
10+
// Should we: Allow callbacks while blocking call is waiting? Not on "return value" waits, but on functions like sleep.
11+
// add a function to allow twrWasmModuleAsync c code (like main) to process events without returning to JavaScript
1912
// fix example/lib/out and .gitignore so correct build artifacts are checked in
2013
// resolve fact that libraries with interfaces are passed in the "io" option. Eg Allow "libs" or other synonym.
2114
// current implementation has no libs: (akin to io:).

0 commit comments

Comments
 (0)