1 /*
2 * This file is part of mpv.
3 *
4 * mpv is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * mpv is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <assert.h>
19 #include <string.h>
20 #include <strings.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <math.h>
26 #include <stdint.h>
27
28 #include <mujs.h>
29
30 #include "osdep/io.h"
31 #include "mpv_talloc.h"
32 #include "common/common.h"
33 #include "options/m_property.h"
34 #include "common/msg.h"
35 #include "common/msg_control.h"
36 #include "common/stats.h"
37 #include "options/m_option.h"
38 #include "input/input.h"
39 #include "options/path.h"
40 #include "misc/bstr.h"
41 #include "osdep/timer.h"
42 #include "osdep/threads.h"
43 #include "stream/stream.h"
44 #include "sub/osd.h"
45 #include "core.h"
46 #include "command.h"
47 #include "client.h"
48 #include "libmpv/client.h"
49
50 // List of builtin modules and their contents as strings.
51 // All these are generated from player/javascript/*.js
52 static const char *const builtin_files[][3] = {
53 {"@/defaults.js",
54 # include "generated/player/javascript/defaults.js.inc"
55 },
56 {0}
57 };
58
59 // Represents a loaded script. Each has its own js state.
60 struct script_ctx {
61 const char *filename;
62 const char *path; // NULL if single file
63 struct mpv_handle *client;
64 struct MPContext *mpctx;
65 struct mp_log *log;
66 char *last_error_str;
67 size_t js_malloc_size;
68 struct stats_ctx *stats;
69 };
70
jctx(js_State * J)71 static struct script_ctx *jctx(js_State *J)
72 {
73 return (struct script_ctx *)js_getcontext(J);
74 }
75
jclient(js_State * J)76 static mpv_handle *jclient(js_State *J)
77 {
78 return jctx(J)->client;
79 }
80
81 static void pushnode(js_State *J, mpv_node *node);
82 static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx);
83 static int jsL_checkint(js_State *J, int idx);
84 static uint64_t jsL_checkuint64(js_State *J, int idx);
85
86 /**********************************************************************
87 * conventions, MuJS notes and vm errors
88 *********************************************************************/
89 // - push_foo functions are called from C and push a value to the vm stack.
90 //
91 // - JavaScript C functions are code which the vm can call as a js function.
92 // By convention, script_bar and script__baz are js C functions. The former
93 // is exposed to end users as bar, and _baz is for internal use.
94 //
95 // - js C functions get a fresh vm stack with their arguments, and may
96 // manipulate their stack as they see fit. On exit, the vm considers the
97 // top value of their stack as their return value, and GC the rest.
98 //
99 // - js C function's stack[0] is "this", and the rest (1, 2, ...) are the args.
100 // On entry the stack has at least the number of args defined for the func,
101 // padded with undefined if called with less, or bigger if called with more.
102 //
103 // - Almost all vm APIs (js_*) may throw an error - a longjmp to the last
104 // recovery/catch point, which could skip releasing resources. This includes
105 // js_try itself(!), except at the outer-most [1] js_try which is always
106 // entering the try part (and the catch part if the try part throws).
107 // The assumption should be that anything can throw and needs careful setup.
108 // One such automated setup is the autofree mechanism. Details later.
109 //
110 // - Unless named s_foo, all the functions at this file (inc. init) which
111 // touch the vm may throw, but either cleanup resources regardless (mostly
112 // autofree) or leave allocated resources on caller-provided talloc context
113 // which the caller should release, typically with autofree (e.g. makenode).
114 //
115 // - Functions named s_foo (safe foo) never throw if called at the outer-most
116 // try-levels, or, inside JS C functions - never throw after allocating.
117 // If they didn't throw then they return 0 on success, 1 on js-errors.
118 //
119 // [1] In practice the N outer-most (nested) tries are guaranteed to enter the
120 // try/carch code, where N is the mujs try-stack size (64 with mujs 1.1.3).
121 // But because we can't track try-level at (called-back) JS C functions,
122 // it's only guaranteed when we know we're near the outer-most try level.
123
124 /**********************************************************************
125 * mpv scripting API error handling
126 *********************************************************************/
127 // - Errors may be thrown on some cases - the reason is at the exception.
128 //
129 // - Some APIs also set last error which can be fetched with mp.last_error(),
130 // where empty string (false-y) is success, or an error string otherwise.
131 //
132 // - The rest of the APIs are guaranteed to return undefined on error or a
133 // true-thy value on success and may or may not set last error.
134 //
135 // - push_success, push_failure, push_status and pushed_error set last error.
136
137 // iserr as true indicates an error, and if so, str may indicate a reason.
138 // Internally ctx->last_error_str is never NULL, and empty indicates success.
set_last_error(struct script_ctx * ctx,bool iserr,const char * str)139 static void set_last_error(struct script_ctx *ctx, bool iserr, const char *str)
140 {
141 ctx->last_error_str[0] = 0;
142 if (!iserr)
143 return;
144 if (!str || !str[0])
145 str = "Error";
146 ctx->last_error_str = talloc_strdup_append(ctx->last_error_str, str);
147 }
148
149 // For use only by wrappers at defaults.js.
150 // arg: error string. Use empty string to indicate success.
script__set_last_error(js_State * J)151 static void script__set_last_error(js_State *J)
152 {
153 const char *e = js_tostring(J, 1);
154 set_last_error(jctx(J), e[0], e);
155 }
156
157 // mp.last_error() . args: none. return the last error without modifying it.
script_last_error(js_State * J)158 static void script_last_error(js_State *J)
159 {
160 js_pushstring(J, jctx(J)->last_error_str);
161 }
162
163 // Generic success for APIs which don't return an actual value.
push_success(js_State * J)164 static void push_success(js_State *J)
165 {
166 set_last_error(jctx(J), 0, NULL);
167 js_pushboolean(J, true);
168 }
169
170 // Doesn't (intentionally) throw. Just sets last_error and pushes undefined
push_failure(js_State * J,const char * str)171 static void push_failure(js_State *J, const char *str)
172 {
173 set_last_error(jctx(J), 1, str);
174 js_pushundefined(J);
175 }
176
177 // Most of the scripting APIs are either sending some values and getting status
178 // code in return, or requesting some value while providing a default in case an
179 // error happened. These simplify the C code for that and always set last_error.
180
push_status(js_State * J,int err)181 static void push_status(js_State *J, int err)
182 {
183 if (err >= 0) {
184 push_success(J);
185 } else {
186 push_failure(J, mpv_error_string(err));
187 }
188 }
189
190 // If err is success then return 0, else push the item at def and return 1
pushed_error(js_State * J,int err,int def)191 static bool pushed_error(js_State *J, int err, int def)
192 {
193 bool iserr = err < 0;
194 set_last_error(jctx(J), iserr, iserr ? mpv_error_string(err) : NULL);
195 if (!iserr)
196 return false;
197
198 js_copy(J, def);
199 return true;
200 }
201
202 /**********************************************************************
203 * Autofree - care-free resource deallocation on vm errors, and otherwise
204 *********************************************************************/
205 // - Autofree (af) functions are called with a talloc context argument which is
206 // freed after the function exits - either normally or because it threw an
207 // error, on the latter case it then re-throws the error after the cleanup.
208 //
209 // Autofree js C functions should have an additional void* talloc arg and
210 // inserted into the vm using af_newcfunction, but otherwise used normally.
211 //
212 // To wrap an autofree function af_TARGET in C:
213 // 1. Create a wrapper s_TARGET which does this:
214 // if (js_try(J))
215 // return 1;
216 // *af = talloc_new(NULL);
217 // af_TARGET(J, ..., *af);
218 // js_endtry(J);
219 // return 0;
220 // 2. Use s_TARGET like so (frees if allocated, throws if {s,af}_TARGET threw):
221 // void *af = NULL;
222 // int r = s_TARGET(J, ..., &af); // use J, af where the callee expects.
223 // talloc_free(af);
224 // if (r)
225 // js_throw(J);
226 //
227 // The reason that the allocation happens inside try/catch is that js_try
228 // itself can throw (if it runs out of try-stack) and therefore the code
229 // inside the try part is not reached - but neither is the catch part(!),
230 // and instead it throws to the next outer catch - but before we've allocated
231 // anything, hence no leaks on such case. If js_try did get entered, then the
232 // allocation happened, and then if af_TARGET threw then s_TARGET will catch
233 // it (and return 1) and we'll free if afterwards.
234
235 // add_af_file, add_af_dir, add_af_mpv_alloc take a valid FILE*/DIR*/char* value
236 // respectively, and fclose/closedir/mpv_free it when the parent is freed.
237
destruct_af_file(void * p)238 static void destruct_af_file(void *p)
239 {
240 fclose(*(FILE**)p);
241 }
242
add_af_file(void * parent,FILE * f)243 static void add_af_file(void *parent, FILE *f)
244 {
245 FILE **pf = talloc(parent, FILE*);
246 *pf = f;
247 talloc_set_destructor(pf, destruct_af_file);
248 }
249
destruct_af_dir(void * p)250 static void destruct_af_dir(void *p)
251 {
252 closedir(*(DIR**)p);
253 }
254
add_af_dir(void * parent,DIR * d)255 static void add_af_dir(void *parent, DIR *d)
256 {
257 DIR **pd = talloc(parent, DIR*);
258 *pd = d;
259 talloc_set_destructor(pd, destruct_af_dir);
260 }
261
destruct_af_mpv_alloc(void * p)262 static void destruct_af_mpv_alloc(void *p)
263 {
264 mpv_free(*(char**)p);
265 }
266
add_af_mpv_alloc(void * parent,char * ma)267 static void add_af_mpv_alloc(void *parent, char *ma)
268 {
269 char **p = talloc(parent, char*);
270 *p = ma;
271 talloc_set_destructor(p, destruct_af_mpv_alloc);
272 }
273
destruct_af_mpv_node(void * p)274 static void destruct_af_mpv_node(void *p)
275 {
276 mpv_free_node_contents((mpv_node*)p); // does nothing for MPV_FORMAT_NONE
277 }
278
279 // returns a new zeroed allocated struct mpv_node, and free it and its content
280 // when the parent is freed.
new_af_mpv_node(void * parent)281 static mpv_node *new_af_mpv_node(void *parent)
282 {
283 mpv_node *p = talloc_zero(parent, mpv_node); // .format == MPV_FORMAT_NONE
284 talloc_set_destructor(p, destruct_af_mpv_node);
285 return p;
286 }
287
288 // Prototype for autofree functions which can be called from inside the vm.
289 typedef void (*af_CFunction)(js_State*, void*);
290
291 // safely run autofree js c function directly
s_run_af_jsc(js_State * J,af_CFunction fn,void ** af)292 static int s_run_af_jsc(js_State *J, af_CFunction fn, void **af)
293 {
294 if (js_try(J))
295 return 1;
296 *af = talloc_new(NULL);
297 fn(J, *af);
298 js_endtry(J);
299 return 0;
300 }
301
302 // The trampoline function through which all autofree functions are called from
303 // inside the vm. Obtains the target function address and autofree-call it.
script__autofree(js_State * J)304 static void script__autofree(js_State *J)
305 {
306 // The target function is at the "af_" property of this function instance.
307 js_currentfunction(J);
308 js_getproperty(J, -1, "af_");
309 af_CFunction fn = (af_CFunction)js_touserdata(J, -1, "af_fn");
310 js_pop(J, 2);
311
312 void *af = NULL;
313 int r = s_run_af_jsc(J, fn, &af);
314 talloc_free(af);
315 if (r)
316 js_throw(J);
317 }
318
319 // Identical to js_newcfunction, but the function is inserted with an autofree
320 // wrapper, and its prototype should have the additional af argument.
af_newcfunction(js_State * J,af_CFunction fn,const char * name,int length)321 static void af_newcfunction(js_State *J, af_CFunction fn, const char *name,
322 int length)
323 {
324 js_newcfunction(J, script__autofree, name, length);
325 js_pushnull(J); // a prototype for the userdata object
326 js_newuserdata(J, "af_fn", fn, NULL); // uses a "af_fn" verification tag
327 js_defproperty(J, -2, "af_", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
328 }
329
330 /**********************************************************************
331 * Initialization and file loading
332 *********************************************************************/
333
get_builtin_file(const char * name)334 static const char *get_builtin_file(const char *name)
335 {
336 for (int n = 0; builtin_files[n][0]; n++) {
337 if (strcmp(builtin_files[n][0], name) == 0)
338 return builtin_files[n][1];
339 }
340 return NULL;
341 }
342
343 // Push up to limit bytes of file fname: from builtin_files, else from the OS.
af_push_file(js_State * J,const char * fname,int limit,void * af)344 static void af_push_file(js_State *J, const char *fname, int limit, void *af)
345 {
346 char *filename = mp_get_user_path(af, jctx(J)->mpctx->global, fname);
347 MP_VERBOSE(jctx(J), "Reading file '%s'\n", filename);
348 if (limit < 0)
349 limit = INT_MAX - 1;
350
351 const char *builtin = get_builtin_file(filename);
352 if (builtin) {
353 js_pushlstring(J, builtin, MPMIN(limit, strlen(builtin)));
354 return;
355 }
356
357 FILE *f = fopen(filename, "rb");
358 if (!f)
359 js_error(J, "cannot open file: '%s'", filename);
360 add_af_file(af, f);
361
362 int len = MPMIN(limit, 32 * 1024); // initial allocation, size*2 strategy
363 int got = 0;
364 char *s = NULL;
365 while ((s = talloc_realloc(af, s, char, len))) {
366 int want = len - got;
367 int r = fread(s + got, 1, want, f);
368
369 if (feof(f) || (len == limit && r == want)) {
370 js_pushlstring(J, s, got + r);
371 return;
372 }
373 if (r != want)
374 js_error(J, "cannot read data from file: '%s'", filename);
375
376 got = got + r;
377 len = MPMIN(limit, len * 2);
378 }
379
380 js_error(J, "cannot allocate %d bytes for file: '%s'", len, filename);
381 }
382
383 // Safely run af_push_file.
s_push_file(js_State * J,const char * fname,int limit,void ** af)384 static int s_push_file(js_State *J, const char *fname, int limit, void **af)
385 {
386 if (js_try(J))
387 return 1;
388 *af = talloc_new(NULL);
389 af_push_file(J, fname, limit, *af);
390 js_endtry(J);
391 return 0;
392 }
393
394 // Called directly, push up to limit bytes of file fname (from builtin/os).
push_file_content(js_State * J,const char * fname,int limit)395 static void push_file_content(js_State *J, const char *fname, int limit)
396 {
397 void *af = NULL;
398 int r = s_push_file(J, fname, limit, &af);
399 talloc_free(af);
400 if (r)
401 js_throw(J);
402 }
403
404 // utils.read_file(..). args: fname [,max]. returns [up to max] bytes as string.
script_read_file(js_State * J)405 static void script_read_file(js_State *J)
406 {
407 int limit = js_isundefined(J, 2) ? -1 : jsL_checkint(J, 2);
408 push_file_content(J, js_tostring(J, 1), limit);
409 }
410
411 // Runs a file with the caller's this, leaves the stack as is.
run_file(js_State * J,const char * fname)412 static void run_file(js_State *J, const char *fname)
413 {
414 MP_VERBOSE(jctx(J), "Loading file %s\n", fname);
415 push_file_content(J, fname, -1);
416 js_loadstring(J, fname, js_tostring(J, -1));
417 js_copy(J, 0); // use the caller's this
418 js_call(J, 0);
419 js_pop(J, 2); // result, file content
420 }
421
422 // The spec defines .name and .message for Error objects. Most engines also set
423 // a very convenient .stack = name + message + trace, but MuJS instead sets
424 // .stackTrace = trace only. Normalize by adding such .stack if required.
425 // Run this before anything such that we can get traces on any following errors.
426 static const char *norm_err_proto_js = "\
427 if (Error().stackTrace && !Error().stack) {\
428 Object.defineProperty(Error.prototype, 'stack', {\
429 get: function() {\
430 return this.name + ': ' + this.message + this.stackTrace;\
431 }\
432 });\
433 }\
434 ";
435
436 static void add_functions(js_State*, struct script_ctx*);
437
438 // args: none. called as script, setup and run the main script
script__run_script(js_State * J)439 static void script__run_script(js_State *J)
440 {
441 js_loadstring(J, "@/norm_err.js", norm_err_proto_js);
442 js_copy(J, 0);
443 js_pcall(J, 0);
444
445 struct script_ctx *ctx = jctx(J);
446 add_functions(J, ctx);
447 run_file(J, "@/defaults.js");
448 run_file(J, ctx->filename); // the main file to run
449
450 if (!js_hasproperty(J, 0, "mp_event_loop") || !js_iscallable(J, -1))
451 js_error(J, "no event loop function");
452 js_copy(J, 0);
453 js_call(J, 0); // mp_event_loop
454 }
455
456 // Safely set last error from stack top: stack trace or toString or generic.
457 // May leave items on stack - the caller should detect and pop if it cares.
s_top_to_last_error(struct script_ctx * ctx,js_State * J)458 static void s_top_to_last_error(struct script_ctx *ctx, js_State *J)
459 {
460 set_last_error(ctx, 1, "unknown error");
461 if (js_try(J))
462 return;
463 if (js_isobject(J, -1))
464 js_hasproperty(J, -1, "stack"); // fetches it if exists
465 set_last_error(ctx, 1, js_tostring(J, -1));
466 js_endtry(J);
467 }
468
469 // MuJS can report warnings through this.
report_handler(js_State * J,const char * msg)470 static void report_handler(js_State *J, const char *msg)
471 {
472 MP_WARN(jctx(J), "[JS] %s\n", msg);
473 }
474
475 // Safely setup the js vm for calling run_script.
s_init_js(js_State * J,struct script_ctx * ctx)476 static int s_init_js(js_State *J, struct script_ctx *ctx)
477 {
478 if (js_try(J))
479 return 1;
480 js_setcontext(J, ctx);
481 js_setreport(J, report_handler);
482 js_newcfunction(J, script__run_script, "run_script", 0);
483 js_pushglobal(J); // 'this' for script__run_script
484 js_endtry(J);
485 return 0;
486 }
487
mp_js_alloc(void * actx,void * ptr,int size_)488 static void *mp_js_alloc(void *actx, void *ptr, int size_)
489 {
490 if (size_ < 0)
491 return NULL;
492
493 struct script_ctx* ctx = actx;
494 size_t size = size_, osize = 0;
495 if (ptr) // free/realloc
496 osize = ta_get_size(ptr);
497
498 void *ret = talloc_realloc_size(actx, ptr, size);
499
500 if (!size || ret) { // free / successful realloc/malloc
501 ctx->js_malloc_size = ctx->js_malloc_size - osize + size;
502 stats_size_value(ctx->stats, "mem", ctx->js_malloc_size);
503 }
504 return ret;
505 }
506
507 /**********************************************************************
508 * Initialization - booting the script
509 *********************************************************************/
510 // s_load_javascript: (entry point) creates the js vm, runs the script, returns
511 // on script exit or uncaught js errors. Never throws.
512 // script__run_script: - loads the built in functions and vars into the vm
513 // - runs the default file[s] and the main script file
514 // - calls mp_event_loop, returns on script-exit or throws.
515 //
516 // Note: init functions don't need autofree. They can use ctx as a talloc
517 // context and free normally. If they throw - ctx is freed right afterwards.
s_load_javascript(struct mp_script_args * args)518 static int s_load_javascript(struct mp_script_args *args)
519 {
520 struct script_ctx *ctx = talloc_ptrtype(NULL, ctx);
521 *ctx = (struct script_ctx) {
522 .client = args->client,
523 .mpctx = args->mpctx,
524 .log = args->log,
525 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),
526 .filename = args->filename,
527 .path = args->path,
528 .js_malloc_size = 0,
529 .stats = stats_ctx_create(ctx, args->mpctx->global,
530 mp_tprintf(80, "script/%s", mpv_client_name(args->client))),
531 };
532
533 stats_register_thread_cputime(ctx->stats, "cpu");
534
535 js_Alloc alloc_fn = NULL;
536 void *actx = NULL;
537
538 char *mem_report = getenv("MPV_LEAK_REPORT");
539 if (mem_report && strcmp(mem_report, "1") == 0) {
540 alloc_fn = mp_js_alloc;
541 actx = ctx;
542 }
543
544 int r = -1;
545 js_State *J = js_newstate(alloc_fn, actx, 0);
546 if (!J || s_init_js(J, ctx))
547 goto error_out;
548
549 set_last_error(ctx, 0, NULL);
550 if (js_pcall(J, 0)) { // script__run_script
551 s_top_to_last_error(ctx, J);
552 goto error_out;
553 }
554
555 r = 0;
556
557 error_out:
558 if (r)
559 MP_FATAL(ctx, "%s\n", ctx->last_error_str);
560 if (J)
561 js_freestate(J);
562
563 talloc_free(ctx);
564 return r;
565 }
566
567 /**********************************************************************
568 * Main mp.* scripting APIs and helpers
569 *********************************************************************/
570 // Return the index in opts of stack[idx] (or of def if undefined), else throws.
checkopt(js_State * J,int idx,const char * def,const char * opts[],const char * desc)571 static int checkopt(js_State *J, int idx, const char *def, const char *opts[],
572 const char *desc)
573 {
574 const char *opt = js_isundefined(J, idx) ? def : js_tostring(J, idx);
575 for (int i = 0; opts[i]; i++) {
576 if (strcmp(opt, opts[i]) == 0)
577 return i;
578 }
579 js_error(J, "Invalid %s '%s'", desc, opt);
580 }
581
582 // args: level as string and a variable numbers of args to print. adds final \n
script_log(js_State * J)583 static void script_log(js_State *J)
584 {
585 const char *level = js_tostring(J, 1);
586 int msgl = mp_msg_find_level(level);
587 if (msgl < 0)
588 js_error(J, "Invalid log level '%s'", level);
589
590 struct mp_log *log = jctx(J)->log;
591 for (int top = js_gettop(J), i = 2; i < top; i++)
592 mp_msg(log, msgl, (i == 2 ? "%s" : " %s"), js_tostring(J, i));
593 mp_msg(log, msgl, "\n");
594 push_success(J);
595 }
596
script_find_config_file(js_State * J,void * af)597 static void script_find_config_file(js_State *J, void *af)
598 {
599 const char *fname = js_tostring(J, 1);
600 char *path = mp_find_config_file(af, jctx(J)->mpctx->global, fname);
601 if (path) {
602 js_pushstring(J, path);
603 } else {
604 push_failure(J, "not found");
605 }
606 }
607
script__request_event(js_State * J)608 static void script__request_event(js_State *J)
609 {
610 const char *event = js_tostring(J, 1);
611 bool enable = js_toboolean(J, 2);
612
613 const char *name;
614 for (int n = 0; n < 256 && (name = mpv_event_name(n)); n++) {
615 if (strcmp(name, event) == 0) {
616 push_status(J, mpv_request_event(jclient(J), n, enable));
617 return;
618 }
619 }
620 push_failure(J, "Unknown event name");
621 }
622
script_enable_messages(js_State * J)623 static void script_enable_messages(js_State *J)
624 {
625 const char *level = js_tostring(J, 1);
626 int e = mpv_request_log_messages(jclient(J), level);
627 if (e == MPV_ERROR_INVALID_PARAMETER)
628 js_error(J, "Invalid log level '%s'", level);
629 push_status(J, e);
630 }
631
632 // args - command [with arguments] as string
script_command(js_State * J)633 static void script_command(js_State *J)
634 {
635 push_status(J, mpv_command_string(jclient(J), js_tostring(J, 1)));
636 }
637
638 // args: strings of command and then variable number of arguments
script_commandv(js_State * J)639 static void script_commandv(js_State *J)
640 {
641 const char *argv[MP_CMD_MAX_ARGS + 1];
642 int length = js_gettop(J) - 1;
643 if (length >= MP_ARRAY_SIZE(argv))
644 js_error(J, "Too many arguments");
645
646 for (int i = 0; i < length; i++)
647 argv[i] = js_tostring(J, 1 + i);
648 argv[length] = NULL;
649 push_status(J, mpv_command(jclient(J), argv));
650 }
651
652 // args: name, string value
script_set_property(js_State * J)653 static void script_set_property(js_State *J)
654 {
655 int e = mpv_set_property_string(jclient(J), js_tostring(J, 1),
656 js_tostring(J, 2));
657 push_status(J, e);
658 }
659
660 // args: name, boolean
script_set_property_bool(js_State * J)661 static void script_set_property_bool(js_State *J)
662 {
663 int v = js_toboolean(J, 2);
664 int e = mpv_set_property(jclient(J), js_tostring(J, 1), MPV_FORMAT_FLAG, &v);
665 push_status(J, e);
666 }
667
668 // args: name [,def]
script_get_property_number(js_State * J)669 static void script_get_property_number(js_State *J)
670 {
671 double result;
672 const char *name = js_tostring(J, 1);
673 int e = mpv_get_property(jclient(J), name, MPV_FORMAT_DOUBLE, &result);
674 if (!pushed_error(J, e, 2))
675 js_pushnumber(J, result);
676 }
677
678 // args: name, native value
script_set_property_native(js_State * J,void * af)679 static void script_set_property_native(js_State *J, void *af)
680 {
681 mpv_node node;
682 makenode(af, &node, J, 2);
683 mpv_handle *h = jclient(J);
684 int e = mpv_set_property(h, js_tostring(J, 1), MPV_FORMAT_NODE, &node);
685 push_status(J, e);
686 }
687
688 // args: name [,def]
script_get_property(js_State * J,void * af)689 static void script_get_property(js_State *J, void *af)
690 {
691 mpv_handle *h = jclient(J);
692 char *res = NULL;
693 int e = mpv_get_property(h, js_tostring(J, 1), MPV_FORMAT_STRING, &res);
694 if (e >= 0)
695 add_af_mpv_alloc(af, res);
696 if (!pushed_error(J, e, 2))
697 js_pushstring(J, res);
698 }
699
700 // args: name [,def]
script_get_property_bool(js_State * J)701 static void script_get_property_bool(js_State *J)
702 {
703 int result;
704 mpv_handle *h = jclient(J);
705 int e = mpv_get_property(h, js_tostring(J, 1), MPV_FORMAT_FLAG, &result);
706 if (!pushed_error(J, e, 2))
707 js_pushboolean(J, result);
708 }
709
710 // args: name, number
script_set_property_number(js_State * J)711 static void script_set_property_number(js_State *J)
712 {
713 double v = js_tonumber(J, 2);
714 mpv_handle *h = jclient(J);
715 int e = mpv_set_property(h, js_tostring(J, 1), MPV_FORMAT_DOUBLE, &v);
716 push_status(J, e);
717 }
718
719 // args: name [,def]
script_get_property_native(js_State * J,void * af)720 static void script_get_property_native(js_State *J, void *af)
721 {
722 const char *name = js_tostring(J, 1);
723 mpv_handle *h = jclient(J);
724 mpv_node *presult_node = new_af_mpv_node(af);
725 int e = mpv_get_property(h, name, MPV_FORMAT_NODE, presult_node);
726 if (!pushed_error(J, e, 2))
727 pushnode(J, presult_node);
728 }
729
730 // args: name [,def]
script_get_property_osd(js_State * J,void * af)731 static void script_get_property_osd(js_State *J, void *af)
732 {
733 const char *name = js_tostring(J, 1);
734 mpv_handle *h = jclient(J);
735 char *res = NULL;
736 int e = mpv_get_property(h, name, MPV_FORMAT_OSD_STRING, &res);
737 if (e >= 0)
738 add_af_mpv_alloc(af, res);
739 if (!pushed_error(J, e, 2))
740 js_pushstring(J, res);
741 }
742
743 // args: id, name, type
script__observe_property(js_State * J)744 static void script__observe_property(js_State *J)
745 {
746 const char *fmts[] = {"none", "native", "bool", "string", "number", NULL};
747 const mpv_format mf[] = {MPV_FORMAT_NONE, MPV_FORMAT_NODE, MPV_FORMAT_FLAG,
748 MPV_FORMAT_STRING, MPV_FORMAT_DOUBLE};
749
750 mpv_format f = mf[checkopt(J, 3, "none", fmts, "observe type")];
751 int e = mpv_observe_property(jclient(J), jsL_checkuint64(J, 1),
752 js_tostring(J, 2),
753 f);
754 push_status(J, e);
755 }
756
757 // args: id
script__unobserve_property(js_State * J)758 static void script__unobserve_property(js_State *J)
759 {
760 int e = mpv_unobserve_property(jclient(J), jsL_checkuint64(J, 1));
761 push_status(J, e);
762 }
763
764 // args: native (array of command and args, similar to commandv) [,def]
script_command_native(js_State * J,void * af)765 static void script_command_native(js_State *J, void *af)
766 {
767 mpv_node cmd;
768 makenode(af, &cmd, J, 1);
769 mpv_node *presult_node = new_af_mpv_node(af);
770 int e = mpv_command_node(jclient(J), &cmd, presult_node);
771 if (!pushed_error(J, e, 2))
772 pushnode(J, presult_node);
773 }
774
775 // args: async-command-id, native-command
script__command_native_async(js_State * J,void * af)776 static void script__command_native_async(js_State *J, void *af)
777 {
778 uint64_t id = jsL_checkuint64(J, 1);
779 struct mpv_node node;
780 makenode(af, &node, J, 2);
781 push_status(J, mpv_command_node_async(jclient(J), id, &node));
782 }
783
784 // args: async-command-id
script__abort_async_command(js_State * J)785 static void script__abort_async_command(js_State *J)
786 {
787 mpv_abort_async_command(jclient(J), jsL_checkuint64(J, 1));
788 push_success(J);
789 }
790
791 // args: none, result in millisec
script_get_time_ms(js_State * J)792 static void script_get_time_ms(js_State *J)
793 {
794 js_pushnumber(J, mpv_get_time_us(jclient(J)) / (double)(1000));
795 }
796
797 // push object with properties names (NULL terminated) with respective vals
push_nums_obj(js_State * J,const char * const names[],const double vals[])798 static void push_nums_obj(js_State *J, const char * const names[],
799 const double vals[])
800 {
801 js_newobject(J);
802 for (int i = 0; names[i]; i++) {
803 js_pushnumber(J, vals[i]);
804 js_setproperty(J, -2, names[i]);
805 }
806 }
807
808 // args: input-section-name, x0, y0, x1, y1
script_input_set_section_mouse_area(js_State * J)809 static void script_input_set_section_mouse_area(js_State *J)
810 {
811 char *section = (char *)js_tostring(J, 1);
812 mp_input_set_section_mouse_area(jctx(J)->mpctx->input, section,
813 jsL_checkint(J, 2), jsL_checkint(J, 3), // x0, y0
814 jsL_checkint(J, 4), jsL_checkint(J, 5)); // x1, y1
815 push_success(J);
816 }
817
818 // args: time-in-ms [,format-string]
script_format_time(js_State * J,void * af)819 static void script_format_time(js_State *J, void *af)
820 {
821 double t = js_tonumber(J, 1);
822 const char *fmt = js_isundefined(J, 2) ? "%H:%M:%S" : js_tostring(J, 2);
823 char *r = talloc_steal(af, mp_format_time_fmt(fmt, t));
824 if (!r)
825 js_error(J, "Invalid time format string '%s'", fmt);
826 js_pushstring(J, r);
827 }
828
829 // TODO: untested
script_get_wakeup_pipe(js_State * J)830 static void script_get_wakeup_pipe(js_State *J)
831 {
832 js_pushnumber(J, mpv_get_wakeup_pipe(jclient(J)));
833 }
834
835 // args: name (str), priority (int), id (uint)
script__hook_add(js_State * J)836 static void script__hook_add(js_State *J)
837 {
838 const char *name = js_tostring(J, 1);
839 int pri = jsL_checkint(J, 2);
840 uint64_t id = jsL_checkuint64(J, 3);
841 push_status(J, mpv_hook_add(jclient(J), id, name, pri));
842 }
843
844 // args: id (uint)
script__hook_continue(js_State * J)845 static void script__hook_continue(js_State *J)
846 {
847 push_status(J, mpv_hook_continue(jclient(J), jsL_checkuint64(J, 1)));
848 }
849
850 /**********************************************************************
851 * mp.utils
852 *********************************************************************/
853
854 // args: [path [,filter]]
script_readdir(js_State * J,void * af)855 static void script_readdir(js_State *J, void *af)
856 {
857 // 0 1 2 3
858 const char *filters[] = {"all", "files", "dirs", "normal", NULL};
859 const char *path = js_isundefined(J, 1) ? "." : js_tostring(J, 1);
860 int t = checkopt(J, 2, "normal", filters, "listing filter");
861
862 DIR *dir = opendir(path);
863 if (!dir) {
864 push_failure(J, "Cannot open dir");
865 return;
866 }
867 add_af_dir(af, dir);
868 set_last_error(jctx(J), 0, NULL);
869 js_newarray(J); // the return value
870 char *fullpath = talloc_strdup(af, "");
871 struct dirent *e;
872 int n = 0;
873 while ((e = readdir(dir))) {
874 char *name = e->d_name;
875 if (t) {
876 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
877 continue;
878 if (fullpath)
879 fullpath[0] = '\0';
880 fullpath = talloc_asprintf_append(fullpath, "%s/%s", path, name);
881 struct stat st;
882 if (stat(fullpath, &st))
883 continue;
884 if (!(((t & 1) && S_ISREG(st.st_mode)) ||
885 ((t & 2) && S_ISDIR(st.st_mode))))
886 {
887 continue;
888 }
889 }
890 js_pushstring(J, name);
891 js_setindex(J, -2, n++);
892 }
893 }
894
script_file_info(js_State * J)895 static void script_file_info(js_State *J)
896 {
897 const char *path = js_tostring(J, 1);
898
899 struct stat statbuf;
900 if (stat(path, &statbuf) != 0) {
901 push_failure(J, "Cannot stat path");
902 return;
903 }
904 // Clear last error
905 set_last_error(jctx(J), 0, NULL);
906
907 const char * stat_names[] = {
908 "mode", "size",
909 "atime", "mtime", "ctime", NULL
910 };
911 const double stat_values[] = {
912 statbuf.st_mode,
913 statbuf.st_size,
914 statbuf.st_atime,
915 statbuf.st_mtime,
916 statbuf.st_ctime
917 };
918 // Create an object and add all fields
919 push_nums_obj(J, stat_names, stat_values);
920
921 // Convenience booleans
922 js_pushboolean(J, S_ISREG(statbuf.st_mode));
923 js_setproperty(J, -2, "is_file");
924
925 js_pushboolean(J, S_ISDIR(statbuf.st_mode));
926 js_setproperty(J, -2, "is_dir");
927 }
928
929
script_split_path(js_State * J)930 static void script_split_path(js_State *J)
931 {
932 const char *p = js_tostring(J, 1);
933 bstr fname = mp_dirname(p);
934 js_newarray(J);
935 js_pushlstring(J, fname.start, fname.len);
936 js_setindex(J, -2, 0);
937 js_pushstring(J, mp_basename(p));
938 js_setindex(J, -2, 1);
939 }
940
script_join_path(js_State * J,void * af)941 static void script_join_path(js_State *J, void *af)
942 {
943 js_pushstring(J, mp_path_join(af, js_tostring(J, 1), js_tostring(J, 2)));
944 }
945
script_get_user_path(js_State * J,void * af)946 static void script_get_user_path(js_State *J, void *af)
947 {
948 const char *path = js_tostring(J, 1);
949 js_pushstring(J, mp_get_user_path(af, jctx(J)->mpctx->global, path));
950 }
951
952 // args: is_append, prefixed file name, data (c-str)
script__write_file(js_State * J,void * af)953 static void script__write_file(js_State *J, void *af)
954 {
955 static const char *prefix = "file://";
956 bool append = js_toboolean(J, 1);
957 const char *fname = js_tostring(J, 2);
958 const char *data = js_tostring(J, 3);
959 const char *opstr = append ? "append" : "write";
960
961 if (strstr(fname, prefix) != fname) // simple protection for incorrect use
962 js_error(J, "File name must be prefixed with '%s'", prefix);
963 fname += strlen(prefix);
964 fname = mp_get_user_path(af, jctx(J)->mpctx->global, fname);
965 MP_VERBOSE(jctx(J), "%s file '%s'\n", opstr, fname);
966
967 FILE *f = fopen(fname, append ? "ab" : "wb");
968 if (!f)
969 js_error(J, "Cannot open (%s) file: '%s'", opstr, fname);
970 add_af_file(af, f);
971
972 int len = strlen(data); // limited by terminating null
973 int wrote = fwrite(data, 1, len, f);
974 if (len != wrote)
975 js_error(J, "Cannot %s to file: '%s'", opstr, fname);
976 js_pushboolean(J, 1); // success. doesn't touch last_error
977 }
978
979 // args: env var name
script_getenv(js_State * J)980 static void script_getenv(js_State *J)
981 {
982 const char *v = getenv(js_tostring(J, 1));
983 if (v) {
984 js_pushstring(J, v);
985 } else {
986 js_pushundefined(J);
987 }
988 }
989
990 // args: none
script_get_env_list(js_State * J)991 static void script_get_env_list(js_State *J)
992 {
993 js_newarray(J);
994 for (int n = 0; environ && environ[n]; n++) {
995 js_pushstring(J, environ[n]);
996 js_setindex(J, -2, n);
997 }
998 }
999
1000 // args: as-filename, content-string, returns the compiled result as a function
script_compile_js(js_State * J)1001 static void script_compile_js(js_State *J)
1002 {
1003 js_loadstring(J, js_tostring(J, 1), js_tostring(J, 2));
1004 }
1005
1006 // args: true = print info (with the warning report function - no info report)
script__gc(js_State * J)1007 static void script__gc(js_State *J)
1008 {
1009 js_gc(J, js_toboolean(J, 1) ? 1 : 0);
1010 push_success(J);
1011 }
1012
1013 /**********************************************************************
1014 * Core functions: pushnode, makenode and the event loop backend
1015 *********************************************************************/
1016
1017 // pushes a js value/array/object from an mpv_node
pushnode(js_State * J,mpv_node * node)1018 static void pushnode(js_State *J, mpv_node *node)
1019 {
1020 int len;
1021 switch (node->format) {
1022 case MPV_FORMAT_NONE: js_pushnull(J); break;
1023 case MPV_FORMAT_STRING: js_pushstring(J, node->u.string); break;
1024 case MPV_FORMAT_INT64: js_pushnumber(J, node->u.int64); break;
1025 case MPV_FORMAT_DOUBLE: js_pushnumber(J, node->u.double_); break;
1026 case MPV_FORMAT_FLAG: js_pushboolean(J, node->u.flag); break;
1027 case MPV_FORMAT_BYTE_ARRAY:
1028 js_pushlstring(J, node->u.ba->data, node->u.ba->size);
1029 break;
1030 case MPV_FORMAT_NODE_ARRAY:
1031 js_newarray(J);
1032 len = node->u.list->num;
1033 for (int n = 0; n < len; n++) {
1034 pushnode(J, &node->u.list->values[n]);
1035 js_setindex(J, -2, n);
1036 }
1037 break;
1038 case MPV_FORMAT_NODE_MAP:
1039 js_newobject(J);
1040 len = node->u.list->num;
1041 for (int n = 0; n < len; n++) {
1042 pushnode(J, &node->u.list->values[n]);
1043 js_setproperty(J, -2, node->u.list->keys[n]);
1044 }
1045 break;
1046 default:
1047 js_pushstring(J, "[UNSUPPORTED_MPV_FORMAT]");
1048 break;
1049 }
1050 }
1051
1052 // For the object at stack index idx, extract the (own) property names into
1053 // keys array (and allocate it to accommodate) and return the number of keys.
get_obj_properties(void * ta_ctx,char *** keys,js_State * J,int idx)1054 static int get_obj_properties(void *ta_ctx, char ***keys, js_State *J, int idx)
1055 {
1056 int length = 0;
1057 js_pushiterator(J, idx, 1);
1058
1059 *keys = talloc_new(ta_ctx);
1060 const char *name;
1061 while ((name = js_nextiterator(J, -1)))
1062 MP_TARRAY_APPEND(ta_ctx, *keys, length, talloc_strdup(ta_ctx, name));
1063
1064 js_pop(J, 1); // the iterator
1065 return length;
1066 }
1067
1068 // true if we don't lose (too much) precision when casting to int64
same_as_int64(double d)1069 static bool same_as_int64(double d)
1070 {
1071 // The range checks also validly filter inf and nan, so behavior is defined
1072 return d >= INT64_MIN && d <= INT64_MAX && d == (int64_t)d;
1073 }
1074
jsL_checkint(js_State * J,int idx)1075 static int jsL_checkint(js_State *J, int idx)
1076 {
1077 double d = js_tonumber(J, idx);
1078 if (!(d >= INT_MIN && d <= INT_MAX))
1079 js_error(J, "int out of range at index %d", idx);
1080 return d;
1081 }
1082
jsL_checkuint64(js_State * J,int idx)1083 static uint64_t jsL_checkuint64(js_State *J, int idx)
1084 {
1085 double d = js_tonumber(J, idx);
1086 if (!(d >= 0 && d <= UINT64_MAX))
1087 js_error(J, "uint64 out of range at index %d", idx);
1088 return d;
1089 }
1090
1091 // From the js stack value/array/object at index idx
makenode(void * ta_ctx,mpv_node * dst,js_State * J,int idx)1092 static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx)
1093 {
1094 if (js_isundefined(J, idx) || js_isnull(J, idx)) {
1095 dst->format = MPV_FORMAT_NONE;
1096
1097 } else if (js_isboolean(J, idx)) {
1098 dst->format = MPV_FORMAT_FLAG;
1099 dst->u.flag = js_toboolean(J, idx);
1100
1101 } else if (js_isnumber(J, idx)) {
1102 double val = js_tonumber(J, idx);
1103 if (same_as_int64(val)) { // use int, because we can
1104 dst->format = MPV_FORMAT_INT64;
1105 dst->u.int64 = val;
1106 } else {
1107 dst->format = MPV_FORMAT_DOUBLE;
1108 dst->u.double_ = val;
1109 }
1110
1111 } else if (js_isarray(J, idx)) {
1112 dst->format = MPV_FORMAT_NODE_ARRAY;
1113 dst->u.list = talloc(ta_ctx, struct mpv_node_list);
1114 dst->u.list->keys = NULL;
1115
1116 int length = js_getlength(J, idx);
1117 dst->u.list->num = length;
1118 dst->u.list->values = talloc_array(ta_ctx, mpv_node, length);
1119 for (int n = 0; n < length; n++) {
1120 js_getindex(J, idx, n);
1121 makenode(ta_ctx, &dst->u.list->values[n], J, -1);
1122 js_pop(J, 1);
1123 }
1124
1125 } else if (js_isobject(J, idx)) {
1126 dst->format = MPV_FORMAT_NODE_MAP;
1127 dst->u.list = talloc(ta_ctx, struct mpv_node_list);
1128
1129 int length = get_obj_properties(ta_ctx, &dst->u.list->keys, J, idx);
1130 dst->u.list->num = length;
1131 dst->u.list->values = talloc_array(ta_ctx, mpv_node, length);
1132 for (int n = 0; n < length; n++) {
1133 js_getproperty(J, idx, dst->u.list->keys[n]);
1134 makenode(ta_ctx, &dst->u.list->values[n], J, -1);
1135 js_pop(J, 1);
1136 }
1137
1138 } else { // string, or anything else as string
1139 dst->format = MPV_FORMAT_STRING;
1140 dst->u.string = talloc_strdup(ta_ctx, js_tostring(J, idx));
1141 }
1142 }
1143
1144 // args: wait in secs (infinite if negative) if mpv doesn't send events earlier.
script_wait_event(js_State * J,void * af)1145 static void script_wait_event(js_State *J, void *af)
1146 {
1147 double timeout = js_isnumber(J, 1) ? js_tonumber(J, 1) : -1;
1148 mpv_event *event = mpv_wait_event(jclient(J), timeout);
1149
1150 mpv_node *rn = new_af_mpv_node(af);
1151 mpv_event_to_node(rn, event);
1152 pushnode(J, rn);
1153 }
1154
1155 /**********************************************************************
1156 * Script functions setup
1157 *********************************************************************/
1158 #define FN_ENTRY(name, length) {#name, length, script_ ## name, NULL}
1159 #define AF_ENTRY(name, length) {#name, length, NULL, script_ ## name}
1160 struct fn_entry {
1161 const char *name;
1162 int length;
1163 js_CFunction jsc_fn;
1164 af_CFunction afc_fn;
1165 };
1166
1167 // Names starting with underscore are wrapped at @defaults.js
1168 // FN_ENTRY is a normal js C function, AF_ENTRY is an autofree js C function.
1169 static const struct fn_entry main_fns[] = {
1170 FN_ENTRY(log, 1),
1171 AF_ENTRY(wait_event, 1),
1172 FN_ENTRY(_request_event, 2),
1173 AF_ENTRY(find_config_file, 1),
1174 FN_ENTRY(command, 1),
1175 FN_ENTRY(commandv, 0),
1176 AF_ENTRY(command_native, 2),
1177 AF_ENTRY(_command_native_async, 2),
1178 FN_ENTRY(_abort_async_command, 1),
1179 FN_ENTRY(get_property_bool, 2),
1180 FN_ENTRY(get_property_number, 2),
1181 AF_ENTRY(get_property_native, 2),
1182 AF_ENTRY(get_property, 2),
1183 AF_ENTRY(get_property_osd, 2),
1184 FN_ENTRY(set_property, 2),
1185 FN_ENTRY(set_property_bool, 2),
1186 FN_ENTRY(set_property_number, 2),
1187 AF_ENTRY(set_property_native, 2),
1188 FN_ENTRY(_observe_property, 3),
1189 FN_ENTRY(_unobserve_property, 1),
1190 FN_ENTRY(get_time_ms, 0),
1191 AF_ENTRY(format_time, 2),
1192 FN_ENTRY(enable_messages, 1),
1193 FN_ENTRY(get_wakeup_pipe, 0),
1194 FN_ENTRY(_hook_add, 3),
1195 FN_ENTRY(_hook_continue, 1),
1196 FN_ENTRY(input_set_section_mouse_area, 5),
1197 FN_ENTRY(last_error, 0),
1198 FN_ENTRY(_set_last_error, 1),
1199 {0}
1200 };
1201
1202 static const struct fn_entry utils_fns[] = {
1203 AF_ENTRY(readdir, 2),
1204 FN_ENTRY(file_info, 1),
1205 FN_ENTRY(split_path, 1),
1206 AF_ENTRY(join_path, 2),
1207 AF_ENTRY(get_user_path, 1),
1208 FN_ENTRY(get_env_list, 0),
1209
1210 FN_ENTRY(read_file, 2),
1211 AF_ENTRY(_write_file, 3),
1212 FN_ENTRY(getenv, 1),
1213 FN_ENTRY(compile_js, 2),
1214 FN_ENTRY(_gc, 1),
1215 {0}
1216 };
1217
1218 // Adds an object <module> with the functions at e to the top object
add_package_fns(js_State * J,const char * module,const struct fn_entry * e)1219 static void add_package_fns(js_State *J, const char *module,
1220 const struct fn_entry *e)
1221 {
1222 js_newobject(J);
1223 for (int n = 0; e[n].name; n++) {
1224 if (e[n].jsc_fn) {
1225 js_newcfunction(J, e[n].jsc_fn, e[n].name, e[n].length);
1226 } else {
1227 af_newcfunction(J, e[n].afc_fn, e[n].name, e[n].length);
1228 }
1229 js_setproperty(J, -2, e[n].name);
1230 }
1231 js_setproperty(J, -2, module);
1232 }
1233
1234 // Called directly, adds functions/vars to the caller's this.
add_functions(js_State * J,struct script_ctx * ctx)1235 static void add_functions(js_State *J, struct script_ctx *ctx)
1236 {
1237 js_copy(J, 0);
1238 add_package_fns(J, "mp", main_fns);
1239 js_getproperty(J, 0, "mp"); // + this mp
1240 add_package_fns(J, "utils", utils_fns);
1241
1242 js_pushstring(J, mpv_client_name(ctx->client));
1243 js_setproperty(J, -2, "script_name");
1244
1245 js_pushstring(J, ctx->filename);
1246 js_setproperty(J, -2, "script_file");
1247
1248 if (ctx->path) {
1249 js_pushstring(J, ctx->path);
1250 js_setproperty(J, -2, "script_path");
1251 }
1252
1253 js_pop(J, 2); // leave the stack as we got it
1254 }
1255
1256 // main export of this file, used by cplayer to load js scripts
1257 const struct mp_scripting mp_scripting_js = {
1258 .name = "javascript",
1259 .file_ext = "js",
1260 .load = s_load_javascript,
1261 };
1262