File: PJS_Runtime.c

package info (click to toggle)
libjavascript-perl 1.08-1%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 412 kB
  • ctags: 203
  • sloc: perl: 1,686; ansic: 1,620; makefile: 55
file content (132 lines) | stat: -rw-r--r-- 3,380 bytes parent folder | download
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
#include "JavaScript.h"

JSTrapStatus PJS_trap_handler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval, void *closure) {
    PJS_Runtime *rt = (PJS_Runtime *) closure;
    PJS_TrapHandler *handler = rt->trap_handlers;
    JSTrapStatus status = JSTRAP_CONTINUE;

    while (handler && status == JSTRAP_CONTINUE) {
        status = (handler->handler)(cx, script, pc, rval, handler->data);
        handler = handler->_next;
    }
    
    return status;
}

/* Perl callback interrupt handler */
JSTrapStatus PJS_perl_trap_handler(JSContext *cx, JSScript *script, jsbytecode *pc, jsval *rval, void *closure) {
    dSP;
    PJS_Context *pcx = PJS_GET_CONTEXT(cx);
    SV *handler = (SV *) closure;
    SV *scx, *rv;
    int rc;
    JSTrapStatus status = JSTRAP_CONTINUE;
    
    if (handler) {
        ENTER ;
        SAVETMPS ;
        PUSHMARK(SP) ;

        scx = sv_newmortal();
        sv_setref_pv(scx, Nullch, (void*) pcx);
        
        XPUSHs(scx);
        XPUSHs(newSViv(*pc));
        
        PUTBACK;
        
        rc = perl_call_sv(SvRV(handler), G_SCALAR | G_EVAL);

        SPAGAIN;

        rv = POPs;

        if (!SvTRUE(rv)) {
            status = JSTRAP_ERROR;
        }

        if (SvTRUE(ERRSV)) {
            sv_setsv(ERRSV, &PL_sv_undef);
        }
        
        PUTBACK;

        FREETMPS;
        LEAVE;
    }
   
    return status;
}

/* Create a runtime */
PJS_Runtime *
PJS_CreateRuntime(int maxbytes) {
    PJS_Runtime *runtime;
    
    Newz(1, runtime, 1, PJS_Runtime);
    if(runtime == NULL) {
        croak("Failed to allocate memoery for PJS_Runtime");
    }

    runtime->rt = JS_NewRuntime(maxbytes);
    if(runtime->rt == NULL) {
        Safefree(runtime);
        croak("Failed to create runtime");
    }
    
    return runtime;
}

/* Free the runtime and any memory allocated by it */
void
PJS_DestroyRuntime(PJS_Runtime *runtime) {
    if (runtime != NULL) {
        JS_DestroyRuntime(runtime->rt);
        Safefree(runtime);
    }
}

/* Adds a trap handler */
void
PJS_AddTrapHandler(PJS_Runtime *inRuntime, PJS_TrapHandler *trapHandler) {
    PJS_TrapHandler *baseHandler = inRuntime->trap_handlers;

    trapHandler->_next = NULL;
    if (inRuntime->trap_handlers) {
        baseHandler = inRuntime->trap_handlers;
        while(baseHandler->_next != NULL) {
            baseHandler = baseHandler->_next;
        }
        baseHandler->_next = trapHandler;
    }
    else {
        inRuntime->trap_handlers = trapHandler;
        JS_SetInterrupt(inRuntime->rt, PJS_trap_handler, (void *) inRuntime);            
    }
}

/* Removes a trap handler */
void
PJS_RemoveTrapHandler(PJS_Runtime *fromRuntime, PJS_TrapHandler *trapHandler) {
    PJS_TrapHandler *current;
    JSTrapHandler old_handler;
    void *ptr;

    if (fromRuntime->trap_handlers == trapHandler) {
        fromRuntime->trap_handlers = trapHandler->_next;
    }
    else {
        /* seek and destroy */
        current = fromRuntime->trap_handlers;
        while (current->_next != NULL && current->_next != trapHandler) {
            current = current->_next;
        }
        if (current->_next == trapHandler) {
            current->_next = current->_next->_next;
        }
    }
    /* Removed last handler, disable trap */
    if (fromRuntime->trap_handlers == NULL) {
        JS_ClearInterrupt(fromRuntime->rt, &old_handler, &ptr);
    }
}