forked from saary/node.net
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWrapInstance.cpp
More file actions
242 lines (188 loc) · 7.14 KB
/
WrapInstance.cpp
File metadata and controls
242 lines (188 loc) · 7.14 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
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
#include "WrapInstance.h"
#include "v8value.h"
#include "MatchType.h"
#include "Helpers.h"
using namespace System::Collections::Generic;
using namespace System::Reflection;
using namespace v8;
Handle<Value> WrapInstance::New(System::Type^ t, System::Object^ o)
{
HandleScope scope;
Local<ObjectTemplate> oTemplate = ObjectTemplate::New();
oTemplate->SetInternalFieldCount(1);
Local<v8::Object> result = oTemplate->NewInstance();
WrapInstance* self = new WrapInstance(t,o);
self->Wrap(result);
result->Set(v8::String::NewSymbol("class"), v8sharp::V8Interop::ToV8(self->_type->Name));
result->Set(v8::String::NewSymbol("methods"), FunctionTemplate::New(ListMethods)->GetFunction() );
result->Set(v8::String::NewSymbol("call"), FunctionTemplate::New(CallMethod)->GetFunction() );
result->Set(v8::String::NewSymbol("async"), FunctionTemplate::New(Async)->GetFunction() );
result->SetHiddenValue(v8::String::NewSymbol("__wrapper"), v8::Boolean::New(true));
// Debug code for checking how the hidden values might work.
//Handle<v8::Value> val1 = result->GetHiddenValue(v8::String::NewSymbol("__wrapper"));
//Handle<v8::Value> val2 = result->GetHiddenValue(v8::String::NewSymbol("__bogus"));
//bool null1 = val1.IsEmpty();
//bool null2 = val2.IsEmpty();
return scope.Close(result);
}
bool WrapInstance::IsWrapped(v8::Handle<v8::Value> value)
{
v8::Handle<v8::Object> obj = value->ToObject();
if (!obj.IsEmpty())
{
v8::Local<v8::Value> sym = obj->GetHiddenValue(v8::String::NewSymbol("__wrapper"));
if (!sym.IsEmpty() && sym->IsBoolean())
{
return sym->IsTrue();
}
}
return false;
}
System::Object^ WrapInstance::Unwrap(v8::Handle<v8::Value> value)
{
HandleScope scope;
v8::Handle<v8::Object> obj = value->ToObject();
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(obj);
return self->_instance;
}
WrapInstance::WrapInstance(System::Type^ t, System::Object^ i)
{
_type = t;
_instance = i;
_isStatic = i == nullptr;
}
Handle<Value> WrapInstance::GetClassName(const Arguments& args)
{
HandleScope scope;
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(args.This());
Handle<Value> result = v8sharp::V8Interop::ToV8(self->_type->Name);
return scope.Close(result);
}
Handle<Value> WrapInstance::ListMethods(const Arguments& args)
{
HandleScope scope;
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(args.This());
List<System::String^>^ methods = gcnew List<System::String^>();
BindingFlags flags = BindingFlags::Public | BindingFlags::Instance;
flags = (self->_isStatic) ? BindingFlags::Public | BindingFlags::Static : flags;
for each(MethodInfo^ m in self->_type->GetMethods(flags))
{
methods->Add(m->Name);
}
Handle<Value> result = v8sharp::V8Interop::ToV8(methods->ToArray());
return scope.Close(result);
}
Handle<Value> WrapInstance::CallMethod(const Arguments& args)
{
if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(v8::Exception::TypeError(
v8::String::New("First argument must be a string, for method name.")));
}
System::String^ path = safe_cast<System::String^>(v8sharp::V8Interop::FromV8(args[0]));
HandleScope scope;
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(args.This());
try
{
MethodInfo^ m = self->_type->GetMethod(path);
if (m == nullptr)
{
throw gcnew System::NotSupportedException(System::String::Format("Method {0} not found", path));
}
array<System::Object^>^ argList = Helpers::ConvertArguments(args, 1, m->GetParameters());
System::Object^ r = m->Invoke(self->_instance, argList);
Handle<Value> result = v8sharp::V8Interop::ToV8(r);
return scope.Close(result);
}
catch(System::Exception^ e)
{
v8::Handle<v8::Value> str = v8sharp::V8Interop::ToV8(Helpers::GetError(e));
v8::Handle<v8::String> err = v8::Handle<v8::String>::Cast(str);
return ThrowException(v8::Exception::Error(err));
}
}
Handle<Value> WrapInstance::Async(const Arguments& args)
{
HandleScope scope;
if (!args[0]->IsString()) {
return ThrowException(v8::Exception::TypeError(
v8::String::New("First argument must be a string, for method name")));
}
if (!args[1]->IsFunction()) {
return ThrowException(v8::Exception::TypeError(
v8::String::New("Second argument must be a callback function")));
}
Local<v8::String> path = Local<v8::String>::Cast(args[0]);
// There's no ToFunction(), use a Cast instead.
Local<Function> callback = Local<Function>::Cast(args[1]);
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(args.This());
// create a state object
Baton* baton = new Baton();
baton->request.data = baton;
baton->caller = self;
baton->callback = Persistent<Function>::New(callback);
try
{
MethodInfo^ m = self->_type->GetMethod(safe_cast<System::String^>(v8sharp::V8Interop::FromV8(path)));
baton->method = m;
array<System::Object^>^ argList = Helpers::ConvertArguments(args, 2, m->GetParameters());
baton->args = argList;
}
catch(System::Exception^ e)
{
v8::Handle<v8::Value> str = v8sharp::V8Interop::ToV8(Helpers::GetError(e));
v8::Handle<v8::String> err = v8::Handle<v8::String>::Cast(str);
return ThrowException(v8::Exception::Error(err));
}
// register a worker thread request
uv_queue_work(uv_default_loop(), &baton->request,
StartAsync, AfterAsync);
return Undefined();
}
// this runs on the worker thread and should not callback or interact with node/v8 in any way
void WrapInstance::StartAsync(uv_work_t* req)
{
Baton *baton = static_cast<Baton*>(req->data);
try
{
baton->error = false;
baton->result = baton->method->Invoke(baton->caller->_instance, baton->args);
}
catch(System::Exception^ e)
{
baton->error = true;
baton->error_message = Helpers::GetError(e);
}
}
// this runs on the main thread and can call back into the JavaScript
void WrapInstance::AfterAsync(uv_work_t *req)
{
HandleScope scope;
Baton* baton = static_cast<Baton*>(req->data);
if (baton->error)
{
Handle<v8::Value> str = v8sharp::V8Interop::ToV8(baton->error_message);
Handle<v8::String> err = Handle<v8::String>::Cast(str);
Handle<Value> argv[] = { err };
TryCatch try_catch;
baton->callback->Call(
Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
else
{
const unsigned argc = 2;
Local<Value> argv[argc] = {
Local<Value>::New(Null()),
Local<Value>::New(v8sharp::V8Interop::ToV8(baton->result))
};
TryCatch try_catch;
baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
baton->callback.Dispose();
delete baton;
}