/
api.c
168 lines (152 loc) · 5.06 KB
/
api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "parrot/parrot.h"
#include "parrot/api.h"
#define GET_RAW_INTERP(p) ((Parrot_ParrotInterpreter_attributes)(p)->data)->interp;
#define EMBED_API_CALLIN(p, i) \
jmp_buf _env; \
Interp * (i) = PMC_IS_NULL(p) ? NULL : GET_RAW_INTERP(p); \
void *_oldtop = (i)->lo_var_ptr; \
if (_oldtop) {} else (1)->lo_var_ptr = &oldtop \
PARROT_ASSERT(i); \
PARROT_ASSERT((i)->lo_val_ptr); \
if (setjmp(_env)) { \
(i)->api_jmp_buf = NULL; \
return 0; \
} else { \
(i)->api_jmp_buf = _env; \
{
#define EMBED_API_CALLOUT(p, i) \
} \
if (!_oldtop) {\
PARROT_ASSERT((i)->lo_var_ptr == &oldtop);\
(i)->lo_var_ptr = NULL;\
}\
return 1; \
}
#define EMBED_API_FAILURE(p, i) \
do { \
if (!_oldtop) {\
PARROT_ASSERT((i)->lo_var_ptr == &oldtop);\
(i)->lo_var_ptr = NULL;\
} \
return 0; \
}
PARROT_API
PARROT_CANNOT_RETURN_NULL
PARROT_MALLOC
INTVAL
Parrot_api_make_interpreter(ARGIN_NULLOK(PMC *parent), INTVAL flags, ARGIN_NULLOK(Parrot_Init_Args *args), ARGOUT(PMC **interp))
{
ASSERT_ARGS(Parrot_api_make_interpreter)
int alt_stacktop;
void *stacktop_ptr = &alt_stacktop;
Parrot_set_config_hash();
{
Parrot_Interp * const parent_raw = PMC_IS_NULL(parent) ? NULL : GET_RAW_INTERP(parent);
Parrot_Interp * const interp_raw = allocate_interpreter(parent_raw, flags);
if (args) {
if (args->stack_top)
stacktop_ptr = args->stacktop;
if (args->gc_system)
interp->gc_sys->sys_type = args->gc_system;
if (args->gc_threshold)
interp->gc_threshold = args->gc_threshold;
if (args->hash_seed)
interp->hash_seed = args->hash_seed;
}
}
initialize_interpreter(interp_raw, stacktop);
*interp_pmc = VTABLE_get_pmc_keyed_int(interp, iglobals, (INTVAL)IGLOBALS_INTERPRETER);
return !PMC_IS_NULL(*interp);
}
PARROT_API
INTVAL
Parrot_api_set_runcore(ARGIN(PMC *interp_pmc), Parrot_Run_core_t core, Parrot_Uint trace)
{
ASSERT_ARGS(Parrot_api_set_runcore)
if (trace)
core = PARROT_SLOW_CORE;
EMBED_API_CALLIN(interp_pmc, interp)
Parrot_set_trace(interp, (Parrot_trace_flags)trace);
Parrot_set_run_core(interp, core);
EMBED_API_CALLOUT(interp_pmc, interp)
}
PARROT_API
INTVAL
Parrot_api_set_executable_name(ARGIN(PMC *interp_pmc), ARGIN(Parrot_String) name)
{
ASSERT_ARGS(Parrot_api_set_executable_name)
EMBED_API_CALLIN(interp_pmc, interp);
PMC * const name_pmc = Parrot_pmc_new(interp, enum_class_String);
VTABLE_set_string_native(interp, name_pmc, name);
VTABLE_set_pmc_keyed_int(interp, interp->iglobals, IGLOBALS_EXECUTABLE,
name_pmc);
EMBED_API_CALLOUT(interp_pmc, interp);
}
PARROT_API
INTVAL
Parrot_api_destroy_interpreter(ARGIN(PMC *interp_pmc))
{
ASSERT_ARGS(Parrot_api_destroy_interpreter)
EMBED_API_CALLIN(interp_pmc, interp);
Parrot_destroy(interp);
EMBED_API_CALLOUT(interp_pmc, interp);
}
PARROT_API
INTVAL
Parrot_api_exit_interpreter(ARGIN(PMC *interp_pmc))
{
ASSERT_ARGS(Parrot_api_exit_interpreter)
EMBED_API_CALLIN(interp_pmc, interp);
Parrot_exit(interp);
EMBED_API_CALLOUT(interp_pmc, interp);
}
/*
=item C<int Parrot_api_load_bytecode_file(PARROT_INTERP, const char *filename)>
Load a bytecode file and return a bytecode PMC.
=cut
*/
PARROT_API
INTVAL
Parrot_api_load_bytecode_file(ARGMOD(PMC *interp_pmc), ARGIN(const char *filename), ARGOUT(PMC **pbc))
{
ASSERT_ARGS(Parrot_api_load_bytecode_file)
EMBED_API_CALLIN(interp_pmc, interp);
PackFile * const pf = Parrot_pbc_read(interp, filename, 0);
if (!pf)
EMBED_API_FAILURE(interp_pmc, interp);
*pbc = Parrot_pmc_new(interp, enum_class_PackFile);
VTABLE_set_pointer(interp, *pbc, pf);
EMBED_API_CALLOUT(interp_pmc, interp);
}
PARROT_API
INTVAL
Parrot_api_run_bytecode(ARGMOD(PMC *interp_pmc), ARGIN(PMC *pbc), ARGIN(PMC *mainargs))
{
ASSERT_ARGS(Parrot_api_run_bytecode)
EMBED_API_CALLIN(interp_pmc, interp);
PackFile * const pf = VTABLE_get_pointer(interp, pbc);
if (!pf)
EMBED_API_FAILURE(interp_pmc, interp);
Parrot_pbc_load(interp, pf);
PackFile_fixup_subs(interp, PBC_IMMEDIATE, NULL);
PackFile_fixup_subs(interp, PBC_POSTCOMP, NULL);
PackFile_fixup_subs(interp, PBC_MAIN, NULL);
EMBED_API_CALLOUT(interp_pmc, interp);
}
PARROT_API
INTVAL
Parrot_api_build_argv_array(ARGMOD(PMC *interp_pmc), INTVAL argc, ARGIN(char **argv), ARGOUT(PMC **args))
{
ASSERT_ARGS(Parrot_api_build_argv_array)
EMBED_API_CALLIN(interp_pmc, interp);
PMC * const userargv = Parrot_pmc_new(interp, enum_class_ResizableStringArray);
INTVAL i;
for (i = 0; i < argc; ++i) {
/* Run through argv, adding everything to the array */
STRING * const arg = Parrot_str_new_init(interp, argv[i], strlen(argv[i]),
Parrot_utf8_encoding_ptr, PObj_external_FLAG);
VTABLE_push_string(interp, userargv, arg);
}
*args = userargv;
EMBED_API_CALLIN(interp_pmc, interp);
}