• 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 UCODE_TYPES_H
 18 #define UCODE_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_CFUNCTION,
 40         UC_CLOSURE,
 41         UC_UPVALUE,
 42         UC_RESOURCE,
 43         UC_PROGRAM,
 44         UC_SOURCE
 45 } uc_type_t;
 46 
 47 typedef struct uc_value {
 48         uint32_t type:4;
 49         uint32_t mark:1;
 50         uint32_t u64_or_constant:1;
 51         uint32_t refcount:26;
 52 } uc_value_t;
 53 
 54 
 55 /* Constant list defintions */
 56 
 57 typedef struct {
 58         size_t isize;
 59         size_t dsize;
 60         uint64_t *index;
 61         char *data;
 62 } uc_value_list_t;
 63 
 64 
 65 /* Source buffer defintions */
 66 
 67 uc_declare_vector(uc_lineinfo_t, uint8_t);
 68 
 69 typedef struct {
 70         uc_value_t header;
 71         char *filename, *runpath, *buffer;
 72         FILE *fp;
 73         size_t off;
 74         uc_lineinfo_t lineinfo;
 75         struct {
 76                 size_t count, offset;
 77                 uc_value_t **entries;
 78         } exports;
 79 } uc_source_t;
 80 
 81 
 82 /* Bytecode chunk defintions */
 83 
 84 typedef struct {
 85         size_t from, to, target, slot;
 86 } uc_ehrange_t;
 87 
 88 typedef struct {
 89         size_t from, to, slot, nameidx;
 90 } uc_varrange_t;
 91 
 92 uc_declare_vector(uc_ehranges_t, uc_ehrange_t);
 93 uc_declare_vector(uc_variables_t, uc_varrange_t);
 94 uc_declare_vector(uc_offsetinfo_t, uint8_t);
 95 
 96 typedef struct {
 97         size_t count;
 98         uint8_t *entries;
 99         uc_ehranges_t ehranges;
100         struct {
101                 uc_variables_t variables;
102                 uc_value_list_t varnames;
103                 uc_offsetinfo_t offsets;
104         } debuginfo;
105 } uc_chunk_t;
106 
107 
108 /* Value type structures */
109 
110 typedef struct uc_weakref {
111         struct uc_weakref *prev;
112         struct uc_weakref *next;
113 } uc_weakref_t;
114 
115 typedef struct uc_function {
116         uc_weakref_t progref;
117         bool arrow, vararg, strict, module;
118         size_t nargs;
119         size_t nupvals;
120         size_t srcidx;
121         size_t srcpos;
122         uc_chunk_t chunk;
123         struct uc_program *program;
124         char name[];
125 } uc_function_t;
126 
127 typedef struct {
128         uc_value_t header;
129         double dbl;
130 } uc_double_t;
131 
132 typedef struct {
133         uc_value_t header;
134         union {
135                 int64_t s64;
136                 uint64_t u64;
137         } i;
138 } uc_integer_t;
139 
140 typedef struct {
141         uc_value_t header;
142         size_t length;
143         char str[];
144 } uc_string_t;
145 
146 typedef struct {
147         uc_value_t header;
148         uc_weakref_t ref;
149         size_t count;
150         uc_value_t *proto;
151         uc_value_t **entries;
152 } uc_array_t;
153 
154 typedef struct {
155         uc_value_t header;
156         uc_weakref_t ref;
157         uc_value_t *proto;
158         struct lh_table *table;
159 } uc_object_t;
160 
161 typedef struct {
162         uc_value_t header;
163         regex_t regexp;
164         bool icase, newline, global;
165         char source[];
166 } uc_regexp_t;
167 
168 typedef struct uc_upval_tref {
169         uc_value_t header;
170         size_t slot;
171         bool closed;
172         uc_value_t *value;
173         struct uc_upval_tref *next;
174 } uc_upvalref_t;
175 
176 typedef struct {
177         uc_value_t header;
178         uc_weakref_t ref;
179         bool is_arrow;
180         uc_function_t *function;
181         uc_upvalref_t **upvals;
182 } uc_closure_t;
183 
184 typedef struct uc_vm uc_vm_t;
185 typedef uc_value_t *(*uc_cfn_ptr_t)(uc_vm_t *, size_t);
186 
187 typedef struct {
188         uc_value_t header;
189         uc_cfn_ptr_t cfn;
190         char name[];
191 } uc_cfunction_t;
192 
193 typedef struct {
194         const char *name;
195         uc_value_t *proto;
196         void (*free)(void *);
197 } uc_resource_type_t;
198 
199 typedef struct {
200         uc_value_t header;
201         uc_resource_type_t *type;
202         void *data;
203 } uc_resource_t;
204 
205 uc_declare_vector(uc_resource_types_t, uc_resource_type_t *);
206 
207 
208 /* Program structure definitions */
209 
210 uc_declare_vector(uc_sources_t, uc_source_t *);
211 
212 typedef struct uc_program {
213         uc_value_t header;
214         uc_value_list_t constants;
215         uc_weakref_t functions;
216         uc_sources_t sources;
217 } uc_program_t;
218 
219 
220 /* Parser definitions */
221 
222 uc_declare_vector(uc_search_path_t, char *);
223 
224 typedef struct {
225         bool lstrip_blocks;
226         bool trim_blocks;
227         bool strict_declarations;
228         bool raw_mode;
229         uc_search_path_t module_search_path;
230         uc_search_path_t force_dynlink_list;
231 } uc_parse_config_t;
232 
233 extern uc_parse_config_t uc_default_parse_config;
234 
235 void uc_search_path_init(uc_search_path_t *search_path);
236 
237 static inline void
238 uc_search_path_add(uc_search_path_t *search_path, char *path) {
239         uc_vector_push(search_path, xstrdup(path));
240 }
241 
242 static inline void
243 uc_search_path_free(uc_search_path_t *search_path) {
244         while (search_path->count > 0)
245                 free(search_path->entries[--search_path->count]);
246 
247         uc_vector_clear(search_path);
248 }
249 
250 
251 /* VM definitions */
252 
253 typedef enum {
254         EXCEPTION_NONE,
255         EXCEPTION_SYNTAX,
256         EXCEPTION_RUNTIME,
257         EXCEPTION_TYPE,
258         EXCEPTION_REFERENCE,
259         EXCEPTION_USER,
260         EXCEPTION_EXIT
261 } uc_exception_type_t;
262 
263 typedef struct {
264         uc_exception_type_t type;
265         uc_value_t *stacktrace;
266         char *message;
267 } uc_exception_t;
268 
269 typedef struct {
270         uint8_t *ip;
271         uc_closure_t *closure;
272         uc_cfunction_t *cfunction;
273         size_t stackframe;
274         uc_value_t *ctx;
275         bool mcall, strict;
276 } uc_callframe_t;
277 
278 uc_declare_vector(uc_callframes_t, uc_callframe_t);
279 uc_declare_vector(uc_stack_t, uc_value_t *);
280 uc_declare_vector(uc_modexports_t, uc_upvalref_t *);
281 
282 typedef struct printbuf uc_stringbuf_t;
283 
284 typedef void (uc_exception_handler_t)(uc_vm_t *, uc_exception_t *);
285 
286 struct uc_vm {
287         uc_stack_t stack;
288         uc_exception_t exception;
289         uc_callframes_t callframes;
290         uc_upvalref_t *open_upvals;
291         uc_parse_config_t *config;
292         uc_value_t *globals;
293         uc_value_t *registry;
294         uc_source_t *sources;
295         uc_weakref_t values;
296         uc_resource_types_t restypes;
297         uc_modexports_t exports;
298         union {
299                 uint32_t u32;
300                 int32_t s32;
301                 uint16_t u16;
302                 int16_t s16;
303                 uint8_t u8;
304                 int8_t s8;
305         } arg;
306         size_t alloc_refs;
307         uint8_t trace;
308         uint8_t gc_flags;
309         uint16_t gc_interval;
310         uc_stringbuf_t *strbuf;
311         uc_exception_handler_t *exhandler;
312         FILE *output;
313 };
314 
315 
316 /* Value API */
317 
318 __hidden void ucv_free(uc_value_t *, bool);
319 __hidden void ucv_unref(uc_weakref_t *);
320 __hidden void ucv_ref(uc_weakref_t *, uc_weakref_t *);
321 
322 uc_value_t *ucv_get(uc_value_t *uv);
323 void ucv_put(uc_value_t *);
324 
325 uc_type_t ucv_type(uc_value_t *);
326 const char *ucv_typename(uc_value_t *);
327 
328 uc_value_t *ucv_boolean_new(bool);
329 bool ucv_boolean_get(uc_value_t *);
330 
331 uc_value_t *ucv_string_new(const char *);
332 uc_value_t *ucv_string_new_length(const char *, size_t);
333 size_t ucv_string_length(uc_value_t *);
334 
335 char *_ucv_string_get(uc_value_t **);
336 #define ucv_string_get(uv) _ucv_string_get((uc_value_t **)&uv)
337 
338 uc_stringbuf_t *ucv_stringbuf_new(void);
339 uc_value_t *ucv_stringbuf_finish(uc_stringbuf_t *);
340 
341 void _ucv_stringbuf_append(uc_stringbuf_t *, const char *, size_t);
342 
343 #define _ucv_is_literal(str) ("" str)
344 #define ucv_stringbuf_append(buf, str) _ucv_stringbuf_append(buf, _ucv_is_literal(str), sizeof(str) - 1)
345 #define ucv_stringbuf_addstr(buf, str, len) _ucv_stringbuf_append(buf, str, len)
346 #define ucv_stringbuf_printf(buf, fmt, ...) sprintbuf(buf, fmt, __VA_ARGS__)
347 
348 uc_value_t *ucv_int64_new(int64_t);
349 uc_value_t *ucv_uint64_new(uint64_t);
350 int64_t ucv_int64_get(uc_value_t *);
351 uint64_t ucv_uint64_get(uc_value_t *);
352 
353 uc_value_t *ucv_double_new(double);
354 double ucv_double_get(uc_value_t *);
355 
356 uc_value_t *ucv_array_new(uc_vm_t *);
357 uc_value_t *ucv_array_new_length(uc_vm_t *, size_t);
358 uc_value_t *ucv_array_get(uc_value_t *, size_t);
359 uc_value_t *ucv_array_pop(uc_value_t *);
360 uc_value_t *ucv_array_push(uc_value_t *, uc_value_t *);
361 uc_value_t *ucv_array_shift(uc_value_t *);
362 uc_value_t *ucv_array_unshift(uc_value_t *, uc_value_t *);
363 void ucv_array_sort(uc_value_t *, int (*)(const void *, const void *));
364 bool ucv_array_delete(uc_value_t *, size_t, size_t);
365 bool ucv_array_set(uc_value_t *, size_t, uc_value_t *);
366 size_t ucv_array_length(uc_value_t *);
367 
368 uc_value_t *ucv_object_new(uc_vm_t *);
369 uc_value_t *ucv_object_get(uc_value_t *, const char *, bool *);
370 bool ucv_object_add(uc_value_t *, const char *, uc_value_t *);
371 bool ucv_object_delete(uc_value_t *, const char *);
372 size_t ucv_object_length(uc_value_t *);
373 
374 #define ucv_object_foreach(obj, key, val)                                                                                                               \
375         char *key = NULL;                                                                                                                                                       \
376         uc_value_t *val = NULL;                                                                                                                                         \
377         struct lh_entry *entry##key;                                                                                                                            \
378         struct lh_entry *entry_next##key = NULL;                                                                                                        \
379         for (entry##key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL;      \
380              (entry##key ? (key = (char *)lh_entry_k(entry##key),                                                                       \
381                             val = (uc_value_t *)lh_entry_v(entry##key),                                                         \
382                             entry_next##key = entry##key->next, entry##key)                                                     \
383                          : 0);                                                                                                                                          \
384              entry##key = entry_next##key)
385 
386 uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t);
387 
388 uc_value_t *ucv_closure_new(uc_vm_t *, uc_function_t *, bool);
389 
390 uc_resource_type_t *ucv_resource_type_add(uc_vm_t *, const char *, uc_value_t *, void (*)(void *));
391 uc_resource_type_t *ucv_resource_type_lookup(uc_vm_t *, const char *);
392 
393 uc_value_t *ucv_resource_new(uc_resource_type_t *, void *);
394 void **ucv_resource_dataptr(uc_value_t *, const char *);
395 
396 uc_value_t *ucv_regexp_new(const char *, bool, bool, bool, char **);
397 
398 uc_value_t *ucv_upvalref_new(size_t);
399 
400 uc_value_t *ucv_prototype_get(uc_value_t *);
401 bool ucv_prototype_set(uc_value_t *, uc_value_t *);
402 
403 uc_value_t *ucv_property_get(uc_value_t *, const char *);
404 
405 uc_value_t *ucv_from_json(uc_vm_t *, json_object *);
406 json_object *ucv_to_json(uc_value_t *);
407 
408 char *ucv_to_string(uc_vm_t *, uc_value_t *);
409 char *ucv_to_jsonstring_formatted(uc_vm_t *, uc_value_t *, char, size_t);
410 void ucv_to_stringbuf_formatted(uc_vm_t *, uc_stringbuf_t *, uc_value_t *, size_t, char, size_t);
411 
412 #define ucv_to_jsonstring(vm, val) ucv_to_jsonstring_formatted(vm, val, '\1', 0)
413 #define ucv_to_stringbuf(vm, buf, val, json) ucv_to_stringbuf_formatted(vm, buf, val, 0, json ? '\1' : '\0', 0)
414 
415 uc_type_t ucv_cast_number(uc_value_t *, int64_t *, double *);
416 
417 uc_value_t *ucv_to_number(uc_value_t *);
418 
419 static inline double
420 ucv_to_double(uc_value_t *v)
421 {
422         uc_value_t *nv;
423         double d;
424 
425         nv = ucv_to_number(v);
426         d = ucv_double_get(nv);
427         ucv_put(nv);
428 
429         return d;
430 }
431 
432 static inline int64_t
433 ucv_to_integer(uc_value_t *v)
434 {
435         uc_value_t *nv;
436         int64_t n;
437 
438         nv = ucv_to_number(v);
439         n = ucv_int64_get(nv);
440         ucv_put(nv);
441 
442         return n;
443 }
444 
445 static inline uint64_t
446 ucv_to_unsigned(uc_value_t *v)
447 {
448         uc_value_t *nv;
449         uint64_t u;
450 
451         nv = ucv_to_number(v);
452         u = ucv_uint64_get(nv);
453         ucv_put(nv);
454 
455         return u;
456 }
457 
458 static inline bool
459 ucv_is_callable(uc_value_t *uv)
460 {
461         switch (ucv_type(uv)) {
462         case UC_CLOSURE:
463         case UC_CFUNCTION:
464                 return true;
465 
466         default:
467                 return false;
468         }
469 }
470 
471 static inline bool
472 ucv_is_arrowfn(uc_value_t *uv)
473 {
474         uc_closure_t *closure = (uc_closure_t *)uv;
475 
476         return (ucv_type(uv) == UC_CLOSURE && closure->is_arrow);
477 }
478 
479 static inline bool
480 ucv_is_u64(uc_value_t *uv)
481 {
482         return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true &&
483                     uv->type == UC_INTEGER);
484 }
485 
486 static inline bool
487 ucv_is_constant(uc_value_t *uv)
488 {
489         return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant == true &&
490                 (uv->type == UC_ARRAY || uv->type == UC_OBJECT));
491 }
492 
493 static inline bool
494 ucv_set_constant(uc_value_t *uv, bool constant)
495 {
496         if (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->u64_or_constant != constant &&
497             (uv->type == UC_ARRAY || uv->type == UC_OBJECT)) {
498                 uv->u64_or_constant = constant;
499 
500                 return true;
501         }
502 
503         return false;
504 }
505 
506 static inline bool
507 ucv_is_scalar(uc_value_t *uv)
508 {
509         switch (ucv_type(uv)) {
510         case UC_NULL:
511         case UC_BOOLEAN:
512         case UC_DOUBLE:
513         case UC_INTEGER:
514         case UC_STRING:
515                 return true;
516 
517         default:
518                 return false;
519         }
520 }
521 
522 bool ucv_is_equal(uc_value_t *, uc_value_t *);
523 bool ucv_is_truish(uc_value_t *);
524 
525 bool ucv_compare(int, uc_value_t *, uc_value_t *, int *);
526 
527 uc_value_t *ucv_key_get(uc_vm_t *, uc_value_t *, uc_value_t *);
528 uc_value_t *ucv_key_set(uc_vm_t *, uc_value_t *, uc_value_t *, uc_value_t *);
529 bool ucv_key_delete(uc_vm_t *, uc_value_t *, uc_value_t *);
530 
531 
532 static inline bool
533 ucv_is_marked(uc_value_t *uv)
534 {
535         return (((uintptr_t)uv & 3) == 0 && uv != NULL && uv->mark == true);
536 }
537 
538 static inline void
539 ucv_set_mark(uc_value_t *uv)
540 {
541         if (((uintptr_t)uv & 3) == 0 && uv != NULL)
542                 uv->mark = true;
543 }
544 
545 static inline void
546 ucv_clear_mark(uc_value_t *uv)
547 {
548         if (((uintptr_t)uv & 3) == 0 && uv != NULL)
549                 uv->mark = false;
550 }
551 
552 void ucv_gc(uc_vm_t *);
553 
554 __hidden void ucv_freeall(uc_vm_t *);
555 
556 #endif /* UCODE_TYPES_H */
557 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt