applied suggested patch, with win32 and PERL_OBJECT additions
[p5sagit/p5-mst-13.2.git] / ext / ByteLoader / ByteLoader.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "byterun.c"
6
7 /* defgv must be accessed differently under threaded perl */
8 /* DEFSV et al are in 5.004_56 */
9 #ifndef DEFSV
10 #define DEFSV           GvSV(defgv)
11 #endif
12
13 static I32
14 #ifdef PERL_OBJECT
15 byteloader_filter(CPerlObj *pPerl, int idx, SV *buf_sv, int maxlen)
16 #else
17 byteloader_filter(int idx, SV *buf_sv, int maxlen)
18 #endif
19 {
20     OP *saveroot = PL_main_root;
21     OP *savestart = PL_main_start;
22
23 #ifdef INDIRECT_BGET_MACROS
24     struct bytesream bs;
25
26     bs.data = PL_rsfp;
27     bs.fgetc = (int(*) _((void*)))fgetc;
28     bs.fread = (int(*) _((char*,size_t,size_t,void*)))fread;
29     bs.freadpv = freadpv;
30 #else
31     byterun(PL_rsfp);
32 #endif
33
34     if (PL_in_eval) {
35         OP *o;
36
37         PL_eval_start = PL_main_start;
38
39         o = newSVOP(OP_CONST, 0, newSViv(1));
40         PL_eval_root = newLISTOP(OP_LINESEQ, 0, PL_main_root, o);
41         PL_main_root->op_next = o;
42         PL_eval_root = newUNOP(OP_LEAVEEVAL, 0, PL_eval_root);
43         o->op_next = PL_eval_root;
44     
45         PL_main_root = saveroot;
46         PL_main_start = savestart;
47     }
48
49     return 0;
50 }
51
52 MODULE = ByteLoader             PACKAGE = ByteLoader
53
54 PROTOTYPES:     ENABLE
55
56 void
57 import(...)
58   PPCODE:
59     filter_add(byteloader_filter, NULL);
60
61 void
62 unimport(...)
63   PPCODE:
64     filter_del(byteloader_filter);