-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSMJS.cpp
More file actions
151 lines (117 loc) · 3.32 KB
/
SMJS.cpp
File metadata and controls
151 lines (117 loc) · 3.32 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
#include "SMJS.h"
#include "extension.h"
#include "modules/MSocket.h"
v8::Isolate *mainIsolate;
time_t lastPing;
bool isPaused = false;
SourceMod::IMutex *pingMutex;
void SMJS_OnPausedTick();
class SMJS_CheckerThread : public SourceMod::IThread{
void RunThread(IThreadHandle *pHandle){
while(1){
pingMutex->Lock();
#ifndef _DEBUG
int now = time(NULL);
if(!isPaused && now - lastPing > 30){
v8::V8::TerminateExecution(mainIsolate);
printf("SERVER STOPPED THINKING, QUITTING\n");
threader->ThreadSleep(3000);
throw "Script timed out";
}
#endif
pingMutex->Unlock();
threader->ThreadSleep(1000);
}
}
void OnTerminate(IThreadHandle *pHandle, bool cancel){
}
};
void OnMessage(Handle<Message> message, Handle<Value> error){
HandleScope handle_scope(mainIsolate);
fprintf(stderr, "----------------------------- JAVASCRIPT EXCEPTION -----------------------------");
v8::String::Utf8Value tmp(message->Get());
const char* exception_string = *tmp;
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
fprintf(stderr, "%s\n", exception_string);
} else {
// Print (filename):(line number): (message).
String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = *filename;
int linenum = message->GetLineNumber();
fprintf(stderr, "%s:%d: %s\n", filename_string, linenum, exception_string);
// Print line of source code.
String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = *sourceline;
fprintf(stderr, "%s\n", sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
fprintf(stderr, " ");
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
fprintf(stderr, "^");
}
fprintf(stderr, "\n");
auto stackTrace = message->GetStackTrace();
if(!stackTrace.IsEmpty()){
int len = stackTrace->GetFrameCount();
for(int i = 0; i < len; ++i){
auto frame = stackTrace->GetFrame(i);
v8::String::Utf8Value scriptName(frame->GetScriptNameOrSourceURL());
v8::String::Utf8Value funName(frame->GetFunctionName());
fprintf(stderr, "%s - %s @ line %d\n", *scriptName, *funName, frame->GetLineNumber());
}
}
}
fprintf(stderr, "--------------------------------------------------------------------------------");
}
void SMJS_Init(){
mainIsolate = v8::Isolate::GetCurrent();
char smjsPath[512];
smutils->BuildPath(Path_SM, smjsPath, sizeof(smjsPath), "sourcemod.js");
v8::V8::AddMessageListener(OnMessage);
pingMutex = threader->MakeMutex();
SMJS_Ping();
threader->MakeThread(new SMJS_CheckerThread());
}
void SMJS_Ping(){
pingMutex->Lock();
lastPing = time(NULL);
pingMutex->Unlock();
}
void SMJS_Pause(){
printf("Entering in deep sleep\n");
pingMutex->Lock();
if(isPaused){
pingMutex->Unlock();
return;
}
isPaused = true;
pingMutex->Unlock();
while(1){
pingMutex->Lock();
if(!isPaused){
pingMutex->Unlock();
return;
}
pingMutex->Unlock();
SMJS_OnPausedTick();
threader->ThreadSleep(100);
}
}
void SMJS_Resume(){
pingMutex->Lock();
if(!isPaused){
pingMutex->Unlock();
return;
}
lastPing = time(NULL);
isPaused = false;
pingMutex->Unlock();
}
void SMJS_OnPausedTick(){
MSocket::Process();
}