<commit_before>/*
   Copyright 2006 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.szegedi.spring.web.jsflow;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.continuations.Continuation;
import org.springframework.web.servlet.ModelAndView;

/**
 * An internal host object used to implement built-in functions for scripts. You
 * should never use it directly. It is public only as an implementation detail.
 * @author <NAME>
 * @version $Id$
 */
public class HostObject extends ScriptableObject
{
    public static final String CLASS_NAME = "HostObject";

    private static final long serialVersionUID = 1L;
    private ScriptStorage scriptStorage;
    private Continuation continuation;
    private Scriptable model;
    private String viewName;
    private int includeLevel;
    
    public HostObject()
    {
        scriptStorage = null;
    }
    
    void setScriptStorage(ScriptStorage scriptStorage)
    {
        this.scriptStorage = scriptStorage;
    }
    
    public String getClassName()
    {
        return CLASS_NAME;
    }
    
    public void jsFunction_inspect(Scriptable obj)
    {
        System.out.println(obj);
    }
    
    public void jsFunction_respond(String viewName, Scriptable model)
    {
        this.viewName = viewName == "undefined" ? null : viewName;
        this.model = model;
    }

    public void jsFunction_wait(Continuation continuation)
    {
        if(includeLevel > 0)
        {
            throw new IllegalStateException("Cannot wait from include");
        }
        this.continuation = continuation;
    }

    public boolean jsGet_hasContinuation()
    {
        return continuation != null;
    }

    ModelAndView getModelAndView(Object continuationId)
    {
        if(model != null)
        {
            model.put(FlowController.STATEID_KEY, model, continuationId);
        }
        return viewName == null ? null : new ModelAndView(viewName, 
                new ScriptableMap(model));
    }
    
    Continuation getContinuation()
    {
        return continuation;
    }
    
    public void jsFunction_include(Scriptable scope, String scriptName)
    throws Exception
    {
        Context cx = Context.getCurrentContext();
        Script script = scriptStorage.getScript(scriptName);
        ++includeLevel;
        try
        {
            script.exec(cx, scope);
        }
        finally
        {
            --includeLevel;
        }
    }
}
<commit_msg>throw a FileNotFoundException when script is not found<commit_after>/*
   Copyright 2006 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.szegedi.spring.web.jsflow;

import java.io.FileNotFoundException;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.continuations.Continuation;
import org.springframework.web.servlet.ModelAndView;

/**
 * An internal host object used to implement built-in functions for scripts. You
 * should never use it directly. It is public only as an implementation detail.
 * @author <NAME>
 * @version $Id$
 */
public class HostObject extends ScriptableObject
{
    public static final String CLASS_NAME = "HostObject";

    private static final long serialVersionUID = 1L;
    private ScriptStorage scriptStorage;
    private Continuation continuation;
    private Scriptable model;
    private String viewName;
    private int includeLevel;
    
    public HostObject()
    {
        scriptStorage = null;
    }
    
    void setScriptStorage(ScriptStorage scriptStorage)
    {
        this.scriptStorage = scriptStorage;
    }
    
    public String getClassName()
    {
        return CLASS_NAME;
    }
    
    public void jsFunction_inspect(Scriptable obj)
    {
        System.out.println(obj);
    }
    
    public void jsFunction_respond(String viewName, Scriptable model)
    {
        this.viewName = viewName == "undefined" ? null : viewName;
        this.model = model;
    }

    public void jsFunction_wait(Continuation continuation)
    {
        if(includeLevel > 0)
        {
            throw new IllegalStateException("Cannot wait from include");
        }
        this.continuation = continuation;
    }

    public boolean jsGet_hasContinuation()
    {
        return continuation != null;
    }

    ModelAndView getModelAndView(Object continuationId)
    {
        if(model != null)
        {
            model.put(FlowController.STATEID_KEY, model, continuationId);
        }
        return viewName == null ? null : new ModelAndView(viewName, 
                new ScriptableMap(model));
    }
    
    Continuation getContinuation()
    {
        return continuation;
    }
    
    public void jsFunction_include(Scriptable scope, String scriptName)
    throws Exception
    {
        Context cx = Context.getCurrentContext();
        Script script = scriptStorage.getScript(scriptName);
        if(script == null)
        {
            throw new FileNotFoundException("script:" + scriptName);
        }
        ++includeLevel;
        try
        {
            script.exec(cx, scope);
        }
        finally
        {
            --includeLevel;
        }
    }
}

{'fraction_non_alphanumeric': 0.07058823529411765, 'fraction_numerical': 0.0030165912518853697, 'mean_word_length': 2.297364495275982, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2144065', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1661, 'n_words': 513}