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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt