-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathDataType.cs
More file actions
183 lines (172 loc) · 4.3 KB
/
Copy pathDataType.cs
File metadata and controls
183 lines (172 loc) · 4.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
namespace MoonSharp.Interpreter
{
/// <summary>
/// Enumeration of possible data types in MoonSharp
/// </summary>
public enum DataType
{
// DO NOT MODIFY ORDER - IT'S SIGNIFICANT
/// <summary>
/// A nil value, as in Lua
/// </summary>
Nil,
/// <summary>
/// A place holder for no value
/// </summary>
Void,
/// <summary>
/// A Lua boolean
/// </summary>
Boolean,
/// <summary>
/// A Lua number
/// </summary>
Number,
/// <summary>
/// A Lua string
/// </summary>
String,
/// <summary>
/// A Lua function
/// </summary>
Function,
/// <summary>
/// A Lua table
/// </summary>
Table,
/// <summary>
/// A set of multiple values
/// </summary>
Tuple,
/// <summary>
/// A userdata reference - that is a wrapped CLR object
/// </summary>
UserData,
/// <summary>
/// A coroutine handle
/// </summary>
Thread,
/// <summary>
/// A callback function
/// </summary>
ClrFunction,
/// <summary>
/// A request to execute a tail call
/// </summary>
TailCallRequest,
/// <summary>
/// A request to coroutine.yield
/// </summary>
YieldRequest,
/// <summary>
/// Return a value from a clr coroutine
/// </summary>
ClrCoroutineReturn,
}
/// <summary>
/// Extension methods to DataType
/// </summary>
public static class LuaTypeExtensions
{
internal const DataType MaxMetaTypes = DataType.Table;
internal const DataType MaxConvertibleTypes = DataType.ClrFunction;
/// <summary>
/// Determines whether this data type can have type metatables.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public static bool CanHaveTypeMetatables(this DataType type)
{
return (int)type < (int)MaxMetaTypes;
}
/// <summary>
/// Converts the DataType to the string returned by the "type(...)" Lua function
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
/// <exception cref="ScriptRuntimeException">The DataType is not a Lua type</exception>
public static string ToErrorTypeString(this DataType type)
{
switch (type)
{
case DataType.Void:
return "no value";
case DataType.Nil:
return "nil";
case DataType.Boolean:
return "boolean";
case DataType.Number:
return "number";
case DataType.String:
return "string";
case DataType.Function:
return "function";
case DataType.ClrFunction:
return "function";
case DataType.Table:
return "table";
case DataType.UserData:
return "userdata";
case DataType.Thread:
return "coroutine";
case DataType.ClrCoroutineReturn:
return "coroutine_return";
case DataType.Tuple:
case DataType.TailCallRequest:
case DataType.YieldRequest:
default:
return string.Format("internal<{0}>", type.ToLuaDebuggerString());
}
}
/// <summary>
/// Converts the DataType to the string returned by the "type(...)" Lua function, with additional values
/// to support debuggers
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
/// <exception cref="ScriptRuntimeException">The DataType is not a Lua type</exception>
public static string ToLuaDebuggerString(this DataType type)
{
return type.ToString().ToLowerInvariant();
}
/// <summary>
/// Converts the DataType to the string returned by the "type(...)" Lua function
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
/// <exception cref="ScriptRuntimeException">The DataType is not a Lua type</exception>
public static string ToLuaTypeString(this DataType type)
{
switch (type)
{
case DataType.Void:
case DataType.Nil:
return "nil";
case DataType.Boolean:
return "boolean";
case DataType.Number:
return "number";
case DataType.String:
return "string";
case DataType.Function:
return "function";
case DataType.ClrFunction:
return "function";
case DataType.Table:
return "table";
case DataType.UserData:
return "userdata";
case DataType.Thread:
return "thread";
case DataType.ClrCoroutineReturn:
return "coroutine_return";
case DataType.Tuple:
case DataType.TailCallRequest:
case DataType.YieldRequest:
default:
throw new ScriptRuntimeException("Unexpected LuaType {0}", type);
}
}
}
}