pluto-lang / pluto

Pluto provides a unified programming interface that allows you to seamlessly tap into cloud capabilities and develop your cloud and AI applications.
https://pluto-lang.vercel.app
Apache License 2.0
86 stars 8 forks source link

Call a function from a lambda function #37

Open jianzs opened 11 months ago

jianzs commented 11 months ago

Currently, the Pluto deducer wraps each function defined in the arguments of the cloud resource handler function and treats it as a lambda resource. However, if a user tries to call another function, it will result in failure. This restriction hinders code reuse through functions.

import { CloudEvent, Queue } from "@plutolang/pluto";

const queue = new Queue("queue");

// This will not be included in the compilation result.
function anotherFunc() {
  console.log("Another");
}

queue.subscribe(async (evt: CloudEvent) => {
  // This is one lambda function.
  anotherFunc();
});

The code provided above is not functioning correctly. To address this issue, we need to incorporate function dependency analysis into the deduction process. Furthermore, we should record this dependency information in the architecture reference, such as the resource's location. Subsequently, when generating the compute module code, we will merge all the required functions together.

The other dependencies, such as classes, interfaces, and types, can be treated the same way.

jianzs commented 10 months ago

After merging #75, developers can now call functions that are located outside of the handler function. However, complex cases are not supported, such as calling a renamed function or a function variable. These cases are related to accessing variables #38

function test(): string {
  return "hello";
}

const aliasTest = test;

const fnVar = (): string => {
  return "world";
}

router.get("/", async(req: HttpRequest): HttpResponse => {
  test(); // valid
  aliasTest(); //  error
  fnVar(); // error
});