Update javascript executor to work w/ compiled ts scripts that export a main function; add output to javalin storeRecordAssociatedScript

This commit is contained in:
2022-11-09 09:49:58 -06:00
parent 2975b90505
commit 955294ae18
3 changed files with 42 additions and 2 deletions

View File

@ -80,11 +80,20 @@ public class QJavaScriptExecutor implements QCodeExecutor
////////////////////////////////////////////////////////////////////////
// wrap the user's code in an immediately-invoked function expression //
// if the user's code (%s below) returns - then our IIFE is done. //
// if the user's code doesn't return, but instead created a 'script' //
// variable, with a 'main' function on it (e.g., from a compiled //
// type script file), then call main function and return its result. //
////////////////////////////////////////////////////////////////////////
String codeToRun = """
(function userDefinedFunction()
{
%s
%s
if(script && script.main)
{
return (script.main());
}
})();
""".formatted(code);

View File

@ -100,6 +100,34 @@ class ExecuteCodeActionTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testCompiledFromTypeScript() throws QException
{
OneTestOutput oneTestOutput = testOne(4, """
var script = (function (exports) {
function cooling() {
output.setD(7);
return (output);
}
cooling();
exports.cooling = cooling;
return exports;
})({});
""");
assertEquals(7, oneTestOutput.testOutput().getD());
assertTrue(oneTestOutput.executeCodeOutput().getOutput() instanceof TestOutput);
assertEquals(7, ((TestOutput) oneTestOutput.executeCodeOutput().getOutput()).getD());
}
/*******************************************************************************
**
*******************************************************************************/