environment
The primary user-facing API for compiling Catspeak programs and configuring the Catspeak runtime environment.
Example
A high-level overview of Catspeak usage. The example walks through how to compile, execute, and introspect the global variables of a Catspeak script:
GameMaker Language (.gml)Copy// parse Catspeak code
var ir = Catspeak.parseString(@'
count = 0
counter = fun {
count += 1
return count
}
');
// compile Catspeak code into a callable GML function
var main = Catspeak.compile(ir);
// initialise the Catspeak script by calling its main entry point
catspeak_execute(main);
// grab the counter function from the script
var counter = catspeak_globals(main).counter;
// call the Catspeak `counter` function from GML!
show_message(counter()); // prints 1
show_message(counter()); // prints 2
show_message(counter()); // prints 3
show_message(counter()); // prints 4
§ Catspeaktop ^
GameMaker Language (.gml)Copy#macro Catspeak global.__catspeak__
The default global Catspeak environment. Mostly exists for UX reasons.
Unless you need to have multiple instances of Catspeak with different
configurations, you should use this. Otherwise, you should create a new
sandboxed Catspeak environment using new CatspeakEnvironment()
.
Returns Struct.CatspeakEnvironment
§ catspeak_executetop ^
GameMaker Language (.gml)Copyfunction catspeak_execute(
callee : Any,
... : Any,
) -> Any
Simple wrapper over catspeak_execute_ext
which infers the self
and
other
context from the current callsite.
📝 NoteGets around a limitation in GML where the
self
andother
of the callsite cannot be accessed from within bound methods. Use this function if you want theself
of a called Catspeak function to be the same as theself
of the callsite in GML land.
Arguments
callee
The function to call. Can be a GML function, Catspeak function, or a function bound using
catspeak_method
....
The arguments to pass to this function.
Returns Any
The result of evaluating the callee
function.
§ catspeak_execute_exttop ^
GameMaker Language (.gml)Copyfunction catspeak_execute_ext(
callee : Any,
self_ : Struct,
args? : Array<Any>,
offset? : Real,
argc? : Real,
) -> Any
Executes a Catspeak-compatible function in the supplied self
scope.
📝 NoteGets around a limitation in GML where the
self
andother
of the callsite cannot be accessed from within bound methods. Use this function if you want theself
of a called Catspeak function to be the same as theself
of the callsite in GML land.
Arguments
callee
The function to call. Can be a GML function, Catspeak function, or a function bound using
catspeak_method
.self_
The
self
context to use when calling this Catspeak function.args
(optional)The argument list to call this function with. Defaults to no arguments.
offset
(optional)The offset in the
args
array to begin reading arguments from. Defaults to 0.argc
(optional)The number of arguments to pass to the function call. Defaults to
array_length(args) - offset
.
Returns Any
The result of evaluating the callee
function.
§ catspeak_get_indextop ^
GameMaker Language (.gml)Copyfunction catspeak_get_index(
callee : Any,
) -> Any
Returns the 'index' of the current method, either by returning the compiled
Catspeak function or the exposed GML function as a method bound to undefined
📝 NotePreferred over 'method_get_index', otherwise you risk breaking your compiled Catspeak functions.
Arguments
callee
The function to get the current global context of. Can be a GML function, Catspeak function, or a function bound using
catspeak_method
.
Returns Any
§ catspeak_get_selftop ^
GameMaker Language (.gml)Copyfunction catspeak_get_self(
callee : Any,
) -> Any
Returns the 'self' of the current method, either by returning the correct Catspeak scope or the exposed GML method scope (if any)
📝 NotePreferred over 'method_get_self', otherwise you risk breaking your compiled Catspeak functions.
Arguments
callee
The function to get the current global context of. Can be a GML function, Catspeak function, or a function bound using
catspeak_method
.
Returns Any
§ catspeak_globalstop ^
GameMaker Language (.gml)Copyfunction catspeak_globals(
callee : Any,
) -> Struct
Returns a struct containing the global variable context of a Catspeak
function, or undefined
if no globals exist.
Arguments
callee
The function to get the global context of. Can be a GML function, Catspeak function, or a function bound using
catspeak_method
.
Returns Struct
§ catspeak_methodtop ^
GameMaker Language (.gml)Copyfunction catspeak_method(
self_ : Any,
callee : Any,
) -> Any
Binds a function to a self
. Similar to the built-in method
function,
except this supports Catspeak functions as well as GML functions.
📝 NotePrefered over using
method
otherwise you risk breaking your compiled Catspeak functions.
Arguments
self_
The scope to bind this function to. Can be a struct or
undefined
.callee
The function to get the global context of. Can be a GML function, Catspeak function, or a function bound using
catspeak_method
.
Returns Any
§ catspeak_special_to_structtop ^
GameMaker Language (.gml)Copyfunction catspeak_special_to_struct(
gmlSpecial : Any,
) -> Struct
Because Catspeak is sandboxed, care must be taken to not expose any
unintentional exploits to modders with GML-specific knowledge. One
exampe of an exploit is using the number -5
to access all the
internal global variables of a game:
GameMaker Language (.gml)Copyvar globalBypass = -5;
show_message(globalBypass.secret);
Catspeak avoids these exploits by requiring that all special values be converted to their struct counterpart; that is, Catspeak does not coerce numbers to these special types implicitly.
Use this function to convert special GML constants, such as self
,
global
, or instances into their struct counterparts. Will return
undefined
if there does not exist a valid conversion.
Arguments
gmlSpecial
Any special GML value to convert into a Catspeak-compatible struct. E.g.
global
or an instance ID.
Returns Struct