2

如何创建不添加堆栈跟踪的发出宏?

import macros

macro emitWithoutStacktrace(): stmt = 
  result = newStmtList()
  result.add quote do:
    {.push stacktrace: off.}
  result.add(newNimNode(nnkPragma).add(newNimNode(nnkExprColonExpr).add(newIdentNode("emit"), newStrLitNode("foobar"))))
  result.add quote do:
    {.pop stacktrace: off.}

proc main = 
  emitWithoutStacktrace()

main()

生产

N_NIMCALL(void, main_104042)(void) {
    nimfr("main", "test.nim")
    nimln(7, "test.nim");
    foobar
    popFrame();
}

我更喜欢的地方

N_NIMCALL(void, main_104042)(void) {
    foobar
    popFrame();
}

没有任何额外的编译器参数,如--stacktrace: off.

4

1 回答 1

1

请注意,此代码也会发生同样的情况:

proc main =
  {.push stacktrace: off.}
  echo "hi"
  {.pop.}

main()

相反,您需要用 push/pop 包围整个过程:

{.push stacktrace: off.}
proc main =
  echo "hi"
{.pop.}

main()

我昨天在尝试做的时候才注意到这一点{.push rangechecks: on.}

于 2015-02-19T10:19:45.513 回答