Peertube-dl/javascript_interpreter_xs/javascript.xs

78 lines
1.9 KiB
Plaintext

#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "duktape.h"
#include "duk_config.h"
#include "javascript_builtins.h"
MODULE = Peertube::DL::Javascript PACKAGE = Peertube::DL::Javascript
PROTOTYPES: ENABLE
SV *
_duk_create_heap_default()
CODE:
duk_context *context = duk_create_heap_default();
if (context) {
duk_push_c_function(context, js_builtin_print, 1);
duk_put_global_string(context, "print");
RETVAL = newSVuv((size_t)context);
} else {
RETVAL = &PL_sv_undef;
}
OUTPUT:
RETVAL
SV *
_duk_push_lstring(SV *, SV *)
CODE:
duk_context *context = (duk_context *) SvUV(ST(0));
if(!context) {
croak("Javascript context undef.", 0);
}
STRLEN len;
char * lstring = SvPV(ST(1), len);
if(!len) {
croak("Empty string on lstring push.");
}
duk_push_lstring(context, lstring, strlen(lstring));
//
// * Example subroutine call
// dSP;
// ENTER;
// SAVETMPS;
// PUSHMARK(SP);
// EXTEND(SP, 1);
// PUSHs(sv_2mortal(newSVpv("Javascript context invalid.", 0)));
// PUTBACK;
//
// call_sv(sv_2mortal(newSVpv("::die", 0)), G_DISCARD);
//
// FREETMPS;
// LEAVE;
//
//
OUTPUT:
RETVAL
void
_duk_peval(SV *)
CODE:
duk_context *context = (duk_context *) SvUV(ST(0));
if(!context) {
croak("Javascript context undef.", 0);
}
if(duk_peval(context) != 0) {
croak("Eval failed:\n%s\n", duk_safe_to_string(context, -1));
}
void
_duk_destroy_heap(SV *)
CODE:
duk_context *context = (duk_context *) SvUV(ST(0));
if (!context) {
croak("Cannot destroy something that is not a context.");
}
duk_destroy_heap(context);