forked from saary/node.net
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWrapAssembly.cpp
More file actions
149 lines (116 loc) · 4.64 KB
/
WrapAssembly.cpp
File metadata and controls
149 lines (116 loc) · 4.64 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
#include "WrapAssembly.h"
#include "WrapInstance.h"
#include "v8value.h"
#include "Helpers.h"
using namespace System::Collections::Generic;
using namespace System::Reflection;
using namespace v8;
WrapAssembly::WrapAssembly(Assembly^ a)
{
_assembly = a;
}
Handle<Value> WrapAssembly::AssemblyName(const Arguments& args)
{
HandleScope scope;
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
Handle<Value> result = v8sharp::V8Interop::ToV8(self->_assembly->FullName);
return scope.Close(result);
}
Handle<Value> WrapAssembly::ListTypes(const Arguments& args)
{
HandleScope scope;
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
List<System::String^>^ types = gcnew List<System::String^>();
for each(Type^ t in self->_assembly->GetTypes())
{
types->Add(t->FullName);
}
Handle<Value> result = v8sharp::V8Interop::ToV8(types->ToArray());
return scope.Close(result);
}
Handle<Value> WrapAssembly::CreateInstance(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 class name.")));
}
HandleScope scope;
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
System::String^ path = (System::String^)v8sharp::V8Interop::FromV8(args[0]);
try
{
Type^ t = self->_assembly->GetType(path);
if (t == nullptr)
{
throw gcnew System::NotSupportedException("Class: '" + path + "' was not found");
}
array<ConstructorInfo^>^ cInfoList = t->GetConstructors();
List<ConstructorInfo^>^ matches = gcnew List<ConstructorInfo^>();
int otherArgCount = args.Length() - 1;
for (int i = 0; i < cInfoList->Length; i++)
{
ConstructorInfo^ ci = cInfoList[i];
array<System::Reflection::ParameterInfo^>^ params = ci->GetParameters();
if (params->Length == otherArgCount)
{
matches->Add(ci);
}
}
if (matches->Count == 0)
{
throw gcnew System::NotSupportedException("Class: '" + path + "' does not have a constructor with " + otherArgCount + " arguments.");
}
else if (matches->Count > 1)
{
Console::WriteLine("Node-Sharp Warning: Constructor matching by length found multiple options.");
}
array<System::Object^>^ argList = Helpers::ConvertArguments(args, 1, matches[0]->GetParameters());
System::Object^ result = matches[0]->Invoke(argList);
return scope.Close(WrapInstance::New(t, 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> WrapAssembly::StaticInstance(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 class name.")));
}
HandleScope scope;
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
System::String^ path = (System::String^)v8sharp::V8Interop::FromV8(args[0]);
try
{
Type^ t = self->_assembly->GetType(path);
if (t == nullptr)
{
throw gcnew System::NotSupportedException("Class: '" + path + "' was not found");
}
return scope.Close(WrapInstance::New(t, nullptr));
}
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<v8::Value> WrapAssembly::New(Assembly ^ a)
{
HandleScope scope;
Local<ObjectTemplate> oTemplate = ObjectTemplate::New();
oTemplate->SetInternalFieldCount(1);
Local<v8::Object> result = oTemplate->NewInstance();
WrapAssembly* self = new WrapAssembly(a);
self->Wrap(result);
result->Set(v8::String::NewSymbol("assembly"), v8sharp::V8Interop::ToV8(self->_assembly->FullName));
result->Set(v8::String::NewSymbol("types"), FunctionTemplate::New(ListTypes)->GetFunction() );
result->Set(v8::String::NewSymbol("new"), FunctionTemplate::New(CreateInstance)->GetFunction() );
result->Set(v8::String::NewSymbol("static"), FunctionTemplate::New(StaticInstance)->GetFunction() );
return scope.Close(result);
}