Commit Graph

26 Commits

Author SHA1 Message Date
Avi Halachmi (:avih) d5a02dd934 js: hooks: allow deferred continuation (match d0ab562b)
The callback now gets an object argument with defer/cont functions.

Like the lua code, the behavior is that each hook event allows at
most one continue, but nothing enforces the order of continuations
if more hook events arrive before prior ones were continued - which
is possible now with the defer option, but wasn't possible before
(continuation was synchronous from the hook event handler).
2020-08-07 15:12:20 +03:00
Avi Halachmi (:avih) b5284a6829 js: mp.set_osd_ass: ignore identical inputs (match ccbb8b1c) 2020-05-10 21:20:28 +03:00
Avi Halachmi (:avih) a23869379c js: osd-overlay update: return the command result (match 7a76b577)
Currently only useful for the new 'compute_bounds' command key.
2020-03-07 12:37:35 +02:00
Avi Halachmi (:avih) 9cf45d8300 js: osd-overlay update: support arbitrary key names
Until now the 'update' method used mp.command_native with a hardcoded
list of key names.

Change it to use whatever keys the user set to this object, so that
we can remain oblivious to new keys which 'osd-overlay' may support.

This is how the lua code did it from the begining. We didn't, and now
we pay the price.

Note: could be implemented either as we have now (clone `this`
excluding the methods) or by moving the methods up the prototype chain
(i.e. class methods) so they don't get enumerated and use `this` as
the command object itself.

However, in the latter approach we'll have to save the values which we
change (name, res_x, res_y) and restore them after the command, so it's
simpler to just clone `this`.
2020-03-07 12:37:35 +02:00
Avi Halachmi (:avih) 756960bf3c 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.
2020-02-07 18:22:12 +02:00
Avi Halachmi (:avih) 68a1b47d4d js: require: don't use ~~/scripts/modules.js/
Directories inside ~~/scripts/ are now loaded as scripts, so don't use
it also for modules. Now there are no default module paths.

To compensate, we now try to run ~~/.init.js right after defaults.js,
so the user may extend the js init procedure via this script, e.g. for
adding default paths to mp.module_paths .
2020-02-07 18:20:33 +02:00
Avi Halachmi (:avih) 5e0875c9e0 js: use osd-dimentions for mp.get_osd_{size,margins}
This matches lua's 11b9315b but with the lagacy field names which the
js code used previously.

Currently the property always returns an object (with dummy/last/null
field values if there are no dimensions), but the code is ready for
a future case where it might return null if there are no dimensions - at
which case it will forward the null, breaking backward compatibility for
a better API.
2020-01-08 11:49:49 +02:00
Avi Halachmi (:avih) 9f2fda7d85 js: support mp.create_osd_overlay (match 07287262)
The legacy mp.set_osd_ass(...) is still supported (but also still
undocumented) as a wrapper for the new mp.create_osd_overlay(...).
2019-12-23 17:52:34 +02:00
Avi Halachmi (:avih) 5a74bf5f95 js: batch key bindings updates (match 96932fe7)
Implemented using one-time idle observer (i.e. setTimeout), to avoid
additional function call(back) every time the event loop enters idle.

The lua mp.flush_key_bindings() is also available, also undocumented.
2019-12-23 17:52:34 +02:00
Avi Halachmi (:avih) b670838b3d js: read_options: on_update: don't re-read the config file
Now that 00af718a made the lua read_options behavior much more similar
to the js behavior, the main difference was that lua does not re-read
the config file at on_update (but it does re-apply its stored content)
while js did re-read it.

Now the js on_update also does not re-read the config file and instead
applies its stored original content.

This is slightly hacky by adding an undocumented optional 4th argument
to read_options which allows overriding the config file content.
2019-12-22 14:51:13 +02:00
Avi Halachmi (:avih) 71ddb22b39 js: read_options: support on_update (match 478a321d)
This is a bit different than the lua code: on script-opts change it
simply re-applies the conf-file and script-opts to the options object,
and if this results in any changed value at options then on_update is
called with the changelist as argument.

This allows a value to revert back to the conf-file value if the
matching script-opts key had a different value and then got deleted.
It also guarantees to call back whenever the options object is
modified, which the lua code doesn't do (e.g. if the caller changed
a value and the observer changed it back - it won't detect a change).
2019-12-21 14:57:10 +02:00
Avi Halachmi (:avih) 3460a7b696 js: support shared script properties (match 7e4819e7) 2019-12-19 16:23:19 +02:00
Avi Halachmi (:avih) 4fc5cd32d0 js: require: allow custom module search paths via mp.module_paths 2019-12-19 16:23:19 +02:00
Avi Halachmi (:avih) b67d124dc0 js: mp.add_key_binding: allow false-y name (match 311cc5b)
This is in addition to the allowed omission of name and/or flags (as
documented).
2019-11-30 12:23:47 +02:00
Avi Halachmi (:avih) 67fef2b152 js: complex key binding: use key_name like the docs say 2019-11-20 16:55:21 +02:00
Avi Halachmi (:avih) 14971424b9 js: report key name for "complex" key bindings - match b08c8f50 2019-11-20 16:28:49 +02:00
Avi Halachmi (:avih) 44f8dccfb6 js: expose mpv_abort_async_command() (match dbe831bd)
With minor difference from lua, as documented.
2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) ad0a525f20 js: reimplement subprocess_detached using the run command (match 7f91e268) 2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) fea39b5a6b js: reimplement subprocess using the subprocess command (match 548ef078)
Semantics changes are the same as at 548ef078 .

Also, the previous C implementation returnd a string for the `stdout`
value, but stdout of the subprocess command is MPV_FORMAT_BYTE_ARRAY
which js previously didn't support, so support it too (at pushnode)
by returning it as a string - the same as the lua code does.
2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) 5b5f776900 js: expose async commands (match 159379980e) 2019-09-11 21:08:04 +03:00
Avi Halachmi (:avih) ec625266c8
js: use new hooks API (match f60826c3) 2018-04-07 16:02:20 -07:00
Avi Halachmi (:avih) 84aa9e7146
js: dump(..): fix incorrect <VISITED> with array argument
When dump's argument is an array, it was displaying <VISITED> for all
the array's object elements (objects, arrays, etc), regardless if they're
actually visited or not.

The reason is that we try to stringify twice: once normally which may
throw (on cycles), and a second time while excluding visited items which
is indicated by binding the replacer to an empty array - in which we hold
the visited items, where the replacer tests if its 'this' is an array or
not and acts accordingly.

However, its "this" may also be an array even if we don't bind it to one,
because its "normal" this is the main stringified object, so the test of
Array.isArray(this) is true when the top object is an array, and the object
items are indeed are in it - so the replacer considers them visited.

Fix by binding to null on the first attempt such that "this" is an array
only when we want it to test for visited items and not when the argument
itself is an array.
2018-04-07 16:02:19 -07:00
Avi Halachmi (:avih) 9a47023c44
js: implement mp.register_idle
Due to earlier misinterpretation of the Lua docs as if mp.register_idle
registers a one-shot callback, the JS docs suggested to use setTimeout.

But the behavior and Lua docs are such that it's a repeating callback
which fires just before the script thread goes to sleep.

Implement it for JS too.
2018-04-07 16:02:19 -07:00
Avi Halachmi (:avih) b04f0cad43
js: implement mp.options.read_options 2018-04-07 16:02:19 -07:00
TheAMM 3c4667c862 js: implement mp.msg.trace()
To match the new Lua helper introduced in
1afdeee1ad

Add documentation for both.
2017-12-16 02:25:24 -08:00
Avi Halachmi (:avih) d223a63bc5 js: add javascript scripting support using MuJS
Implements JS with almost identical API to the Lua support.

Key differences from Lua:
- The global mp, mp.msg and mp.utils are always available.
- Instead of returning x, error, return x and expose mp.last_error().
- Timers are JS standard set/clear Timeout/Interval.
- Supports CommonJS modules/require.
- Added at mp.utils: getenv, read_file, write_file and few more.
- Global print and dump (expand objects) functions.
- mp.options currently not supported.

See DOCS/man/javascript.rst for more details.
2017-06-14 12:29:32 +02:00