summaryrefslogtreecommitdiffstats
path: root/test/src/test-c-machine.scxml.c
blob: b75c6a538d8892fc171eca4620f8e45473ebd531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
/**
  Generated from source:
  file:///Users/sradomski/Documents/TK/Code/uscxml/test/w3c/ecma/test144.scxml
*/

#ifndef USCXML_NO_STDTYPES_H
#  include <stdint.h> /* explicit types */
#endif
#include <stddef.h> /* NULL */

#ifndef USCXML_NO_GEN_C_MACROS

/**
 * All macros used for the scxml types and functions
 * 
 * ** IMPORTANT: Make sure to set the following macros prior to including. **
 *               They are used to represent the machine in the types to follow
 *               and to allocate stack memory during a micro-step function.
 *               When in doubt, overprovide.
 * 
 *    USCXML_NR_STATES_TYPE
 *      as the smallest type for positive integers that can contain the
 *      largest number of states from an individual state machine. E.g.:
 *      < 2^8  states => uint8_t
 *      < 2^16 states => uint16_t
 *      < 2^32 states => uint32_t
 */

#ifndef USCXML_NR_STATES_TYPE 
#  define USCXML_NR_STATES_TYPE uint8_t
#endif 

/**
 *    USCXML_NR_TRANS_TYPE
 *      the same as above but for the number of transitions.
 */

#ifndef USCXML_NR_TRANS_TYPE 
#  define USCXML_NR_TRANS_TYPE uint8_t
#endif 

/** 
 *    USCXML_MAX_NR_STATES_BYTES
 *      the smallest multiple of 8 that, if multiplied by 8,
 *      is larger than USCXML_NR_STATES_TYPE, e.g:
 *      1-8   states => 1
 *      9-16  states => 2
 *      17-24 states => 3
 *      25-32 states => 4
 *      ...
 */

#ifndef USCXML_MAX_NR_STATES_BYTES 
#  define USCXML_MAX_NR_STATES_BYTES 1
#endif 

/**
 *    USCXML_MAX_NR_TRANS_BYTES
 *      same as above but for transitions.
 */

#ifndef USCXML_MAX_NR_TRANS_BYTES 
#  define USCXML_MAX_NR_TRANS_BYTES 1
#endif 

/**
 *    USCXML_NUMBER_STATES / USCXML_NUMBER_TRANS
 *      Per default the number of states / transitions is retrieved from the machine
 *      info in the uscxml_ctx struct, but you can also hard-code it per macro.
 */

#ifndef USCXML_NUMBER_STATES
#  define USCXML_NUMBER_STATES (ctx->machine->nr_states)
#endif

#ifndef USCXML_NUMBER_TRANS
#  define USCXML_NUMBER_TRANS (ctx->machine->nr_transitions)
#endif

/**
 *    USCXML_GET_STATE / USCXML_GET_TRANS
 *      Per default an individual state or transitions is retrieved from the machine
 *      info in the uscxml_ctx struct, but you can also hard-code it per macro.
 */

#ifndef USCXML_GET_STATE
#  define USCXML_GET_STATE(i) (ctx->machine->states[i])
#endif

#ifndef USCXML_GET_TRANS
#  define USCXML_GET_TRANS(i) (ctx->machine->transitions[i])
#endif


/* Common macros below */

#define BIT_HAS(idx, bitset)   ((bitset[idx >> 3] &  (1 << (idx & 7))) != 0)
#define BIT_SET_AT(idx, bitset)  bitset[idx >> 3] |= (1 << (idx & 7));
#define BIT_CLEAR(idx, bitset)   bitset[idx >> 3] &= (1 << (idx & 7)) ^ 0xFF;

#ifdef __GNUC__
#  define likely(x)       (__builtin_expect(!!(x), 1))
#  define unlikely(x)     (__builtin_expect(!!(x), 0))
#else
#  define likely(x)       (x)
#  define unlikely(x)     (x)
#endif

/* error return codes */
#define USCXML_ERR_OK                0
#define USCXML_ERR_IDLE              1
#define USCXML_ERR_DONE              2
#define USCXML_ERR_MISSING_CALLBACK  3
#define USCXML_ERR_FOREACH_DONE      4
#define USCXML_ERR_EXEC_CONTENT      5
#define USCXML_ERR_INVALID_TARGET    6
#define USCXML_ERR_INVALID_TYPE      7
#define USCXML_ERR_UNSUPPORTED       8

#define USCXML_TRANS_SPONTANEOUS      0x01
#define USCXML_TRANS_TARGETLESS       0x02
#define USCXML_TRANS_INTERNAL         0x04
#define USCXML_TRANS_HISTORY          0x08
#define USCXML_TRANS_INITIAL          0x10

#define USCXML_STATE_ATOMIC           0x01
#define USCXML_STATE_PARALLEL         0x02
#define USCXML_STATE_COMPOUND         0x03
#define USCXML_STATE_FINAL            0x04
#define USCXML_STATE_HISTORY_DEEP     0x05
#define USCXML_STATE_HISTORY_SHALLOW  0x06
#define USCXML_STATE_INITIAL          0x07
#define USCXML_STATE_HAS_HISTORY      0x80  /* highest bit */
#define USCXML_STATE_MASK(t)     (t & 0x7F) /* mask highest bit */

#define USCXML_CTX_PRISTINE           0x00
#define USCXML_CTX_SPONTANEOUS        0x01
#define USCXML_CTX_INITIALIZED        0x02
#define USCXML_CTX_TOP_LEVEL_FINAL    0x04
#define USCXML_CTX_TRANSITION_FOUND   0x08
#define USCXML_CTX_FINISHED           0x10

#define USCXML_ELEM_DATA_IS_SET(data) (data->id != NULL)
#define USCXML_ELEM_DONEDATA_IS_SET(donedata) (donedata->content != NULL || donedata->contentexpr != NULL || donedata->params != NULL)
#define USCXML_ELEM_PARAM_IS_SET(param) (param->name != NULL)
#define USCXML_MACHINE_IS_SET(machine) (machine->nr_states > 0)

#define USCXML_NO_GEN_C_MACROS
#endif


#ifndef USCXML_NO_GEN_C_TYPES

/**
 * All types required to represent an SCXML state chart.
 * Just predefine the USCXML_NO_GEN_C_TYPES macro if you do not need them.
 */

typedef struct uscxml_machine uscxml_machine;
typedef struct uscxml_transition uscxml_transition;
typedef struct uscxml_state uscxml_state;
typedef struct uscxml_ctx uscxml_ctx;
typedef struct uscxml_elem_invoke uscxml_elem_invoke;

typedef struct uscxml_elem_send uscxml_elem_send;
typedef struct uscxml_elem_param uscxml_elem_param;
typedef struct uscxml_elem_data uscxml_elem_data;
typedef struct uscxml_elem_assign uscxml_elem_assign;
typedef struct uscxml_elem_donedata uscxml_elem_donedata;
typedef struct uscxml_elem_foreach uscxml_elem_foreach;

typedef void* (*dequeue_internal_t)(const uscxml_ctx* ctx);
typedef void* (*dequeue_external_t)(const uscxml_ctx* ctx);
typedef int (*is_enabled_t)(const uscxml_ctx* ctx, const uscxml_transition* transition);
typedef int (*is_matched_t)(const uscxml_ctx* ctx, const uscxml_transition* transition, const void* event);
typedef int (*is_true_t)(const uscxml_ctx* ctx, const char* expr);
typedef int (*exec_content_t)(const uscxml_ctx* ctx, const uscxml_state* state, const void* event);
typedef int (*raise_done_event_t)(const uscxml_ctx* ctx, const uscxml_state* state, const uscxml_elem_donedata* donedata);
typedef int (*invoke_t)(const uscxml_ctx* ctx, const uscxml_state* s, const uscxml_elem_invoke* invocation, unsigned char uninvoke);

typedef int (*exec_content_log_t)(const uscxml_ctx* ctx, const char* label, const char* expr);
typedef int (*exec_content_raise_t)(const uscxml_ctx* ctx, const char* event);
typedef int (*exec_content_send_t)(const uscxml_ctx* ctx, const uscxml_elem_send* send);
typedef int (*exec_content_foreach_init_t)(const uscxml_ctx* ctx, const uscxml_elem_foreach* foreach);
typedef int (*exec_content_foreach_next_t)(const uscxml_ctx* ctx, const uscxml_elem_foreach* foreach);
typedef int (*exec_content_foreach_done_t)(const uscxml_ctx* ctx, const uscxml_elem_foreach* foreach);
typedef int (*exec_content_assign_t)(const uscxml_ctx* ctx, const uscxml_elem_assign* assign);
typedef int (*exec_content_init_t)(const uscxml_ctx* ctx, const uscxml_elem_data* data);
typedef int (*exec_content_cancel_t)(const uscxml_ctx* ctx, const char* sendid, const char* sendidexpr);
typedef int (*exec_content_finalize_t)(const uscxml_ctx* ctx, const uscxml_elem_invoke* invoker, const void* event);
typedef int (*exec_content_script_t)(const uscxml_ctx* ctx, const char* src, const char* content);

/**
 * A single SCXML state-machine.
 */
struct uscxml_machine {
    unsigned char               flags;           /* Unused */
    USCXML_NR_STATES_TYPE       nr_states;       /* Make sure to set type per macro! */
    USCXML_NR_TRANS_TYPE        nr_transitions;  /* Make sure to set type per macro! */
    const char*                 name;
    const char*                 datamodel;
    const char*                 uuid;            /* currently MD5 sum */
    const uscxml_state*         states;
    const uscxml_transition*    transitions;
    const uscxml_machine*       parent;
    const uscxml_elem_donedata* donedata;
    const exec_content_t        script;          /* Global script elements */
};

/**
 * All information pertaining to a <data> element.
 * With late data binding, blocks of data elements are separated by NULL
 * use USCXML_ELEM_DATA_IS_SET to test for end of a block.
 */
struct uscxml_elem_data {
    const char* id;
    const char* src;
    const char* expr;
    const char* content;
};

/**
 * All information pertaining to an <assign> element.
 */
struct uscxml_elem_assign {
    const char* location;
    const char* expr;
    const char* content;
};

/**
 * All information pertaining to any state element.
 */
struct uscxml_state {
    const char* name;                                  /* eventual name          */
    const USCXML_NR_STATES_TYPE parent;                /* parent                 */
    const exec_content_t on_entry;                     /* on entry handlers      */
    const exec_content_t on_exit;                      /* on exit handlers       */
    const invoke_t invoke;                             /* invocations            */
    const char children[USCXML_MAX_NR_STATES_BYTES];   /* all children           */
    const char completion[USCXML_MAX_NR_STATES_BYTES]; /* default completion     */
    const char ancestors[USCXML_MAX_NR_STATES_BYTES];  /* all ancestors          */
    const uscxml_elem_data* data;                      /* data with late binding */
    const unsigned char type;                          /* One of USCXML_STATE_*  */
};

/**
 * All information pertaining to a <transitions> element.
 */
struct uscxml_transition {
    const USCXML_NR_STATES_TYPE source;
    const char target[USCXML_MAX_NR_STATES_BYTES];
    const char* event;
    const char* condition;
    const is_enabled_t is_enabled;
    const exec_content_t on_transition;
    const unsigned char type;
    const char conflicts[USCXML_MAX_NR_TRANS_BYTES];
    const char exit_set[USCXML_MAX_NR_STATES_BYTES];
};

/**
 * All information pertaining to a <foreach> element.
 */
struct uscxml_elem_foreach {
    const char* array;
    const char* item;
    const char* index;
};

/**
 * All information pertaining to a <param> element.
 * Blocks of params are separated by NULL params, use
 * USCXML_ELEM_PARAM_IS_SET to test for end of a block.
 */
struct uscxml_elem_param {
    const char* name;
    const char* expr;
    const char* location;
};

/**
 * All information pertaining to a <donedata> element.
 */
struct uscxml_elem_donedata {
    const USCXML_NR_STATES_TYPE source;
    const char* content;
    const char* contentexpr;
    const uscxml_elem_param* params;
};

/**
 * All information pertaining to an <invoke> element.
 */
struct uscxml_elem_invoke {
    const uscxml_machine* machine;
    const char* type;
    const char* typeexpr;
    const char* src;
    const char* srcexpr;
    const char* id;
    const char* idlocation;
    const char* sourcename;
    const char* namelist;
    const unsigned char autoforward;
    const uscxml_elem_param* params;
    exec_content_finalize_t finalize;
    const char* content;
    const char* contentexpr;
};

/**
 * All information pertaining to a <send> element.
 */
struct uscxml_elem_send {
    const char* event;
    const char* eventexpr;
    const char* target;
    const char* targetexpr;
    const char* type;
    const char* typeexpr;
    const char* id;
    const char* idlocation;
    const char* delay;
    const char* delayexpr;
    const char* namelist;    /* not space-separated, still as in attribute value */
    const char* content;
    const char* contentexpr;
    const uscxml_elem_param* params;
};

/**
 * Represents an instance of a state-chart at runtime/
 */
struct uscxml_ctx {
    unsigned char         flags;
    const uscxml_machine* machine;

    char config[USCXML_MAX_NR_STATES_BYTES]; /* Make sure these macros specify a sufficient size */
    char history[USCXML_MAX_NR_STATES_BYTES];
    char invocations[USCXML_MAX_NR_STATES_BYTES];
    char initialized_data[USCXML_MAX_NR_STATES_BYTES];

    void* user_data;
    void* event;

    dequeue_internal_t dequeue_internal;
    dequeue_external_t dequeue_external;
    is_matched_t       is_matched;
    is_true_t          is_true;
    raise_done_event_t raise_done_event;

    exec_content_log_t          exec_content_log;
    exec_content_raise_t        exec_content_raise;
    exec_content_send_t         exec_content_send;
    exec_content_foreach_init_t exec_content_foreach_init;
    exec_content_foreach_next_t exec_content_foreach_next;
    exec_content_foreach_done_t exec_content_foreach_done;
    exec_content_assign_t       exec_content_assign;
    exec_content_init_t         exec_content_init;
    exec_content_cancel_t       exec_content_cancel;
    exec_content_script_t       exec_content_script;

    invoke_t invoke;
};

#define USCXML_NO_GEN_C_TYPES
#endif

/* forward declare machines to allow references */
extern const uscxml_machine _uscxml_7B67993D_machine;

#ifndef USCXML_NO_ELEM_INFO

static const uscxml_elem_donedata _uscxml_7B67993D_elem_donedatas[1] = {
    /* source, content, contentexpr, params */
    { 0, NULL, NULL, NULL }
};

#endif

#ifndef USCXML_NO_ELEM_INFO

#endif

#ifndef USCXML_NO_EXEC_CONTENT

static int _uscxml_7B67993D_s0_on_entry_0(const uscxml_ctx* ctx, const uscxml_state* state, const void* event) {
    int err = USCXML_ERR_OK;
    if likely(ctx->exec_content_raise != NULL) {
        if unlikely((ctx->exec_content_raise(ctx, "foo")) != USCXML_ERR_OK) return err;
    } else {
        return USCXML_ERR_MISSING_CALLBACK;
    }
    if likely(ctx->exec_content_raise != NULL) {
        if unlikely((ctx->exec_content_raise(ctx, "bar")) != USCXML_ERR_OK) return err;
    } else {
        return USCXML_ERR_MISSING_CALLBACK;
    }
    return USCXML_ERR_OK;
}

static int _uscxml_7B67993D_s0_on_entry(const uscxml_ctx* ctx, const uscxml_state* state, const void* event) {
    _uscxml_7B67993D_s0_on_entry_0(ctx, state, event);
    return USCXML_ERR_OK;
}

static int _uscxml_7B67993D_pass_on_entry_0(const uscxml_ctx* ctx, const uscxml_state* state, const void* event) {
    int err = USCXML_ERR_OK;
    if likely(ctx->exec_content_log != NULL) {
        if unlikely((ctx->exec_content_log(ctx, "Outcome", "'pass'")) != USCXML_ERR_OK) return err;
    } else {
        return USCXML_ERR_MISSING_CALLBACK;
    }
    return USCXML_ERR_OK;
}

static int _uscxml_7B67993D_pass_on_entry(const uscxml_ctx* ctx, const uscxml_state* state, const void* event) {
    _uscxml_7B67993D_pass_on_entry_0(ctx, state, event);
    return USCXML_ERR_OK;
}

static int _uscxml_7B67993D_fail_on_entry_0(const uscxml_ctx* ctx, const uscxml_state* state, const void* event) {
    int err = USCXML_ERR_OK;
    if likely(ctx->exec_content_log != NULL) {
        if unlikely((ctx->exec_content_log(ctx, "Outcome", "'fail'")) != USCXML_ERR_OK) return err;
    } else {
        return USCXML_ERR_MISSING_CALLBACK;
    }
    return USCXML_ERR_OK;
}

static int _uscxml_7B67993D_fail_on_entry(const uscxml_ctx* ctx, const uscxml_state* state, const void* event) {
    _uscxml_7B67993D_fail_on_entry_0(ctx, state, event);
    return USCXML_ERR_OK;
}

#endif

#ifndef USCXML_NO_ELEM_INFO

static const uscxml_state _uscxml_7B67993D_states[5] = {
    {   /* state number 0 */
        /* name       */ NULL,
        /* parent     */ 0,
        /* onentry    */ NULL,
        /* onexit     */ NULL,
        /* invoke     */ NULL,
        /* children   */ { 0x1e /* 01111 */ },
        /* completion */ { 0x02 /* 01000 */ }, 	
        /* ancestors  */ { 0x00 /* 00000 */ },
        /* data       */ NULL,
        /* type       */ USCXML_STATE_COMPOUND,
    },
    {   /* state number 1 */
        /* name       */ "s0",
        /* parent     */ 0,
        /* onentry    */ _uscxml_7B67993D_s0_on_entry,
        /* onexit     */ NULL,
        /* invoke     */ NULL,
        /* children   */ { 0x00 /* 00000 */ },
        /* completion */ { 0x00 /* 00000 */ }, 	
        /* ancestors  */ { 0x01 /* 10000 */ },
        /* data       */ NULL,
        /* type       */ USCXML_STATE_ATOMIC,
    },
    {   /* state number 2 */
        /* name       */ "s1",
        /* parent     */ 0,
        /* onentry    */ NULL,
        /* onexit     */ NULL,
        /* invoke     */ NULL,
        /* children   */ { 0x00 /* 00000 */ },
        /* completion */ { 0x00 /* 00000 */ }, 	
        /* ancestors  */ { 0x01 /* 10000 */ },
        /* data       */ NULL,
        /* type       */ USCXML_STATE_ATOMIC,
    },
    {   /* state number 3 */
        /* name       */ "pass",
        /* parent     */ 0,
        /* onentry    */ _uscxml_7B67993D_pass_on_entry,
        /* onexit     */ NULL,
        /* invoke     */ NULL,
        /* children   */ { 0x00 /* 00000 */ },
        /* completion */ { 0x00 /* 00000 */ }, 	
        /* ancestors  */ { 0x01 /* 10000 */ },
        /* data       */ NULL,
        /* type       */ USCXML_STATE_FINAL,
    },
    {   /* state number 4 */
        /* name       */ "fail",
        /* parent     */ 0,
        /* onentry    */ _uscxml_7B67993D_fail_on_entry,
        /* onexit     */ NULL,
        /* invoke     */ NULL,
        /* children   */ { 0x00 /* 00000 */ },
        /* completion */ { 0x00 /* 00000 */ }, 	
        /* ancestors  */ { 0x01 /* 10000 */ },
        /* data       */ NULL,
        /* type       */ USCXML_STATE_FINAL,
    }
};

#endif

#ifndef USCXML_NO_ELEM_INFO

static const uscxml_transition _uscxml_7B67993D_transitions[4] = {
    {   /* transition number 0 with priority 0
           target: s1
         */
        /* source     */ 1,
        /* target     */ { 0x04 /* 00100 */ },
        /* event      */ "foo",
        /* condition  */ NULL,
        /* is_enabled */ NULL,
        /* ontrans    */ NULL,
        /* type       */ 0,
        /* conflicts  */ { 0x0f /* 1111 */ }, 
        /* exit set   */ { 0x1e /* 01111 */ }
    },
    {   /* transition number 1 with priority 1
           target: fail
         */
        /* source     */ 1,
        /* target     */ { 0x10 /* 00001 */ },
        /* event      */ "*",
        /* condition  */ NULL,
        /* is_enabled */ NULL,
        /* ontrans    */ NULL,
        /* type       */ 0,
        /* conflicts  */ { 0x0f /* 1111 */ }, 
        /* exit set   */ { 0x1e /* 01111 */ }
    },
    {   /* transition number 2 with priority 2
           target: pass
         */
        /* source     */ 2,
        /* target     */ { 0x08 /* 00010 */ },
        /* event      */ "bar",
        /* condition  */ NULL,
        /* is_enabled */ NULL,
        /* ontrans    */ NULL,
        /* type       */ 0,
        /* conflicts  */ { 0x0f /* 1111 */ }, 
        /* exit set   */ { 0x1e /* 01111 */ }
    },
    {   /* transition number 3 with priority 3
           target: fail
         */
        /* source     */ 2,
        /* target     */ { 0x10 /* 00001 */ },
        /* event      */ "*",
        /* condition  */ NULL,
        /* is_enabled */ NULL,
        /* ontrans    */ NULL,
        /* type       */ 0,
        /* conflicts  */ { 0x0f /* 1111 */ }, 
        /* exit set   */ { 0x1e /* 01111 */ }
    }
};

#endif

#ifndef USCXML_NO_ELEM_INFO

#ifndef USCXML_MACHINE
#  define USCXML_MACHINE _uscxml_7B67993D_machine
#endif
#define USCXML_MACHINE_0 _uscxml_7B67993D_machine

const uscxml_machine _uscxml_7B67993D_machine = {
        /* flags          */ 0,
        /* nr_states      */ 5,
        /* nr_transitions */ 4,
        /* name           */ "",
        /* datamodel      */ "ecmascript",
        /* uuid           */ "7B67993D8309FD356AECB23C2C98EE79",
        /* states         */ &_uscxml_7B67993D_states[0], 
        /* transitions    */ &_uscxml_7B67993D_transitions[0], 
        /* parent         */ NULL,
        /* donedata       */ &_uscxml_7B67993D_elem_donedatas[0], 
        /* script         */ NULL
};

#endif

#ifdef USCXML_VERBOSE
/**
 * Print name of states contained in a (debugging).
 */
static void printStateNames(const uscxml_ctx* ctx, const char* a, size_t length) {
    size_t i;
    const char* seperator = "";
    for (i = 0; i < length; i++) {
        if (BIT_HAS(i, a)) {
            printf("%s%s", seperator, (USCXML_GET_STATE(i).name != NULL ? USCXML_GET_STATE(i).name : "UNK"));
            seperator = ", ";
        }
    }
    printf("\n");
}

/**
 * Print bits set in a in a binary representation (debugging).
 */
static void printBitsetIndices(const char* a, size_t length) {
    size_t i;
    const char* seperator = "";
    for (i = 0; i < length; i++) {
        if (BIT_HAS(i, a)) {
            printf("%s%lu", seperator, i);
            seperator = ", ";
        }
    }
    printf("\n");
}
#endif

#ifndef USCXML_NO_BIT_OPERATIONS
/**
 * Return true if there is a common bit in a and b.
 */
static int bit_has_and(const char* a, const char* b, size_t i) {
    while(i--) {
        if (a[i] & b[i])
            return 1;
    }
    return 0;
}

/**
 * Set all bits to 0, this corresponds to memset(a, 0, i), 
 * but does not require string.h or cstring.
 */
static void bit_clear_all(char* a, size_t i) {
    while(i--) {
        a[i] = 0;
    }
}

/**
 * Return true if there is any bit set in a.
 */
static int bit_has_any(const char* a, size_t i) {
    while(i--) {
        if (a[i] > 0)
            return 1;
    }
    return 0;
}

/**
 * Set all bits from given mask in dest, this is |= for bit arrays.
 */
static void bit_or(char* dest, const char* mask, size_t i) {
    while(i--) {
        dest[i] |= mask[i];
    }
}

/**
 * Copy all bits from source to dest, this corresponds to memcpy(a, b, i), 
 * but does not require string.h or cstring.
 */
static void bit_copy(char* dest, const char* source, size_t i) {
    while(i--) {
        dest[i] = source[i];
    }
}

/**
 * Unset bits from mask in dest.
 */
static void bit_and_not(char* dest, const char* mask, size_t i) {
    while(i--) {
        dest[i] &= ~mask[i];
    }
}

/**
 * Set bits from mask in dest.
 */
static void bit_and(char* dest, const char* mask, size_t i) {
    while(i--) {
        dest[i] &= mask[i];
    };
}

#define USCXML_NO_BIT_OPERATIONS
#endif

#ifndef USCXML_NO_STEP_FUNCTION
int uscxml_step(uscxml_ctx* ctx) {

    USCXML_NR_STATES_TYPE i, j, k;
    USCXML_NR_STATES_TYPE nr_states_bytes = ((USCXML_NUMBER_STATES + 7) & ~7) >> 3;
    USCXML_NR_TRANS_TYPE  nr_trans_bytes  = ((USCXML_NUMBER_TRANS + 7) & ~7) >> 3;
    int err = USCXML_ERR_OK;
    char conflicts  [USCXML_MAX_NR_TRANS_BYTES];
    char trans_set  [USCXML_MAX_NR_TRANS_BYTES];
    char target_set [USCXML_MAX_NR_STATES_BYTES];
    char exit_set   [USCXML_MAX_NR_STATES_BYTES];
    char entry_set  [USCXML_MAX_NR_STATES_BYTES];
    char tmp_states [USCXML_MAX_NR_STATES_BYTES];

#ifdef USCXML_VERBOSE
    printf("Config: ");
    printStateNames(ctx, ctx->config, USCXML_NUMBER_STATES);
#endif

    if (ctx->flags & USCXML_CTX_FINISHED)
        return USCXML_ERR_DONE;

    if (ctx->flags & USCXML_CTX_TOP_LEVEL_FINAL) {
        /* exit all remaining states */
        i = USCXML_NUMBER_STATES;
        while(i-- > 0) {
            if (BIT_HAS(i, ctx->config)) {
                /* call all on exit handlers */
                if (USCXML_GET_STATE(i).on_exit != NULL) {
                    if unlikely((err = USCXML_GET_STATE(i).on_exit(ctx, &USCXML_GET_STATE(i), ctx->event)) != USCXML_ERR_OK)
                        return err;
                }
            }
            if (BIT_HAS(i, ctx->invocations)) {
                if (USCXML_GET_STATE(i).invoke != NULL)
                    USCXML_GET_STATE(i).invoke(ctx, &USCXML_GET_STATE(i), NULL, 1);
                BIT_CLEAR(i, ctx->invocations);
            }
        }
        ctx->flags |= USCXML_CTX_FINISHED;
        return USCXML_ERR_DONE;
    }

    bit_clear_all(target_set, nr_states_bytes);
    bit_clear_all(trans_set, nr_trans_bytes);
    if unlikely(ctx->flags == USCXML_CTX_PRISTINE) {
        if (ctx->machine->script != NULL)
            ctx->machine->script(ctx, &ctx->machine->states[0], NULL);
        bit_or(target_set, ctx->machine->states[0].completion, nr_states_bytes);
        ctx->flags |= USCXML_CTX_SPONTANEOUS | USCXML_CTX_INITIALIZED;
        goto ESTABLISH_ENTRY_SET;
    }

    if (ctx->flags & USCXML_CTX_SPONTANEOUS) {
        ctx->event = NULL;
        goto SELECT_TRANSITIONS;
    }
    if (ctx->dequeue_internal != NULL && (ctx->event = ctx->dequeue_internal(ctx)) != NULL) {
        goto SELECT_TRANSITIONS;
    }

    /* manage invocations */
    for (i = 0; i < USCXML_NUMBER_STATES; i++) {
        /* uninvoke */
        if (!BIT_HAS(i, ctx->config) && BIT_HAS(i, ctx->invocations)) {
            if (USCXML_GET_STATE(i).invoke != NULL)
                USCXML_GET_STATE(i).invoke(ctx, &USCXML_GET_STATE(i), NULL, 1);
            BIT_CLEAR(i, ctx->invocations)
        }
        /* invoke */
        if (BIT_HAS(i, ctx->config) && !BIT_HAS(i, ctx->invocations)) {
            if (USCXML_GET_STATE(i).invoke != NULL)
                USCXML_GET_STATE(i).invoke(ctx, &USCXML_GET_STATE(i), NULL, 0);
            BIT_SET_AT(i, ctx->invocations)
        }
    }

    if (ctx->dequeue_external != NULL && (ctx->event = ctx->dequeue_external(ctx)) != NULL) {
        goto SELECT_TRANSITIONS;
    }

    if (ctx->dequeue_external == NULL) {
        return USCXML_ERR_DONE;
    }
    return USCXML_ERR_IDLE;

SELECT_TRANSITIONS:
    bit_clear_all(conflicts, nr_trans_bytes);
    bit_clear_all(exit_set, nr_states_bytes);
    for (i = 0; i < USCXML_NUMBER_TRANS; i++) {
        /* never select history or initial transitions automatically */
        if unlikely(USCXML_GET_TRANS(i).type & (USCXML_TRANS_HISTORY | USCXML_TRANS_INITIAL))
            continue;

        /* is the transition active? */
        if (BIT_HAS(USCXML_GET_TRANS(i).source, ctx->config)) {
            /* is it non-conflicting? */
            if (!BIT_HAS(i, conflicts)) {
                /* is it spontaneous with an event or vice versa? */
                if ((USCXML_GET_TRANS(i).event == NULL && ctx->event == NULL) || 
                    (USCXML_GET_TRANS(i).event != NULL && ctx->event != NULL)) {
                    /* is it enabled? */
                    if ((ctx->event == NULL || ctx->is_matched(ctx, &USCXML_GET_TRANS(i), ctx->event) > 0) &&
                        (USCXML_GET_TRANS(i).condition == NULL || USCXML_GET_TRANS(i).is_enabled(ctx, &USCXML_GET_TRANS(i)) > 0)) {
                        /* remember that we found a transition */
                        ctx->flags |= USCXML_CTX_TRANSITION_FOUND;

                        /* transitions that are pre-empted */
                        bit_or(conflicts, USCXML_GET_TRANS(i).conflicts, nr_trans_bytes);

                        /* states that are directly targeted (resolve as entry-set later) */
                        bit_or(target_set, USCXML_GET_TRANS(i).target, nr_states_bytes);

                        /* states that will be left */
                        bit_or(exit_set, USCXML_GET_TRANS(i).exit_set, nr_states_bytes);

                        BIT_SET_AT(i, trans_set);
                    }
                }
            }
        }
    }
    bit_and(exit_set, ctx->config, nr_states_bytes);

    if (ctx->flags & USCXML_CTX_TRANSITION_FOUND) {
        ctx->flags |= USCXML_CTX_SPONTANEOUS;
        ctx->flags &= ~USCXML_CTX_TRANSITION_FOUND;
    } else {
        ctx->flags &= ~USCXML_CTX_SPONTANEOUS;
    }

#ifdef USCXML_VERBOSE
    printf("Targets: ");
    printStateNames(ctx, target_set, USCXML_NUMBER_STATES);
#endif

#ifdef USCXML_VERBOSE
    printf("Exiting: ");
    printStateNames(ctx, exit_set, USCXML_NUMBER_STATES);
#endif

#ifdef USCXML_VERBOSE
    printf("History: ");
    printStateNames(ctx, ctx->history, USCXML_NUMBER_STATES);
#endif

/* REMEMBER_HISTORY: */
    for (i = 0; i < USCXML_NUMBER_STATES; i++) {
        if unlikely(USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_HISTORY_SHALLOW ||
                    USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_HISTORY_DEEP) {
            /* a history state whose parent is about to be exited */
            if unlikely(BIT_HAS(USCXML_GET_STATE(i).parent, exit_set)) {
                bit_copy(tmp_states, USCXML_GET_STATE(i).completion, nr_states_bytes);

                /* set those states who were enabled */
                bit_and(tmp_states, ctx->config, nr_states_bytes);

                /* clear current history with completion mask */
                bit_and_not(ctx->history, USCXML_GET_STATE(i).completion, nr_states_bytes);

                /* set history */
                bit_or(ctx->history, tmp_states, nr_states_bytes);
            }
        }
    }

ESTABLISH_ENTRY_SET:
    /* calculate new entry set */
    bit_copy(entry_set, target_set, nr_states_bytes);

    /* iterate for ancestors */
    for (i = 0; i < USCXML_NUMBER_STATES; i++) {
        if (BIT_HAS(i, entry_set)) {
            bit_or(entry_set, USCXML_GET_STATE(i).ancestors, nr_states_bytes);
        }
    }

    /* iterate for descendants */
    for (i = 0; i < USCXML_NUMBER_STATES; i++) {
        if (BIT_HAS(i, entry_set)) {
            switch (USCXML_STATE_MASK(USCXML_GET_STATE(i).type)) {
                case USCXML_STATE_PARALLEL: {
                    bit_or(entry_set, USCXML_GET_STATE(i).completion, nr_states_bytes);
                    break;
                }
                case USCXML_STATE_HISTORY_SHALLOW:
                case USCXML_STATE_HISTORY_DEEP: {
                    if (!bit_has_and(USCXML_GET_STATE(i).completion, ctx->history, nr_states_bytes) &&
                        !BIT_HAS(USCXML_GET_STATE(i).parent, ctx->config)) {
                        /* nothing set for history, look for a default transition */
                        for (j = 0; j < USCXML_NUMBER_TRANS; j++) {
                            if unlikely(ctx->machine->transitions[j].source == i) {
                                bit_or(entry_set, ctx->machine->transitions[j].target, nr_states_bytes);
                                if(USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_HISTORY_DEEP &&
                                   !bit_has_and(ctx->machine->transitions[j].target, USCXML_GET_STATE(i).children, nr_states_bytes)) {
                                    for (k = i + 1; k < USCXML_NUMBER_STATES; k++) {
                                        if (BIT_HAS(k, ctx->machine->transitions[j].target)) {
                                            bit_or(entry_set, ctx->machine->states[k].ancestors, nr_states_bytes);
                                            break;
                                        }
                                    }
                                }
                                BIT_SET_AT(j, trans_set);
                                break;
                            }
                            /* Note: SCXML mandates every history to have a transition! */
                        }
                    } else {
                        bit_copy(tmp_states, USCXML_GET_STATE(i).completion, nr_states_bytes);
                        bit_and(tmp_states, ctx->history, nr_states_bytes);
                        bit_or(entry_set, tmp_states, nr_states_bytes);
                        if (USCXML_GET_STATE(i).type == (USCXML_STATE_HAS_HISTORY | USCXML_STATE_HISTORY_DEEP)) {
                            /* a deep history state with nested histories -> more completion */
                            for (j = i + 1; j < USCXML_NUMBER_STATES; j++) {
                                if (BIT_HAS(j, USCXML_GET_STATE(i).completion) &&
                                    BIT_HAS(j, entry_set) &&
                                    (ctx->machine->states[j].type & USCXML_STATE_HAS_HISTORY)) {
                                    for (k = j + 1; k < USCXML_NUMBER_STATES; k++) {
                                        /* add nested history to entry_set */
                                        if ((USCXML_STATE_MASK(ctx->machine->states[k].type) == USCXML_STATE_HISTORY_DEEP ||
                                             USCXML_STATE_MASK(ctx->machine->states[k].type) == USCXML_STATE_HISTORY_SHALLOW) &&
                                            BIT_HAS(k, ctx->machine->states[j].children)) {
                                            /* a nested history state */
                                            BIT_SET_AT(k, entry_set);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                }
                case USCXML_STATE_INITIAL: {
                    for (j = 0; j < USCXML_NUMBER_TRANS; j++) {
                        if (ctx->machine->transitions[j].source == i) {
                            BIT_SET_AT(j, trans_set);
                            BIT_CLEAR(i, entry_set);
                            bit_or(entry_set, ctx->machine->transitions[j].target, nr_states_bytes);
                            for (k = i + 1; k < USCXML_NUMBER_STATES; k++) {
                                if (BIT_HAS(k, ctx->machine->transitions[j].target)) {
                                    bit_or(entry_set, ctx->machine->states[k].ancestors, nr_states_bytes);
                                }
                            }
                        }
                    }
                    break;
                }
                case USCXML_STATE_COMPOUND: { /* we need to check whether one child is already in entry_set */
                    if (!bit_has_and(entry_set, USCXML_GET_STATE(i).children, nr_states_bytes) &&
                        (!bit_has_and(ctx->config, USCXML_GET_STATE(i).children, nr_states_bytes) ||
                         bit_has_and(exit_set, USCXML_GET_STATE(i).children, nr_states_bytes)))
                    {
                        bit_or(entry_set, USCXML_GET_STATE(i).completion, nr_states_bytes);
                        if (!bit_has_and(USCXML_GET_STATE(i).completion, USCXML_GET_STATE(i).children, nr_states_bytes)) {
                            /* deep completion */
                            for (j = i + 1; j < USCXML_NUMBER_STATES; j++) {
                                if (BIT_HAS(j, USCXML_GET_STATE(i).completion)) {
                                    bit_or(entry_set, ctx->machine->states[j].ancestors, nr_states_bytes);
                                    break; /* completion of compound is single state */
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
    }

#ifdef USCXML_VERBOSE
    printf("Transitions: ");
    printBitsetIndices(trans_set, sizeof(char) * 8 * nr_trans_bytes);
#endif

/* EXIT_STATES: */
    i = USCXML_NUMBER_STATES;
    while(i-- > 0) {
        if (BIT_HAS(i, exit_set) && BIT_HAS(i, ctx->config)) {
            /* call all on exit handlers */
            if (USCXML_GET_STATE(i).on_exit != NULL) {
                if unlikely((err = USCXML_GET_STATE(i).on_exit(ctx, &USCXML_GET_STATE(i), ctx->event)) != USCXML_ERR_OK)
                    return err;
            }
            BIT_CLEAR(i, ctx->config);
        }
    }

/* TAKE_TRANSITIONS: */
    for (i = 0; i < USCXML_NUMBER_TRANS; i++) {
        if (BIT_HAS(i, trans_set) && (USCXML_GET_TRANS(i).type & (USCXML_TRANS_HISTORY | USCXML_TRANS_INITIAL)) == 0) {
            /* call executable content in transition */
            if (USCXML_GET_TRANS(i).on_transition != NULL) {
                if unlikely((err = USCXML_GET_TRANS(i).on_transition(ctx,
                                                                              &ctx->machine->states[USCXML_GET_TRANS(i).source],
                                                                              ctx->event)) != USCXML_ERR_OK)
                    return err;
            }
        }
    }

#ifdef USCXML_VERBOSE
    printf("Entering: ");
    printStateNames(ctx, entry_set, USCXML_NUMBER_STATES);
#endif

/* ENTER_STATES: */
    for (i = 0; i < USCXML_NUMBER_STATES; i++) {
        if (BIT_HAS(i, entry_set) && !BIT_HAS(i, ctx->config)) {
            /* these are no proper states */
            if unlikely(USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_HISTORY_DEEP ||
                        USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_HISTORY_SHALLOW ||
                        USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_INITIAL)
                continue;

            BIT_SET_AT(i, ctx->config);

            /* initialize data */
            if (!BIT_HAS(i, ctx->initialized_data)) {
                if unlikely(USCXML_GET_STATE(i).data != NULL && ctx->exec_content_init != NULL) {
                    ctx->exec_content_init(ctx, USCXML_GET_STATE(i).data);
                }
                BIT_SET_AT(i, ctx->initialized_data);
            }

            if (USCXML_GET_STATE(i).on_entry != NULL) {
                if unlikely((err = USCXML_GET_STATE(i).on_entry(ctx, &USCXML_GET_STATE(i), ctx->event)) != USCXML_ERR_OK)
                    return err;
            }

            /* take history and initial transitions */
            for (j = 0; j < USCXML_NUMBER_TRANS; j++) {
                if unlikely(BIT_HAS(j, trans_set) &&
                            (ctx->machine->transitions[j].type & (USCXML_TRANS_HISTORY | USCXML_TRANS_INITIAL)) &&
                            ctx->machine->states[ctx->machine->transitions[j].source].parent == i) {
                    /* call executable content in transition */
                    if (ctx->machine->transitions[j].on_transition != NULL) {
                        if unlikely((err = ctx->machine->transitions[j].on_transition(ctx,
                                                                                      &USCXML_GET_STATE(i),
                                                                                      ctx->event)) != USCXML_ERR_OK)
                            return err;
                    }
                }
            }

            /* handle final states */
            if unlikely(USCXML_STATE_MASK(USCXML_GET_STATE(i).type) == USCXML_STATE_FINAL) {
                if unlikely(USCXML_GET_STATE(i).ancestors[0] == 0x01) {
                    ctx->flags |= USCXML_CTX_TOP_LEVEL_FINAL;
                } else {
                    /* raise done event */
                    const uscxml_elem_donedata* donedata = &ctx->machine->donedata[0];
                    while(USCXML_ELEM_DONEDATA_IS_SET(donedata)) {
                        if unlikely(donedata->source == i)
                            break;
                        donedata++;
                    }
                    ctx->raise_done_event(ctx, &ctx->machine->states[USCXML_GET_STATE(i).parent], (USCXML_ELEM_DONEDATA_IS_SET(donedata) ? donedata : NULL));
                }

                /**
                 * are we the last final state to leave a parallel state?:
                 * 1. Gather all parallel states in our ancestor chain
                 * 2. Find all states for which these parallels are ancestors
                 * 3. Iterate all active final states and remove their ancestors
                 * 4. If a state remains, not all children of a parallel are final
                 */
                for (j = 0; j < USCXML_NUMBER_STATES; j++) {
                    if unlikely(USCXML_STATE_MASK(ctx->machine->states[j].type) == USCXML_STATE_PARALLEL &&
                                BIT_HAS(j, USCXML_GET_STATE(i).ancestors)) {
                        bit_clear_all(tmp_states, nr_states_bytes);
                        for (k = 0; k < USCXML_NUMBER_STATES; k++) {
                            if unlikely(BIT_HAS(j, ctx->machine->states[k].ancestors) && BIT_HAS(k, ctx->config)) {
                                if (USCXML_STATE_MASK(ctx->machine->states[k].type) == USCXML_STATE_FINAL) {
                                    bit_and_not(tmp_states, ctx->machine->states[k].ancestors, nr_states_bytes);
                                } else {
                                    BIT_SET_AT(k, tmp_states);
                                }
                            }
                        }
                        if unlikely(!bit_has_any(tmp_states, nr_states_bytes)) {
                            ctx->raise_done_event(ctx, &ctx->machine->states[j], NULL);
                        }
                    }
                }

            }

        }
    }

    return USCXML_ERR_OK;
}

#define USCXML_NO_STEP_FUNCTION
#endif