我正在编写一个脚本testmodule.lua并想检查这个文件是从另一个脚本导入/需要的还是由lua testmodule.lua.
如果直接通过命令行启动,我可以做一些测试或运行一个主函数,否则只导出一些函数而不做任何事情。
Python 有一个__name__声明:
if __name__ == '__main__':
main_entry()
Run Code Online (Sandbox Code Playgroud)
lua中有类似的东西吗?
在单个文件中编写一个 shell util 很有用,它可以直接运行并由其他 lua 脚本导入。但是当一些脚本导入这个文件时,我不喜欢调用 main 函数。
我编写了一个非常简单的程序,它在没有-O2以下情况下正常运行:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint32_t A[4] = { 1, 2, 3, 4 };
float B[4] = { 0, 0, 0, 0 };
float C[4] = { 5, 6, 7, 8 };
int i;
// convert integer A to float B
for (i = 0; i < 4; i++)
B[i] = (float)A[i];
// memory copy from B to C
uint32_t *src = (uint32_t*)(B);
uint32_t *dst = (uint32_t*)(C);
dst[0] = src[0];
dst[1] = src[1];
dst[2] = …Run Code Online (Sandbox Code Playgroud)