Skip to content

Commit

Permalink
added catspeak_method function
Browse files Browse the repository at this point in the history
  • Loading branch information
katsaii committed Jul 14, 2024
1 parent 7a3c612 commit 9eb8e2f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src-lts/scripts/scr_catspeak_codegen/scr_catspeak_codegen.gml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ function is_catspeak(value) {
return is_method(value) && method_get_index(value) == __catspeak_function__;
}

/// Checks whether a value is a valid Catspeak function bound using
/// `catspeak_method`.
///
/// @param {Any} value
/// The value to check is a Catspeak method.
///
/// @return {Bool}
function is_catspeak_method(value) {
return is_method(value) && method_get_index(value) == __catspeak_function_method__;
}

/// Used by Catspeak code generators to expose foreign GML functions,
/// constants, and properties to the generated Catspeak programs.
function CatspeakForeignInterface() constructor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,10 @@ function catspeak_special_to_struct(gmlSpecial) {
/// The result of evaluating the `callee` function.
function catspeak_execute(callee) {
static args = [];
for (var i = argument_count; i >= 0; i -= 1) {
args[@ i] = argument[i];
for (var i = argument_count; i >= 1; i -= 1) {
args[@ i - 1] = argument[i];
}
return catspeak_execute_ext(callee, self, args);
return catspeak_execute_ext(callee, self, args, 0, argument_count - 1);
}

#macro __CATSPEAK_BEGIN_SELF \
Expand Down Expand Up @@ -603,9 +603,38 @@ function catspeak_globals(callee) {
if (is_catspeak(callee)) {
return callee.getGlobals();
}
if (is_catspeak_method(callee)) {
// TODO
}
return undefined;
}

function catspeak_method(self_, callee) {
if (is_catspeak(callee)) {
return method({
callee : callee,
self_ : self_,
}, __catspeak_function_method__);
}
if (is_catspeak_method(callee)) {
var methodData = method_get_self(callee);
return method({
callee : methodData.callee,
self_ : self_,
}, __catspeak_function_method__);
}
return method(self_, callee);
}

/// @ignore
function __catspeak_function_method__() {
static args = [];
for (var i = argument_count; i >= 0; i -= 1) {
args[@ i] = argument[i];
}
return catspeak_execute_ext(callee, self_, args, 0, argument_count);
}

/// @ignore
function __catspeak_init_engine() {
// initialise the default Catspeak env
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ test_add(function () : Test("env-function-set-self") constructor {
assertEq("hi", result.hi);
});

test_add(function () : Test("env-function-method") constructor {
var env = new CatspeakEnvironment();
var f = env.compile(env.parseString(@'
return fun { self };
'));
var fun = catspeak_method({ bye : "bye" }, f());
var result = catspeak_execute_ext(fun, { bye : "sike!" });
assertTypeof(result, "struct");
assertEq("bye", result.bye);
});

test_add(function () : Test("env-function-method-2") constructor {
var env = new CatspeakEnvironment();
var f = env.compile(env.parseString(@'
return fun { self };
'));
var fun = catspeak_method({ bye : "bye" }, f());
var result = fun();
assertTypeof(result, "struct");
assertEq("bye", result.bye);
});

function EngineFunctionMethodCallTest__Construct() constructor {
// this is part of the below test, but needs to live in global scope
// otherwise the name will be mangled
Expand Down

0 comments on commit 9eb8e2f

Please sign in to comment.