Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对LuaException进行定义:lua代码和error方法抛出的异常才是LuaException #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

flysion
Copy link

@flysion flysion commented Nov 24, 2021

当前的lua扩展有一个很大的问题:把一个PHP方法registerCallback给lua之后,如果lua调用这个方法出现php的异常,lua的代码还是会继续往下走,示例:

$lua->registerCallback("test", function() {
    throw new \Exception("test");
});

$lua->eval(<<<LUA
test() -- php 抛出了异常
print("Hello php.") -- 代码继续走到了这里
LUA
);

以上是题外话,但是和这个改进相关。当lua侧代码出现了问题需要终止的时候,只有一种终止方法,那就是error方法。php侧需要捕获这个异常做一些特殊处理的时候只需要判断 $e instanceof \LuaException。其次 php 需要得到 error 方法的参数,示例:

try {
    $lua->eval("error({code=1,message='test'})");
} catch(\LuaException $e) {
    var_dump($e->err);
}

当前的lua扩展有一个很大的问题:把一个PHP方法registerCallback给lua之后,如果lua调用这个方法出现php的异常,lua的代码还是会继续往下走,示例:
```
$lua->registerCallback("test", function() {
    throw new \Exception("test");
});

$lua->eval(<<<LUA
test() -- php 抛出了异常
print("Hello php.") -- 代码继续走到了这里
LUA
);
```

---

以上是题外话,但是和这个改进相关。当lua侧代码出现了问题需要终止的时候,只有一种终止方法,那就是error方法。php侧需要捕获这个异常做一些特殊处理的时候只需要判断 `$e instanceof \LuaException`。其次 php 需要得到 error 方法的参数,示例:
```
try {
    $lua->eval("error({code=1,message='test'})");
} catch(\LuaException $e) {
    var_dump($e->err);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant