JavaScript Support: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
As of revision 20509, KoLmafia supports scripting in JavaScript! You can run JS code from the CLI using <code>js <nowiki><code></nowiki></code>, and you can call scripts through any of the normal methods. Consult and "lifecycle" scripts (e.g. <code>betweenBattleScript</code>) are supported as well as of revision 20519. '''This support is still experimental - you have been warned.''' | As of revision 20509, KoLmafia supports scripting in JavaScript! You can run JS code from the CLI using <code>js <nowiki><code></nowiki></code>, and you can call scripts through any of the normal methods. Consult and "lifecycle" scripts (e.g. <code>betweenBattleScript</code>) are supported as well as of revision 20519. '''This support is still experimental - you have been warned.''' | ||
* All the methods in the ASH runtime library are available, with names of methods converted to camelCase. So, for example, <code>print_html</code> in ASH becomes <code> | * All the methods in the ASH runtime library are available, with names of methods converted to camelCase. So, for example, <code>print_html</code> in ASH becomes <code>printHtml</code> in JS. | ||
* In scripts called from a file, you can access the runtime library by calling <code>require("kolmafia")</code>, so <code>const { printHtml } = require("kolmafia")</code>. If running from the command line via <code>js</code>, the runtime library functions are all available in the global scope (so you can do <code>js print("Hello world!");</code>). | |||
* ASH maps are converted to plain JS objects, and ASH arrays are converted to JS arrays. | * ASH maps are converted to plain JS objects, and ASH arrays are converted to JS arrays. | ||
* You can look at the type reference for the JS version of the ASH runtime library with <code>jsref</code>, which works just like <code>ashref</code>. | * You can look at the type reference for the JS version of the ASH runtime library with <code>jsref</code>, which works just like <code>ashref</code>. |
Revision as of 03:42, 12 December 2020
As of revision 20509, KoLmafia supports scripting in JavaScript! You can run JS code from the CLI using js <code>
, and you can call scripts through any of the normal methods. Consult and "lifecycle" scripts (e.g. betweenBattleScript
) are supported as well as of revision 20519. This support is still experimental - you have been warned.
- All the methods in the ASH runtime library are available, with names of methods converted to camelCase. So, for example,
print_html
in ASH becomesprintHtml
in JS. - In scripts called from a file, you can access the runtime library by calling
require("kolmafia")
, soconst { printHtml } = require("kolmafia")
. If running from the command line viajs
, the runtime library functions are all available in the global scope (so you can dojs print("Hello world!");
). - ASH maps are converted to plain JS objects, and ASH arrays are converted to JS arrays.
- You can look at the type reference for the JS version of the ASH runtime library with
jsref
, which works just likeashref
. - Objects like monsters and items can be constructed via the
Monster
andItem
global objects, along with the rest of ASH's enumerated types (stat, phylum, location, etc.).Monster.get
takes a number or a string, just like$monster
in ASH, or an array of numbers and strings to construct an array of monsters.Monster.all
works like$monsters[]
in ASH; it takes no arguments and returns an array of all monsters. - Mafia supports
require
for both ASH and JS scripts. For ASH scripts, it will execute top-level code but only export functions, not variables, in the top scope. - If you want Mafia to run your
main
function, you must export it by adding it to module.exports, just as you would for a Node module. For the uninitiated, this just means addingmodule.exports.main = main
to the end of your script. You will want to do the same with any function or value you want to be available to other scripts via require.