Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / include / mruby / irep.h
1 /**
2 ** @file mruby/irep.h - mrb_irep structure
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #ifndef MRUBY_IREP_H
8 #define MRUBY_IREP_H
9
10 #include "common.h"
11 #include <mruby/compile.h>
12
13 /**
14  * Compiled mruby scripts.
15  */
16 MRB_BEGIN_DECL
17
18 enum irep_pool_type {
19   IREP_TT_STRING,
20   IREP_TT_FIXNUM,
21   IREP_TT_FLOAT,
22 };
23
24 struct mrb_locals {
25   mrb_sym name;
26   uint16_t r;
27 };
28
29 /* Program data array struct */
30 typedef struct mrb_irep {
31   uint16_t nlocals;        /* Number of local variables */
32   uint16_t nregs;          /* Number of register variables */
33   uint8_t flags;
34
35   const mrb_code *iseq;
36   mrb_value *pool;
37   mrb_sym *syms;
38   struct mrb_irep **reps;
39
40   struct mrb_locals *lv;
41   /* debug info */
42   struct mrb_irep_debug_info* debug_info;
43
44   uint16_t ilen, plen, slen, rlen;
45   uint32_t refcnt;
46 } mrb_irep;
47
48 #define MRB_ISEQ_NO_FREE 1
49
50 MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb);
51
52 /** load mruby bytecode functions
53 * Please note! Currently due to interactions with the GC calling these functions will
54 * leak one RProc object per function call.
55 * To prevent this save the current memory arena before calling and restore the arena
56 * right after, like so
57 * int ai = mrb_gc_arena_save(mrb);
58 * mrb_value status = mrb_load_irep(mrb, buffer);
59 * mrb_gc_arena_restore(mrb, ai);
60 */
61
62 /* @param [const uint8_t*] irep code, expected as a literal */
63 MRB_API mrb_value mrb_load_irep(mrb_state*, const uint8_t*);
64
65 /*
66  * @param [const void*] irep code
67  * @param [size_t] size of irep buffer. If -1 is given, it is considered unrestricted.
68  */
69 MRB_API mrb_value mrb_load_irep_buf(mrb_state*, const void*, size_t);
70
71 /* @param [const uint8_t*] irep code, expected as a literal */
72 MRB_API mrb_value mrb_load_irep_cxt(mrb_state*, const uint8_t*, mrbc_context*);
73
74 /*
75  * @param [const void*] irep code
76  * @param [size_t] size of irep buffer. If -1 is given, it is considered unrestricted.
77  */
78 MRB_API mrb_value mrb_load_irep_buf_cxt(mrb_state*, const void*, size_t, mrbc_context*);
79
80 void mrb_irep_free(mrb_state*, struct mrb_irep*);
81 void mrb_irep_incref(mrb_state*, struct mrb_irep*);
82 void mrb_irep_decref(mrb_state*, struct mrb_irep*);
83 void mrb_irep_cutref(mrb_state*, struct mrb_irep*);
84 void mrb_irep_remove_lv(mrb_state *mrb, mrb_irep *irep);
85
86 struct mrb_insn_data {
87   uint8_t insn;
88   uint16_t a;
89   uint16_t b;
90   uint8_t c;
91 };
92
93 struct mrb_insn_data mrb_decode_insn(const mrb_code *pc);
94
95 MRB_END_DECL
96
97 #endif  /* MRUBY_IREP_H */