This repository was archived by the owner on Mar 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.cs
More file actions
53 lines (42 loc) · 1.47 KB
/
Expression.cs
File metadata and controls
53 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Acornima.Ast;
using Jint;
using Jint.Native;
using JSNet.Utils;
using Overlayer.Tags.Attributes;
using Overlayer.Utils;
using System;
using System.Collections.Generic;
namespace Overlayer.Scripting;
public static class Expression {
public static readonly Dictionary<string, ExprContext> expressions = new();
[Tag("Expression", NotPlaying = true)]
public static object Expr(string expr) {
if(expressions.TryGetValue(expr, out var res)) {
return res == null || !res.prepared.IsValid ? null : (object)res.Run();
}
var prepared = Engine.PrepareScript(JSUtils.RemoveImports(expr));
if(!prepared.IsValid) {
expressions[expr] = new ExprContext(null, prepared);
return null;
}
var engine = Main.JSApi.PrepareInterpreter();
var ctx = new ExprContext(engine, prepared);
expressions[expr] = ctx;
return ctx.Run();
}
public class ExprContext {
public Engine engine;
public Prepared<Script> prepared;
public bool IsFaulted;
public ExprContext(Engine engine, Prepared<Script> prepared) {
this.engine = engine;
this.prepared = prepared;
}
public JsValue Run() {
if(IsFaulted || engine == null || !prepared.IsValid) {
return JsValue.Null;
}
return MiscUtils.ExecuteSafe(() => engine.Evaluate(prepared), out var ex) ?? JsValue.Null;
}
}
}