This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
forked from audreyt/Pugs.hs
/
p5embed.c
353 lines (286 loc) · 6.08 KB
/
p5embed.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#ifndef HsPerl5DefinedC
#define HsPerl5DefinedC 1
#include "p5embed.h"
#include <XSUB.h>
#include "perlxsi.c"
/* define to enable pugsembed debug messages */
#define PERL5_EMBED_DEBUG 0
#if PERL5_EMBED_DEBUG
#define oRZ ""
#define hate Perl_croak(aTHX_ "hate software")
#else
#define oRZ "#"
#define hate
#endif
const char HsPerl5Preamble[] =
"package __HsPerl5__;\n\n"
"sub MkCode {\n"
" my $val = shift;\n"
" sub { unshift @_, $val; goto &__HsPerl5__::Invoke }\n"
"}\n"
"1;\n";
/* Workaround for mapstart: the only op which needs a different ppaddr */
#undef Perl_pp_mapstart
#define Perl_pp_mapstart Perl_pp_grepstart
#undef OP_MAPSTART
#define OP_MAPSTART OP_GREPSTART
static PerlInterpreter *my_perl;
int _P5EMBED_INIT = 0;
SV *
perl5_sv_undef ()
{
return(&PL_sv_undef);
}
SV *
perl5_sv_yes ()
{
return(&PL_sv_yes);
}
SV *
perl5_sv_no ()
{
return(&PL_sv_no);
}
SV **
perl5_eval(char *code, int len, int cxt)
{
dSP;
SV* sv;
int count;
ENTER;
SAVETMPS;
sv = newSVpvn(code, len);
#ifdef SvUTF8_on
SvUTF8_on(sv);
#endif
count = eval_sv(sv, cxt);
SvREFCNT_dec(sv);
return perl5_return_conv(count);
}
SV **
perl5_return_conv (int count) {
SV **out;
int i;
dSP;
SPAGAIN;
if (SvTRUE(ERRSV)) {
Newz(42, out, 3, SV*);
if (SvROK(ERRSV)) {
out[0] = newSVsv(ERRSV);
out[1] = NULL;
}
else {
out[0] = ERRSV;
out[1] = ERRSV; /* for Haskell-side to read PV */
}
out[2] = NULL;
}
else {
Newz(42, out, count+2, SV*);
out[0] = NULL;
for (i=count; i>0; --i) {
out[i] = newSVsv(POPs);
}
out[count+1] = NULL;
}
PUTBACK;
FREETMPS;
LEAVE;
/* pugs_setenv(old_env); */
return out;
}
char *
perl5_SvPV ( SV *sv )
{
char *rv;
rv = SvPV_nolen(sv);
return rv;
}
SV *
perl5_newSVpvn ( char * pv, int len )
{
SV *sv = newSVpvn(pv, len);
#ifdef SvUTF8_on
SvUTF8_on(sv);
#endif
return(sv);
}
SV **
perl5_apply(SV *sub, SV *inv, SV** args, int cxt)
{
SV **arg;
SV *rv;
SV *sv;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (inv != NULL) {
XPUSHs(inv);
}
for (arg = args; *arg != NULL; arg++) {
XPUSHs(*arg);
}
PUTBACK;
if (inv != NULL) {
perl5_return_conv(call_method(SvPV_nolen(sub), cxt|G_EVAL));
}
else {
perl5_return_conv(call_sv(sub, cxt|G_EVAL));
}
}
SV *
perl5_newSViv ( int iv )
{
return(newSViv(iv));
}
SV *
perl5_newSVnv ( double iv )
{
return(newSVnv(iv));
}
int
perl5_SvIV ( SV *sv )
{
return((int)SvIV(sv));
}
double
perl5_SvNV ( SV *sv )
{
return((double)SvNV(sv));
}
bool
perl5_SvTRUE ( SV * sv )
{
bool rv;
rv = SvTRUE(sv);
return(rv ? 1 : 0);
}
SV *
perl5_make_cv ( HsStablePtr *sub )
{
SV *sv = newSV(0);
SV *ret = NULL;
int count;
sv_setref_pv(sv, "__HsPerl5__::Code", sub);
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(sv));
PUTBACK;
count = call_pv("__HsPerl5__::MkCode", G_SCALAR);
if (count != 1) {
croak("Big trouble\n");
}
SPAGAIN;
ret = newSVsv(POPs);
PUTBACK;
FREETMPS;
LEAVE;
return (ret);
}
XS(__HsPerl5__Invoke) {
HsStablePtr *sub;
SV **stack;
SV **ret;
SV **cur;
SV *sv;
int i;
IV tmp = 0;
dXSARGS;
if (items < 1) {
hate;
}
sv = ST(0);
tmp = SvIV((SV*)SvRV(sv));
sub = (HsStablePtr *)tmp;
New(6, stack, items, SV*);
for (i = 1; i < items; ++i) {
stack[i-1] = ST(i);
}
stack[i-1] = NULL;
Safefree(stack);
SPAGAIN;
ret = hsPerl5Apply(sub, stack, GIMME_V);
if (ret == NULL) {
XSRETURN(0);
}
for (cur = ret; *cur != NULL; ++cur) {
EXTEND(SP, 1);
ST(cur - ret) = *cur;
}
Safefree(ret);
XSRETURN(cur - ret);
}
PerlInterpreter *
perl5_init ( int argc, char **argv )
{
int exitstatus;
int i;
#ifdef PERL_GPROF_MONCONTROL
PERL_GPROF_MONCONTROL(0);
#endif
#if (defined(USE_5005THREADS) || defined(USE_ITHREADS)) && defined(HAS_PTHREAD_ATFORK)
/* XXX Ideally, this should really be happening in perl_alloc() or
* perl_construct() to keep libperl.a transparently fork()-safe.
* It is currently done here only because Apache/mod_perl have
* problems due to lack of a call to cancel pthread_atfork()
* handlers when shared objects that contain the handlers may
* be dlclose()d. This forces applications that embed perl to
* call PTHREAD_ATFORK() explicitly, but if and only if it hasn't
* been called at least once before in the current process.
* --GSAR 2001-07-20 */
PTHREAD_ATFORK(Perl_atfork_lock,
Perl_atfork_unlock,
Perl_atfork_unlock);
#endif
if (!PL_do_undump) {
my_perl = perl_alloc();
if (!my_perl)
exit(1);
perl_construct( my_perl );
PL_perl_destruct_level = 0;
}
#ifdef PERL_EXIT_DESTRUCT_END
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
#endif /* PERL_EXIT_DESTRUCT_END */
#ifdef PERL_EXIT_EXPECTED
PL_exit_flags |= PERL_EXIT_EXPECTED;
#endif /* PERL_EXIT_EXPECTED */
#if (defined(CSH) && defined(PL_cshname))
if (!PL_cshlen)
PL_cshlen = strlen(PL_cshname);
#endif
exitstatus = perl_parse(my_perl, xs_init, argc, argv, (char **)NULL);
if (exitstatus == 0)
exitstatus = perl_run( my_perl );
_P5EMBED_INIT = 1;
newXS((char*) "__HsPerl5__::Invoke", __HsPerl5__Invoke, (char*)__FILE__);
/*
newXS((char*) "pugs::guts::eval_apply", _pugs_guts_eval_apply, (char*)__FILE__);
*/
#if PERL5_EMBED_DEBUG
fprintf(stderr, "(%s)", pugs_guts_code);
#endif
eval_pv(HsPerl5Preamble, TRUE);
if (SvTRUE(ERRSV)) {
STRLEN n_a;
printf("Error init perl: %s\n", SvPV(ERRSV,n_a));
exit(1);
}
return my_perl;
}
SV *
perl5_get_sv(const char *name)
{
SV *sv = get_sv(name, 1);
return sv;
}
SV *
perl5_get_cv(const char *name)
{
SV *cv = (SV*)(get_cv(name, 0));
return cv;
}
#endif