-
Notifications
You must be signed in to change notification settings - Fork 8
/
machines.h
154 lines (117 loc) · 5.51 KB
/
machines.h
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
/* Copyright 2018 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Little Sheens C API */
#include <stdlib.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/* JSON is a string in JSON syntax. */
typedef char * JSON;
/* S is just a string (in contrast to JSON). */
typedef char * S;
/* Most of the following symbols have the prefix "mach_", which is
short for "machines". I thought long and hard about "meh_", which
is phonetially short for "meh-sheens", but I obviously didn't
follow through with that brilliant idea. */
/* provider is the signature for a function that can resolve the given
SpecName to Spec. The first argument will be _ctx. The second
argument is a string that represents the JSON representation of the
cached spec if any. If that cached representation is the current
representation, then the provide can just return NULL. */
typedef char * (*mach_provider)(void*, const char *, const char *);
/* A generic mode type. */
typedef unsigned int mach_mode;
/* mach_make_ctx creates a new Little Sheens context (but does not use
it). Call mach_set_ctx() to use the ctx you create. For example:
mach_set_ctx(mach_make_ctx());
mach_open();
...
mach_close();
free(mach_get_ctx());
*/
void *mach_make_ctx() ;
/* mach_set_ctx sets the given context as the (global) active
context. */
void mach_set_ctx(void *c) ;
/* mach_get_ctx just returns the (global) active context. */
void *mach_get_ctx() ;
/* mach_open creates and initializes the runtime. (Currently there is
only one global runtime.) */
int mach_open();
/* mach_close frees the runtime. */
void mach_close();
/* mach_process takes a machine State and a Message and returns the
new state (if any) and any emitted messages and other data that
indicates what happened.
Also see mach_crew_process. */
int mach_process(JSON state, JSON message, JSON dst, int limit);
/* MACH_FREE_FOR_PROVIDER is a mode for a spec provider that indicates
the the caller of the provider should free the spec (JSON) that the
provider returns. Useful in mach_set_spec_provider().*/
#define MACH_FREE_FOR_PROVIDER (1UL<<2UL)
/* mach_set_spec_provider registers the given function so that
mach_process can resolve the SpecName to a Spec. Available modes:
MACH_FREE_FOR_PROVIDER, which will cause the string returned by the
provider to be freed. Use 0 if you don't want that. */
void mach_set_spec_provider(void * ctx, mach_provider f, mach_mode m);
/* mach_eval is a utilty function that executes the given ECMAScript
source and writes the result, which better be a string, to dst.
Returns MACH_OKAY on success.
*/
int mach_eval(char* src, char* dst, int limit);
/* mach_match is a utility that does pattern matching. Returns
bindings rendered as JSON to dst. Returns MACH_OKAY on success.
*/
int mach_match(JSON pattern, JSON message, JSON bindings, JSON dst,
int limit);
/* Experimental objects API below */
/* MACH_OKAY is a return code reporting that all is well in the
world. */
#define MACH_OKAY (0)
/* MACH_SAD is a return code reporting grave, unspecified sadness. */
#define MACH_SAD (1)
/* MACH_TOO_BIG is a return code reporting that the result, when
serialized, was larger than the given limit. */
#define MACH_TOO_BIG (2)
/* mach_make_crew creates a new Crew with the given id. The new Crew
is serialized as JSON and written to dst. */
int mach_make_crew(S id, JSON dst, size_t limit);
/* mach_set_machine adds or updates a machine in the given Crew.
Reserializes the Crew and writes it to dst. */
int mach_set_machine(JSON crew, S id, S specRef, JSON bindings, S node, JSON dst, size_t limit);
/* mach_rem_machine removes the given machine from the given Crew.
Reserializes the Crew and writes it to dst. */
int mach_rem_machine(JSON crew, S id, JSON dst, size_t limit) ;
/* mach_crew_process gives the message to the Crew for processing.
Writes a map from machine ids to steppeds as JSON to dst. */
int mach_crew_process(JSON crew, JSON message, JSON dst, size_t limit) ;
/* mach_crew_update updates the Crew to reflect the net state changes
of the given steppeds (as written by mach_crew_process. */
int mach_crew_update(JSON crew, JSON steppeds, JSON dst, size_t limit) ;
/* mach_get_emitted just extracts emitted messages from the given
steppeds map (as written by mach_crew_process). */
int mach_get_emitted(JSON steppeds, JSON dsts[], int most, size_t limit) ;
/* mach_do_emitted iterates over emitted messages. Not useful with
closures? */
int mach_do_emitted(JSON steppeds, int (*f)(JSON)) ;
/* mach_set_spec_cache sets the spec cache entries limit. */
int mach_set_spec_cache_limit(int limit) ;
/* mach_clear_spec_cache clears the spec cache. */
int mach_clear_spec_cache() ;
/* mach_enable_spec_cache enables (1) or disables (0) the spec cache. */
int mach_enable_spec_cache(int enable) ;
/* A utility for seeing the current Duktape stack. */
void mach_dump_stack(FILE *out, char *tag);
#ifdef __cplusplus
}
#endif // __cplusplus