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

Sources/ucode/include/ucode/lib.h

  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 __hidden bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact);
 35 __hidden bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off);
 36 __hidden void uc_error_message_indent(char **msg);
 37 
 38 __hidden uc_value_t *uc_require_library(uc_vm_t *vm, uc_value_t *nameval, bool so_only);
 39 
 40 /* vm helper */
 41 static inline uc_value_t *
 42 _uc_fn_this_res(uc_vm_t *vm)
 43 {
 44         return vm->callframes.entries[vm->callframes.count - 1].ctx;
 45 }
 46 
 47 static inline void *
 48 _uc_fn_this(uc_vm_t *vm, const char *expected_type)
 49 {
 50         return ucv_resource_dataptr(_uc_fn_this_res(vm), expected_type);
 51 }
 52 
 53 static inline void *
 54 _uc_fn_thisval(uc_vm_t *vm, const char *expected_type)
 55 {
 56         return ucv_resource_data(_uc_fn_this_res(vm), expected_type);
 57 }
 58 
 59 #define uc_fn_this(...) _uc_fn_this(vm, __VA_ARGS__)
 60 #define uc_fn_thisval(...) _uc_fn_thisval(vm, __VA_ARGS__)
 61 
 62 static inline uc_value_t *
 63 _uc_fn_arg(uc_vm_t *vm, size_t nargs, size_t n)
 64 {
 65         if (n >= nargs)
 66                 return NULL;
 67 
 68         return uc_vm_stack_peek(vm, nargs - n - 1);
 69 }
 70 
 71 #define uc_fn_arg(...) _uc_fn_arg(vm, nargs, __VA_ARGS__)
 72 
 73 #define uc_call(nargs) uc_vm_call(vm, false, nargs)
 74 #define uc_value_push(val) uc_vm_stack_push(vm, val)
 75 #define uc_value_pop() uc_vm_stack_pop(vm)
 76 
 77 
 78 /* resource type helper */
 79 
 80 static inline uc_value_t *
 81 uc_resource_new(uc_resource_type_t *type, void *data)
 82 {
 83         return ucv_resource_new(type, data);
 84 }
 85 
 86 static inline uc_resource_type_t *
 87 _uc_type_declare(uc_vm_t *vm, const char *name, const uc_function_list_t *list, size_t len, void (*freefn)(void *))
 88 {
 89         uc_value_t *proto = ucv_object_new(NULL);
 90 
 91         while (len-- > 0)
 92                 ucv_object_add(proto, list[len].name,
 93                         ucv_cfunction_new(list[len].name, list[len].func));
 94 
 95         return ucv_resource_type_add(vm, name, proto, freefn);
 96 }
 97 
 98 #define uc_type_declare(vm, name, functions, freefn) \
 99         _uc_type_declare(vm, name, functions, ARRAY_SIZE(functions), freefn)
100 
101 
102 /* function helper */
103 
104 #define uc_function_register(object, name, function) \
105         ucv_object_add(object, name, ucv_cfunction_new(name, function))
106 
107 static inline bool
108 _uc_function_list_register(uc_value_t *object, const uc_function_list_t *list, size_t len)
109 {
110         bool rv = true;
111 
112         while (len-- > 0)
113                 rv &= uc_function_register(object, list[len].name, list[len].func);
114 
115         return rv;
116 }
117 
118 #define uc_function_list_register(object, functions) \
119         _uc_function_list_register(object, functions, ARRAY_SIZE(functions))
120 
121 #endif /* UCODE_LIB_H */
122 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt