QuickJS.xs

filter: Statementstmt 88.6% 242 / 273 Branchbran 75.5% 74 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.1% 316 / 371 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« easyxs/easyxs_perlcall.h ↑ Summary quickjs/quickjs.h »
1 #include "easyxs/easyxs.h"
2  
3 #include "quickjs/quickjs.h"
4 #include "quickjs/quickjs-libc.h"
5  
6 #define PERL_NS_ROOT "JavaScript::QuickJS"
7  
8 typedef struct {
9 JSContext *ctx;
10 pid_t pid;
11 } perl_qjs_s;
12  
13 typedef struct {
14 JSContext *ctx;
15 JSValue jsfunc;
16 pid_t pid;
17 } perl_qjs_func_s;
18  
19 typedef struct {
20 #ifdef MULTIPLICITY
21 tTHX aTHX;
22 #endif
23 SV** svs;
24 U32 svs_count;
25 U32 refcount;
26 } ctx_opaque_s;
27  
28 const char* __jstype_name_back[] = {
29 [JS_TAG_BIG_DECIMAL - JS_TAG_FIRST] = "big decimal",
30 [JS_TAG_BIG_INT - JS_TAG_FIRST] = "big integer",
31 [JS_TAG_BIG_FLOAT - JS_TAG_FIRST] = "big float",
32 [JS_TAG_SYMBOL - JS_TAG_FIRST] = "symbol",
33 [JS_TAG_MODULE - JS_TAG_FIRST] = "module",
34 [JS_TAG_OBJECT - JS_TAG_FIRST] = "object",
35 [JS_TAG_FLOAT64 - JS_TAG_FIRST] = "float64",
36  
37 /* Small hack to ensure we can always read: */
38 [99] = NULL,
39 };
40  
41 #define _jstype_name(typenum) __jstype_name_back[ typenum - JS_TAG_FIRST ]
42  
43 static SV* _JSValue_to_SV (pTHX_ JSContext* ctx, JSValue jsval, SV** err_svp);
44  
45 4 static inline SV* _JSValue_object_to_SV (pTHX_ JSContext* ctx, JSValue jsval, SV** err_svp) {
46 JSPropertyEnum *tab_atom;
47 uint32_t tab_atom_count;
48  
49 4 int propnameserr = JS_GetOwnPropertyNames(ctx, &tab_atom, &tab_atom_count, jsval, JS_GPN_STRING_MASK);
50  
51 PERL_UNUSED_VAR(propnameserr);
52 assert(!propnameserr);
53  
54 4 HV* hv = newHV();
55  
56 9 ▶ for(int i = 0; i < tab_atom_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i < tab_atom_count; i++) { 5 4 9
57 5 JSValue key = JS_AtomToString(ctx, tab_atom[i].atom);
58 STRLEN strlen;
59 5 const char* keystr = JS_ToCStringLen(ctx, &strlen, key);
60  
61 5 JSValue value = JS_GetProperty(ctx, jsval, tab_atom[i].atom);
62  
63 5 SV* val_sv = _JSValue_to_SV(aTHX_ ctx, value, err_svp);
64  
65 5 ▶ if (val_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_sv) { 5 0 5
66 5 hv_store(hv, keystr, -strlen, val_sv, 0);
67 }
68  
69 5 JS_FreeCString(ctx, keystr);
70 5 JS_FreeValue(ctx, key);
71 5 JS_FreeValue(ctx, value);
72 5 JS_FreeAtom(ctx, tab_atom[i].atom);
73  
74 5 ▶ if (!val_sv) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) break; 0 5 5
75 }
76  
77 4 js_free(ctx, tab_atom);
78  
79 4 ▶ if (*err_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (*err_svp) { 0 4 4
80 0 SvREFCNT_dec( (SV*) hv );
81 0 return NULL;
82 }
83  
84 4 return newRV_noinc((SV*) hv);
85 }
86  
87 5 static inline SV* _JSValue_array_to_SV (pTHX_ JSContext* ctx, JSValue jsval, SV** err_svp) {
88 5 JSValue jslen = JS_GetPropertyStr(ctx, jsval, "length");
89 uint32_t len;
90 5 JS_ToUint32(ctx, &len, jslen);
91 5 JS_FreeValue(ctx, jslen);
92  
93 5 AV* av = newAV();
94  
95 5 ▶ if (len) {
Branch: 1 branch
BranchTrueFalseTotal
if (len) { 3 2 5
96 3 av_fill( av, len - 1 );
97 16 ▶ for (uint32_t i=0; i<len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i=0; i<len; i++) { 13 3 16
98 13 JSValue jsitem = JS_GetPropertyUint32(ctx, jsval, i);
99  
100 13 SV* val_sv = _JSValue_to_SV(aTHX_ ctx, jsitem, err_svp);
101  
102 13 ▶ if (val_sv) av_store( av, i, val_sv );
Branch: 1 branch
BranchTrueFalseTotal
if (val_sv) av_store( av, i, val_sv ); 13 0 13
103  
104 13 JS_FreeValue(ctx, jsitem);
105  
106 13 ▶ if (!val_sv) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) break; 0 13 13
107 }
108 }
109  
110 5 ▶ if (*err_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (*err_svp) { 0 5 5
111 0 SvREFCNT_dec((SV*) av);
112 0 return NULL;
113 }
114  
115 5 return newRV_noinc((SV*) av);
116 }
117  
118 /* NO JS exceptions allowed here! */
119 40 static SV* _JSValue_to_SV (pTHX_ JSContext* ctx, JSValue jsval, SV** err_svp) {
120 SV* RETVAL;
121  
122 40 int tag = JS_VALUE_GET_NORM_TAG(jsval);
123  
124 40 switch (tag) {
125 0 case JS_TAG_EXCEPTION:
126 0 *err_svp = newSVpvs("DEV ERROR: Exception must be unwrapped!");
127 0 return NULL;
128  
129 8 case JS_TAG_STRING:
130 STMT_START {
131 STRLEN strlen;
132 8 const char* str = JS_ToCStringLen(ctx, &strlen, jsval);
133 8 RETVAL = newSVpvn_flags(str, strlen, SVf_UTF8);
134 8 JS_FreeCString(ctx, str);
135 } STMT_END;
136 8 break;
137  
138 11 case JS_TAG_INT:
139 11 RETVAL = newSViv(JS_VALUE_GET_INT(jsval));
140 11 break;
141  
142 4 case JS_TAG_FLOAT64:
143 4 RETVAL = newSVnv(JS_VALUE_GET_FLOAT64(jsval));
144 4 break;
145  
146 3 case JS_TAG_BOOL:
147 3 ▶ RETVAL = boolSV(JS_VALUE_GET_BOOL(jsval));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(JS_VALUE_GET_BOOL(jsval)); 2 1 3
148 3 break;
149  
150 2 case JS_TAG_NULL:
151 case JS_TAG_UNDEFINED:
152 2 RETVAL = &PL_sv_undef;
153 2 break;
154  
155 10 case JS_TAG_OBJECT:
156 10 ▶ if (JS_IsFunction(ctx, jsval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JS_IsFunction(ctx, jsval)) { 1 9 10
157 1 SV* func_sv = exs_new_structref(perl_qjs_func_s, PERL_NS_ROOT "::Function");
158 1 perl_qjs_func_s* pqjs = exs_structref_ptr(func_sv);
159  
160 1 *pqjs = (perl_qjs_func_s) {
161 .ctx = ctx,
162 .jsfunc = jsval,
163 1 .pid = getpid(),
164 };
165  
166 1 ctx_opaque_s* ctxdata = JS_GetContextOpaque(ctx);
167 1 ctxdata->refcount++;
168  
169 1 CV* perlfunc = get_cv(PERL_NS_ROOT "::_wrap_jsfunc", 0);
170 assert(perlfunc);
171  
172 1 SV* perlargs[] = {
173 func_sv,
174 NULL,
175 };
176  
177 1 RETVAL = exs_call_sv_scalar((SV*) perlfunc, perlargs);
178 }
179 9 ▶ else if (JS_IsArray(ctx, jsval)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (JS_IsArray(ctx, jsval)) { 5 4 9
180 5 RETVAL = _JSValue_array_to_SV(aTHX_ ctx, jsval, err_svp);
181 }
182 else {
183 4 RETVAL = _JSValue_object_to_SV(aTHX_ ctx, jsval, err_svp);
184 }
185  
186 10 break;
187  
188 2 default:
189 STMT_START {
190 2 const char* typename = _jstype_name(tag);
191  
192 2 ▶ if (typename) {
Branch: 1 branch
BranchTrueFalseTotal
if (typename) { 2 0 2
193 2 *err_svp = newSVpvf("Cannot convert JS %s (QuickJS tag %d) to Perl!", typename, tag);
194 }
195 else {
196 0 *err_svp = newSVpvf("Cannot convert (unexpected) JS tag value %d to Perl!", tag);
197 }
198  
199 2 return NULL;
200 } STMT_END;
201 }
202  
203 38 return RETVAL;
204 }
205  
206 3 static inline void _ctx_add_sv(pTHX_ JSContext* ctx, SV* sv) {
207 3 ctx_opaque_s* ctxdata = JS_GetContextOpaque(ctx);
208  
209 3 ctxdata->svs_count++;
210  
211 3 ▶ if (ctxdata->svs_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctxdata->svs_count == 1) { 2 1 3
212 2 Newx(ctxdata->svs, ctxdata->svs_count, SV*);
213 }
214 else {
215 1 Renew(ctxdata->svs, ctxdata->svs_count, SV*);
216 }
217  
218 3 ctxdata->svs[ctxdata->svs_count - 1] = SvREFCNT_inc(sv);
219 3 }
220  
221 static JSValue _sv_to_jsvalue(pTHX_ JSContext* ctx, SV* value, SV** error);
222  
223 #define _MAX_ERRSV_TO_JS_TRIES 10
224  
225 /* Kind of like _sv_to_jsvalue(), but we need to handle the case where
226 the error SV’s own conversion to a JSValue fails. In that case, we
227 warn on the 1st error, then propagate the 2nd. Repeat until there are
228 no errors.
229 */
230 3 static JSValue _sv_error_to_jsvalue(pTHX_ JSContext* ctx, SV* error) {
231 3 SV* error2 = NULL;
232  
233 3 uint32_t tries = 0;
234  
235 JSValue to_js;
236  
237 while (1) {
238 4 to_js = _sv_to_jsvalue(aTHX_ ctx, error, &error2);
239  
240 4 ▶ if (!error2) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!error2) break; 3 1 4
241  
242 1 warn_sv(error);
243  
244 1 tries++;
245 1 ▶ if (tries > _MAX_ERRSV_TO_JS_TRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if (tries > _MAX_ERRSV_TO_JS_TRIES) { 0 1 1
246 0 warn_sv(error2);
247 0 return JS_NewString(ctx, "Failed to convert Perl error to JavaScript after " STRINGIFY(_MAX_ERRSV_TO_JS_TRIES) " tries!");
248 }
249  
250 1 error = error2;
251 1 error2 = NULL;
252 }
253  
254 3 return to_js;
255 }
256  
257 4 static JSValue __do_perl_callback(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int jsmagic, JSValue *func_data) {
258  
259 #ifdef MULTIPLICITY
260 ctx_opaque_s* ctxdata = JS_GetContextOpaque(ctx);
261 pTHX = ctxdata->aTHX;
262 #endif
263  
264 PERL_UNUSED_VAR(jsmagic);
265 4 SV* cb_sv = ((SV**) func_data)[0];
266  
267 4 SV* args[argc + 1];
268 4 args[argc] = NULL;
269  
270 4 SV* error_sv = NULL;
271  
272 5 ▶ for (int a=0; a<argc; a++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int a=0; a<argc; a++) { 3 2 5
273 3 args[a] = _JSValue_to_SV(aTHX_ ctx, argv[a], &error_sv);
274  
275 3 ▶ if (error_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (error_sv) { 2 1 3
276 2 ▶ while (--a >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (--a >= 0) { 0 2 2
277 0 SvREFCNT_dec(args[a]);
278 }
279  
280 2 break;
281 }
282 }
283  
284 4 ▶ if (!error_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!error_sv) { 2 2 4
285 2 SV* from_perl = exs_call_sv_scalar_trapped(cb_sv, args, &error_sv);
286  
287 2 ▶ if (from_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if (from_perl) { 1 1 2
288 1 JSValue to_js = _sv_to_jsvalue(aTHX_ ctx, from_perl, &error_sv);
289  
290 1 ▶ if (!error_sv) return to_js;
Branch: 1 branch
BranchTrueFalseTotal
if (!error_sv) return to_js; 1 0 1
291 }
292 }
293  
294 3 JSValue jserr = _sv_error_to_jsvalue(aTHX_ ctx, error_sv);
295  
296 3 return JS_Throw(ctx, jserr);
297 }
298  
299 29 static JSValue _sv_to_jsvalue(pTHX_ JSContext* ctx, SV* value, SV** error_svp) {
300 29 ▶ if (!SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(value)) { 0 29 29
301 0 return JS_NULL;
302 }
303  
304 29 ▶ if (SvROK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(value)) { 11 18 29
305 11 ▶ if (!sv_isobject(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_isobject(value)) { 9 2 11
306 9 switch (SvTYPE(SvRV(value))) {
307 3 case SVt_PVCV:
308 3 _ctx_add_sv(aTHX_ ctx, value);
309  
310 /* A hack to store our callback via the func_data pointer: */
311 3 JSValue dummy = JS_MKPTR(JS_TAG_INT, value);
312  
313 9 return JS_NewCFunctionData(
314 ctx,
315 __do_perl_callback,
316 0, 0,
317 1, &dummy
318 );
319  
320 3 case SVt_PVAV: STMT_START {
321 3 AV* av = (AV*) SvRV(value);
322 3 JSValue jsarray = JS_NewArray(ctx);
323 6 ▶ JS_SetPropertyStr(ctx, jsarray, "length", JS_NewUint32(ctx, 1 + av_len(av)));
Branch: 1 branch
BranchTrueFalseTotal
JS_SetPropertyStr(ctx, jsarray, "length", JS_NewUint32(ctx, 1 + av_len(av))); 3 0 3
324 6 ▶ for (int32_t i=0; i <= av_len(av); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int32_t i=0; i <= av_len(av); i++) { 3 3 6
325 3 SV** svp = av_fetch(av, i, 0);
326 assert(svp);
327 assert(*svp);
328  
329 3 JSValue jsval = _sv_to_jsvalue(aTHX_ ctx, *svp, error_svp);
330  
331 3 ▶ if (*error_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (*error_svp) { 0 3 3
332 0 JS_FreeValue(ctx, jsarray);
333 0 return _sv_error_to_jsvalue(aTHX_ ctx, *error_svp);
334 }
335  
336 3 JS_SetPropertyUint32(ctx, jsarray, i, jsval);
337 }
338  
339 3 return jsarray;
340 } STMT_END;
341  
342 3 case SVt_PVHV: STMT_START {
343 3 HV* hv = (HV*) SvRV(value);
344 3 JSValue jsobj = JS_NewObject(ctx);
345  
346 3 hv_iterinit(hv);
347  
348 HE* hvent;
349 5 ▶ while ( (hvent = hv_iternext(hv)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (hvent = hv_iternext(hv)) ) { 2 3 5
350 2 SV* key_sv = hv_iterkeysv(hvent);
351 2 SV* val_sv = hv_iterval(hv, hvent);
352  
353 STRLEN keylen;
354 2 const char* key = SvPVutf8(key_sv, keylen);
355  
356 2 JSValue jsval = _sv_to_jsvalue(aTHX_ ctx, val_sv, error_svp);
357 2 ▶ if (*error_svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (*error_svp) { 0 2 2
358 0 JS_FreeValue(ctx, jsobj);
359 0 return _sv_error_to_jsvalue(aTHX_ ctx, *error_svp);
360 }
361  
362 2 JSAtom prop = JS_NewAtomLen(ctx, key, keylen);
363  
364 /* NB: ctx takes over jsval. */
365 2 JS_DefinePropertyValue(ctx, jsobj, prop, jsval, JS_PROP_WRITABLE);
366  
367 2 JS_FreeAtom(ctx, prop);
368 }
369  
370 3 return jsobj;
371 } STMT_END;
372  
373 0 default:
374 /* We’ll croak below. */
375 0 break;
376 }
377 }
378 }
379 else {
380 #ifdef SvIsBOOL
381 18 ▶ if (SvIsBOOL(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsBOOL(value)) { 0 18 18
382 0 return JS_NewBool(ctx, SvTRUE(value));
383 }
384 #endif
385  
386 18 ▶ if (SvNOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOK(value)) { 2 16 18
387 4 return JS_NewFloat64(ctx, (double) SvNV(value));
388 }
389  
390 16 ▶ if (SvUOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUOK(value)) { 0 16 16
391 if (sizeof(IV) == sizeof(uint64_t)) {
392 0 return JS_NewInt64(ctx, (uint64_t) SvIV(value));
393 }
394  
395 return JS_NewInt32(ctx, (uint32_t) SvIV(value));
396 }
397 16 ▶ if (SvIOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(value)) { 8 8 16
398 if (sizeof(IV) == sizeof(int64_t)) {
399 16 return JS_NewInt64(ctx, (int64_t) SvIV(value));
400 }
401  
402 return JS_NewInt32(ctx, (int32_t) SvIV(value));
403 }
404  
405 8 ▶ if (SvPOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(value)) { 8 0 8
406 STRLEN len;
407 8 const char* str = SvPVutf8(value, len);
408  
409 8 return JS_NewStringLen(ctx, str, len);
410 }
411 }
412  
413 2 *error_svp = newSVpvf("Cannot convert %" SVf " to JavaScript!", value);
414  
415 2 return JS_NULL;
416 }
417  
418 4 JSContext* _create_new_jsctx( pTHX_ JSRuntime *rt ) {
419 4 JSContext *ctx = JS_NewContext(rt);
420  
421 ctx_opaque_s* ctxdata;
422 4 Newxz(ctxdata, 1, ctx_opaque_s);
423 4 JS_SetContextOpaque(ctx, ctxdata);
424  
425 4 *ctxdata = (ctx_opaque_s) {
426 .refcount = 1,
427 #ifdef MULTIPLICITY
428 .aTHX = aTHX,
429 #endif
430 };
431  
432 4 return ctx;
433 }
434  
435 23 static inline SV* _return_jsvalue_or_croak(pTHX_ JSContext* ctx, JSValue jsret) {
436 SV* err;
437 SV* RETVAL;
438  
439 23 ▶ if (JS_IsException(jsret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (JS_IsException(jsret)) { 4 19 23
440 4 JSValue jserr = JS_GetException(ctx);
441 //err = _JSValue_to_SV(aTHX_ ctx, jserr);
442  
443 /* Ideal here is to capture all aspects of the error object,
444 including its `name` and members. But for now just give
445 a string.
446  
447 JSValue jslen = JS_GetPropertyStr(ctx, jserr, "name");
448 STRLEN namelen;
449 const char* namestr = JS_ToCStringLen(ctx, &namelen, jslen);
450 */
451  
452 STRLEN strlen;
453 4 const char* str = JS_ToCStringLen(ctx, &strlen, jserr);
454  
455 4 err = newSVpvn_flags(str, strlen, SVf_UTF8);
456  
457 4 JS_FreeCString(ctx, str);
458 4 JS_FreeValue(ctx, jserr);
459 4 RETVAL = NULL; // silence uninitialized warning
460 }
461 else {
462 19 err = NULL;
463 19 RETVAL = _JSValue_to_SV(aTHX_ ctx, jsret, &err);
464 }
465  
466 23 JS_FreeValue(ctx, jsret);
467  
468 23 ▶ if (err) croak_sv(err);
Branch: 1 branch
BranchTrueFalseTotal
if (err) croak_sv(err); 4 19 23
469  
470 19 return RETVAL;
471 }
472  
473 5 static void _free_jsctx(pTHX_ JSContext* ctx) {
474 5 ctx_opaque_s* ctxdata = JS_GetContextOpaque(ctx);
475  
476 5 ▶ if (--ctxdata->refcount == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ctxdata->refcount == 0) { 4 1 5
477 4 JSRuntime *rt = JS_GetRuntime(ctx);
478  
479 7 ▶ for (U32 i=0; i<ctxdata->svs_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (U32 i=0; i<ctxdata->svs_count; i++) { 3 4 7
480 3 SvREFCNT_dec(ctxdata->svs[i]);
481 }
482  
483 4 Safefree(ctxdata);
484  
485 4 JS_FreeContext(ctx);
486 4 JS_FreeRuntime(rt);
487 }
488 5 }
489  
490 /* ---------------------------------------------------------------------- */
491  
492 MODULE = JavaScript::QuickJS PACKAGE = JavaScript::QuickJS
493  
494 PROTOTYPES: DISABLE
495  
496 SV*
497 new (SV* classname_sv)
498 CODE:
499 4 JSRuntime *rt = JS_NewRuntime();
500 4 JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker, NULL);
501 4 JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
502  
503 4 JSContext *ctx = _create_new_jsctx(aTHX_ rt);
504  
505 4 RETVAL = exs_new_structref(perl_qjs_s, SvPVbyte_nolen(classname_sv));
506 4 perl_qjs_s* pqjs = exs_structref_ptr(RETVAL);
507  
508 4 *pqjs = (perl_qjs_s) {
509 .ctx = ctx,
510 4 .pid = getpid(),
511 };
512  
513 OUTPUT:
514 RETVAL
515  
516 void
517 DESTROY (SV* self_sv)
518 CODE:
519 4 perl_qjs_s* pqjs = exs_structref_ptr(self_sv);
520  
521 4 ▶ if (PL_dirty && pqjs->pid == getpid()) {
Branch: 2 branches
BranchTrueFalseTotal
if (PL_dirty && pqjs->pid == getpid()) { 0 4 4
if (PL_dirty && pqjs->pid == getpid()) { 0 0 0
522 0 warn("DESTROYing %" SVf " at global destruction; memory leak likely!\n", self_sv);
523 }
524  
525  
526 4 _free_jsctx(aTHX_ pqjs->ctx);
527  
528 SV*
529 std (SV* self_sv)
530 ALIAS:
531 os = 1
532 helpers = 2
533 CODE:
534 1 RETVAL = SvREFCNT_inc(self_sv);
535  
536 1 perl_qjs_s* pqjs = exs_structref_ptr(self_sv);
537  
538 1 switch (ix) {
539 0 case 0:
540 0 js_init_module_std(pqjs->ctx, "std");
541 0 break;
542 0 case 1:
543 0 js_init_module_os(pqjs->ctx, "os");
544 0 break;
545 1 case 2:
546 1 js_std_add_helpers(pqjs->ctx, 0, NULL);
547  
548 1 default:
549 assert(0);
550 }
551  
552 OUTPUT:
553 RETVAL
554  
555 SV*
556 set_globals (SV* self_sv, ...)
557 CODE:
558 17 ▶ if (items < 2) croak("Need at least 1 key/value pair.");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Need at least 1 key/value pair."); 0 17 17
559  
560 17 ▶ if (!(items % 2)) croak("Need an even list of key/value pairs.");
Branch: 1 branch
BranchTrueFalseTotal
if (!(items % 2)) croak("Need an even list of key/value pairs."); 0 17 17
561  
562 17 I32 valscount = (items - 1) >> 1;
563  
564 17 perl_qjs_s* pqjs = exs_structref_ptr(self_sv);
565  
566 SV* jsname_sv, *value_sv;
567  
568 17 SV* error = NULL;
569  
570 17 JSAtom jsnames[valscount];
571 17 JSValue jsvals[valscount];
572  
573 34 ▶ for (int i=0; i < valscount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i=0; i < valscount; i++) { 18 16 34
574 18 jsname_sv = ST( 1 + (i << 1) );
575 18 value_sv = ST( 2 + (i << 1) );
576  
577 STRLEN jsnamelen;
578 18 const char* jsname_str = SvPVutf8(jsname_sv, jsnamelen);
579  
580 18 JSValue jsval = _sv_to_jsvalue(aTHX_ pqjs->ctx, value_sv, &error);
581  
582 18 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 1 17 18
583 1 ▶ while (i-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (i-- > 0) { 0 1 1
584 0 JS_FreeAtom(pqjs->ctx, jsnames[i]);
585 0 JS_FreeValue(pqjs->ctx, jsvals[i]);
586 }
587  
588 1 croak_sv(error);
589 }
590  
591 17 jsnames[i] = JS_NewAtomLen(pqjs->ctx, jsname_str, jsnamelen);
592 17 jsvals[i] = jsval;
593 }
594  
595 16 JSValue jsglobal = JS_GetGlobalObject(pqjs->ctx);
596  
597 33 ▶ for (int i=0; i < valscount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i=0; i < valscount; i++) { 17 16 33
598 /* NB: ctx takes over jsval. */
599 17 JS_DefinePropertyValue(pqjs->ctx, jsglobal, jsnames[i], jsvals[i], JS_PROP_WRITABLE);
600 17 JS_FreeAtom(pqjs->ctx, jsnames[i]);
601 }
602  
603 16 JS_FreeValue(pqjs->ctx, jsglobal);
604  
605 16 RETVAL = SvREFCNT_inc(self_sv);
606  
607 OUTPUT:
608 RETVAL
609  
610 SV*
611 eval (SV* self_sv, SV* js_code_sv)
612 ALIAS:
613 eval_module = 1
614 CODE:
615 22 perl_qjs_s* pqjs = exs_structref_ptr(self_sv);
616 22 JSContext *ctx = pqjs->ctx;
617  
618 STRLEN js_code_len;
619 22 const char* js_code = SvPVutf8(js_code_sv, js_code_len);
620  
621 22 int eval_flags = ix ? JS_EVAL_TYPE_MODULE : JS_EVAL_TYPE_GLOBAL;
622 22 JSValue jsret = JS_Eval(ctx, js_code, js_code_len, "", eval_flags);
623  
624 22 RETVAL = _return_jsvalue_or_croak(aTHX_ ctx, jsret);
625  
626 OUTPUT:
627 RETVAL
628  
629 # ----------------------------------------------------------------------
630 # This package isn’t exposed to Perl code; it just exists to facilitate
631 # calling JS functions from Perl. All instances are wrapped in closures.
632  
633 MODULE = JavaScript::QuickJS PACKAGE = JavaScript::QuickJS::Function
634  
635 void
636 DESTROY( SV* self_sv )
637 CODE:
638 1 perl_qjs_func_s* pqjs = exs_structref_ptr(self_sv);
639  
640 /* Functions don’t appear to need to be freed … ? */
641  
642 1 _free_jsctx(aTHX_ pqjs->ctx);
643  
644  
645 SV*
646 call( SV* self_sv, ... )
647 CODE:
648 1 perl_qjs_func_s* pqjs = exs_structref_ptr(self_sv);
649  
650 1 U32 params_count = items - 1;
651  
652 1 JSValue jsvars[params_count];
653  
654 1 SV* error = NULL;
655  
656 2 ▶ for (uint32_t i=0; i<params_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i=0; i<params_count; i++) { 1 1 2
657 1 SV* cur_sv = ST(i+1);
658  
659 1 JSValue jsval = _sv_to_jsvalue(aTHX_ pqjs->ctx, cur_sv, &error);
660  
661 1 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 1 1
662 0 while (--i >= 0) {
663 0 JS_FreeValue(pqjs->ctx, jsvars[i]);
664 }
665  
666 croak_sv(error);
667 }
668  
669 1 jsvars[i] = jsval;
670 }
671  
672 1 JSValue jsret = JS_Call(pqjs->ctx, pqjs->jsfunc, JS_NULL, params_count, jsvars);
673  
674 1 RETVAL = _return_jsvalue_or_croak(aTHX_ pqjs->ctx, jsret);
675  
676 OUTPUT:
677 RETVAL
« easyxs/easyxs_perlcall.h ↑ Summary quickjs/quickjs.h »