Update placeholder and validation

This commit is contained in:
2026-01-08 13:32:30 +01:00
parent c7ebada781
commit 6e29256ee9
4 changed files with 38 additions and 6 deletions

View File

@@ -0,0 +1,2 @@
function spawnUserJavaScriptWorker(jsSource, data) {
}

View File

@@ -43,12 +43,42 @@ function getUserJavaScriptSourceError(src) {
return { title: "Syntax Error", details: e.message }; return { title: "Syntax Error", details: e.message };
} }
const functionNames = ast.body.filter((node) => node.type === "FunctionDeclaration").map((node) => node.id.name); let hasInit = false;
if (!functionNames.includes("init")) { let hasTick = false;
return { title: "Missing function: init", details: "Your code must include a function named 'init'." };
for (const node of ast.body) {
if (node.type !== "ExpressionStatement") continue;
const expr = node.expression;
if (expr.type !== "AssignmentExpression") continue;
const left = expr.left;
const right = expr.right;
if (
left.type === "MemberExpression" &&
left.object.type === "Identifier" &&
left.object.name === "exports" &&
left.property.type === "Identifier" &&
(right.type === "FunctionExpression" || right.type === "ArrowFunctionExpression")
) {
if (left.property.name === "init") hasInit = true;
if (left.property.name === "tick") hasTick = true;
} }
if (!functionNames.includes("tick")) { }
return { title: "Missing function: tick", details: "Your code must include a function named 'tick'." };
if (!hasInit) {
return {
title: "Missing function: init",
details: "You must assign a function to exports.init",
};
}
if (!hasTick) {
return {
title: "Missing function: tick",
details: "You must assign a function to exports.tick",
};
} }
return undefined; return undefined;

View File

@@ -392,7 +392,7 @@
<textarea <textarea
class="text-input" class="text-input"
id="custom-asset-code" id="custom-asset-code"
placeholder="function init({ surface, assets, channel }) {&#10;&#10;}&#10;&#10;function tick() {&#10;&#10;}" placeholder="exports.init = ({ surface, assets, channel }) => {&#10;&#10;};&#10;&#10;exports.tick = () => {&#10;&#10;};"
rows="25" rows="25"
required required
></textarea> ></textarea>