Loading...
Searching...
No Matches
mjs_core.h
1/*
2 * Copyright (c) 2017 Cesanta Software Limited
3 * All rights reserved
4 */
5
6#ifndef MJS_CORE_H
7#define MJS_CORE_H
8
9#include "mjs_ffi.h"
10#include "mjs_gc.h"
11#include "mjs_internal.h"
12
13#if defined(__cplusplus)
14extern "C" {
15#endif /* __cplusplus */
16
17#define JUMP_INSTRUCTION_SIZE 2
18
19enum mjs_call_stack_frame_item {
20 CALL_STACK_FRAME_ITEM_RETVAL_STACK_IDX, /* TOS */
21 CALL_STACK_FRAME_ITEM_LOOP_ADDR_IDX,
22 CALL_STACK_FRAME_ITEM_SCOPE_IDX,
23 CALL_STACK_FRAME_ITEM_RETURN_ADDR,
24 CALL_STACK_FRAME_ITEM_THIS,
25
26 CALL_STACK_FRAME_ITEMS_CNT
27};
28
29struct mjs_vals {
30 /* Current `this` value */
31 mjs_val_t this_obj;
32 mjs_val_t dataview_proto;
33
34 /*
35 * The object against which the last `OP_GET` was invoked. Needed for
36 * "method invocation pattern".
37 */
38 mjs_val_t last_getprop_obj;
39};
40
42 /* Global index of the bcode part */
43 size_t start_idx;
44
45 /* Actual bcode data */
46 struct {
47 const char* p; /* Memory chunk pointer */
48 size_t len; /* Memory chunk length */
49 } data;
50
51 /*
52 * Result of evaluation (not parsing: if there is an error during parsing,
53 * the bcode is not even committed). It is used to determine whether we
54 * need to evaluate the file: if file was already evaluated, and the result
55 * was MJS_OK, then we won't evaluate it again. Otherwise, we will.
56 */
57 mjs_err_t exec_res : 4;
58
59 /* If set, bcode data does not need to be freed */
60 unsigned in_rom : 1;
61};
62
63struct mjs {
64 struct mbuf bcode_gen;
65 struct mbuf bcode_parts;
66 size_t bcode_len;
67 struct mbuf stack;
68 struct mbuf call_stack;
69 struct mbuf arg_stack;
70 struct mbuf scopes; /* Scope objects */
71 struct mbuf loop_addresses; /* Addresses for breaks & continues */
72 struct mbuf owned_strings; /* Sequence of (varint len, char data[]) */
73 struct mbuf foreign_strings; /* Sequence of (varint len, char *data) */
74 struct mbuf owned_values;
75 struct mbuf json_visited_stack;
76 struct mbuf array_buffers;
77 struct mjs_vals vals;
78 char* error_msg;
79 char* stack_trace;
80 enum mjs_err error;
81 mjs_ffi_resolver_t* dlsym; /* Symbol resolver function for FFI */
82 void* dlsym_handle;
83 ffi_cb_args_t* ffi_cb_args; /* List of FFI args descriptors */
84 size_t cur_bcode_offset;
85 mjs_flags_poller_t exec_flags_poller;
86 void* context;
87
88 struct gc_arena object_arena;
89 struct gc_arena property_arena;
90 struct gc_arena ffi_sig_arena;
91
92 unsigned inhibit_gc : 1;
93 unsigned need_gc : 1;
94 unsigned generate_jsc : 1;
95};
96
97/*
98 * Bcode header: type of the items, and item numbers.
99 */
100typedef uint32_t mjs_header_item_t;
101enum mjs_header_items {
102 MJS_HDR_ITEM_TOTAL_SIZE, /* Total size of the bcode (not counting the
103 OP_BCODE_HEADER byte) */
104 MJS_HDR_ITEM_BCODE_OFFSET, /* Offset to the start of the actual bcode (not
105 counting the OP_BCODE_HEADER byte) */
106 MJS_HDR_ITEM_MAP_OFFSET, /* Offset to the start of offset-to-line_no mapping
107 k*/
108
109 MJS_HDR_ITEMS_CNT
110};
111
112MJS_PRIVATE size_t mjs_get_func_addr(mjs_val_t v);
113
114MJS_PRIVATE int mjs_getretvalpos(struct mjs* mjs);
115
116MJS_PRIVATE enum mjs_type mjs_get_type(mjs_val_t v);
117
118/*
119 * Prints stack trace starting from the given bcode offset; other offsets
120 * (if any) will be fetched from the call_stack.
121 */
122MJS_PRIVATE void mjs_gen_stack_trace(struct mjs* mjs, size_t offset);
123
124MJS_PRIVATE mjs_val_t vtop(struct mbuf* m);
125MJS_PRIVATE size_t mjs_stack_size(const struct mbuf* m);
126MJS_PRIVATE mjs_val_t* vptr(struct mbuf* m, int idx);
127MJS_PRIVATE void push_mjs_val(struct mbuf* m, mjs_val_t v);
128MJS_PRIVATE mjs_val_t mjs_pop_val(struct mbuf* m);
129MJS_PRIVATE mjs_val_t mjs_pop(struct mjs* mjs);
130MJS_PRIVATE void mjs_push(struct mjs* mjs, mjs_val_t v);
131MJS_PRIVATE void mjs_die(struct mjs* mjs);
132
133#if defined(__cplusplus)
134}
135#endif /* __cplusplus */
136
137#endif /* MJS_CORE_H */
Definition mjs_mm.h:25
Definition mbuf.h:48
Definition mjs_core.h:41
Definition mjs_ffi.h:107
Definition mjs_core.h:29
Definition mjs_core.h:63