/
pl_duk.h
90 lines (75 loc) · 3.01 KB
/
pl_duk.h
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
#ifndef PL_DUK_H
#define PL_DUK_H
#include "duktape.h"
#include "EXTERN.h"
#include "perl.h"
#include "ppport.h"
#define DUK_OPT_NAME_GATHER_STATS "gather_stats"
#define DUK_OPT_NAME_SAVE_MESSAGES "save_messages"
#define DUK_OPT_NAME_MAX_MEMORY_BYTES "max_memory_bytes"
#define DUK_OPT_NAME_MAX_TIMEOUT_US "max_timeout_us"
#define DUK_OPT_NAME_CATCH_PERL_EXCEPTIONS "catch_perl_exceptions"
#define DUK_OPT_FLAG_GATHER_STATS 0x01
#define DUK_OPT_FLAG_SAVE_MESSAGES 0x02
#define DUK_OPT_FLAG_MAX_MEMORY_BYTES 0x04
#define DUK_OPT_FLAG_MAX_TIMEOUT_US 0x08
#define DUK_OPT_FLAG_CATCH_PERL_EXCEPTIONS 0x10
#define PL_NAME_ROOT "_perl_"
#define PL_NAME_GENERIC_CALLBACK "generic_callback"
#define PL_SLOT_CREATE(name) (PL_NAME_ROOT "." #name)
#define PL_SLOT_GENERIC_CALLBACK PL_SLOT_CREATE(PL_NAME_GENERIC_CALLBACK)
/*
* This is our internal data structure. For now it only contains a pointer to
* a duktape context. We will add other stuff here.
*/
typedef struct Duk {
int inited;
duk_context* ctx;
int pagesize_bytes;
unsigned long flags;
HV* version;
HV* stats;
HV* msgs;
HV* funcref; // Structure to hold the name and references to perl callbacks
size_t total_allocated_bytes;
size_t max_allocated_bytes;
double max_timeout_us;;
double eval_start_us;
} Duk;
/*
* We use these two functions to convert back and forth between the Perl
* representation of an object and the JS one.
*
* Because data in Perl and JS can be nested (array of hashes of arrays of...),
* the functions are recursive.
*
* pl_duk_to_perl: takes a JS value from a given position in the duktape stack,
* and creates the equivalent Perl value.
*
* pl_perl_to_duk: takes a Perl value and leaves the equivalent JS value at the
* top of the duktape stack.
*/
SV* pl_duk_to_perl(pTHX_ duk_context* ctx, int pos);
int pl_perl_to_duk(pTHX_ SV* value, duk_context* ctx);
/*
* Return a Perl string with the type of the duktape variable
*/
const char* pl_typeof(pTHX_ duk_context* ctx, int pos);
/*
* This is a generic dispatcher that allows calling any Perl function from JS.
*/
int pl_call_perl_sv(duk_context* ctx, SV* func);
/* Get / set the value for a global object or a slot in an object */
SV* pl_exists_global_or_property(pTHX_ duk_context* ctx, const char* name);
SV* pl_typeof_global_or_property(pTHX_ duk_context* ctx, const char* name);
SV* pl_instanceof_global_or_property(pTHX_ duk_context* ctx, const char* object, const char* class);
SV* pl_get_global_or_property(pTHX_ duk_context* ctx, const char* name);
int pl_set_global_or_property(pTHX_ Duk* duk, const char* name, SV* value);
int pl_del_global_or_property(pTHX_ Duk* duk, const char* name);
SV* pl_eval(pTHX_ Duk* duk, const char* js, const char* file);
/* Run the Duktape GC */
int pl_run_gc(Duk* duk);
SV* pl_global_objects(pTHX_ duk_context* ctx);
HV* pl_get_version_info(pTHX);
int pl_console_callback(void* data, duk_uint_t flags,const char* fmt, va_list ap);
#endif