<commit_before>package sweethome

import grails.plugins.quartz.TriggerDescriptor
import grails.plugins.quartz.TriggerUtils
import grails.transaction.Transactional
import org.quartz.CronTrigger
import org.quartz.SimpleTrigger
import org.quartz.Trigger
import org.quartz.impl.triggers.AbstractTrigger
import sweethome.scripting.JsHome
import sweethome.scripting.JsLocation
import sweethome.scripting.JsLog
import sweethome.scripting.JsScript

import javax.script.Bindings
import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.ScriptException
import javax.script.CompiledScript;
import javax.script.Compilable;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

@Transactional
class ScriptingService {

    def quartzScheduler
    def jobManagerService
    def brokerMessagingTemplate

    def exec(Script script) {
        //ScriptEngineManager engineManager = new ScriptEngineManager()
        //ScriptEngine engine = engineManager.getEngineByName("nashorn")

        NashornScriptEngineFactory factory = new NashornScriptEngineFactory()
        //ScriptEngine engine = factory.getScriptEngine("-strict", "--no-java", "--no-syntax-extensions");
        ScriptEngine engine = factory.getScriptEngine()

        //Bindings binding = engine.getBindings(ScriptContext.ENGINE_SCOPE)
        Bindings binding = engine.createBindings()

        JsLog jsLog = new JsLog(brokerMessagingTemplate, script.id)

        JsHome jsHome = new JsHome()
        Location.list().each {
            jsHome.put it.name, new JsLocation(it, jsHome)
        }
        binding.put "home", jsHome
        binding.put "log", jsLog
        binding.put "console", jsLog
        binding.put "script", new JsScript()

        try {
            //engine.eval(script.code)

            // instead of eval - just compile and eval
            final CompiledScript compiledScript = ((Compilable)engine).compile(script.code)
            compiledScript.eval(binding)
        } catch (ScriptException ex) {
            jsLog.log("error", null, ex.message)
            throw ex
        } catch(Exception e){
            jsLog.log("error", null, ex)
            throw e
        } finally {
            jsHome.close()
        }<commit_msg>code was little bit optimized<commit_after>package sweethome

import grails.plugins.quartz.TriggerDescriptor
import grails.plugins.quartz.TriggerUtils
import grails.transaction.Transactional
import org.quartz.CronTrigger
import org.quartz.SimpleTrigger
import org.quartz.Trigger
import org.quartz.impl.triggers.AbstractTrigger
import sweethome.scripting.JsHome
import sweethome.scripting.JsLocation
import sweethome.scripting.JsLog
import sweethome.scripting.JsScript

import javax.annotation.PostConstruct
import javax.script.Bindings
import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.ScriptException
import javax.script.CompiledScript;
import javax.script.Compilable;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;

@Transactional
class ScriptingService {

    private ScriptEngine engine

    def quartzScheduler
    def jobManagerService
    def brokerMessagingTemplate

    @PostConstruct
    def init() {
        //ScriptEngineManager engineManager = new ScriptEngineManager()
        //ScriptEngine engine = engineManager.getEngineByName("nashorn")

        NashornScriptEngineFactory factory = new NashornScriptEngineFactory()
        //ScriptEngine engine = factory.getScriptEngine("-strict", "--no-java", "--no-syntax-extensions");
        engine = factory.getScriptEngine()
    }

    def exec(Script script) {

        //Bindings binding = engine.getBindings(ScriptContext.ENGINE_SCOPE)
        Bindings binding = engine.createBindings()

        JsLog jsLog = new JsLog(brokerMessagingTemplate, script.id)

        JsHome jsHome = new JsHome()
        Location.list().each {
            jsHome.put it.name, new JsLocation(it, jsHome)
        }
        binding.put "home", jsHome
        binding.put "log", jsLog
        binding.put "console", jsLog
        binding.put "script", new JsScript()

        try {
            //engine.eval(script.code)

            // instead of eval - just compile and eval
            final CompiledScript compiledScript = ((Compilable)engine).compile(script.code)
            compiledScript.eval(binding)
        } catch (ScriptException ex) {
            jsLog.log("error", null, ex.message)
            throw ex
        } catch(Exception e){
            jsLog.log("error", null, ex)
            throw e
        } finally {
            jsHome.close()
        }
{'fraction_non_alphanumeric': 0.08616634594825744, 'fraction_numerical': 0.0, 'mean_word_length': 3.096322241681261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7316155', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1251, 'n_words': 283}