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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
#include "globals.h"
#include "V8Script.h"
#include "utf8_helpers.h"
using namespace v8;
V8Script::V8Script() : exceptionCallback(NULL), exceptionCallbackParam(NULL)
{
}
V8Script::~V8Script()
{
dispose();
}
static Handle<Value> IsEmptyCallback(const Arguments& args)
{
if (args.Length() < 1)
return Undefined();
HandleScope scope;
for(int i = 0; i < args.Length(); i++)
{
Local<Value> arg = args[0];
if (arg.IsEmpty() || arg->IsNull() || arg->IsUndefined())
{
return Boolean::New(true);
}
else if (arg->IsExternal())
{
Local<External> wrap = Local<External>::Cast(arg);
FieldState *field = (FieldState *) wrap->Value();
if (field->isEmpty())
return Boolean::New(true);
}
else if (arg->IsString())
{
Local<String> str = Local<String>::Cast(arg);
if (str->Length() <= 0)
return Boolean::New(true);
}
}
return Boolean::New(false);
}
static Handle<Value> RGBCallback(const Arguments& args)
{
if (args.Length() != 3)
return Undefined();
COLORREF color = RGB(args[0]->Int32Value(), args[1]->Int32Value(), args[2]->Int32Value());
return Int32::New(color);
}
bool V8Script::compile(const TCHAR *source, Dialog *dlg)
{
dispose();
HandleScope handle_scope;
Handle<ObjectTemplate> global = ObjectTemplate::New();
global->Set(String::New("IsEmpty"), FunctionTemplate::New(&IsEmptyCallback));
global->Set(String::New("RGB"), FunctionTemplate::New(&RGBCallback));
context = Context::New(NULL, global);
Context::Scope context_scope(context);
context->Global()->Set(String::New("window"), wrappers.createDialogWrapper(), ReadOnly);
context->Global()->Set(String::New("opts"), wrappers.createOptionsWrapper(), ReadOnly);
for(unsigned int i = 0; i < dlg->getFieldCount(); i++)
{
Field *field = dlg->getField(i);
context->Global()->Set(String::New(field->getName()), wrappers.createWrapper(field->getType()), ReadOnly);
}
wrappers.clearTemplates();
TryCatch try_catch;
Local<Script> tmpScript = Script::Compile(String::New((const V8_TCHAR *) source), String::New(dlg->getName()));
if (tmpScript.IsEmpty())
{
reportException(&try_catch);
dispose();
return false;
}
script = Persistent<Script>::New(tmpScript);
return true;
}
void V8Script::dispose()
{
script.Dispose();
script.Clear();
context.Dispose();
context.Clear();
}
bool V8Script::isValid()
{
return !context.IsEmpty() && !script.IsEmpty();
}
static Handle<Object> get(Local<Object> obj, const char *field)
{
return Handle<Object>::Cast(obj->Get(String::New(field)));
}
Handle<Function> V8Script::getConfigureFunction(Dialog *dlg)
{
DialogState *state = dlg->createState();
HandleScope handle_scope;
Context::Scope context_scope(context);
// Run once to get function
Local<Object> global = context->Global();
wrappers.fillWrapper(get(global, "window"), state);
wrappers.fillWrapper(get(global, "opts"), (SkinOptions *) NULL, false);
for(unsigned int i = 0; i < state->fields.size(); i++)
{
FieldState *field = state->fields[i];
wrappers.fillWrapper(get(global, field->getField()->getName()), field);
}
// I don't care for the catch here
TryCatch try_catch;
script->Run();
delete state;
Handle<Value> configure_val = context->Global()->Get(String::New("configure"));
if (configure_val.IsEmpty() || !configure_val->IsFunction())
return Handle<Function>();
Handle<Function> configure = Handle<Function>::Cast(configure_val);
return handle_scope.Close(configure);
}
std::pair<SkinOptions *,DialogState *> V8Script::configure(Dialog *dlg)
{
if (!isValid())
return std::pair<SkinOptions *,DialogState *>(NULL, NULL);
SkinOptions *opts = new SkinOptions();
DialogState *state = dlg->createState();
HandleScope handle_scope;
Context::Scope context_scope(context);
Handle<Function> configure = getConfigureFunction(dlg);
if (configure.IsEmpty())
return std::pair<SkinOptions *,DialogState *>(opts, state);
Local<Object> global = context->Global();
wrappers.fillWrapper(get(global, "opts"), opts, true);
wrappers.fillWrapper(get(global, "window"), state);
for(unsigned int i = 0; i < state->fields.size(); i++)
{
FieldState *field = state->fields[i];
wrappers.fillWrapper(get(global, field->getField()->getName()), field);
}
global->Set(String::New("NUMBER"), String::New("NUMBER"));
global->Set(String::New("CHECKBOX"), String::New("CHECKBOX"));
global->Set(String::New("TEXT"), String::New("TEXT"));
TryCatch try_catch;
Handle<Value> result = configure->Call(global, 0, NULL);
if (result.IsEmpty())
{
reportException(&try_catch);
delete opts;
delete state;
return std::pair<SkinOptions *,DialogState *>(NULL, NULL);;
}
return std::pair<SkinOptions *,DialogState *>(opts, state);
}
bool V8Script::run(DialogState * state, SkinOptions *opts)
{
if (!isValid())
return false;
HandleScope handle_scope;
Context::Scope context_scope(context);
Local<Object> global = context->Global();
wrappers.fillWrapper(get(global, "window"), state);
wrappers.fillWrapper(get(global, "opts"), opts, false);
for(unsigned int i = 0; i < state->fields.size(); i++)
{
FieldState *field = state->fields[i];
wrappers.fillWrapper(get(global, field->getField()->getName()), field);
}
TryCatch try_catch;
Handle<Value> result = script->Run();
if (result.IsEmpty())
{
reportException(&try_catch);
return false;
}
return true;
}
void V8Script::setExceptionCallback(ExceptionCallback cb, void *param)
{
exceptionCallback = cb;
exceptionCallbackParam = param;
}
void V8Script::reportException(v8::TryCatch *try_catch)
{
std::string err;
char tmp[1024];
HandleScope handle_scope;
String::Utf8Value exception(try_catch->Exception());
Handle<Message> message = try_catch->Message();
if (message.IsEmpty())
{
// V8 didn't provide any extra information about this error; just
// print the exception.
_snprintf(tmp, 1024, "%s\n", *exception);
err += tmp;
}
else
{
// Print (filename):(line number): (message).
String::Utf8Value filename(message->GetScriptResourceName());
int linenum = message->GetLineNumber();
_snprintf(tmp, 1024, "%s:%i: %s\n", *filename, linenum, *exception);
err += tmp;
// Print line of source code.
String::Utf8Value sourceline(message->GetSourceLine());
_snprintf(tmp, 1024, "%s\n", *sourceline);
err += tmp;
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
err += " ";
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
err += "^";
}
err += "\n";
}
Utf8ToTchar tcharErr(err.c_str());
OutputDebugString(tcharErr);
if (exceptionCallback != NULL)
exceptionCallback(exceptionCallbackParam, tcharErr);
}
|