<commit_before>/**
 * Continuation support for Helma. You need to run Rhino in interpreter mode
 * (rhino.optlevel = -1) for Continuations to work.
 *
 * This framework adds three static methods to the Continuation constructor:
 *
 * <ul>
 *   <li>Continuation.nextUrl()</li>
 *   <li>Continuation.nextPage()</li>
 * </ul>
 *
 * Example usage:
 *
 * <pre><code>
 *    function continuation_action() {
 *        res.write('<form method="post" action="' + Continuation.nextUrl() + '">\
 *                    <input name="foo"/>\
 *                    <input type="submit"/>\
 *                   </form>');
 *        Continuation.nextPage();
 *        var foo = req.data.foo;
 *        res.write('<a href="' + Continuation.nextUrl() + '">click here</a>');
 *        Continuation.nextPage();
 *        res.write("you said: " + foo);
 *    }
 * </code></pre>
 *
 */

import('helma.system', 'system');

export('resume');


var log = require('helma.logging').getLogger(__name__);
var continuation_id = null;


function onRequest(req) {
    if (req.params.helma_continuation) {
        system.setRhinoOptimizationLevel(-1);
    }
}


/**
 * Get the id for the next continuation, suitable for GET forms where
 * the id has to be set via hidden input field.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation url
 */
Continuation.nextId = function(req, id) {
    id = getId(req, id);
    continuation_id = id;
    return continuation_id;
};

/**
 * Convenience method that returns the URL for the next continuation,
 * built from the current URL with an added continuation_id parameter.
 * Suitable for POST forms and links.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation url
 */
Continuation.nextUrl = function(req, id) {
    id = getId(req, id);
    continuation_id = id;
    return Continuation.getUrl(req, id);
};

/**
 * Get the url for a continuation with the given id.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation url
 */
Continuation.getUrl = function(req, id) {
   return req.path + "?helma_continuation=" + getId(req, id);
}

/**
 * Stop current execution and register continuation for later resumption.
 * @param id the continuation id. If not given a new id is generated.
 */
Continuation.nextPage = function(req, id) {
    // capture continuation and store it in callback container
    id = getId(req, id);
    setCallback(req, id, new Continuation());
    // trick to exit current context: call empty continuation
    new org.mozilla.javascript.NativeContinuation()();
};

Continuation.startId = function(req, id) {
    id = req.data.helma_continuation || id;
    return id;    
}

/**
 * This is a utility method used at the start of a continuation action.
 * If the current request does not have a continuation id, it is redirected
 * to a request containing one. If the request does have a continuation id,
 * the current state is registered as continuation start marker. This can be
 * used to avoid re-executing earlier code containing definition of local
 * variables.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation result
 */
Continuation.markStart = function(req, res, id) {
    if (!req.data.helma_continuation) {
        // set query param so helma knows to switch rhino optimization level to -1
        res.redirect(Continuation.nextUrl(req, id));
    } else {
        id = req.data.helma_continuation;
        var cont = new Continuation();
        log.info("Recording continuation start: " + id);
        setCallback(req, id, cont);
        continuation_id = null;
        cont([req, res]);
    }
}

/**
 * Register current state with the given id but don't exit execution context<commit_msg>Make helma.webapp.continuation work with recent changes in Helma NG, replacing req.data with req.params.<commit_after>/**
 * Continuation support for Helma. You need to run Rhino in interpreter mode
 * (rhino.optlevel = -1) for Continuations to work.
 *
 * This framework adds three static methods to the Continuation constructor:
 *
 * <ul>
 *   <li>Continuation.nextUrl()</li>
 *   <li>Continuation.nextPage()</li>
 * </ul>
 *
 * Example usage:
 *
 * <pre><code>
 *    function continuation_action() {
 *        res.write('<form method="post" action="' + Continuation.nextUrl() + '">\
 *                    <input name="foo"/>\
 *                    <input type="submit"/>\
 *                   </form>');
 *        Continuation.nextPage();
 *        var foo = req.params.foo;
 *        res.write('<a href="' + Continuation.nextUrl() + '">click here</a>');
 *        Continuation.nextPage();
 *        res.write("you said: " + foo);
 *    }
 * </code></pre>
 *
 */

import('helma.system', 'system');

export('resume');


var log = require('helma.logging').getLogger(__name__);
var continuation_id = null;


function onRequest(req) {
    if (req.params.helma_continuation) {
        system.setRhinoOptimizationLevel(-1);
    }
}


/**
 * Get the id for the next continuation, suitable for GET forms where
 * the id has to be set via hidden input field.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation url
 */
Continuation.nextId = function(req, id) {
    id = getId(req, id);
    continuation_id = id;
    return continuation_id;
};

/**
 * Convenience method that returns the URL for the next continuation,
 * built from the current URL with an added continuation_id parameter.
 * Suitable for POST forms and links.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation url
 */
Continuation.nextUrl = function(req, id) {
    id = getId(req, id);
    continuation_id = id;
    return Continuation.getUrl(req, id);
};

/**
 * Get the url for a continuation with the given id.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation url
 */
Continuation.getUrl = function(req, id) {
   return req.path + "?helma_continuation=" + getId(req, id);
}

/**
 * Stop current execution and register continuation for later resumption.
 * @param id the continuation id. If not given a new id is generated.
 */
Continuation.nextPage = function(req, id) {
    // capture continuation and store it in callback container
    id = getId(req, id);
    setCallback(req, id, new Continuation());
    // trick to exit current context: call empty continuation
    new org.mozilla.javascript.NativeContinuation()();
};

Continuation.startId = function(req, id) {
    id = req.params.helma_continuation || id;
    return id;    
}

/**
 * This is a utility method used at the start of a continuation action.
 * If the current request does not have a continuation id, it is redirected
 * to a request containing one. If the request does have a continuation id,
 * the current state is registered as continuation start marker. This can be
 * used to avoid re-executing earlier code containing definition of local
 * variables.
 * @param id the continuation id. If not given a new id is generated.
 * @return the continuation result
 */
Continuation.markStart = function(req, res, id) {
    if (!req.params.helma_continuation) {
        // set query param so helma knows to switch rhino optimization level to -1
        res.redirect(Continuation.nextUrl(req, id));
    } else {
        id = req.params.helma_continuation;
        var cont = new Continuation();
        log.info("Recording continuation start: " + id);
        setCallback(req, id, cont);
        continuation_id = null;
        cont([req, res]);
    }
}

/**
 * Register current state with the given id but don't exit execution context
{'fraction_non_alphanumeric': 0.11524019163537486, 'fraction_numerical': 0.0007769001683283698, 'mean_word_length': 3.0738396624472575, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1064537', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2181, 'n_words': 813}