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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt