-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
V8Simple.cs
227 lines (227 loc) · 13.8 KB
/
V8Simple.cs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Fuse.Scripting.V8.Simple
{
// -------------------------------------------------------------------------
// Types
public enum JSType
{
Null,
Int,
Double,
String,
Bool,
Object,
Array,
Function,
External,
}
public enum JSRuntimeError
{
NoError,
InvalidCast,
StringTooLong,
TypeError,
}
[StructLayout(LayoutKind.Sequential)]
public struct JSContext
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSValue
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSString
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSObject
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSArray
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSFunction
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSExternal
{
readonly IntPtr _handle;
}
[StructLayout(LayoutKind.Sequential)]
public struct JSScriptException
{
readonly IntPtr _handle;
public override bool Equals(object that) { return that is JSScriptException ? this == (JSScriptException)that : false; }
public override int GetHashCode() { return _handle.GetHashCode(); }
public static bool operator ==(JSScriptException e1, JSScriptException e2) { return e1._handle == e2._handle; }
public static bool operator !=(JSScriptException e1, JSScriptException e2) { return e1._handle != e2._handle; }
}
public delegate JSValue JSCallback(JSContext context, IntPtr data, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]JSValue[] args, int numArgs, out JSValue error);
public delegate void JSExternalFinalizer(IntPtr external);
public delegate void JSCallbackFinalizer(IntPtr data);
public delegate void JSDebugMessageHandler(IntPtr data, JSString message);
// -------------------------------------------------------------------------
// Context
public static class Context
{
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="RetainJSContext")]
public static extern void Retain(JSContext context);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="ReleaseJSContext")]
public static extern void Release(JSContext context);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSContext")]
public static extern JSContext Create([MarshalAs(UnmanagedType.FunctionPtr)]JSCallbackFinalizer callbackFinalizer, [MarshalAs(UnmanagedType.FunctionPtr)]JSExternalFinalizer externalFinalizer);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSContextEvaluateCreate")]
public static extern JSValue EvaluateCreate(JSContext context, JSString fileName, JSString code, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSContextCopyGlobalObject")]
public static extern JSObject CopyGlobalObject(JSContext context);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetV8Version")]
public static extern IntPtr GetV8VersionPtr();
public static string GetV8Version() { return Marshal.PtrToStringAnsi(GetV8VersionPtr()); }
}
// -------------------------------------------------------------------------
// Debug
public static class Debug
{
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="SetJSDebugMessageHandler")]
public static extern void SetMessageHandler(JSContext context, IntPtr data, [MarshalAs(UnmanagedType.FunctionPtr)]JSDebugMessageHandler messageHandler);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="SendJSDebugCommand")]
public static extern void SendCommand(JSContext context, [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 2)]string command, int length);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="ProcessJSDebugMessages")]
public static extern void ProcessMessages(JSContext context);
}
// -------------------------------------------------------------------------
// Value
public static class Value
{
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSValueType")]
public static extern JSType GetType(JSValue value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="RetainJSValue")]
public static extern void Retain(JSContext context, JSValue value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="ReleaseJSValue")]
public static extern void Release(JSContext context, JSValue value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsInt")]
public static extern int AsInt(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsDouble")]
public static extern double AsDouble(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsString")]
public static extern JSString AsString(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsBool")]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool AsBool(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsObject")]
public static extern JSObject AsObject(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsArray")]
public static extern JSArray AsArray(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsFunction")]
public static extern JSFunction AsFunction(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueAsExternal")]
public static extern JSExternal AsExternal(JSValue value, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSValueStrictEquals")]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool StrictEquals(JSContext context, JSValue obj1, JSValue obj2);
// --------------------------------------------------------------------------
// Primitives
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSNull")]
public static extern JSValue JSNull();
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSInt")]
public static extern JSValue CreateInt(int value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSDouble")]
public static extern JSValue CreateDouble(double value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSBool")]
public static extern JSValue CreateBool([MarshalAs(UnmanagedType.I1)]bool value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateExternalJSArrayBuffer")]
public static extern JSObject CreateExternalArrayBuffer(JSContext context, IntPtr data, int byteLength);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSCallback")]
public static extern JSFunction CreateCallback(JSContext context, IntPtr data, [MarshalAs(UnmanagedType.FunctionPtr)]JSCallback callback, out JSScriptException error);
// --------------------------------------------------------------------------
// String
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSString")]
public static extern JSString CreateString(JSContext context, [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 2)]string buffer, int length, out JSRuntimeError error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSStringLength")]
public static extern int Length(JSContext context, JSString str);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="WriteJSStringBuffer")]
public static extern void Write(JSContext context, JSString str, [Out, MarshalAs(UnmanagedType.LPWStr)]StringBuilder buffer, [MarshalAs(UnmanagedType.I1)]bool nullTerminate);
public static string ToString(JSContext context, JSString str)
{
var sb = new StringBuilder(Length(context, str) + 1);
Write(context, str, sb, true);
return sb.ToString();
}
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSStringAsValue")]
public static extern JSValue AsValue(JSString str);
// -------------------------------------------------------------------------
// Object
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CopyJSObjectProperty")]
public static extern JSValue CopyProperty(JSContext context, JSObject obj, JSString key, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="SetJSObjectProperty")]
public static extern void SetProperty(JSContext context, JSObject obj, JSString key, JSValue value, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CopyJSObjectOwnPropertyNames")]
public static extern JSArray CopyOwnPropertyNames(JSContext context, JSObject obj, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSObjectHasProperty")]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool HasProperty(JSContext context, JSObject obj, JSString key, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSObjectArrayBufferData")]
public static extern IntPtr GetArrayBufferData(JSContext context, JSObject obj, out JSRuntimeError outError);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSObjectAsValue")]
public static extern JSValue AsValue(JSObject obj);
// -------------------------------------------------------------------------
// Array
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CopyJSArrayPropertyAtIndex")]
public static extern JSValue CopyProperty(JSContext context, JSArray arr, int index, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="SetJSArrayPropertyAtIndex")]
public static extern void SetProperty(JSContext context, JSArray arr, int index, JSValue value, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSArrayLength")]
public static extern int Length(JSContext context, JSArray arr);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSArrayAsValue")]
public static extern JSValue AsValue(JSArray arr);
// -------------------------------------------------------------------------
// Function
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CallJSFunctionCreate")]
public static extern JSValue CallCreate(JSContext context, JSFunction function, JSObject thisObject, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)]JSValue[] args, int numArgs, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="ConstructJSFunctionCreate")]
public static extern JSObject ConstructCreate(JSContext context, JSFunction function, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]JSValue[] args, int numArgs, out JSScriptException error);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSFunctionAsValue")]
public static extern JSValue AsValue(JSFunction fun);
// -------------------------------------------------------------------------
// External
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="CreateJSExternal")]
public static extern JSExternal CreateExternal(JSContext context, IntPtr value);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSExternalValue")]
public static extern IntPtr GetExternalValue(JSContext context, JSExternal external);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="JSExternalAsValue")]
public static extern JSValue AsValue(JSExternal external);
}
// -------------------------------------------------------------------------
// Exceptions
public static class ScriptException
{
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="RetainJSScriptException")]
public static extern void Retain(JSContext context, JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="ReleaseJSScriptException")]
public static extern void Release(JSContext context, JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSScriptException")]
public static extern JSValue GetException(JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSScriptExceptionMessage")]
public static extern JSString GetMessage(JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSScriptExceptionFileName")]
public static extern JSString GetFileName(JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSScriptExceptionLineNumber")]
public static extern int GetLineNumber(JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSScriptExceptionStackTrace")]
public static extern JSString GetStackTrace(JSScriptException e);
[DllImport("V8Simple.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="GetJSScriptExceptionSourceLine")]
public static extern JSString GetSourceLine(JSScriptException e);
}
}