diff options
-rw-r--r-- | DOCS/man/javascript.rst | 336 | ||||
-rw-r--r-- | DOCS/man/mpv.rst | 2 | ||||
-rw-r--r-- | options/options.c | 6 | ||||
-rw-r--r-- | player/javascript.c | 1307 | ||||
-rw-r--r-- | player/javascript/defaults.js | 495 | ||||
-rw-r--r-- | player/scripting.c | 4 | ||||
-rw-r--r-- | wscript | 4 | ||||
-rw-r--r-- | wscript_build.py | 7 |
8 files changed, 2159 insertions, 2 deletions
diff --git a/DOCS/man/javascript.rst b/DOCS/man/javascript.rst new file mode 100644 index 0000000000..0c099cad0a --- /dev/null +++ b/DOCS/man/javascript.rst @@ -0,0 +1,336 @@ +JavaScript +========== + +JavaScript support in mpv is near identical to its Lua support. Use this section +as reference on differences and availability of APIs, but otherwise you should +refer to the Lua documentation for API details and general scripting in mpv. + +Example +------- + +JavaScript code which leaves fullscreen mode when the player is paused: + +:: + + function on_pause_change(name, value) { + if (value == true) + mp.set_property("fullscreen", "no"); + } + mp.observe_property("pause", "bool", on_pause_change); + + +Similarities with Lua +--------------------- + +mpv tries to load a script file as JavaScript if it has a ``.js`` extension, but +otherwise, the documented Lua options, script directories, loading, etc apply to +JavaScript files too. + +Script initialization and lifecycle is the same as with Lua, and most of the Lua +functions at the modules ``mp``, ``mp.utils`` and ``mp.msg`` are available to +JavaScript with identical APIs - including running commands, getting/setting +properties, registering events/key-bindings/property-changes/hooks, etc. + +Differences from Lua +-------------------- + +No need to load modules. ``mp``, ``mp.utils`` and ``mp.msg`` are preloaded, and +you can use e.g. ``var cwd = mp.utils.getcwd();`` without prior setup. +``mp.options`` is currently not implemented, but ``mp.get_opt(...)`` is. + +Errors are slightly different. Where the Lua APIs return ``nil`` for error, +the JavaScript ones return ``undefined``. Where Lua returns ``something, error`` +JavaScript returns only ``something`` - and makes ``error`` available via +``mp.last_error()``. Note that only some of the functions have this additional +``error`` value - typically the same ones which have it in Lua. + +Standard APIs are preferred. For instance ``setTimeout`` and ``JSON.stringify`` +are available, but ``mp.add_timeout`` and ``mp.utils.format_json`` are not. + +No standard library. This means that interaction with anything outside of mpv is +limited to the available APIs, typically via ``mp.utils``. However, some file +functions were added, and CommonJS ``require`` is available too - where the +loaded modules have the same privileges as normal scripts. + +Language features - ECMAScript 5 +-------------------------------- + +The scripting backend which mpv currently uses is MuJS - a compatible minimal +ES5 interpreter. As such, ``String.substring`` is implemented for instance, +while the common but non-standard ``String.substr`` is not. Please consult the +MuJS pages on language features and platform support - http://mujs.com . + +Unsupported Lua APIs and their JS alternatives +---------------------------------------------- + +``mp.add_timeout(seconds, fn)`` JS: ``id = setTimeout(fn, ms)`` + +``mp.add_periodic_timer(seconds, fn)`` JS: ``id = setInterval(fn, ms)`` + +``mp.register_idle(fn)`` JS: ``id = setTimeout(fn)`` + +``mp.unregister_idle(fn)`` JS: ``clearTimeout(id)`` + +``utils.parse_json(str [, trail])`` JS: ``JSON.parse(str)`` + +``utils.format_json(v)`` JS: ``JSON.stringify(v)`` + +``utils.to_string(v)`` see ``dump`` below. + +``mp.suspend()`` JS: none (deprecated). + +``mp.resume()`` JS: none (deprecated). + +``mp.resume_all()`` JS: none (deprecated). + +``mp.get_next_timeout()`` see event loop below. + +``mp.dispatch_events([allow_wait])`` see event loop below. + +``mp.options`` module is not implemented currently for JS. + +Scripting APIs - identical to Lua +--------------------------------- + +(LE) - Last-Error, indicates that ``mp.last_error()`` can be used after the +call to test for success (empty string) or failure (non empty reason string). +Otherwise, where the Lua APIs return ``nil`` on error, JS returns ``undefined``. + +``mp.command(string)`` (LE) + +``mp.commandv(arg1, arg2, ...)`` (LE) + +``mp.command_native(table [,def])`` (LE) + +``mp.get_property(name [,def])`` (LE) + +``mp.get_property_osd(name [,def])`` (LE) + +``mp.get_property_bool(name [,def])`` (LE) + +``mp.get_property_number(name [,def])`` (LE) + +``mp.get_property_native(name [,def])`` (LE) + +``mp.set_property(name, value)`` (LE) + +``mp.set_property_bool(name, value)`` (LE) + +``mp.set_property_number(name, value)`` (LE) + +``mp.set_property_native(name, value)`` (LE) + +``mp.get_time()`` + +``mp.add_key_binding(key, name|fn [,fn [,flags]])`` + +``mp.add_forced_key_binding(...)`` + +``mp.remove_key_binding(name)`` + +``mp.register_event(name, fn)`` + +``mp.unregister_event(fn)`` + +``mp.observe_property(name, type, fn)`` + +``mp.unobserve_property(fn)`` + +``mp.get_opt(key)`` + +``mp.get_script_name()`` + +``mp.osd_message(text [,duration])`` + +``mp.get_wakeup_pipe()`` + +``mp.enable_messages(level)`` + +``mp.register_script_message(name, fn)`` + +``mp.unregister_script_message(name)`` + +``mp.msg.log(level, ...)`` + +``mp.msg.fatal(...)`` + +``mp.msg.error(...)`` + +``mp.msg.warn(...)`` + +``mp.msg.info(...)`` + +``mp.msg.verbose(...)`` + +``mp.msg.debug(...)`` + +``mp.utils.getcwd()`` (LE) + +``mp.utils.readdir(path [, filter])`` (LE) + +``mp.utils.split_path(path)`` + +``mp.utils.join_path(p1, p2)`` + +``mp.utils.subprocess(t)`` + +``mp.utils.subprocess_detached(t)`` + +``mp.add_hook(type, priority, fn)`` + +Additional utilities +-------------------- + +``mp.last_error()`` + If used after an API call which updates last error, returns an empty string + if the API call succeeded, or a non-empty error reason string otherwise. + +``Error.stack`` (string) + When using ``try { ... } catch(e) { ... }``, then ``e.stack`` is the stack + trace of the error - if it was created using the ``Error(...)`` constructor. + +``print`` (global) + A convenient alias to ``mp.msg.info``. + +``dump`` (global) + Like ``print`` but also expands objects and arrays recursively. + +``mp.utils.getenv(name)`` + Returns the value of the host environment variable ``name``, or empty str. + +``mp.utils.get_user_path(path)`` + Expands (mpv) meta paths like ``~/x``, ``~~/y``, ``~~desktop/z`` etc. + ``read_file``, ``write_file`` and ``require`` already use this internaly. + +``mp.utils.read_file(fname [,max])`` + Returns the content of file ``fname`` as string. If ``max`` is provided and + not negative, limit the read to ``max`` bytes. + +``mp.utils.write_file(fname, str)`` + (Over)write file ``fname`` with text content ``str``. ``fname`` must be + prefixed with ``file://`` as simple protection against accidental arguments + switch, e.g. ``mp.utils.write_file("file://~/abc.txt", "hello world")``. + +Note: ``read_file`` and ``write_file`` throw on errors, allow text content only. + +``mp.get_time_ms()`` + Same as ``mp.get_time()`` but in ms instead of seconds. + +``mp.get_script_file()`` + Returns the file name of the current script. + +``exit()`` (global) + Make the script exit at the end of the current event loop iteration. + Note: please reomve added key bindings before calling ``exit()``. + +``mp.utils.compile_js(fname, content_str)`` + Compiles the JS code ``content_str`` as file name ``fname`` (without loading + anything from the filesystem), and returns it as a function. Very similar + to a ``Function`` constructor, but shows at stack traces as ``fname``. + +Timers (global) +--------------- + +The standard HTML/node.js timers are available: + +``id = setTimeout(fn [,duration [,arg1 [,arg2...]]])`` + +``id = setTimeout(code_string [,duration])`` + +``clearTimeout(id)`` + +``id = setInterval(fn [,duration [,arg1 [,arg2...]]])`` + +``id = setInterval(code_string [,duration])`` + +``clearInterval(id)`` + +``setTimeout`` and ``setInterval`` return id, and later call ``fn`` (or execute +``code_string``) after ``duration`` ms. Interval also repeat every ``duration``. + +``duration`` has a minimum and default value of 0, ``code_string`` is +a plain string which is evaluated as JS code, and ``[,arg1 [,arg2..]]`` are used +as arguments (if provided) when calling back ``fn``. + +The ``clear...(id)`` functions cancel timer ``id``, and are irreversible. + +Note: timers always call back asynchronously, e.g. ``setTimeout(fn)`` will never +call ``fn`` before returning. ``fn`` will be called either at the end of this +event loop iteration or at a later event loop iteration. This is true also for +intervals - which also never call back twice at the same event loop iteration. + +Additionally, timers are processed after the event queue is empty, so it's valid +to use ``setTimeout(fn)`` instead of Lua's ``mp.register_idle(fn)``. + +CommonJS modules and ``require(id)`` +------------------------------------ + +CommonJS Modules are a standard system where scripts can export common functions +for use by other scripts. A module is a script which adds properties (functions, +etc) to its invisible ``exports`` object, which another script can access by +loading it with ``require(module-id)`` - which returns that ``exports`` object. + +Modules and ``require`` are supported, standard compliant, and generally similar +to node.js. However, most node.js modules won't run due to missing modules such +as ``fs``, ``process``, etc, but some node.js modules with minimal dependencies +do work. In general, this is for mpv modules and not a node.js replacement. + +A ``.js`` file extension is always added to ``id``, e.g. ``require("./foo")`` +will load the file ``./foo.js`` and return its ``exports`` object. + +An id is relative (to the script which ``require``'d it) if it starts with +``./`` or ``../``. Otherwise, it's considered a "top-level id" (CommonJS term). + +Top level id is evaluated as absolute filesystem path if possible (e.g. ``/x/y`` +or ``~/x``). Otherwise, it's searched at ``scripts/modules.js/`` in mpv config +dirs - in normal config search order. E.g. ``require("x")`` is searched as file +``x.js`` at those dirs, and id ``foo/x`` is searched as file ``foo/x.js``. + +No ``global`` variable, but a module's ``this`` at its top lexical scope is the +global object - also in strict mode. If you have a module which needs ``global`` +as the global object, you could do ``this.global = this;`` before ``require``. + +Functions and variables declared at a module don't pollute the global object. + +The event loop +-------------- + +The event loop poll/dispatch mpv events as long as the queue is not empty, then +processes the timers, then waits for the next event, and repeats this forever. + +You could put this code at your script to replace the built-in event loop, and +also print every event which mpv sends to your script: + +:: + + function mp_event_loop() { + var wait = 0; + do { + var e = mp.wait_event(wait); + dump(e); // there could be a lot of prints... + if (e.event != "none") { + mp.dispatch_event(e); + wait = 0; + } else { + wait = mp.process_timers() / 1000; + } + } while (mp.keep_running); + } + + +``mp_event_loop`` is a name which mpv tries to call after the script loads. +The internal implementation is similar to this (without ``dump`` though..). + +``e = mp.wait_event(wait)`` returns when the next mpv event arrives, or after +``wait`` seconds if positive and no mpv events arrived. ``wait`` value of 0 +returns immediately (with ``e.event == "none"`` if the queue is empty). + +``mp.dispatch_event(e)`` calls back the handlers registered for ``e.event``, +if there are such (event handlers, property observers, script messages, etc). + +``mp.process_timers()`` calls back the already-added, non-canceled due timers, +and returns the duration in ms till the next due timer (possibly 0), or -1 if +there are no pending timers. Must not be called recursively. + +Note: ``exit()`` is also registered for the ``shutdown`` event, and its +implementation is a simple ``mp.keep_running = false``. diff --git a/DOCS/man/mpv.rst b/DOCS/man/mpv.rst index 13f9395b4c..3ca135439d 100644 --- a/DOCS/man/mpv.rst +++ b/DOCS/man/mpv.rst @@ -847,6 +847,8 @@ works like in older mpv releases. The profiles are currently defined as follows: .. include:: lua.rst +.. include:: javascript.rst + .. include:: ipc.rst .. include:: changes.rst diff --git a/options/options.c b/options/options.c index d20aa03b99..324a1c9a3e 100644 --- a/options/options.c +++ b/options/options.c @@ -308,14 +308,16 @@ const m_option_t mp_opts[] = { M_OPT_FIXED | CONF_NOCFG | CONF_PRE_PARSE | M_OPT_FILE), OPT_STRINGLIST("reset-on-next-file", reset_options, 0), -#if HAVE_LUA +#if HAVE_LUA || HAVE_JAVASCRIPT OPT_STRINGLIST("script", script_files, M_OPT_FIXED | M_OPT_FILE), OPT_KEYVALUELIST("script-opts", script_opts, 0), + OPT_FLAG("load-scripts", auto_load_scripts, 0), +#endif +#if HAVE_LUA OPT_FLAG("osc", lua_load_osc, UPDATE_BUILTIN_SCRIPTS), OPT_FLAG("ytdl", lua_load_ytdl, UPDATE_BUILTIN_SCRIPTS), OPT_STRING("ytdl-format", lua_ytdl_format, 0), OPT_KEYVALUELIST("ytdl-raw-options", lua_ytdl_raw_options, 0), - OPT_FLAG("load-scripts", auto_load_scripts, 0), #endif // ------------------------- stream options -------------------- diff --git a/player/javascript.c b/player/javascript.c new file mode 100644 index 0000000000..8522328c41 --- /dev/null +++ b/player/javascript.c @@ -0,0 +1,1307 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <string.h> +#include <strings.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +#include <math.h> +#include <stdint.h> + +#include <mujs.h> + +#include "osdep/io.h" +#include "mpv_talloc.h" +#include "common/common.h" +#include "options/m_property.h" +#include "common/msg.h" +#include "common/msg_control.h" +#include "options/m_option.h" +#include "input/input.h" +#include "options/path.h" +#include "misc/bstr.h" +#include "osdep/subprocess.h" +#include "osdep/timer.h" +#include "osdep/threads.h" +#include "stream/stream.h" +#include "sub/osd.h" +#include "core.h" +#include "command.h" +#include "client.h" +#include "libmpv/client.h" + +#define MIN(a,b) ((a)<(b)?(a):(b)) + +// List of builtin modules and their contents as strings. +// All these are generated from player/javascript/*.js +static const char *const builtin_files[][3] = { + {"@/defaults.js", +# include "player/javascript/defaults.js.inc" + }, + {0} +}; + +// Represents a loaded script. Each has its own js state. +struct script_ctx { + const char *filename; + struct mpv_handle *client; + struct MPContext *mpctx; + struct mp_log *log; + char *last_error_str; +}; + +static struct script_ctx *jctx(js_State *J) +{ + return (struct script_ctx *)js_getcontext(J); +} + +static mpv_handle *jclient(js_State *J) +{ + return jctx(J)->client; +} + +/********************************************************************** + * conventions, MuJS notes and vm errors + *********************************************************************/ +// - push_foo functions are called from C and push a value to the vm stack. +// +// - JavaScript C functions are code which the vm can call as a js function. +// By convention, script_bar and script__baz are js C functions. The former +// is exposed to end users as bar, and _baz is for internal use. +// +// - js C functions get a fresh vm stack with their arguments, and may +// manipulate their stack as they see fit. On exit, the vm considers the +// top value of their stack as their return value, and GC the rest. +// +// - js C function's stack[0] is "this", and the rest (1, 2, ...) are the args. +// On entry the stack has at least the number of args defined for the func, +// padded with undefined if called with less, or bigger if called with more. +// +// - Almost all vm APIs (js_*) may throw an error - a longjmp to the last +// recovery/catch point, which could skip releasing resources. Use protected +// code (e.g. js_pcall) between aquisition and release. Alternatively, use +// the autofree mechanism to manage it more easily. See more details below. +// +// - Unless named s_foo, all the functions at this file (inc. init) which +// touch the vm may throw, but either cleanup resources regardless (mostly +// autofree) or leave allocated resources on caller-provided talloc context +// which the caller should release, typically with autofree (e.g. makenode). +// +// - Functions named s_foo (safe foo) never throw, return 0 on success, else 1. + +/********************************************************************** + * mpv scripting API error handling + *********************************************************************/ +// - Errors may be thrown on some cases - the reason is at the exception. +// +// - Some APIs also set last error which can be fetched with mp.last_error(), +// where empty string (false-y) is success, or an error string otherwise. +// +// - The rest of the APIs are guaranteed to return undefined on error or a +// true-thy value on success and may or may not set last error. +// +// - push_success, push_failure, push_status and pushed_error set last error. + +// iserr as true indicates an error, and if so, str may indicate a reason. +// Internally ctx->last_error_str is never NULL, and empty indicates success. +static void set_last_error(struct script_ctx *ctx, bool iserr, const char *str) +{ + ctx->last_error_str[0] = 0; + if (!iserr) + return; + if (!str || !str[0]) + str = "Error"; + ctx->last_error_str = talloc_strdup_append(ctx->last_error_str, str); +} + +// For use only by wrappers at defaults.js. +// arg: error string. Use empty string to indicate success. +static void script__set_last_error(js_State *J) +{ + const char *e = js_tostring(J, 1); + set_last_error(jctx(J), e[0], e); +} + +// mp.last_error() . args: none. return the last error without modifying it. +static void script_last_error(js_State *J) +{ + js_pushstring(J, jctx(J)->last_error_str); +} + +// Generic success for APIs which don't return an actual value. +static void push_success(js_State *J) +{ + set_last_error(jctx(J), 0, NULL); + js_pushboolean(J, true); +} + +// Doesn't (intentionally) throw. Just sets last_error and pushes undefined +static void push_failure(js_State *J, const char *str) +{ + set_last_error(jctx(J), 1, str); + js_pushundefined(J); +} + +// Most of the scripting APIs are either sending some values and getting status +// code in return, or requesting some value while providing a default in case an +// error happened. These simplify the C code for that and always set last_error. + +static void push_status(js_State *J, int err) +{ + if (err >= 0) { + push_success(J); + } else { + push_failure(J, mpv_error_string(err)); + } +} + + // If err is success then return 0, else push the item at def and return 1 +static bool pushed_error(js_State *J, int err, int def) +{ + bool iserr = err < 0; + set_last_error(jctx(J), iserr, iserr ? mpv_error_string(err) : NULL); + if (!iserr) + return false; + + js_copy(J, def); + return true; +} + +/********************************************************************** + * Autofree - care-free resource deallocation on vm errors, and otherwise + *********************************************************************/ +// - Autofree (af) functions are called with a talloc context argument which is +// freed after the function exits - either normally or because it threw an +// error, on the latter case it then re-throws the error after the cleanup. +// +// Autofree js C functions should have an additional void* talloc arg and +// inserted into the vm using af_newcfunction, but otherwise used normally. +// +// To wrap an autofree function af_TARGET in C: +// 1. Create a wrapper s_TARGET which runs af_TARGET safely inside js_try. +// 2. Use s_TARGET like so (always autofree, and throws if af_TARGET threw): +// void *af = talloc_new(NULL); +// int r = s_TARGET(J, ..., af); // use J, af where the callee expects. +// talloc_free(af); +// if (r) +// js_throw(J); + +// add_af_file, add_af_dir, add_af_mpv_alloc take a valid FILE*/DIR*/char* value +// respectively, and fclose/closedir/mpv_free it when the parent is freed. + +static void destruct_af_file(void *p) +{ + fclose(*(FILE**)p); +} + +static void add_af_file(void *parent, FILE *f) +{ + FILE **pf = talloc(parent, FILE*); + *pf = f; + talloc_set_destructor(pf, destruct_af_file); +} + +static void destruct_af_dir(void *p) +{ + closedir(*(DIR**)p); +} + +static void add_af_dir(void *parent, DIR *d) +{ + DIR **pd = talloc(parent, DIR*); + *pd = d; + talloc_set_destructor(pd, destruct_af_dir); +} + +static void destruct_af_mpv_alloc(void *p) +{ + mpv_free(*(char**)p); +} + +static void add_af_mpv_alloc(void *parent, char *ma) +{ + char **p = talloc(parent, char*); + *p = ma; + talloc_set_destructor(p, destruct_af_mpv_alloc); +} + +static void destruct_af_mpv_node(void *p) +{ + mpv_free_node_contents((mpv_node*)p); // does nothing for MPV_FORMAT_NONE +} + +// returns a new zeroed allocated struct mpv_node, and free it and its content +// when the parent is freed. +static mpv_node *new_af_mpv_node(void *parent) +{ + mpv_node *p = talloc_zero(parent, mpv_node); // .format == MPV_FORMAT_NONE + talloc_set_destructor(p, destruct_af_mpv_node); + return p; +} + +// Prototype for autofree functions which can be called from inside the vm. +typedef void (*af_CFunction)(js_State*, void*); + +// safely run autofree js c function directly +static int s_run_af_jsc(js_State *J, af_CFunction fn, void *af) +{ + if (js_try(J)) + return 1; + fn(J, af); + js_endtry(J); + return 0; +} + +// The trampoline function through which all autofree functions are called from +// inside the vm. Obtains the target function address and autofree-call it. +static void script__autofree(js_State *J) +{ + // The target function is at the "af_" property of this function instance. + js_currentfunction(J); + js_getproperty(J, -1, "af_"); + af_CFunction fn = (af_CFunction)js_touserdata(J, -1, "af_fn"); + js_pop(J, 2); + + void *af = talloc_new(NULL); + int r = s_run_af_jsc(J, fn, af); + talloc_free(af); + if (r) + js_throw(J); +} + +// Identical to js_newcfunction, but the function is inserted with an autofree +// wrapper, and its prototype should have the additional af argument. +static void af_newcfunction(js_State *J, af_CFunction fn, const char *name, + int length) +{ + js_newcfunction(J, script__autofree, name, length); + js_pushnull(J); // a prototype for the userdata object + js_newuserdata(J, "af_fn", fn, NULL); // uses a "af_fn" verification tag + js_defproperty(J, -2, "af_", JS_READONLY | JS_DONTENUM | JS_DONTCONF); +} + +/********************************************************************** + * Initialization and file loading + *********************************************************************/ + +static const char *get_builtin_file(const char *name) +{ + for (int n = 0; builtin_files[n][0]; n++) { + if (strcmp(builtin_files[n][0], name) == 0) + return builtin_files[n][1]; + } + return NULL; +} + +// Push up to limit bytes of file fname: from builtin_files, else from the OS. +static void af_push_file(js_State *J, const char *fname, int limit, void *af) +{ + char *filename = mp_get_user_path(af, jctx(J)->mpctx->global, fname); + MP_VERBOSE(jctx(J), "Reading file '%s'\n", filename); + if (limit < 0) + limit = INT_MAX - 1; + + const char *builtin = get_builtin_file(filename); + if (builtin) { + js_pushlstring(J, builtin, MIN(limit, strlen(builtin))); + return; + } + + FILE *f = fopen(filename, "rb"); + if (!f) + js_error(J, "cannot open file: '%s'", filename); + add_af_file(af, f); + + int len = MIN(limit, 32 * 1024); // initial allocation, size*2 strategy + int got = 0; + char *s = NULL; + while ((s = talloc_realloc(af, s, char, len))) { + int want = len - got; + int r = fread(s + got, 1, want, f); + + if (feof(f) || (len == limit && r == want)) { + js_pushlstring(J, s, got + r); + return; + } + if (r != want) + js_error(J, "cannot read data from file: '%s'", filename); + + got = got + r; + len = MIN(limit, len * 2); + } + + js_error(J, "cannot allocate %d bytes for file: '%s'", len, filename); +} + +// Safely run af_push_file. +static int s_push_file(js_State *J, const char *fname, int limit, void *af) +{ + if (js_try(J)) + return 1; + af_push_file(J, fname, limit, af); + js_endtry(J); + return 0; +} + +// Called directly, push up to limit bytes of file fname (from builtin/os). +static void push_file_content(js_State *J, const char *fname, int limit) +{ + void *af = talloc_new(NULL); + int r = s_push_file(J, fname, limit, af); + talloc_free(af); + if (r) + js_throw(J); +} + +// utils.read_file(..). args: fname [,max]. returns [up to max] bytes as string. +static void script_read_file(js_State *J) +{ + int limit = js_isundefined(J, 2) ? -1 : js_tonumber(J, 2); + push_file_content(J, js_tostring(J, 1), limit); +} + +// Runs a file with the caller's this, leaves the stack as is. +static void run_file(js_State *J, const char *fname) +{ + MP_VERBOSE(jctx(J), "Loading file %s\n", fname); + push_file_content(J, fname, -1); + js_loadstring(J, fname, js_tostring(J, -1)); + js_copy(J, 0); // use the caller's this + js_call(J, 0); + js_pop(J, 2); // result, file content +} + +// The spec defines .name and .message for Error objects. Most engines also set +// a very convenient .stack = name + message + trace, but MuJS instead sets +// .stackTrace = trace only. Normalize by adding such .stack if required. +// Run this before anything such that we can get traces on any following errors. +static const char *norm_err_proto_js = "\ + if (Error().stackTrace && !Error().stack) {\ + Object.defineProperty(Error.prototype, 'stack', {\ + get: function() {\ + return this.name + ': ' + this.message + this.stackTrace;\ + }\ + });\ + }\ +"; + +static void add_functions(js_State*, struct script_ctx*); + +// args: none. called as script, setup and run the main script +static void script__run_script(js_State *J) +{ + js_loadstring(J, "@/norm_err.js", norm_err_proto_js); + js_copy(J, 0); + js_pcall(J, 0); + + struct script_ctx *ctx = jctx(J); + add_functions(J, ctx); + run_file(J, "@/defaults.js"); + run_file(J, ctx->filename); // the main file to run + + if (!js_hasproperty(J, 0, "mp_event_loop") || !js_iscallable(J, -1)) + js_error(J, "no event loop function"); + js_copy(J, 0); + js_call(J, 0); // mp_event_loop +} + +// Safely set last error from stack top: stack trace or toString or generic. +// May leave items on stack - the caller should detect and pop if it cares. +static void s_top_to_last_error(struct script_ctx *ctx, js_State *J) +{ + set_last_error(ctx, 1, "unknown error"); + if (js_try(J)) + return; + if (js_isobject(J, -1)) + js_hasproperty(J, -1, "stack"); // fetches it if exists + set_last_error(ctx, 1, js_tostring(J, -1)); + js_endtry(J); +} + +// MuJS can report warnings through this. +static void report_handler(js_State *J, const char *msg) +{ + MP_WARN(jctx(J), "[JS] %s\n", msg); +} + +// Safely setup the js vm for calling run_script. +static int s_init_js(js_State *J, struct script_ctx *ctx) +{ + if (js_try(J)) + return 1; + js_setcontext(J, ctx); + js_setreport(J, report_handler); + js_newcfunction(J, script__run_script, "run_script", 0); + js_pushglobal(J); // 'this' for script__run_script + js_endtry(J); + return 0; +} + +/********************************************************************** + * Initialization - booting the script + *********************************************************************/ +// s_load_javascript: (entry point) creates the js vm, runs the script, returns +// on script exit or uncaught js errors. Never throws. +// script__run_script: - loads the built in functions and vars into the vm +// - runs the default file[s] and the main script file +// - calls mp_event_loop, returns on script-exit or throws. +// +// Note: init functions don't need autofree. They can use ctx as a talloc +// context and free normally. If they throw - ctx is freed right afterwards. +static int s_load_javascript(struct mpv_handle *client, const char *fname) +{ + struct script_ctx *ctx = talloc_ptrtype(NULL, ctx); + *ctx = (struct script_ctx) { + .client = client, + .mpctx = mp_client_get_core(client), + .log = mp_client_get_log(client), + .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"), + .filename = fname, + }; + + int r = -1; + js_State *J = js_newstate(NULL, NULL, 0); + if (!J || s_init_js(J, ctx)) + goto error_out; + + set_last_error(ctx, 0, NULL); + if (js_pcall(J, 0)) { // script__run_script + s_top_to_last_error(ctx, J); + goto error_out; + } + + r = 0; + +error_out: + if (r) + MP_FATAL(ctx, "%s\n", ctx->last_error_str); + if (J) + js_freestate(J); + + talloc_free(ctx); + return r; +} + +/********************************************************************** + * Main mp.* scripting APIs and helpers + *********************************************************************/ +static void pushnode(js_State *J, mpv_node *node); +static void makenode(void *ta_ctx, mpv_node *dst, js_State *J, int idx); + +// Return the index in opts of stack[idx] (or of def if undefined), else throws. +static int checkopt(js_State *J, int idx, const char *def, const char *opts[], + const char *desc) +{ + const char *opt = js_isundefined(J, idx) ? def : js_tostring(J, idx); + for (int i = 0; opts[i]; i++) { + if (strcmp(opt, opts[i]) == 0) + return i; + } + js_error(J, "Invalid %s '%s'", desc, opt); +} + +// args: level as string and a variable numbers of args to print. adds final \n +static void script_log(js_State *J) +{ + const char *level = js_tostring(J, 1); + int msgl = mp_msg_find_level(level); + if (msgl < 0) + js_error(J, "Invalid log level '%s'", level); + + struct mp_log *log = jctx(J)->log; + for (int top = js_gettop(J), i = 2; i < top; i++) + mp_msg(log, msgl, (i == 2 ? "%s" : " %s"), js_tostring(J, i)); + mp_msg(log, msgl, "\n"); + push_success(J); +} + +static void script_find_config_file(js_State *J, void *af) +{ + const char *fname = js_tostring(J, 1); + char *path = mp_find_config_file(af, jctx(J)->mpctx->global, fname); + if (path) { + js_pushstring(J, path); + } else { + push_failure(J, "not found"); + } +} + +static void script__request_event(js_State *J) +{ + const char *event = js_tostring(J, 1); + bool enable = js_toboolean(J, 2); + + const char *name; + for (int n = 0; n < 256 && (name = mpv_event_name(n)); n++) { + if (strcmp(name, event) == 0) { + push_status(J, mpv_request_event(jclient(J), n, enable)); + return; + } + } + push_failure(J, "Unknown event name"); +} + +static void script_enable_messages(js_State *J) +{ + const char *level = js_tostring(J, 1); + if (mp_msg_find_level(level) < 0) + js_error(J, "Invalid log level '%s'", level); + push_status(J, mpv_request_log_messages(jclient(J), level)); +} + +// args - command [with arguments] as string +static void script_command(js_State *J) +{ + push_status(J, mpv_command_string(jclient(J), js_tostring(J, 1))); +} + +// args: strings of command and then variable number of arguments +static void script_commandv(js_State *J) +{ + const char *argv[MP_CMD_MAX_ARGS + 1]; + int length = js_gettop(J) - 1; + if (length >= MP_ARRAY_SIZE(argv)) + js_error(J, "Too many arguments"); + + for (int i = 0; i < length; i++) + argv[i] = js_tostring(J, 1 + i); + argv[length] = NULL; + push_status(J, mpv_command(jclient(J), argv)); +} + +// args: name, string value +static void script_set_property(js_State *J) +{ + int e = mpv_set_property_string(jclient(J), js_tostring(J, 1), + js_tostring(J, 2)); + push_status(J, e); +} + +// args: name, boolean +static void script_set_property_bool(js_State *J) +{ + int v = js_toboolean(J, 2); + int e = mpv_set_property(jclient(J), js_tostring(J, 1), MPV_FORMAT_FLAG, &v); + push_status(J, e); +} + |