<commit_before>// This file is part of the VroomJs library.
//
// Author:
//     <NAME> <<EMAIL>>
//
// Copyright © 2013 <NAME> <<EMAIL>>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;

namespace VroomJs
{
	public class JsEngine : IDisposable
	{
        [DllImport("vroomjs")]
        static extern IntPtr jsengine_new(
            Delegate keepaliveRemove,
            Delegate keepaliveGetPropertyValue,
            Delegate keepaliveSetPropertyValue
        );

        [DllImport("vroomjs")]
        static extern void jsengine_dispose(HandleRef engine);

        [DllImport("vroomjs")]
        static extern JsValue jsengine_execute(HandleRef engine, [MarshalAs(UnmanagedType.LPWStr)] string str);

        [DllImport("vroomjs")]
        static extern JsValue jsengine_get_value(HandleRef engine, [MarshalAs(UnmanagedType.LPWStr)] string name);

        [DllImport("vroomjs")]
        static extern void jsengine_set_value(HandleRef engine, [MarshalAs(UnmanagedType.LPWStr)] string name, JsValue value);

        [DllImport("vroomjs")]
        static extern JsValue jsvalue_alloc_string([MarshalAs(UnmanagedType.LPWStr)] string str);

        [DllImport("vroomjs")]
        static extern void jsvalue_dispose(JsValue value);

        public JsEngine()
		{
            _keepalives = new Dictionary<int,object>();
            _keepalive_remove = new Action<int>(KeepAliveRemove);
            _keepalive_get_property_value = new Func<int,string,JsValue>(KeepAliveGetPropertyValue);
            _keepalive_set_property_value = new Func<int,string,JsValue,JsValue>(KeepAliveSetPropertyValue);

            _engine = new HandleRef(this, jsengine_new(_keepalive_remove, _keepalive_get_property_value, _keepalive_set_property_value));
		}

        HandleRef _engine;
        Dictionary<int,object> _keepalives;
        int _keepalives_count;

        // Make sure the delegates we pass to the C++ engine won't fly away during a GC.
        Delegate _keepalive_remove;
        Delegate _keepalive_get_property_value;
        Delegate _keepalive_set_property_value;

        public object Execute(string code)
        {
            if (code == null)
                throw new ArgumentNullException("code");

            CheckDisposed();

            JsValue v = jsengine_execute(_engine, code);
            object res = JsValueToObject(v);
            jsvalue_dispose(v);

            Exception e = res as Exception;
            if (e != null)
                throw e;
            return res;
        }

        public object GetValue(string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            CheckDisposed();

            JsValue v = jsengine_get_value(_engine, name);
            object res = JsValueToObject(v);
            jsvalue_dispose(v);

            Exception e = res as Exception;
            if (e != null)
                throw e;
            return res;
        }

        public void SetValue(string name, object value)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            CheckDisposed();

            jsengine_set_value(_engine, name, ObjectToJsValue(value));
        }

        int KeepAliveSet(object obj)
        {
            _keepalives.Add(_keepalives_count, obj);
            return _keepalives_count++;
        }

        object KeepAliveGet(int slot)
        {
            object obj;
            if (_keepalives.TryGetValue(slot, out obj))
                return obj;
            return null;
        }

        void KeepAliveRemove(int slot)
        {
            var obj = KeepAliveGet(slot);<commit_msg>[interop] Better names for some methods<commit_after>// This file is part of the VroomJs library.
//
// Author:
//     <NAME> <<EMAIL>>
//
// Copyright © 2013 <NAME> <<EMAIL>>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;

namespace VroomJs
{
	public class JsEngine : IDisposable
	{
        [DllImport("vroomjs")]
        static extern IntPtr jsengine_new(
            Delegate keepaliveRemove,
            Delegate keepaliveGetPropertyValue,
            Delegate keepaliveSetPropertyValue
        );

        [DllImport("vroomjs")]
        static extern void jsengine_dispose(HandleRef engine);

        [DllImport("vroomjs")]
        static extern JsValue jsengine_execute(HandleRef engine, [MarshalAs(UnmanagedType.LPWStr)] string str);

        [DllImport("vroomjs")]
        static extern JsValue jsengine_get_variable(HandleRef engine, [MarshalAs(UnmanagedType.LPWStr)] string name);

        [DllImport("vroomjs")]
        static extern void jsengine_set_variable(HandleRef engine, [MarshalAs(UnmanagedType.LPWStr)] string name, JsValue value);

        [DllImport("vroomjs")]
        static extern JsValue jsvalue_alloc_string([MarshalAs(UnmanagedType.LPWStr)] string str);

        [DllImport("vroomjs")]
        static extern void jsvalue_dispose(JsValue value);

        public JsEngine()
		{
            _keepalives = new Dictionary<int,object>();
            _keepalive_remove = new Action<int>(KeepAliveRemove);
            _keepalive_get_property_value = new Func<int,string,JsValue>(KeepAliveGetPropertyValue);
            _keepalive_set_property_value = new Func<int,string,JsValue,JsValue>(KeepAliveSetPropertyValue);

            _engine = new HandleRef(this, jsengine_new(_keepalive_remove, _keepalive_get_property_value, _keepalive_set_property_value));
		}

        HandleRef _engine;
        Dictionary<int,object> _keepalives;
        int _keepalives_count;

        // Make sure the delegates we pass to the C++ engine won't fly away during a GC.
        Delegate _keepalive_remove;
        Delegate _keepalive_get_property_value;
        Delegate _keepalive_set_property_value;

        public dynamic Execute(string code)
        {
            if (code == null)
                throw new ArgumentNullException("code");

            CheckDisposed();

            JsValue v = jsengine_execute(_engine, code);
            object res = JsValueToObject(v);
            jsvalue_dispose(v);

            Exception e = res as Exception;
            if (e != null)
                throw e;
            return res;
        }

        public dynamic GetVariable(string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            CheckDisposed();

            JsValue v = jsengine_get_variable(_engine, name);
            object res = JsValueToObject(v);
            jsvalue_dispose(v);

            Exception e = res as Exception;
            if (e != null)
                throw e;
            return res;
        }

        public void SetVariable(string name, object value)
        {
            if (name == null)
                throw new ArgumentNullException("name");

            CheckDisposed();

            jsengine_set_variable(_engine, name, ObjectToJsValue(value));

            // TODO: Check the result of the operation for errors.
        }

        int KeepAliveSet(object obj)
        {
            _keepalives.Add(_keepalives_count, obj);
            return _keepalives_count++;
        }

        object KeepAliveGet(int slot)
        {
            object obj;
            if (_keepalives.TryGetValue(slot, out obj))
                return obj;
            return null;
        }

        void KeepAliveRemove(int slot)
        {
            var obj = KeepAliveGet(slot);
{'fraction_non_alphanumeric': 0.0802558283474314, 'fraction_numerical': 0.0008252527336496802, 'mean_word_length': 2.4823994252873565, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '4121598', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2527, 'n_words': 821}