0

ポストフックに関するトピックを 1 つ見つけましたが、それは私が達成したいことと同じではないと思います。 私が見つけたトピック

必要なものは次のとおりです。

local someBoolean = false
function doSomething() -- this is the function used in __index in a proxy table
  someBoolean = true
  return aFunction -- this is function that we can NOT alter
end

リターン後にコード「someBoolean = false」を実行できるようにする必要があります...(はい、それが起こるはずがないことはわかっています:p)aFunctionには他の関数自体が含まれている可能性があるため、someBooleanを全体でtrueにする必要がありますaFunction のスコープですが、その後は false に戻す必要があります

うまく説明できていなかったら申し訳ありません。私の実際のプロジェクトの関連コードをコピーして貼り付けるのは大きすぎるので、あなたの時間を無駄にしたくありません。私はしばらくこれに固執していましたが、それを理解できないようです...

(編集:関数は実際にはプロキシテーブルの __index 関数であるため、関数の後に someBoolean = false を置くことはできません)

編集:関連するコード。少しはっきりしているといいのですが

local function objectProxyDelegate(t, key)
  if not done then  -- done = true when our object is fully initialised
    return cls[key]   -- cls is the class, newinst is the new instance (duh...)
  end
  print("trying to delegate " .. key)
  if accessTable.public[key] then
    print(key .. " passed the test")
    objectScope = true
    if accessTable.static[key] then -- static function. return the static one
      return cls[key]   -- we need to somehow set objectScope back to false after this, otherwise we'll keep overriding protected/private functions
    else
      return newinst[key]
    end
  elseif objectScope then
    print("overridden protected/private")
    return cls[key]
  end
  if accessTable.private[key] then
    error ("This function is not visible. (private)", 2)
  elseif accessTable.protected[key] then
    error ("This function is not visible to an instance. (protected)", 2)
  else
    error ("The function " .. key .. " doesn't exiist in " .. newinst:getType(), 2)                      -- den deze...
  end
end
4

2 に答える 2

1

(関数を評価するのではなく) 関数を返す必要があるため、aFunction代わりに のプロキシを作成して返すことができます。これがどのように機能するかを次に示します (Nicol Bolas によるソリューションから取得した一連のコードを使用):

local someBoolean = false

function doSomething(...) -- this is the function used in __index in a proxy table
  someBoolean = true

  -- Return a proxy function instead of aFunction
  return function(...)
    local rets = { aFunction(...) }
    someBoolean = false
    return table.unpack(rets)
  end
end
于 2012-08-23T03:44:56.570 に答える
0

ステートメントのにコードを実行することはできません。return正解は、関数を呼び出し、戻り値をキャッチし、変数を設定してから、戻り値返すことです。例えば:

local someBoolean = false
function doSomething(...) -- this is the function used in __index in a proxy table
  someBoolean = true
  local rets = { aFunction(...) } -- this is function that we can NOT alter
  someBoolean = false
  return table.unpack(rets)
end
于 2012-08-23T00:27:18.783 に答える