This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Callback.cs
145 lines (134 loc) · 4.98 KB
/
Callback.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Web.Script.Serialization;
using Noesis.Javascript;
using TrifleJS.API;
namespace TrifleJS
{
public class Callback
{
/// <summary>
/// A queue of pending callbacks that should be executed in
/// the V8 environment. This is used for event handling on
/// modules that use a separate thread (ie. ChildProcess),
/// as making the callback inside those threads does not
/// expose our current V8 environment.
/// </summary>
public static Queue<Callback> queue = new Queue<Callback>();
/// <summary>
/// Adds items to the callback queue
/// </summary>
/// <param name="id"></param>
/// <param name="arguments"></param>
public static void Queue(string id, bool once, params object[] arguments)
{
queue.Enqueue(new Callback { Id = id, Once = once, Arguments = arguments } );
}
/// <summary>
/// Processes the callback queue
/// (used in the event loop)
/// </summary>
public static void ProcessQueue()
{
while (queue.Count > 0)
{
Callback callback = queue.Dequeue();
Callback.Execute(callback.Id, callback.Once, callback.Arguments);
}
}
/// <summary>
/// Used for queueing callbacks
/// </summary>
internal string Id {get; set;}
internal bool Once { get; set; }
internal object[] Arguments { get; set; }
/// <summary>
/// Allows callback from C# middleware to the V8 JavaScript Runtime.
/// Deletes the original callback function.
/// </summary>
/// <param name="id">Callback id</param>
/// <param name="arguments">any arguments to pass to the callback</param>
/// <returns></returns>
public static bool ExecuteOnce(string id, params object[] arguments)
{
return Execute(id, true, arguments);
}
/// <summary>
/// Allows callback from C# middleware to the V8 JavaScript Runtime.
/// Keeps the original callback function to allow multiple execution.
/// </summary>
/// <param name="id">Callback id</param>
/// <param name="arguments">any arguments to pass to the callback</param>
/// <returns></returns>
public static bool Execute(string id, params object[] arguments)
{
return Execute(id, false, arguments);
}
/// <summary>
/// Executes a callback
/// </summary>
public static bool Execute(string id, bool once, params object[] arguments)
{
try
{
if (arguments == null) { arguments = new object[0]; }
String cmd = String.Format(@"trifle.Callback.{0}('{1}', [{2}]);",
once ? "executeOnce" : "execute",
id,
String.Join(",", Context.Parse(arguments))
);
Program.Context.Run(cmd, "Callback#" + id);
}
catch (Exception ex) {
API.Context.Handle(ex);
return false;
}
return true;
}
/// <summary>
/// Allows callbacks from IE to C# and the V8 Javascript Runtime,
/// PLEASE NOTE: Debugging inside this class is likely to
/// make IE loose focus, causing unexpected behaviour.
/// </summary>
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class External
{
/// <summary>
/// Initialises the ObjectForScripting class
/// </summary>
/// <param name="page">A page where scripts are being executed</param>
public External(API.Modules.WebPage page) {
this.page = page;
}
public API.Modules.WebPage page;
/// <summary>
/// Outputs a debug message
/// </summary>
/// <param name="message"></param>
public void xdebug(string message) {
API.Console.xdebug(message);
}
/// <summary>
/// Performs a callback in V8 environment
/// </summary>
/// <param name="id"></param>
public void doCallback(string id)
{
Callback.Execute(id);
}
/// <summary>
/// Fires an event in the page object (V8 runtime) passing some arguments
/// </summary>
/// <param name="shortname"></param>
/// <param name="jsonArgs"></param>
/// <returns></returns>
public object fireEvent(string nickname, string jsonArgs) {
return page._fireEvent(nickname, jsonArgs);
}
}
}
}