This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
forked from audreyt/Pugs.hs
/
p5embed.c
590 lines (472 loc) · 13.5 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "p5embed.h"
#include <XSUB.h>
#include "perlxsi.c"
#include "pugsembed.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
/* 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;
const char pugs_guts_code[] =
"use strict;\n\n"
"package pugs;\n\n"
"our $AUTOLOAD;\n"
"sub AUTOLOAD { pugs::guts::invoke($AUTOLOAD, @_) } \n"
"sub DESTROY {}\n\n"
"package pugs::guts;\n"
"our @ISA=('pugs');\n"
"sub Code { my ($class, $val) = @_;\n"
" sub { pugs::guts::invoke($val, undef, @_) } }\n"
"sub Array { my ($class, $val) = @_;\n"
" my $array; tie @$array, 'pugs::array', $val;\n"
oRZ" warn 'returning '.$array;\n"
" return $array; }\n\n"
"sub Hash { my ($class, $val) = @_;\n"
" my $hash; tie %$hash, 'pugs::hash', $val;\n"
oRZ" warn 'returning '.$hash;\n"
" return $hash; }\n\n"
"sub Pair { goto &Hash }\n"
"sub Scalar { my ($class, $val) = @_;\n"
" my $scalar; tie $$scalar, 'pugs::scalar', $val;\n"
oRZ" warn 'returning '.$scalar;\n"
" return $scalar; }\n\n"
"sub Handle { my ($class, $val) = @_;\n"
" tie *FH, 'pugs::handle', $val;\n"
oRZ" warn 'returning '.$handle;\n"
" return *FH; }\n\n"
"our $AUTOLOAD;\n"
"sub AUTOLOAD { my $type = $AUTOLOAD; $type =~ s/.*:://;\n"
" return if $type =~ m/^[A-Z]*$/; die 'unhandled support type: '.$type } \n"
oRZ"warn 'compiled .'.__PACKAGE__;\n\n"
"package pugs::array;\n"
"our $AUTOLOAD;\n"
"sub AUTOLOAD { my $type = $AUTOLOAD; $type =~ s/.*:://;\n"
" warn 'unhandled support type: '.$type } \n"
"sub TIEARRAY {\n"
" my ($class, $val) = @_;\n"
" bless \\$val, $class; }\n\n"
"sub DEREF { ${$_[0]} }\n"
"sub STORE {\n"
" my ($self, $index, $elem) = @_;\n"
oRZ" warn 'store! '.$elem;\n"
" pugs::guts::eval_apply('sub ($x is rw, $y is rw, $z is rw) { item($x[$y] = $z);\n"
oRZ" warn $x\n"
" }', $$self, $index, $elem) }\n\n"
"sub PUSH {\n"
" my ($self, @elems) = @_;\n"
" pugs::guts::eval_apply('sub ($x is rw, @*y) { item $x.push(@y);\n"
oRZ" warn $x\n"
" }', $$self, @elems) }\n\n"
"sub FETCHSIZE {\n"
" my ($self) = @_;\n"
" my $ret = pugs::guts::invoke('elems', $$self); \n"
oRZ" warn 'FETCHSIZE: '.$ret;\n"
" $ret; }\n\n"
"sub EXISTS {\n"
" my ($self, $index) = @_;\n"
" pugs::guts::eval_apply('sub ($x, $y) { item $x.exists($y) }', $$self, $index) }\n"
"sub FETCH {\n"
" my ($self, $index) = @_;\n"
oRZ" warn 'FETCH: '.$index;\n"
" pugs::guts::eval_apply('sub ($x, $y) { item $x.[$y] }', $$self, $index) }\n"
"package pugs::hash;\n"
"our $AUTOLOAD;\n"
"sub AUTOLOAD { my $type = $AUTOLOAD; $type =~ s/.*:://;\n"
" warn 'unhandled support type: '.$type } \n"
"sub TIEHASH {\n"
" my ($class, $val) = @_;\n"
" bless [$val,0], $class; }\n\n"
"sub DEREF { $_[0][0] }\n"
"sub FETCH {\n"
" my ($self, $index) = @_;\n"
oRZ" warn 'FETCH: '.$index;\n"
" pugs::guts::eval_apply('sub ($x, $y) { item $x.{$y} }', $self->[0], $index) }\n"
"sub DELETE {\n"
" my ($self, $index) = @_;\n"
oRZ" warn 'DELETE: '.$index;\n"
" pugs::guts::eval_apply('sub ($x, $y) { item $x.delete($y) }', $self->[0], $index) }\n"
"sub CLEAR {\n"
" my ($self) = @_;\n"
" pugs::guts::eval_apply('sub ($x) { item $x.delete($x.keys) }', $self->[0]) }\n"
"sub STORE {\n"
" my ($self, $index, $elem) = @_;\n"
oRZ" warn 'store! '.$elem;\n"
" pugs::guts::eval_apply('sub ($x is rw, $y, $z) { item($x{$y} = $z);\n"
oRZ" warn $x\n"
" }', $self->[0], $index, $elem) }\n\n"
"sub FIRSTKEY {\n"
" my ($self) = @_;\n"
" my $ret = pugs::guts::invoke('keys', $self->[0]); \n"
oRZ" warn $ret;\n"
" $self->[1] = 0; $self->[2] = $ret;"
" $self->NEXTKEY; }\n"
"sub NEXTKEY {\n"
" my ($self) = @_;\n"
" return undef if $self->[1] > $#{$self->[2]};"
" $self->[2]->[$self->[1]++]; }"
"package pugs::scalar;\n"
"our $AUTOLOAD;\n"
"sub AUTOLOAD { my $type = $AUTOLOAD; $type =~ s/.*:://;\n"
" warn 'unhandled support type: '.$type } \n"
"sub TIESCALAR {\n"
" my ($class, $val) = @_;\n"
" bless \\$val, $class; }\n\n"
"sub DEREF { ${$_[0]} }\n"
"sub FETCH {\n"
" my ($self) = @_;\n"
" pugs::guts::eval_apply('sub ($x) { ~$x }', $$self) }\n"
"sub STORE {\n"
" my ($self, $val) = @_;\n"
" pugs::guts::eval_apply('sub ($x is rw, $y) { $$x = $y;\n"
oRZ" warn $x\n"
" }', $$self, $val) }\n\n"
"package pugs::handle;\n"
"our $AUTOLOAD;\n"
"sub AUTOLOAD { my $type = $AUTOLOAD; $type =~ s/.*:://;\n"
" warn 'unhandled support type: '.$type } \n"
"sub TIEHANDLE {\n"
" my ($class, $val) = @_;\n"
" bless \\$val, $class; }\n\n"
"sub DEREF { ${$_[0]} }\n"
"sub PRINT {\n"
" my ($self, @vals) = shift;\n"
" pugs::guts::eval_apply('sub ($x, *@y) { $x.print(@y) }', $$self, @vals) }\n"
"sub READLINE {\n"
" my ($self) = @_;\n"
" pugs::guts::eval_apply('sub ($x) { ~($x.readline);\n"
oRZ" warn $x\n"
" }', $$self) }\n\n"
"sub GETC {\n"
" my ($self) = @_;\n"
" pugs::guts::eval_apply('sub ($x) { ~($x.getc);\n"
oRZ" warn $x\n"
" }', $$self) }\n\n"
oRZ"warn 'compiled';\n"
"1;\n";
XS(_pugs_guts_invoke) {
Val *val, *inv, **stack;
SV *ret, *sv;
int i;
dXSARGS;
if (items < 1) {
hate;
}
sv = ST(0);
if (sv_isa(sv, "pugs")) {
val = pugs_SvToVal(ST(0));
}
else {
char *method, *fullname;
fullname = SvPV_nolen(sv);
method = strrchr(fullname, ':');
method = method ? method+1 : fullname;
val = pugs_PvnToVal(method, strlen(method));
}
inv = SvOK(ST(1)) ? pugs_SvToVal(ST(1)) : NULL;
New(6, stack, items, Val*);
for (i = 2; i < items; ++i) {
stack[i-2] = pugs_SvToVal(ST(i));
}
stack[i-2] = NULL;
ST(0) = pugs_Apply(val, inv, stack, GIMME_V);
/* sv_dump (ret); */
Safefree(stack);
XSRETURN(1);
}
XS(_pugs_guts_eval_apply) {
Val *val, *inv, **stack;
int i;
dXSARGS;
if (items < 1) {
hate;
}
val = pugs_Eval(SvPV_nolen(ST(0)));
New(6, stack, items, Val*);
for (i = 1; i < items; ++i) {
#if PERL5_EMBED_DEBUG
fprintf(stderr, "put into stack: %s\n", SvPV_nolen(ST(i)));
#endif
stack[i-1] = pugs_SvToVal(ST(i));
}
stack[i-1] = NULL;
ST(0) = pugs_Apply(val, NULL, stack, GIMME_V);
Safefree(stack);
XSRETURN(1);
}
#ifdef HAS_PROCSELFEXE
/* This is a function so that we don't hold on to MAXPATHLEN
bytes of stack longer than necessary
*/
STATIC void
S_procself_val(pTHX_ SV *sv, char *arg0)
{
char buf[MAXPATHLEN];
int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
/* On Playstation2 Linux V1.0 (kernel 2.2.1) readlink(/proc/self/exe)
includes a spurious NUL which will cause $^X to fail in system
or backticks (this will prevent extensions from being built and
many tests from working). readlink is not meant to add a NUL.
Normal readlink works fine.
*/
if (len > 0 && buf[len-1] == '\0') {
len--;
}
/* FreeBSD's implementation is acknowledged to be imperfect, sometimes
returning the text "unknown" from the readlink rather than the path
to the executable (or returning an error from the readlink). Any valid
path has a '/' in it somewhere, so use that to validate the result.
See http://www.freebsd.org/cgi/query-pr.cgi?pr=35703
*/
if (len > 0 && memchr(buf, '/', len)) {
sv_setpvn(sv,buf,len);
}
else {
sv_setpv(sv,arg0);
}
}
#endif /* HAS_PROCSELFEXE */
# if defined(__APPLE__)
# include <crt_externs.h> /* for the env array */
# define p5embed_environ (*_NSGetEnviron())
# else
# define p5embed_environ environ
# endif
PerlInterpreter *
perl5_init ( int argc, char **argv )
{
int exitstatus;
int i;
#ifdef PERL_GPROF_MONCONTROL
PERL_GPROF_MONCONTROL(0);
#endif
#ifdef PERL_SYS_INIT3
PERL_SYS_INIT3(&argc,&argv,&p5embed_environ);
#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*) "pugs::guts::invoke", _pugs_guts_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(pugs_guts_code, TRUE);
if (SvTRUE(ERRSV)) {
STRLEN n_a;
printf("Error init perl: %s\n", SvPV(ERRSV,n_a));
exit(1);
}
return my_perl;
}
char *
perl5_SvPV ( SV *sv )
{
char *rv;
rv = SvPV_nolen(sv);
return rv;
}
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);
}
bool
perl5_SvROK ( SV * sv )
{
bool rv;
sv_dump(sv);
rv = SvROK(sv);
return(rv ? 1 : 0);
}
SV *
perl5_sv_undef ()
{
return(&PL_sv_undef);
}
SV *
perl5_newSVpvn ( char * pv, int len )
{
SV *sv = newSVpvn(pv, len);
#ifdef SvUTF8_on
SvUTF8_on(sv);
#endif
return(sv);
}
SV *
perl5_newSViv ( int iv )
{
return(newSViv(iv));
}
SV *
perl5_newSVnv ( double iv )
{
return(newSVnv(iv));
}
SV **
perl5_apply(SV *sub, SV *inv, SV** args, void *env, int cxt)
{
SV **arg;
SV **out;
SV *rv;
SV *sv;
void *old_env = pugs_getenv();
int count, i;
dSP;
ENTER;
SAVETMPS;
pugs_setenv(env);
PUSHMARK(SP);
if (inv != NULL) {
XPUSHs(inv);
}
for (arg = args; *arg != NULL; arg++) {
XPUSHs(*arg);
}
PUTBACK;
if (inv != NULL) {
count = call_method(SvPV_nolen(sub), cxt|G_EVAL);
}
else {
count = call_sv(sub, cxt|G_EVAL);
}
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;
}
SV *
perl5_get_sv(const char *name)
{
SV *sv = get_sv(name, 1);
/* sv_dump(sv); */
return sv;
}
SV *
perl5_eval(char *code, void *env, int cxt)
{
dSP;
SV* sv;
void *old_env = pugs_getenv();
ENTER;
SAVETMPS;
pugs_setenv(env);
sv = newSVpv(code, 0);
#ifdef SvUTF8_on
SvUTF8_on(sv);
#endif
eval_sv(sv, cxt);
SvREFCNT_dec(sv);
SPAGAIN;
sv = POPs;
SvREFCNT_inc(sv);
PUTBACK;
if (SvTRUE(ERRSV)) {
STRLEN n_a;
fprintf(stderr, "Error eval perl5: \"%s\"\n*** %s\n", code, SvPV(ERRSV,n_a));
}
FREETMPS;
LEAVE;
pugs_setenv(old_env);
return sv;
}
bool
perl5_can(SV *inv, char *subname)
{
int rv;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(inv);
XPUSHs(newSVpv(subname, 0));
PUTBACK;
call_pv("UNIVERSAL::can", G_SCALAR);
SPAGAIN;
rv = (POPi != 0);
/* printf("Checking: %s->can(%s), ret %d\n", SvPV_nolen(inv), subname, rv); */
PUTBACK;
FREETMPS;
LEAVE;
return rv;
}
void perl5_finalize ( SV* sv )
{
SvREFCNT_dec(sv);
}