forked from saary/node.net
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWrapBase.cpp
More file actions
84 lines (63 loc) · 2.33 KB
/
WrapBase.cpp
File metadata and controls
84 lines (63 loc) · 2.33 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
#include "WrapBase.h"
#include "v8value.h"
#include "WrapAssembly.h"
using namespace System::Collections::Generic;
using namespace System::IO;
using namespace System::Reflection;
using namespace v8;
WrapBase::WrapBase()
{
_searchBase = System::Environment::CurrentDirectory;
}
Handle<Value> WrapBase::ListAssemblies(const v8::Arguments& args)
{
HandleScope scope;
WrapBase* self = node::ObjectWrap::Unwrap<WrapBase>(args.This());
DirectoryInfo^ d = gcnew DirectoryInfo(self->_searchBase);
List<System::String^>^ names = gcnew List<System::String^>();
for each(FileInfo^ f in d->EnumerateFiles("*.dll"))
{
names->Add(f->Name);
}
return scope.Close( v8sharp::V8Interop::ToV8(names->ToArray()) );
}
Handle<Value> WrapBase::LoadAssembly(const v8::Arguments& args)
{
if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(v8::Exception::TypeError(
v8::String::New("First argument must be a string, for dll name.")));
}
HandleScope scope;
WrapBase* self = node::ObjectWrap::Unwrap<WrapBase>(args.This());
System::String^ path = safe_cast<System::String^>(v8sharp::V8Interop::FromV8(args[0]) );
try
{
DirectoryInfo^ d = gcnew DirectoryInfo(self->_searchBase);
array<FileInfo^>^ files = d->GetFiles(path);
if (files->Length <= 0)
{
throw gcnew System::Exception(System::String::Format("File {0} not found", path));
}
FileInfo^ f = files[0];
Assembly^ a = Assembly::LoadFile(f->FullName);
return scope.Close( WrapAssembly::New(a) );
}
catch(System::Exception^ e)
{
Handle<Value> str = v8sharp::V8Interop::ToV8(e->Message);
Handle<v8::String> err = Handle<v8::String>::Cast(str);
return ThrowException(v8::Exception::Error(err));
}
}
Handle<Value> WrapBase::New()
{
HandleScope scope;
Local<ObjectTemplate> oTemplate = ObjectTemplate::New();
oTemplate->SetInternalFieldCount(1);
Local<v8::Object> result = oTemplate->NewInstance();
WrapBase* self = new WrapBase();
self->Wrap(result);
result->Set(v8::String::NewSymbol("list"), FunctionTemplate::New(ListAssemblies)->GetFunction() );
result->Set(v8::String::NewSymbol("load"), FunctionTemplate::New(LoadAssembly)->GetFunction() );
return scope.Close(result);
}