History

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.

Avi Halachmi (:avih) committed 6 years ago

Commit 756960b1 parent 68a1b474 files · +16 −1Browse files

Unified · Split

4 files
  1. DOCS/interface-changes.rst +2 −0
  2. DOCS/man/javascript.rst +4 −1
  3. player/javascript.c +7 −0
  4. player/javascript/defaults.js +3 −0
DOCS/interface-changes.rst+2−0
@@ -28,6 +28,8 @@ Interface changes · 27 unmodified lines
2828 - directories in ~/.mpv/scripts/ (or equivalent) now have special semantics
2929 (see mpv Lua scripting docs)
3030 - names starting with "." in ~/.mpv/scripts/ (or equivalent) are now ignored
31+ - js modules: ~~/scripts/modules.js/ is no longer used, global paths can be
32+ set with custom init (see docs), dir-scripts first look at <dir>/modules/
3133 --- mpv 0.32.0 ---
3234 - change behavior when using legacy option syntax with options that start
3335 with two dashes (``--`` instead of a ``-``). Now, using the recommended
DOCS/man/javascript.rst+4−1
@@ -139,6 +139,8 @@ success, ``fn`` is called always a-sync, ``error`` is empty string on success. · 138 unmodified lines
139139
140140 ``mp.get_script_name()``
141141
142+``mp.get_script_directory()``
143+
142144 ``mp.osd_message(text [,duration])``
143145
144146 ``mp.get_wakeup_pipe()``
@@ -309,7 +311,8 @@ or ``~/x``. Otherwise it's considered a global module id and searched according · 164 unmodified lines
309311 load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js``
310312 inside dir ``foo`` at one of the paths.
311313
312−The ``mp.module_paths`` array is empty by default.
314+The ``mp.module_paths`` array is empty by default except for scripts which are
315+loaded as a directory where it contains one item - ``<directory>/modules/``.
313316 ``mp.module_paths`` may be updated from a script (preferably via custom init -
314317 see below) which will affect future calls to ``require`` for global module id's
315318 which are not already loaded/cached.
player/javascript.c+7−0
@@ -59,6 +59,7 @@ static const char *const builtin_files[][3] = { · 58 unmodified lines
5959 // Represents a loaded script. Each has its own js state.
6060 struct script_ctx {
6161 const char *filename;
62+ const char *path; // NULL if single file
6263 struct mpv_handle *client;
6364 struct MPContext *mpctx;
6465 struct mp_log *log;
@@ -477,6 +478,7 @@ static int s_load_javascript(struct mp_script_args *args) · 412 unmodified lines
477478 .log = args->log,
478479 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),
479480 .filename = args->filename,
481+ .path = args->path,
480482 };
481483
482484 int r = -1;
@@ -1281,6 +1283,11 @@ static void add_functions(js_State *J, struct script_ctx *ctx) · 798 unmodified lines
12811283 js_pushstring(J, ctx->filename);
12821284 js_setproperty(J, -2, "script_file");
12831285
1286+ if (ctx->path) {
1287+ js_pushstring(J, ctx->path);
1288+ js_setproperty(J, -2, "script_path");
1289+ }
1290+
12841291 js_pop(J, 2); // leave the stack as we got it
12851292 }
12861293
player/javascript/defaults.js+3−0
@@ -463,6 +463,8 @@ function process_timers() { · 462 unmodified lines
463463 *********************************************************************/
464464
465465 mp.module_paths = []; // global modules search paths
466+if (mp.script_path !== undefined) // loaded as a directory
467+ mp.module_paths.push(mp.utils.join_path(mp.script_path, "modules"));
466468
467469 // Internal meta top-dirs. Users should not rely on these names.
468470 var MODULES_META = "~~modules",
@@ -645,6 +647,7 @@ mp.options = { read_options: read_options }; · 176 unmodified lines
645647 g.print = mp.msg.info; // convenient alias
646648 mp.get_script_name = function() { return mp.script_name };
647649 mp.get_script_file = function() { return mp.script_file };
650+mp.get_script_directory = function() { return mp.script_path };
648651 mp.get_time = function() { return mp.get_time_ms() / 1000 };
649652 mp.utils.getcwd = function() { return mp.get_property("working-directory") };
650653 mp.dispatch_event = dispatch_event;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t