You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package is available for [purchase](https://assetstore.unity.com/packages/tools/visual-scripting/c-eval-56706) on the Unity Asset Store. A valid license is required for use in any project.
4
5
5
-
This is a paid [package](https://assetstore.unity.com/packages/tools/visual-scripting/c-eval-56706), you can use it anywhere if you [purchased it](https://assetstore.unity.com/packages/tools/visual-scripting/c-eval-56706).
6
+
# Overview
7
+
This package delivers a C# parsing and expression execution API designed for [Unity](http://unity3d.com/) compatibility across multiple platforms. Implemented in C# 3.5 with no external dependencies, it maintains broad compatibility with Unity versions and .NET frameworks.
6
8
7
-
# Introduction
9
+
## Verified Platform Support
10
+
• iOS
11
+
• Android
12
+
• WebGL
13
+
• Windows/macOS/Linux
8
14
9
-
This package provides the API for parsing and expression execution written in C#. It is specially designed to work with the [Unity](http://unity3d.com/)on various platforms. Since it is written in C# 3.5 and has no additional dependencies, it should work with any version of Unity (and .NET framework).
15
+
The solution should function on additional Unity-supported platforms.
10
16
11
-
It is tested to work on:
12
-
* IOS
13
-
* Android
14
-
* WebGL
15
-
* PC/Mac
17
+
> **Important Note for AOT Platforms (iOS, WebGL, IL2CPP):**
18
+
> Projects targeting AOT compilation require inclusion of a [link.xml](https://github.com/deniszykov/csharp-eval-unity3d/blob/master/src/GameDevWare.Dynamic.Expressions.Unity.2021/Assets/Plugins/GameDevWare.Dynamic.Expressions/link.xml) file in the project root directory. Refer to Unity's documentation on [IL code stripping](https://docs.unity3d.com/Manual/IL2CPP-BytecodeStripping.html) for additional context.
16
19
17
-
It should work on any other platforms.
20
+
## Core API
21
+
• **CSharpExpression**
22
+
- Evaluate
23
+
- Parse
24
+
• **AotCompilation**
25
+
- RegisterFunc
26
+
- RegisterForFastCall (performance optimization)
18
27
19
-
> :warning: For AOT execution platforms (**iOS, WebGL, IL2CPP**) a [link.xml](https://github.com/deniszykov/csharp-eval-unity3d/blob/master/src/GameDevWare.Dynamic.Expressions.Unity.2021/Assets/Plugins/GameDevWare.Dynamic.Expressions/link.xml) should be added to project's root directory. Read more about [IL code stripping](https://docs.unity3d.com/Manual/IL2CPP-BytecodeStripping.html) in official documentation.
28
+
## Implementation Examples
20
29
21
-
**API**
22
-
* CSharpExpression
23
-
* Evaluate
24
-
* Parse
25
-
* AotCompilation
26
-
* RegisterFunc
27
-
* RegisterForFastCall (optional stuff)
28
-
29
-
## Example
30
-
Parsing C# expression into **System.Linq.Expression.Expression[T]**:
[Type inference](https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/type-inference) is not available and your should always specify generic parameters on types and methods.
71
-
72
-
**Known Types**
73
-
74
-
For security reasons the parser does not provide access to any types except:
75
-
* types used in arguments
76
-
* primitive types
77
-
*`Math`, `Array`, `Func<>` types
78
-
79
-
To access other types your should pass **typeResolver** parameter in **Parse** and **Evaluate** method:
46
+
## Parser Specifications
47
+
The parser implements C# 4 grammar with support for:
48
+
49
+
• Arithmetic, bitwise, and logical operations
50
+
• [Conditional](https://msdn.microsoft.com/en-us/library/ty67wk28.aspx) and [null-coalescing operators](https://msdn.microsoft.com/en-us/library/ms173224.aspx)
51
+
• Method/delegate/constructor invocation
52
+
• [Property/field access](https://msdn.microsoft.com/en-us/library/6zhxzbds.aspx) and [indexers](https://msdn.microsoft.com/en-gb/library/6x16t2tx.aspx)
53
+
• Type operations ([casting, conversion](https://msdn.microsoft.com/en-us/library/ms173105.aspx), [is](https://msdn.microsoft.com/en-us/library/scekt9xw.aspx)/[as](https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx)/[typeof](https://msdn.microsoft.com/en-us/library/58918ffs.aspx)/[default](https://msdn.microsoft.com/en-us/library/xwth0h0d.aspx))
54
+
• [Expression grouping](https://msdn.microsoft.com/en-us/library/0z4503sa.aspx) and [checked/unchecked contexts](https://msdn.microsoft.com/en-us/library/khy08726.aspx)
55
+
• [Type aliases](https://msdn.microsoft.com/en-us/library/ya5y69ds.aspx) and [null-conditional operators](https://msdn.microsoft.com/en-us/library/dn986595.aspx)
> **Note:**[Type inference](https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/type-inference) is not implemented - explicit generic parameter specification is required.
66
+
67
+
## Type Resolution
68
+
For security, the parser restricts type access to:
69
+
• Argument types
70
+
• Primitive types
71
+
• Math, Array, and Func<> types
72
+
73
+
Additional types require specification through the typeResolver parameter:
iOS, WebGL and most console platforms use AOT compilation which imposes following restrictions on the dynamic code execution:
102
-
103
-
* only **Expression<Func<...>>** delegate type could be used with **CompileAot()**.
104
-
* only static methods using primitives (int, float, string, object ...) are optimized for fast calls, others are called with reflection.
105
-
* all used classes/methods/properties should be visible to [Unity's static code analyser](https://docs.unity3d.com/Manual/ScriptingRestrictions.html)
106
-
* ⚠️ An additional preparation should be made for AOT execution platforms. This [link.xml](https://github.com/deniszykov/csharp-eval-unity3d/blob/master/src/GameDevWare.Dynamic.Expressions.Unity.2021/Assets/Plugins/GameDevWare.Dynamic.Expressions/link.xml) should be added in project's root folder. Read more about [IL code stripping](https://docs.unity3d.com/Manual/IL2CPP-BytecodeStripping.html) in official documentation.
95
+
**AOT Environment Requirements (iOS, WebGL and most console platforms):**
96
+
1. Only Expression<Func<...>> delegate types supported
97
+
2. Only static methods using primitives arguments receive optimization
98
+
3. All referenced members must be visible to [Unity's static analyzer](https://docs.unity3d.com/Manual/ScriptingRestrictions.html) to prevent eager [IL code stripping](https://docs.unity3d.com/Manual/IL2CPP-BytecodeStripping.html)
99
+
4. ⚠️ Required [link.xml](https://github.com/deniszykov/csharp-eval-unity3d/blob/master/src/GameDevWare.Dynamic.Expressions.Unity.2021/Assets/Plugins/GameDevWare.Dynamic.Expressions/link.xml) configuration (see above)
107
100
108
101
**See Also**
109
102
*[AOT Exception Patterns and Hacks](https://github.com/neuecc/UniRx/wiki/AOT-Exception-Patterns-and-Hacks)
110
103
*[Ahead of Time Compilation (AOT)](http://www.mono-project.com/docs/advanced/runtime/docs/aot/)
111
104
112
-
### WebGL and iOS
113
105
114
-
* Only [Func<>](https://msdn.microsoft.com/en-us/library/bb534960(v=vs.110).aspx) (up to 4 arguments) lambdas are supported with `CompileAot()`.
115
-
* Instance methods calls performs slowly due reflection
116
-
* Moderate boxing for value types (see roadmap)
106
+
### Platform-Specific Considerations
117
107
118
-
You can prepare [Func<>](https://msdn.microsoft.com/en-us/library/bb534960(v=vs.110).aspx) lambda for AOT compilation/execution by registering it with **AotCompilation.RegisterFunc**.
108
+
**WebGL and iOS:**
109
+
• Func<> lambdas limited to 4 arguments
110
+
• Instance methods incur reflection overhead
111
+
• Value types experience moderate boxing
119
112
113
+
**Preparation for AOT Execution:**
120
114
```csharp
121
-
AotCompilation.RegisterFunc<int, bool>(); //will enable Func<int, bool> lambdas anywhere in expressions
115
+
AotCompilation.RegisterFunc<int, bool>(); //Enables Func<int, bool> in expressions
122
116
```
123
117
124
-
### Unity 2020.3.2 Issues
125
-
This version of Unity has runtime bug related to lambda compilation. Please use following workaround for IL2CPP/AOT runtime:
0 commit comments