Skip to content

Commit 3953090

Browse files
author
Jett Jones
committed
add syntax to call static methods
1 parent 1c2e39b commit 3953090

11 files changed

+105
-62
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Debug
22
*.log
3-
_Resharper.Sharp
3+
_Resharper.*
44
.node-gyp
55
bindings.sln*
66
ipch
77
Release
88
*\ipch
99
*.sdf
10+
Sharp/Sharp.opensdf
1011
*.suo
1112
*\bin\*
1213
*\obj\*

Sharp.sln

Lines changed: 0 additions & 30 deletions
This file was deleted.

Sharp/WrapAssembly.cpp

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ WrapAssembly::WrapAssembly(Assembly^ a)
1414
}
1515

1616
Handle<Value> WrapAssembly::AssemblyName(const Arguments& args)
17-
{
17+
{
1818
HandleScope scope;
1919
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
20-
20+
2121
Handle<Value> result = v8sharp::V8Interop::ToV8(self->_assembly->FullName);
2222

2323
return scope.Close(result);
@@ -46,16 +46,16 @@ Handle<Value> WrapAssembly::CreateInstance(const Arguments& args)
4646
return ThrowException(v8::Exception::TypeError(
4747
v8::String::New("First argument must be a string, for class name.")));
4848
}
49-
49+
5050
HandleScope scope;
5151
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
52-
52+
5353
System::String^ path = (System::String^)v8sharp::V8Interop::FromV8(args[0]);
5454

5555
try
5656
{
5757
Type^ t = self->_assembly->GetType(path);
58-
58+
5959
if (t == nullptr)
6060
{
6161
throw gcnew System::NotSupportedException("Class: '" + path + "' was not found");
@@ -67,7 +67,7 @@ Handle<Value> WrapAssembly::CreateInstance(const Arguments& args)
6767
int otherArgCount = args.Length() - 1;
6868
for (int i = 0; i < cInfoList->Length; i++)
6969
{
70-
ConstructorInfo^ ci = cInfoList[i];
70+
ConstructorInfo^ ci = cInfoList[i];
7171
array<System::Reflection::ParameterInfo^>^ params = ci->GetParameters();
7272
if (params->Length == otherArgCount)
7373
{
@@ -86,14 +86,46 @@ Handle<Value> WrapAssembly::CreateInstance(const Arguments& args)
8686

8787
array<System::Object^>^ argList = Helpers::ConvertArguments(args, 1, matches[0]->GetParameters());
8888
System::Object^ result = matches[0]->Invoke(argList);
89-
89+
9090
return scope.Close(WrapInstance::New(t, result));
9191
}
9292
catch (System::Exception^ e)
9393
{
9494
v8::Handle<v8::Value> str = v8sharp::V8Interop::ToV8(Helpers::GetError(e));
9595
v8::Handle<v8::String> err = v8::Handle<v8::String>::Cast(str);
96-
96+
97+
return ThrowException(v8::Exception::Error(err));
98+
}
99+
}
100+
101+
Handle<Value> WrapAssembly::StaticInstance(const Arguments& args)
102+
{
103+
if (args.Length() < 1 || !args[0]->IsString()) {
104+
return ThrowException(v8::Exception::TypeError(
105+
v8::String::New("First argument must be a string, for class name.")));
106+
}
107+
108+
HandleScope scope;
109+
WrapAssembly* self = node::ObjectWrap::Unwrap<WrapAssembly>(args.This());
110+
111+
System::String^ path = (System::String^)v8sharp::V8Interop::FromV8(args[0]);
112+
113+
try
114+
{
115+
Type^ t = self->_assembly->GetType(path);
116+
117+
if (t == nullptr)
118+
{
119+
throw gcnew System::NotSupportedException("Class: '" + path + "' was not found");
120+
}
121+
122+
return scope.Close(WrapInstance::New(t, nullptr));
123+
}
124+
catch (System::Exception^ e)
125+
{
126+
v8::Handle<v8::Value> str = v8sharp::V8Interop::ToV8(Helpers::GetError(e));
127+
v8::Handle<v8::String> err = v8::Handle<v8::String>::Cast(str);
128+
97129
return ThrowException(v8::Exception::Error(err));
98130
}
99131
}
@@ -108,10 +140,11 @@ Handle<v8::Value> WrapAssembly::New(Assembly ^ a)
108140

109141
WrapAssembly* self = new WrapAssembly(a);
110142
self->Wrap(result);
111-
143+
112144
result->Set(v8::String::NewSymbol("assembly"), v8sharp::V8Interop::ToV8(self->_assembly->FullName));
113145
result->Set(v8::String::NewSymbol("types"), FunctionTemplate::New(ListTypes)->GetFunction() );
114146
result->Set(v8::String::NewSymbol("new"), FunctionTemplate::New(CreateInstance)->GetFunction() );
115-
147+
result->Set(v8::String::NewSymbol("static"), FunctionTemplate::New(StaticInstance)->GetFunction() );
148+
116149
return scope.Close(result);
117150
}

Sharp/WrapAssembly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WrapAssembly : public node::ObjectWrap
1616
static v8::Handle<v8::Value> AssemblyName(const v8::Arguments& args);
1717
static v8::Handle<v8::Value> ListTypes(const v8::Arguments& args);
1818
static v8::Handle<v8::Value> CreateInstance(const v8::Arguments& args);
19+
static v8::Handle<v8::Value> StaticInstance(const v8::Arguments& args);
1920

2021
static v8::Handle<v8::Value> New(System::Reflection::Assembly ^ a);
2122
};

Sharp/WrapInstance.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using namespace System::Reflection;
88
using namespace v8;
99

1010
Handle<Value> WrapInstance::New(System::Type^ t, System::Object^ o)
11-
{
11+
{
1212
HandleScope scope;
1313

1414
Local<ObjectTemplate> oTemplate = ObjectTemplate::New();
@@ -22,14 +22,14 @@ Handle<Value> WrapInstance::New(System::Type^ t, System::Object^ o)
2222
result->Set(v8::String::NewSymbol("methods"), FunctionTemplate::New(ListMethods)->GetFunction() );
2323
result->Set(v8::String::NewSymbol("call"), FunctionTemplate::New(CallMethod)->GetFunction() );
2424
result->Set(v8::String::NewSymbol("async"), FunctionTemplate::New(Async)->GetFunction() );
25-
25+
2626
result->SetHiddenValue(v8::String::NewSymbol("__wrapper"), v8::Boolean::New(true));
2727

2828
// Debug code for checking how the hidden values might work.
2929
//Handle<v8::Value> val1 = result->GetHiddenValue(v8::String::NewSymbol("__wrapper"));
3030
//Handle<v8::Value> val2 = result->GetHiddenValue(v8::String::NewSymbol("__bogus"));
3131

32-
//bool null1 = val1.IsEmpty();
32+
//bool null1 = val1.IsEmpty();
3333
//bool null2 = val2.IsEmpty();
3434

3535
return scope.Close(result);
@@ -63,30 +63,34 @@ WrapInstance::WrapInstance(System::Type^ t, System::Object^ i)
6363
{
6464
_type = t;
6565
_instance = i;
66+
_isStatic = i == nullptr;
6667
}
6768

6869
Handle<Value> WrapInstance::GetClassName(const Arguments& args)
6970
{
7071
HandleScope scope;
7172
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(args.This());
72-
73+
7374
Handle<Value> result = v8sharp::V8Interop::ToV8(self->_type->Name);
7475

7576
return scope.Close(result);
7677
}
77-
78+
7879
Handle<Value> WrapInstance::ListMethods(const Arguments& args)
7980
{
8081
HandleScope scope;
8182
WrapInstance* self = node::ObjectWrap::Unwrap<WrapInstance>(args.This());
82-
83-
List<System::String^>^ methods = gcnew List<System::String^>();
84-
85-
for each(MethodInfo^ m in self->_type->GetMethods())
83+
84+
List<System::String^>^ methods = gcnew List<System::String^>();
85+
86+
BindingFlags flags = BindingFlags::Public | BindingFlags::Instance;
87+
flags = (self->_isStatic) ? BindingFlags::Public | BindingFlags::Static : flags;
88+
89+
for each(MethodInfo^ m in self->_type->GetMethods(flags))
8690
{
8791
methods->Add(m->Name);
8892
}
89-
93+
9094
Handle<Value> result = v8sharp::V8Interop::ToV8(methods->ToArray());
9195

9296
return scope.Close(result);
@@ -112,13 +116,13 @@ Handle<Value> WrapInstance::CallMethod(const Arguments& args)
112116
{
113117
throw gcnew System::NotSupportedException(System::String::Format("Method {0} not found", path));
114118
}
115-
119+
116120
array<System::Object^>^ argList = Helpers::ConvertArguments(args, 1, m->GetParameters());
117121

118122
System::Object^ r = m->Invoke(self->_instance, argList);
119-
123+
120124
Handle<Value> result = v8sharp::V8Interop::ToV8(r);
121-
125+
122126
return scope.Close(result);
123127
}
124128
catch(System::Exception^ e)
@@ -129,7 +133,7 @@ Handle<Value> WrapInstance::CallMethod(const Arguments& args)
129133
return ThrowException(v8::Exception::Error(err));
130134
}
131135
}
132-
136+
133137
Handle<Value> WrapInstance::Async(const Arguments& args)
134138
{
135139
HandleScope scope;
@@ -168,7 +172,7 @@ Handle<Value> WrapInstance::Async(const Arguments& args)
168172
{
169173
v8::Handle<v8::Value> str = v8sharp::V8Interop::ToV8(Helpers::GetError(e));
170174
v8::Handle<v8::String> err = v8::Handle<v8::String>::Cast(str);
171-
175+
172176
return ThrowException(v8::Exception::Error(err));
173177
}
174178

@@ -178,7 +182,7 @@ Handle<Value> WrapInstance::Async(const Arguments& args)
178182

179183
return Undefined();
180184
}
181-
185+
182186
// this runs on the worker thread and should not callback or interact with node/v8 in any way
183187
void WrapInstance::StartAsync(uv_work_t* req)
184188
{
@@ -187,7 +191,7 @@ void WrapInstance::StartAsync(uv_work_t* req)
187191
try
188192
{
189193
baton->error = false;
190-
baton->result = baton->method->Invoke(baton->caller->_instance, baton->args);
194+
baton->result = baton->method->Invoke(baton->caller->_instance, baton->args);
191195
}
192196
catch(System::Exception^ e)
193197
{
@@ -202,7 +206,7 @@ void WrapInstance::AfterAsync(uv_work_t *req)
202206
HandleScope scope;
203207
Baton* baton = static_cast<Baton*>(req->data);
204208

205-
if (baton->error)
209+
if (baton->error)
206210
{
207211
Handle<v8::Value> str = v8sharp::V8Interop::ToV8(baton->error_message);
208212
Handle<v8::String> err = Handle<v8::String>::Cast(str);
@@ -215,9 +219,9 @@ void WrapInstance::AfterAsync(uv_work_t *req)
215219

216220
if (try_catch.HasCaught()) {
217221
node::FatalException(try_catch);
218-
}
219-
}
220-
else
222+
}
223+
}
224+
else
221225
{
222226
const unsigned argc = 2;
223227
Local<Value> argv[argc] = {

Sharp/WrapInstance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class WrapInstance : node::ObjectWrap
3030
private:
3131
gcroot<System::Object^> _instance;
3232
gcroot<System::Type^> _type;
33+
bool _isStatic;
3334

3435
WrapInstance(System::Type^ t, System::Object^ i);
3536

StubClass/Static.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+

2+
namespace StubClass
3+
{
4+
public class Static
5+
{
6+
public static Static Instance()
7+
{
8+
return new Static();
9+
}
10+
11+
public static int GetInt()
12+
{
13+
return 574716;
14+
}
15+
16+
public static string GetString()
17+
{
18+
return "static";
19+
}
20+
21+
public string InstanceString()
22+
{
23+
return "instance." + this.GetHashCode();
24+
}
25+
}
26+
}

StubClass/StubClass.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<Compile Include="Input.cs" />
4040
<Compile Include="Output.cs" />
4141
<Compile Include="Properties\AssemblyInfo.cs" />
42+
<Compile Include="Static.cs" />
4243
</ItemGroup>
4344
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4445
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

bin/Sharp.node

1 KB
Binary file not shown.

bin/StubClass.dll

512 Bytes
Binary file not shown.

bin/sharptest.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ console.log(str);
2626

2727
str = stub.new('StubClass.Constructor', oc.call('ReturnObject')).call('ToString')
2828
console.log(str);
29+
30+
// calling static methods
31+
var ss = stub.static('StubClass.Static')
32+
console.log('Static1: ' + ss.call('GetInt'));
33+
34+
console.log('Static2: '+ ss.call('Instance').call('InstanceString'));

0 commit comments

Comments
 (0)