• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6721074695

01 Nov 2023 02:37PM UTC coverage: 43.897% (+0.001%) from 43.896%
6721074695

push

github

luttje
expose input bag to scripts + explain it in docs

765 of 2403 branches covered (0.0%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 2 files covered. (100.0%)

3903 of 8231 relevant lines covered (47.42%)

14350.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Core/Key2Joy.Core/Mapping/Actions/Scripting/JavascriptScriptAction.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Threading.Tasks;
5
using Jint;
6
using Jint.Native;
7
using Jint.Native.Object;
8
using Jint.Runtime.Descriptors;
9
using Jint.Runtime.Interop;
10
using Key2Joy.Contracts;
11
using Key2Joy.Contracts.Mapping;
12
using Key2Joy.Contracts.Mapping.Actions;
13
using Key2Joy.Contracts.Mapping.Triggers;
14
using Key2Joy.Util;
15

16
namespace Key2Joy.Mapping.Actions.Scripting;
17

18
[Action(
19
    Description = "Javascript Action",
20
    NameFormat = "Javascript Script: {0}",
21
    GroupName = "Scripting",
22
    GroupImage = "script_code"
23
)]
24
public class JavascriptAction : BaseScriptActionWithEnvironment<Engine>
25
{
26
    public JavascriptAction(string name)
27
        : base(name) => this.ImageResource = "JS";
×
28

29
    public override async Task Execute(AbstractInputBag inputBag)
30
    {
31
        this.Environment.SetValue("INPUT", inputBag);
×
32

33
        try
34
        {
35
            lock (LockObject)
×
36
            {
37
                this.Environment.Execute(this.GetExecutableScript());
×
38
            }
×
39
        }
×
40
        catch (Jint.Runtime.JavaScriptException ex)
×
41
        {
42
            Output.WriteLine(ex);
×
43
        }
×
44

45
        await base.Execute(inputBag);
×
46
    }
×
47

48
    public override void RegisterScriptingEnum(ExposedEnumeration enumeration)
49
    {
50
        StringBuilder enumInjectScript = new();
×
51
        enumInjectScript.Append(enumeration.Name + " = {");
×
52

53
        foreach (var kvp in enumeration.KeyValues)
×
54
        {
55
            var enumKey = kvp.Key;
×
56
            var enumValue = kvp.Value;
×
57

58
            enumInjectScript.Append(enumKey);
×
59
            enumInjectScript.Append(": ");
×
60
            enumInjectScript.Append(enumValue);
×
61

62
            enumInjectScript.Append(",\n");
×
63
        }
64

65
        enumInjectScript.Append("};");
×
66

67
        var enumInjection = enumInjectScript.ToString();
×
68
        this.Environment.Execute(enumInjection);
×
69

70
        //this.environment.Execute($"Print(JSON.stringify({enumInjection}))");
71
    }
×
72

73
    public override void RegisterScriptingMethod(ExposedMethod exposedMethod, AbstractAction scriptActionInstance)
74
    {
75
        exposedMethod.Prepare(scriptActionInstance);
×
76

77
        var functionName = exposedMethod.FunctionName;
×
78
        var parents = functionName.Split('.');
×
79
        var methodInfo = exposedMethod.GetExecutorMethodInfo();
×
80
        var @delegate = new DelegateWrapper(this.Environment, methodInfo.CreateDelegate(exposedMethod));
×
81

82
        if (parents.Length > 1)
×
83
        {
84
            var currentObject = this.Environment.Realm.GlobalObject;
×
85

86
            for (var i = 0; i < parents.Length; i++)
×
87
            {
88
                if (i != parents.Length - 1)
×
89
                {
90
                    if (!currentObject.TryGetValue(parents[i], out var child))
×
91
                    {
92
                        child = new JsObject(this.Environment);
×
93
                    }
94

95
                    if (child is not ObjectInstance childObject)
×
96
                    {
97
                        throw new NotImplementedException($"Tried using a non object({parents[i]}) as object parent while registering function: {functionName}!");
×
98
                    }
99

100
                    currentObject.FastSetProperty(parents[i], new PropertyDescriptor(childObject, false, true, true));
×
101
                    currentObject = childObject;
×
102
                }
103
                else
104
                {
105
                    currentObject.FastSetProperty(parents[i], new PropertyDescriptor(@delegate, false, true, true));
×
106
                }
107
            }
108

109
            return;
×
110
        }
111

112
        this.Environment.SetValue(
×
113
            functionName,
×
114
            methodInfo);
×
115
    }
×
116

117
    public override Engine MakeEnvironment() => new Engine(options =>
×
118
    {
×
119
        options.Interop.AllowSystemReflection = true;
×
120
    });
×
121

122
    public override void RegisterEnvironmentObjects()
123
    {
124
        this.Environment.SetValue("Print", new Action<object[]>(this.Print));
×
125

126
        base.RegisterEnvironmentObjects();
×
127
    }
×
128

129
    public override void OnStartListening(AbstractTriggerListener listener, ref IList<AbstractAction> otherActions) => base.OnStartListening(listener, ref otherActions);
×
130

131
    public override void OnStopListening(AbstractTriggerListener listener)
132
    {
133
        base.OnStopListening(listener);
×
134

135
        this.Environment = null;
×
136
    }
×
137

138
    public override bool Equals(object obj)
139
    {
140
        if (obj is not JavascriptAction action)
×
141
        {
142
            return false;
×
143
        }
144

145
        return action.Name == this.Name
×
146
            && action.Script == this.Script;
×
147
    }
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc