<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Noesis.Javascript;
using ConsoleLib.Console;

namespace ConsoleLib.Console
{
    public class JavascriptConsoleProcessor : IConsoleProcessor
    {
        protected JavascriptContext _jsContext;
        protected List<RegisteredConsoleObject> _registeredConsoleObjects { get; set; }

        public JavascriptConsoleProcessor()
        {
            _jsContext = new JavascriptContext();
            _registeredConsoleObjects = new List<RegisteredConsoleObject>();
        }

        public string ProcessInput(string input)
        {
            object result;
            try { result = _jsContext.Run(input); }
            catch (JavascriptException ex)
            {
                return ex.Message;
            }

            if (result != null)
                return result.ToString();

            return string.Empty;
        }

        public void RegisterObject(string name, object obj)
        {
            _jsContext.SetParameter(name, obj);
        }

        public void UnregisterObject(string name, object obj)
        {
            _jsContext.SetParameter(name, null);
        }
    }
}<commit_msg>Updated javascript processor to lessen instability with multile threads (V8 engine doesn't support multithreading)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Noesis.Javascript;
using ConsoleLib.Console;

namespace ConsoleLib.Console
{
    public class JavascriptConsoleProcessor : IConsoleProcessor
    {
        protected JavascriptContext _jsContext;
        protected List<RegisteredConsoleObject> _registeredConsoleObjects { get; set; }
        protected object _lockObject = new object();

        public JavascriptConsoleProcessor()
        {
            _jsContext = new JavascriptContext();
            _registeredConsoleObjects = new List<RegisteredConsoleObject>();
        }

        public string ProcessInput(string input)
        {
            object result;
            try 
            { 
                // V8 engine is unstable if accessed simultaneously 
                lock (_lockObject)
                {
                    result = _jsContext.Run(input);
                }
            }
            catch (JavascriptException ex)
            {
                return ex.Message;
            }

            if (result != null)
                return result.ToString();

            return string.Empty;
        }

        public void RegisterObject(string name, object obj)
        {
            _jsContext.SetParameter(name, obj);
        }

        public void UnregisterObject(string name, object obj)
        {
            _jsContext.SetParameter(name, null);
        }
    }
}
{'fraction_non_alphanumeric': 0.06979241231209735, 'fraction_numerical': 0.0007158196134574087, 'mean_word_length': 1.9267015706806283, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1774327', 'n_tokens_mistral': 691, 'n_tokens_neox': 636, 'n_words': 169}