如何在Rhino中调用continuations

How to call continuations in Rhino?

本文关键字:调用 continuations Rhino      更新时间:2023-09-26

我正试图在Rhino中使用continuation,但我发现如何使用它的指令不一。我想在JS中创建和使用continuation。

https://developer.mozilla.org/en-US/docs/New_in_Rhino_1.7R2#Java_API.C2.A0for_Continuations

展示了如何从Java中以处理异常的形式使用它们(我认为这是错误的看待方式)。

http://wiki.apache.org/cocoon/RhinoWithContinuations

显示了一种现在已被弃用的使用它们的方式——不再允许显式创建延续。

有人能澄清一下吗?我现在可以使用1.7R2发行说明中的4种方法,仅通过Java端操作,用Rhino创建JS延续吗?

这已经有一段时间了,但我已经解决了,所以最好写下来。

Rhino中的Continuation处理仅限于Java端-您可以通过运行一个支持Continuations的函数来进入JS,并在抛出ContinuationPending异常或调用引发该异常的Java代码时从Javascript代码返回Java。您可以返回Java代码中的catch。

这段代码开始执行一个支持Continuations的函数——进入Javascript。

try {
    openGlobalContext();
    _globalContext.callFunctionWithContinuations(_func, _scope,
        new Object[0]);
} catch (ContinuationPending pending) {
    _cont = pending;
} finally {
    closeGlobalContext();
}

这段Java代码,当被解释的Javascript调用时,会"离开"Javascript代码,到达引发延续异常的位置。也就是说,SomeMoreCode()将运行。

public void createContinuation(){
    try {
        openGlobalContext();
        ContinuationPending pending =
            _globalContext.captureContinuation();
        throw pending;
    } finally {
        closeGlobalContext();
    }
    SomeMoreCode();
}