Also, add the function mp.get_script_directory() to let scripts know if they're loaded as a directory and where.
Showing the changes to player/javascript.c (1 of 4 files). Show all
Unified · Split
| @@ -59,6 +59,7 @@ static const char *const builtin_files[][3] = { · 58 unmodified lines | ||
| 59 | 59 | // Represents a loaded script. Each has its own js state. |
| 60 | 60 | struct script_ctx { |
| 61 | 61 | const char *filename; |
| 62 | + const char *path; // NULL if single file | |
| 62 | 63 | struct mpv_handle *client; |
| 63 | 64 | struct MPContext *mpctx; |
| 64 | 65 | struct mp_log *log; |
| @@ -477,6 +478,7 @@ static int s_load_javascript(struct mp_script_args *args) · 412 unmodified lines | ||
| 477 | 478 | .log = args->log, |
| 478 | 479 | .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"), |
| 479 | 480 | .filename = args->filename, |
| 481 | + .path = args->path, | |
| 480 | 482 | }; |
| 481 | 483 | |
| 482 | 484 | int r = -1; |
| @@ -1281,6 +1283,11 @@ static void add_functions(js_State *J, struct script_ctx *ctx) · 798 unmodified lines | ||
| 1281 | 1283 | js_pushstring(J, ctx->filename); |
| 1282 | 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 | 1291 | js_pop(J, 2); // leave the stack as we got it |
| 1285 | 1292 | } |
| 1286 | 1293 | |