• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/include/ucode/types.h

  1 /*
  2  * Copyright (C) 2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #ifndef __TYPES_H_
 18 #define __TYPES_H_
 19 
 20 #include <stdbool.h>
 21 #include <stdint.h>
 22 #include <regex.h>
 23 #include <json-c/json.h>
 24 
 25 #include "util.h"
 26 
 27 
 28 /* Value types and generic value header */
 29 
 30 typedef enum uc_type {
 31         UC_NULL,
 32         UC_INTEGER,
 33         UC_BOOLEAN,
 34         UC_STRING,
 35         UC_DOUBLE,
 36         UC_ARRAY,
 37         UC_OBJECT,
 38         UC_REGEXP,
 39         UC_FUNCTION,
 40         UC_CFUNCTION,
 41         UC_CLOSURE,
 42         UC_UPVALUE,
 43         UC_RESOURCE
 44 } uc_type_t;
 45 
 46 typedef struct uc_value {
 47         uint32_t type:4;
 48         uint32_t mark:1;
 49         uint32_t u64:1;
 50         uint32_t refcount:26;
 51 } uc_value_t;
 52 
 53 
 54 /* Constant list defintions */
 55 
 56 typedef struct {
 57         size_t isize;
 58         size_t dsize;
 59         uint64_t *index;
 60         char *data;
 61 } uc_value_list_t;
 62 
 63 
 64 /* Source buffer defintions */
 65 
 66 uc_declare_vector(uc_lineinfo_t, uint8_t);
 67 
 68 typedef struct {
 69         char *filename, *buffer;
 70         FILE *fp;
 71         size_t usecount, off;
 72         uc_lineinfo_t lineinfo;
 73 } uc_source_t;
 74 
 75 
 76 /* Bytecode chunk defintions */
 77 
 78 typedef struct {
 79         size_t from, to, target, slot;
 80 } uc_ehrange_t;
 81 
 82 typedef struct {
 83         size_t from, to, slot, nameidx;
 84 } uc_varrange_t;
 85 
 86 uc_declare_vector(uc_ehranges_t, uc_ehrange_t);
 87 uc_declare_vector(uc_variables_t, uc_varrange_t);
 88 uc_declare_vector(uc_offsetinfo_t, uint8_t);
 89 
 90 typedef struct {
 91         size_t count;
 92         uint8_t *entries;
 93         uc_ehranges_t ehranges;
 94         struct {
 95                 uc_variables_t variables;
 96                 uc_value_list_t varnames;
 97                 uc_offsetinfo_t offsets;
 98         } debuginfo;
 99 } uc_chunk_t;
100 
101 
102 /* Value type structures */
103 
104 typedef struct uc_weakref {
105         struct uc_weakref *prev;
106         struct uc_weakref *next;
107 } uc_weakref_t;
108 
109 typedef struct {
110         uc_value_t header;
111         double dbl;
112 } uc_double_t;
113 
114 typedef struct {
115         uc_value_t header;
116         union {
117                 int64_t s64;
118                 uint64_t u64;
119         } i;
120 } uc_integer_t;
121 
122 typedef struct {
123         uc_value_t header;
124         size_t length;
125         char str[];
126 } uc_string_t;
127 
128 typedef struct {
129         uc_value_t header;
130         uc_weakref_t ref;
131         size_t count;
132         uc_value_t *proto;
133         uc_value_t **entries;
134 } uc_array_t;
135 
136 typedef struct {
137         uc_value_t header;
138         uc_weakref_t ref;
139         uc_value_t *proto;
140         struct lh_table *table;
141 } uc_object_t;
142 
143 typedef struct {
144         uc_value_t header;
145         regex_t regexp;
146         bool icase, newline, global;
147         char source[];
148 } uc_regexp_t;
149 
150 typedef struct uc_function {
151         uc_value_t header;
152         bool arrow, vararg, strict, root;
153         size_t nargs;
154         size_t nupvals;
155         size_t srcpos;
156         uc_chunk_t chunk;
157         struct uc_program *program;
158         uc_weakref_t progref;
159         char name[];
160 } uc_function_t;
161 
162 typedef struct uc_upval_tref {
163         uc_value_t header;
164         size_t slot;
165         bool closed;
166         uc_value_t *value;
167         struct uc_upval_tref *next;
168 } uc_upvalref_t;
169 
170 typedef struct {
171         uc_value_t header;
172         uc_weakref_t ref;
173         bool is_arrow;
174         uc_function_t *function;
175         uc_upvalref_t **upvals;
176 } uc_closure_t;
177 
178 typedef struct uc_vm uc_vm_t;
179 typedef uc_value_t *(*uc_cfn_ptr_t)(uc_vm_t *, size_t);
180 
181 typedef struct {
182         uc_value_t header;
183         uc_cfn_ptr_t cfn;
184         char name[];
185 } uc_cfunction_t;
186 
187 typedef struct {
188         const char *name;
189         uc_value_t *proto;
190         void (*free)(void *);
191 } uc_resource_type_t;
192 
193 typedef struct {
194         uc_value_t header;
195         uc_resource_type_t *type;
196         void *data;
197 } uc_resource_t;
198 
199 uc_declare_vector(uc_resource_types_t, uc_resource_type_t *);
200 
201 
202 /* Program structure definitions */
203 
204 typedef struct uc_program {
205         uc_value_list_t constants;
206         uc_weakref_t functions;
207         uc_source_t *source;
208 } uc_program_t;
209 
210 
211 /* Parser definitions */
212 
213 typedef struct {
214         bool lstrip_blocks;
215         bool trim_blocks;
216         bool strict_declarations;
217         bool raw_mode;
218 } uc_parse_config_t;
219 
220 
221 /* VM definitions */
222 
223 typedef enum {
224         EXCEPTION_NONE,
225         EXCEPTION_SYNTAX,
226         EXCEPTION_RUNTIME,
227         EXCEPTION_TYPE,
228         EXCEPTION_REFERENCE,
229         EXCEPTION_USER,
230         EXCEPTION_EXIT
231 } uc_exception_type_t;
232 
233 typedef struct {
234         uc_exception_type_t type;
235         uc_value_t *stacktrace;
236         char *message;
237 } uc_exception_t;
238 
239 typedef struct {
240         uint8_t *ip;
241         uc_closure_t *closure;
242         uc_cfunction_t *cfunction;
243         size_t stackframe;
244         uc_value_t *ctx;
245         bool mcall, strict;
246 } uc_callframe_t;
247 
248 uc_declare_vector(uc_callframes_t, uc_callframe_t);
249 uc_declare_vector(uc_stack_t, uc_value_t *);
250 
251 typedef struct printbuf uc_stringbuf_t;
252 
253 typedef void (uc_exception_handler_t)(uc_vm_t *, uc_exception_t *);
254 
255 struct uc_vm {
256         uc_stack_t stack;
257         uc_exception_t exception;
258         uc_callframes_t callframes;
259         uc_upvalref_t *open_upvals;
260         uc_parse_config_t *config;
261         uc_value_t *globals;
262         uc_value_t *registry;
263         uc_source_t *sources;
264         uc_weakref_t values;
265         uc_resource_types_t restypes;
266         union {
267                 uint32_t u32;
268                 int32_t s32;
269                 uint16_t u16;
270                 int16_t s16;
271                 uint8_t u8;
272                 int8_t s8;
273         } arg;
274         size_t spread_values;
275         uint8_t trace;
276         uc_stringbuf_t *strbuf;
277         uc_exception_handler_t *exhandler;
278         FILE *output;
279 };
280 
281 
282 /* Value API */
283 
284 void ucv_free(uc_value_t *, bool);
285 void ucv_put(uc_value_t *);
286 
287 void ucv_unref(uc_weakref_t *);
288 void ucv_ref(uc_weakref_t *, uc_weakref_t *);
289 
290 uc_value_t *ucv_get(uc_value_t *uv);
291 
292 uc_type_t ucv_type(uc_value_t *);
293 const char *ucv_typename(uc_value_t *);
294 
295 uc_value_t *ucv_boolean_new(bool);
296 bool ucv_boolean_get(uc_value_t *);
297 
298 uc_value_t *ucv_string_new(const char *);
299 uc_value_t *ucv_string_new_length(const char *, size_t);
300 size_t ucv_string_length(uc_value_t *);
301 
302 char *_ucv_string_get(uc_value_t **);
303 #define ucv_string_get(uv) _ucv_string_get((uc_value_t **)&uv)
304 
305 uc_stringbuf_t *ucv_stringbuf_new(void);
306 uc_value_t *ucv_stringbuf_finish(uc_stringbuf_t *);
307 
308 void _ucv_stringbuf_append(uc_stringbuf_t *, const char *, size_t);
309 
310 #define _ucv_is_literal(str) ("" str)
311 #define ucv_stringbuf_append(buf, str) _ucv_stringbuf_append(buf, _ucv_is_literal(str), sizeof(str) - 1)
312 #define ucv_stringbuf_addstr(buf, str, len) _ucv_stringbuf_append(buf, str, len)
313 #define ucv_stringbuf_printf(buf, fmt, ...) sprintbuf(buf, fmt, __VA_ARGS__)
314 
315 uc_value_t *ucv_int64_new(int64_t);
316 uc_value_t *ucv_uint64_new(uint64_t);
317 int64_t ucv_int64_get(uc_value_t *);
318 uint64_t ucv_uint64_get(uc_value_t *);
319 
320 uc_value_t *ucv_double_new(double);
321 double ucv_double_get(uc_value_t *);
322 
323 uc_value_t *ucv_array_new(uc_vm_t *);
324 uc_value_t *ucv_array_new_length(uc_vm_t *, size_t);
325 uc_value_t *ucv_array_get(uc_value_t *, size_t);
326 uc_value_t *ucv_array_pop(uc_value_t *);
327 uc_value_t *ucv_array_push(uc_value_t *, uc_value_t *);
328 uc_value_t *ucv_array_shift(uc_value_t *);
329 uc_value_t *ucv_array_unshift(uc_value_t *, uc_value_t *);
330 void ucv_array_sort(uc_value_t *, int (*)(const void *, const void *));
331 bool ucv_array_delete(uc_value_t *, size_t, size_t);
332 bool ucv_array_set(uc_value_t *, size_t, uc_value_t *);
333 size_t ucv_array_length(uc_value_t *);
334 
335 uc_value_t *ucv_object_new(uc_vm_t *);
336 uc_value_t *ucv_object_get(uc_value_t *, const char *, bool *);
337 bool ucv_object_add(uc_value_t *, const char *, uc_value_t *);
338 bool ucv_object_delete(uc_value_t *, const char *);
339 size_t ucv_object_length(uc_value_t *);
340 
341 #define ucv_object_foreach(obj, key, val)                                                                                                               \
342         char *key = NULL;                                                                                                                                                       \
343         uc_value_t *val = NULL;                                                                                                                                         \
344         struct lh_entry *entry##key;                                                                                                                            \
345         struct lh_entry *entry_next##key = NULL;                                                                                                        \
346         for (entry##key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL;      \
347              (entry##key ? (key = (char *)lh_entry_k(entry##key),                                                                       \
348                             val = (uc_value_t *)lh_entry_v(entry##key),                                                         \
349                             entry_next##key = entry##key->next, entry##key)                                                     \
350                          : 0);                                                                                                                                          \
351              entry##key = entry_next##key)
352 
353 uc_value_t *ucv_function_new(const char *, size_t, uc_program_t *);
354 size_t ucv_function_srcpos(uc_value_t *, size_t);
355 
356 uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t);
357 
358 uc_value_t *ucv_closure_new(uc_vm_t *, uc_function_t *, bool);
359 
360 uc_resource_type_t *ucv_resource_type_add(uc_vm_t *, const char *, uc_value_t *, void (*)(void *));
361 uc_resource_type_t *ucv_resource_type_lookup(uc_vm_t *, const char *);
362 
363 uc_value_t *ucv_resource_new(uc_resource_type_t *, void *);
364 void **ucv_resource_dataptr(uc_value_t *, const char *);
365 
366 uc_value_t *ucv_regexp_new(const char *, bool, bool, bool, char **);
367 
368 uc_value_t *ucv_upvalref_new(size_t);
369 
370 uc_value_t *ucv_prototype_get(uc_value_t *);
371 bool ucv_prototype_set(uc_value_t *, uc_value_t *);
372 
373 uc_value_t *ucv_property_get(uc_value_t *, const char *);
374 
375 uc_value_t *ucv_from_json(uc_vm_t *, json_object *);
376 json_object *ucv_to_json(uc_value_t *);
377 
378 char *ucv_to_string(uc_vm_t *, uc_value_t *);
379 char *ucv_to_jsonstring_formatted(uc_vm_t *, uc_value_t *, char, size_t);
380 void ucv_to_stringbuf_formatted(uc_vm_t *, uc_stringbuf_t *, uc_value_t *, size_t, char, size_t);
381 
382 #define ucv_to_jsonstring(vm, val) ucv_to_jsonstring_formatted(vm, val, '\1', 0)
383 #define ucv_to_stringbuf(vm, buf, val, json) ucv_to_stringbuf_formatted(vm, buf, val, 0, json ? '\1' : '\0', 0)
384 
385 uc_type_t ucv_cast_number(uc_value_t *, int64_t *, double *);
386 
387 uc_value_t *ucv_to_number(uc_value_t *);
388 
389 static inline double
390 ucv_to_double(uc_value_t *v)
391 {
392         uc_value_t *nv;
393         double d;
394 
395         nv = ucv_to_number(v);
396         d = ucv_double_get(nv);
397         ucv_put(nv);
398 
399         return d;
400 }
401 
402 static inline int64_t
403 ucv_to_integer(uc_value_t *v)
404 {
405         uc_value_t *nv;
406         int64_t n;
407 
408         nv = ucv_to_number(v);
409         n = ucv_int64_get(nv);
410         ucv_put(nv);
411 
412         return n;
413 }
414 
415 static inline uint64_t
416 ucv_to_unsigned(uc_value_t *v)
417 {
418         uc_value_t *nv;
419         uint64_t u;
420 
421         nv = ucv_to_number(v);
422         u = ucv_uint64_get(nv);
423         ucv_put(nv);
424 
425         return u;
426 }
427 
428 static inline bool
429 ucv_is_callable(uc_value_t *uv)
430 {
431         switch (ucv_type(uv)) {
432         case UC_CLOSURE:
433         case UC_CFUNCTION:
434                 return true;
435 
436         default:
437                 return false;
438         }
439 }
440 
441 static inline bool
442 ucv_is_arrowfn(uc_value_t *uv)
443 {
444         uc_closure_t *closure = (uc_closure_t *)uv;
445 
446         return (ucv_type(uv) == UC_CLOSURE && closure->is_arrow);
447 }
448 
449 static inline bool
450 ucv_is_u64(uc_value_t *uv)
451 {
452         return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64 == true);
453 }
454 
455 static inline bool
456 ucv_is_scalar(uc_value_t *uv)
457 {
458         switch (ucv_type(uv)) {
459         case UC_NULL:
460         case UC_BOOLEAN:
461         case UC_DOUBLE:
462         case UC_INTEGER:
463         case UC_STRING:
464                 return true;
465 
466         default:
467                 return false;
468         }
469 }
470 
471 bool ucv_is_equal(uc_value_t *, uc_value_t *);
472 bool ucv_is_truish(uc_value_t *);
473 
474 bool ucv_compare(int, uc_value_t *, uc_value_t *, int *);
475 
476 uc_value_t *ucv_key_get(uc_vm_t *, uc_value_t *, uc_value_t *);
477 uc_value_t *ucv_key_set(uc_vm_t *, uc_value_t *, uc_value_t *, uc_value_t *);
478 bool ucv_key_delete(uc_vm_t *, uc_value_t *, uc_value_t *);
479 
480 
481 static inline bool
482 ucv_is_marked(uc_value_t *uv)
483 {
484         return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->mark == true);
485 }
486 
487 static inline void
488 ucv_set_mark(uc_value_t *uv)
489 {
490         if (((uintptr_t)uv & 3) == 0 && uv != NULL)
491                 uv->mark = true;
492 }
493 
494 static inline void
495 ucv_clear_mark(uc_value_t *uv)
496 {
497         if (((uintptr_t)uv & 3) == 0 && uv != NULL)
498                 uv->mark = false;
499 }
500 
501 void ucv_gc(uc_vm_t *);
502 
503 void ucv_freeall(uc_vm_t *);
504 
505 #endif /* __TYPES_H_ */
506 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt