JavaScript Support: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
Ikzann (talk | contribs)
Page creation
 
Ikzann (talk | contribs)
Initial write. Some of this is premature.
Line 1: Line 1:
As of revision 20509, KoLmafia supports scripting in JavaScript!
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 XXX.
 
* All the methods in the ASH runtime library are available on the <code>Lib</code> global object in JS, with names of methods converted to camelCase. So, for example, <code>print_html</code> in ASH becomes <code>Lib.printHtml</code> in JS.
* 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 <code>Lib</code> with <code>jsref</code>, which works just like <code>ashref</code>.
* Objects like monsters and items can be constructed via the <code>Monster</code> and <code>Item</code> global objects, along with the rest of ASH's enumerated types (stat, phylum, location, etc.). <code>Monster.get</code> takes a number or a string, just like <code>$monster</code> in ASH, or an array of numbers and strings to construct an array of monsters. <code>Monster.all</code> works like <code>$monsters[]</code> in ASH; it takes no arguments and returns an array of all monsters.
* Mafia supports <code>require</code> 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.

Revision as of 15:20, 22 November 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 XXX.

  • All the methods in the ASH runtime library are available on the Lib global object in JS, with names of methods converted to camelCase. So, for example, print_html in ASH becomes Lib.printHtml in JS.
  • 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 Lib with jsref, which works just like ashref.
  • Objects like monsters and items can be constructed via the Monster and Item 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.