/
Embed.xs
251 lines (200 loc) · 5.77 KB
/
Embed.xs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#define PERL_NO_GET_CONTEXT
#include "parrot/parrot.h"
#include "parrot/embed.h"
#include "parrot/extend.h"
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define get_interp( i ) (INT2PTR(Interpreter_struct*, SvIV( SvRV( (SV*)i ) )))->interp
typedef Parrot_Interp Parrot_Interpreter;
typedef struct Interpreter_struct
{
SV *parent;
Parrot_Interp interp;
} Interpreter_struct;
typedef struct PMC_struct
{
SV *interp;
Parrot_PMC pmc;
} PMC_struct;
Interpreter_struct*
make_interp( pTHX_ SV *parent, PARROT_INTERP )
{
Interpreter_struct *interp_struct;
if (!interp)
return NULL;
New( 0, interp_struct, 1, Interpreter_struct );
SvREFCNT_inc( parent );
interp_struct->parent = parent;
interp_struct->interp = interp;
return interp_struct;
}
PMC_struct*
make_pmc( pTHX_ SV *interp, Parrot_PMC pmc )
{
PMC_struct *pmc_struct;
if (!pmc)
return NULL;
New( 0, pmc_struct, 1, PMC_struct );
SvREFCNT_inc( interp );
pmc_struct->interp = interp;
pmc_struct->pmc = pmc;
Parrot_register_pmc( get_interp( interp ), pmc );
return pmc_struct;
}
MODULE = Parrot::Embed PACKAGE = Parrot::Interpreter
Parrot_Interp
new( class, ... )
char *class
PREINIT:
SV *parent_sv = NULL;
Parrot_Interp parent;
Parrot_Interp interp;
Parrot_PackFile pf;
CODE:
if (items == 1)
parent = 0;
else if ( items == 2 && sv_derived_from( ST(1), "Parrot::Interpreter" ))
{
parent_sv = ST(1);
parent = get_interp( parent_sv );
}
else
Perl_croak( aTHX_ "Usage: Parrot::Interpreter->new( [ parent ] )" );
interp = Parrot_new( (Parrot_Interp)parent );
pf = PackFile_new( interp, 0 );
Parrot_pbc_load( interp, pf );
/* Can't use RETVAL/OUTPUT here because typemap loses class variable */
ST(0) = sv_newmortal();
sv_setref_pv( ST(0), class, (void*)make_interp( aTHX_ parent_sv, interp ) );
bool
load_file( interp, filename )
Interpreter_struct *interp
char *filename
PREINIT:
Parrot_Interp real_interp;
Parrot_PackFile pf;
CODE:
real_interp = interp->interp;
pf = Parrot_pbc_read( real_interp, filename, 0 );
if (!pf)
Perl_croak( aTHX_
"File '%s' not found in $parrot_interpreter->load_file()", filename );
Parrot_pbc_load( real_interp, pf );
RETVAL = 1;
OUTPUT:
RETVAL
PMC_struct*
find_global( interp, global, ... )
Interpreter_struct *interp
char *global
PREINIT:
SV *namespace;
Parrot_Interp real_interp;
Parrot_String p_namespace;
Parrot_String p_global;
Parrot_PMC pmc;
CODE:
if ( items < 2 || items > 3 )
Perl_croak( aTHX_
"Usage: $parrot_interpreter->find_global( name, [ namespace ] )");
real_interp = interp->interp;
p_global = Parrot_str_new_constant( real_interp, global );
if (items == 3)
namespace = ST(2);
else
namespace = &PL_sv_undef;
if (namespace != &PL_sv_undef )
{
char *ns_copy = savepv(SvPV_nolen(namespace));
char *ns_str = ns_copy;
char *prev = ns_str;
PMC *ns = NULL;
while (*ns_str++)
{
STRING *ns_part;
if (! (*ns_str == ':' && *(ns_str + 1) == ':'))
continue;
*ns_str = 0;
ns_str += 2;
if (!ns)
ns = Parrot_find_global_cur(real_interp,
Parrot_str_new_constant(real_interp, prev));
else
ns = Parrot_find_global_n(real_interp, ns,
Parrot_str_new_constant(real_interp, prev));
prev = ns_str;
}
if (!ns)
ns = Parrot_find_global_cur(real_interp,
Parrot_str_new_constant(real_interp, prev));
else
ns = Parrot_find_global_n(real_interp, ns,
Parrot_str_new_constant(real_interp, prev));
pmc = Parrot_find_global_n(real_interp, ns, p_global);
Safefree(ns_copy);
}
else
pmc = Parrot_find_global_cur( real_interp, p_global );
RETVAL = make_pmc( aTHX_ ST(0), pmc );
OUTPUT:
RETVAL
PMC_struct*
compile( interp, code )
Interpreter_struct *interp
char *code
PREINIT:
STRING *code_type;
STRING *error;
Parrot_Interp real_interp;
Parrot_PMC out_pmc;
CODE:
real_interp = interp->interp;
code_type = Parrot_str_new_constant( real_interp, "PIR" );
out_pmc = Parrot_compile_string( real_interp, code_type, code, &error );
RETVAL = make_pmc( aTHX_ ST(0), out_pmc );
OUTPUT:
RETVAL
void
DESTROY( interp )
Interpreter_struct *interp
CODE:
if (interp->parent)
SvREFCNT_dec( interp->parent );
Parrot_destroy( interp->interp );
MODULE = Parrot::Embed PACKAGE = Parrot::PMC
PMC_struct*
invoke( pmc, signature, argument )
PMC_struct *pmc
const char *signature
const char *argument
PREINIT:
Parrot_PMC pmc_actual;
Parrot_PMC out_pmc;
Parrot_Interp interp;
Parrot_String arg_string;
CODE:
pmc_actual = pmc->pmc;
interp = get_interp( pmc->interp );
arg_string = Parrot_str_new_constant( interp, argument );
Parrot_ext_call( interp, pmc_actual, signature, arg_string, &out_pmc );
RETVAL = make_pmc( aTHX_ pmc->interp, out_pmc );
OUTPUT:
RETVAL
char *
get_string( pmc )
PMC_struct *pmc
CODE:
RETVAL = Parrot_PMC_get_cstring( get_interp( pmc->interp ), pmc->pmc );
OUTPUT:
RETVAL
void
DESTROY( pmc )
PMC_struct *pmc
PREINIT:
Parrot_Interp interp;
CODE:
interp = get_interp( pmc->interp );
if (interp)
SvREFCNT_dec( interp );
Parrot_unregister_pmc( interp, pmc->pmc );