当前位置: 首页 > 面试题库 >

脚本尝试创建全局变量

东门晟
2023-03-14
问题内容

我想将一个脚本加载到redis中,该脚本将导出将来执行的脚本将依赖的函数,但是尝试定义全局函数失败,对于全局变量也是如此:

redis 127.0.0.1:6379> EVAL "function alex() return 3.1415 end" 0
(error) ERR Error running script (call to f_f24a5a054d91ccc74c2629e113f8f639bbedbfa2): user_script:1: Script attempted to create global variable 'alex'

如何定义全局函数和变量?


问题答案:

查看文件scripting.c中的源代码

/* This function installs metamethods in the global table _G that prevent
 * the creation of globals accidentally.
 *
 * It should be the last to be called in the scripting engine initialization
 * sequence, because it may interact with creation of globals. */
void scriptingEnableGlobalsProtection(lua_State *lua) {
    char *s[32];
    sds code = sdsempty();
    int j = 0;

    /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html.
     * Modified to be adapted to Redis. */
    s[j++]="local mt = {}\n";
    s[j++]="setmetatable(_G, mt)\n";
    s[j++]="mt.__newindex = function (t, n, v)\n";
    s[j++]="  if debug.getinfo(2) then\n";
    s[j++]="    local w = debug.getinfo(2, \"S\").what\n";
    s[j++]="    if w ~= \"main\" and w ~= \"C\" then\n";
    s[j++]="      error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n";
    s[j++]="    end\n";
    s[j++]="  end\n";
    s[j++]="  rawset(t, n, v)\n";
    s[j++]="end\n";
    s[j++]="mt.__index = function (t, n)\n";
    s[j++]="  if debug.getinfo(2) and debug.getinfo(2, \"S\").what ~= \"C\" then\n";
    s[j++]="    error(\"Script attempted to access unexisting global variable '\"..tostring(n)..\"'\", 2)\n";
    s[j++]="  end\n";
    s[j++]="  return rawget(t, n)\n";
    s[j++]="end\n";
    s[j++]=NULL;

    for (j = 0; s[j] != NULL; j++) code = sdscatlen(code,s[j],strlen(s[j]));
    luaL_loadbuffer(lua,code,sdslen(code),"@enable_strict_lua");
    lua_pcall(lua,0,0,0);
    sdsfree(code);
}

的doc字符串scriptingEnableGlobalsProtection表示意图是将常见错误通知脚本作者(不使用local)。

看来这不是安全功能,所以我们有两个解决方案:

可以删除此保护:

local mt = setmetatable(_G, nil)
-- define global functions / variables
function alex() return 3.1415 end
-- return globals protection mechanizm
setmetatable(_G, mt)

或使用rawset

local function alex() return 3.1415 end
rawset(_G, "alex", alex)


 类似资料:
  • 我得到一个编译错误的代码: 我想让“match”成为一个全局变量。 我的编译错误是: “内部类TestingProgramm中的静态声明非法。匹配修饰符”static“只允许在常量变量声明中使用 在初始化期间使用静态非final变量。“ 我不知道这个错误是什么意思,也不知道如何修复它。

  • 问题内容: 我有一个全局变量,需要在我的ViewController之间共享。 在Objective-C中,我可以定义静态变量,但是找不到在Swift中定义全局变量的方法。 您知道这样做的方法吗? 问题答案: 来自官方的Swift编程指南: 全局变量是在任何函数,方法,闭包或类型上下文之外定义的变量。全局常数和变量总是延迟计算的。 您可以在任何文件中定义它,也可以在任何位置访问它。因此,您可以在任

  • 在JavaScript中,我可以这样做: 然后在另一个文件中: 它将定义

  • 问题内容: 在我的android应用程序中,我需要放置可变成员id的位置。问题是,它是从在线API获取的,我需要找到一种存储/检索它的方法 我尝试将其放在自定义类中,但是问题是,如果我取消活动,它将丢失,我还知道有一种方法可以扩展应用程序。 所以我想知道存储全局变量的最佳方法是什么? 我必须实现: 将变量保存在onSaveState上 将其保存在sharepref 手动保存 手动检索 谢谢 更新:

  • 问题内容: 我试图设置一个全局变量。就我而言,只是一个布尔标志,用于指示是否首次显示视图: 呈现视图后,我想将此标志设置为false: 然后检查它: 因此,我想创建一个全局变量。在哪里以及如何?我试过了: 在我的视图控制器领域 在我的AppDelegate.swift文件中的方法中 在班上 没运气。我收到一条错误消息 (注意:我意识到,在这个问题中,我背叛了我对Swift中如何处理范围的无知。请原

  • 问题内容: 有没有一种方法可以从字符串创建全局变量?我知道您可以像这样从字符串中创建变量: 因此,使hello变量等于10。我不知道如何使该用户输入变量成为全局变量,但这不起作用: 问题答案: 您可以使用以下功能: