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
28 - directories in ~/.mpv/scripts/ (or equivalent) now have special semantics28 - directories in ~/.mpv/scripts/ (or equivalent) now have special semantics
29 (see mpv Lua scripting docs)29 (see mpv Lua scripting docs)
30 - names starting with "." in ~/.mpv/scripts/ (or equivalent) are now ignored30 - 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/
31 --- mpv 0.32.0 ---33 --- mpv 0.32.0 ---
32 - change behavior when using legacy option syntax with options that start34 - change behavior when using legacy option syntax with options that start
33 with two dashes (``--`` instead of a ``-``). Now, using the recommended35 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
139 139
140``mp.get_script_name()``140``mp.get_script_name()``
141 141
142``mp.get_script_directory()``
143
142``mp.osd_message(text [,duration])``144``mp.osd_message(text [,duration])``
143 145
144``mp.get_wakeup_pipe()``146``mp.get_wakeup_pipe()``
@@ -309,7 +311,8 @@ or ``~/x``. Otherwise it's considered a global module id and searched according · 164 unmodified lines
309load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js``311load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js``
310inside dir ``foo`` at one of the paths.312inside dir ``foo`` at one of the paths.
311 313
312The ``mp.module_paths`` array is empty by default.314The ``mp.module_paths`` array is empty by default except for scripts which are
315loaded as a directory where it contains one item - ``<directory>/modules/``.
313``mp.module_paths`` may be updated from a script (preferably via custom init -316``mp.module_paths`` may be updated from a script (preferably via custom init -
314see below) which will affect future calls to ``require`` for global module id's317see below) which will affect future calls to ``require`` for global module id's
315which are not already loaded/cached.318which are not already loaded/cached.
player/javascript.c+7−0
@@ -59,6 +59,7 @@ static const char *const builtin_files[][3] = { · 58 unmodified lines
59// Represents a loaded script. Each has its own js state.59// Represents a loaded script. Each has its own js state.
60struct script_ctx {60struct script_ctx {
61 const char *filename;61 const char *filename;
62 const char *path; // NULL if single file
62 struct mpv_handle *client;63 struct mpv_handle *client;
63 struct MPContext *mpctx;64 struct MPContext *mpctx;
64 struct mp_log *log;65 struct mp_log *log;
@@ -477,6 +478,7 @@ static int s_load_javascript(struct mp_script_args *args) · 412 unmodified lines
477 .log = args->log,478 .log = args->log,
478 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),479 .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"),
479 .filename = args->filename,480 .filename = args->filename,
481 .path = args->path,
480 };482 };
481 483
482 int r = -1;484 int r = -1;
@@ -1281,6 +1283,11 @@ static void add_functions(js_State *J, struct script_ctx *ctx) · 798 unmodified lines
1281 js_pushstring(J, ctx->filename);1283 js_pushstring(J, ctx->filename);
1282 js_setproperty(J, -2, "script_file");1284 js_setproperty(J, -2, "script_file");
1283 1285
1286 if (ctx->path) {
1287 js_pushstring(J, ctx->path);
1288 js_setproperty(J, -2, "script_path");
1289 }
1290
1284 js_pop(J, 2); // leave the stack as we got it1291 js_pop(J, 2); // leave the stack as we got it
1285}1292}
1286 1293
player/javascript/defaults.js+3−0
@@ -463,6 +463,8 @@ function process_timers() { · 462 unmodified lines
463 *********************************************************************/463 *********************************************************************/
464 464
465mp.module_paths = []; // global modules search paths465mp.module_paths = []; // global modules search paths
466if (mp.script_path !== undefined) // loaded as a directory
467 mp.module_paths.push(mp.utils.join_path(mp.script_path, "modules"));
466 468
467// Internal meta top-dirs. Users should not rely on these names.469// Internal meta top-dirs. Users should not rely on these names.
468var MODULES_META = "~~modules",470var MODULES_META = "~~modules",
@@ -645,6 +647,7 @@ mp.options = { read_options: read_options }; · 176 unmodified lines
645g.print = mp.msg.info; // convenient alias647g.print = mp.msg.info; // convenient alias
646mp.get_script_name = function() { return mp.script_name };648mp.get_script_name = function() { return mp.script_name };
647mp.get_script_file = function() { return mp.script_file };649mp.get_script_file = function() { return mp.script_file };
650mp.get_script_directory = function() { return mp.script_path };
648mp.get_time = function() { return mp.get_time_ms() / 1000 };651mp.get_time = function() { return mp.get_time_ms() / 1000 };
649mp.utils.getcwd = function() { return mp.get_property("working-directory") };652mp.utils.getcwd = function() { return mp.get_property("working-directory") };
650mp.dispatch_event = dispatch_event;653mp.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