1 /* 2 * Copyright (C) 2020-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_LIB_H 18 #define UCODE_LIB_H 19 20 #include "vm.h" 21 #include "lexer.h" 22 23 24 typedef struct { 25 const char *name; 26 uc_cfn_ptr_t func; 27 } uc_function_list_t; 28 29 extern const uc_function_list_t uc_stdlib_functions[]; 30 31 void uc_stdlib_load(uc_value_t *scope); 32 uc_cfn_ptr_t uc_stdlib_function(const char *name); 33 34 bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact); 35 bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off); 36 37 38 /* vm helper */ 39 40 static inline void * 41 _uc_fn_this(uc_vm_t *vm, const char *expected_type) 42 { 43 return ucv_resource_dataptr(vm->callframes.entries[vm->callframes.count - 1].ctx, expected_type); 44 } 45 46 #define uc_fn_this(...) _uc_fn_this(vm, __VA_ARGS__) 47 48 static inline uc_value_t * 49 _uc_fn_arg(uc_vm_t *vm, size_t nargs, size_t n) 50 { 51 if (n >= nargs) 52 return NULL; 53 54 return uc_vm_stack_peek(vm, nargs - n - 1); 55 } 56 57 #define uc_fn_arg(...) _uc_fn_arg(vm, nargs, __VA_ARGS__) 58 59 #define uc_call(nargs) uc_vm_call(vm, false, nargs) 60 #define uc_value_push(val) uc_vm_stack_push(vm, val) 61 #define uc_value_pop() uc_vm_stack_pop(vm) 62 63 64 /* resource type helper */ 65 66 static inline uc_value_t * 67 uc_resource_new(uc_resource_type_t *type, void *data) 68 { 69 return ucv_resource_new(type, data); 70 } 71 72 static inline uc_resource_type_t * 73 _uc_type_declare(uc_vm_t *vm, const char *name, const uc_function_list_t *list, size_t len, void (*freefn)(void *)) 74 { 75 uc_value_t *proto = ucv_object_new(NULL); 76 77 while (len-- > 0) 78 ucv_object_add(proto, list[len].name, 79 ucv_cfunction_new(list[len].name, list[len].func)); 80 81 return ucv_resource_type_add(vm, name, proto, freefn); 82 } 83 84 #define uc_type_declare(vm, name, functions, freefn) \ 85 _uc_type_declare(vm, name, functions, ARRAY_SIZE(functions), freefn) 86 87 88 /* function helper */ 89 90 #define uc_function_register(object, name, function) \ 91 ucv_object_add(object, name, ucv_cfunction_new(name, function)) 92 93 static inline bool 94 _uc_function_list_register(uc_value_t *object, const uc_function_list_t *list, size_t len) 95 { 96 bool rv = true; 97 98 while (len-- > 0) 99 rv &= uc_function_register(object, list[len].name, list[len].func); 100 101 return rv; 102 } 103 104 #define uc_function_list_register(object, functions) \ 105 _uc_function_list_register(object, functions, ARRAY_SIZE(functions)) 106 107 #endif /* UCODE_LIB_H */ 108
This page was automatically generated by LXR 0.3.1. • OpenWrt