summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAvi Halachmi (:avih) <avihpit@yahoo.com>2020-02-07 17:35:48 +0200
committerAvi Halachmi (:avih) <avihpit@yahoo.com>2020-02-07 18:22:12 +0200
commit756960bf3cd6110c85b97f3edbd3bf0e2a2b91d4 (patch)
tree004dd368108cf52d8d73964f91a8ae7d86100437
parent68a1b47d4d4a7d59d4f43850902d4194eb7c0552 (diff)
downloadmpv-756960bf3cd6110c85b97f3edbd3bf0e2a2b91d4.tar.bz2
mpv-756960bf3cd6110c85b97f3edbd3bf0e2a2b91d4.tar.xz
js: require: directory-scripts: first look at <dir>/modules/
Also, add the function mp.get_script_directory() to let scripts know if they're loaded as a directory and where.
-rw-r--r--DOCS/interface-changes.rst2
-rw-r--r--DOCS/man/javascript.rst5
-rw-r--r--player/javascript.c7
-rw-r--r--player/javascript/defaults.js3
4 files changed, 16 insertions, 1 deletions
diff --git a/DOCS/interface-changes.rst b/DOCS/interface-changes.rst
index 3e0347bc7f..efbfdc510e 100644
--- a/DOCS/interface-changes.rst
+++ b/DOCS/interface-changes.rst
@@ -28,6 +28,8 @@ Interface changes
- directories in ~/.mpv/scripts/ (or equivalent) now have special semantics
(see mpv Lua scripting docs)
- names starting with "." in ~/.mpv/scripts/ (or equivalent) are now ignored
+ - js modules: ~~/scripts/modules.js/ is no longer used, global paths can be
+ set with custom init (see docs), dir-scripts first look at <dir>/modules/
--- mpv 0.32.0 ---
- change behavior when using legacy option syntax with options that start
with two dashes (``--`` instead of a ``-``). Now, using the recommended
diff --git a/DOCS/man/javascript.rst b/DOCS/man/javascript.rst
index 49ddb481e4..0a43d213b6 100644
--- a/DOCS/man/javascript.rst
+++ b/DOCS/man/javascript.rst
@@ -139,6 +139,8 @@ success, ``fn`` is called always a-sync, ``error`` is empty string on success.
``mp.get_script_name()``
+``mp.get_script_directory()``
+
``mp.osd_message(text [,duration])``
``mp.get_wakeup_pipe()``
@@ -309,7 +311,8 @@ or ``~/x``. Otherwise it's considered a global module id and searched according
load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js``
inside dir ``foo`` at one of the paths.
-The ``mp.module_paths`` array is empty by default.
+The ``mp.module_paths`` array is empty by default except for scripts which are
+loaded as a directory where it contains one item - ``<directory>/modules/``.
``mp.module_paths`` may be updated from a script (preferably via custom init -
see below) which will affect future calls to ``require`` for global module id's
which are not already loaded/cached.
diff --git a/player/javascript.c b/player/javascript.c
index 6047add255..f3d810e29c 100644
--- a/player/javascript.c
+++ b/player/javascript.c
@@ -59,6 +59,7 @@ static const char *const builtin_files[][3] = {
// Represents a loaded script. Each has its own js state.
struct script_ctx {
const char *filename;
+ const char *path; // NULL if single file
struct mpv_handle *client;
struct MPContext *mpctx;
struct mp_log *log;
@@ -477,6 +478,7 @@ static int s_load_javascript(struct mp_script_args *args)
.log = args->log,
.last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),
.filename = args->filename,
+ .path = args->path,
};
int r = -1;
@@ -1281,6 +1283,11 @@ static void add_functions(js_State *J, struct script_ctx *ctx)
js_pushstring(J, ctx->filename);
js_setproperty(J, -2, "script_file");
+ if (ctx->path) {
+ js_pushstring(J, ctx->path);
+ js_setproperty(J, -2, "script_path");
+ }
+
js_pop(J, 2); // leave the stack as we got it
}
diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js
index 588e8d6891..dcea6734a0 100644
--- a/player/javascript/defaults.js
+++ b/player/javascript/defaults.js
@@ -463,6 +463,8 @@ function process_timers() {
*********************************************************************/
mp.module_paths = []; // global modules search paths
+if (mp.script_path !== undefined) // loaded as a directory
+ mp.module_paths.push(mp.utils.join_path(mp.script_path, "modules"));
// Internal meta top-dirs. Users should not rely on these names.
var MODULES_META = "~~modules",
@@ -645,6 +647,7 @@ mp.options = { read_options: read_options };
g.print = mp.msg.info; // convenient alias
mp.get_script_name = function() { return mp.script_name };
mp.get_script_file = function() { return mp.script_file };
+mp.get_script_directory = function() { return mp.script_path };
mp.get_time = function() { return mp.get_time_ms() / 1000 };
mp.utils.getcwd = function() { return mp.get_property("working-directory") };
mp.dispatch_event = dispatch_event;