Skip to content

Commit a22c940

Browse files
committed
wren/mirror: Make MethodMirror callable (instance must be the first argument)
1 parent 1ce4ee9 commit a22c940

File tree

4 files changed

+169
-6
lines changed

4 files changed

+169
-6
lines changed

src/optional/wren_opt_mirror.wren

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ class ClassMirror is ObjectMirror {
5555
methodNames { _methodNames ||
5656
(_methodNames = ClassMirror.methodNames(reflectee)) }
5757

58-
methodMirrors { _methodMirrors }
58+
methodMirrors {
59+
if (_methodMirrors) return _methodMirrors
60+
61+
_methodMirrors = {}
62+
for (methodName in methodNames) {
63+
_methodMirrors[methodName] = MethodMirror.new_(this, methodName, 0)
64+
}
65+
return _methodMirrors
66+
}
5967

6068
hasMethod(signature) { ClassMirror.hasMethod(reflectee, signature) }
6169
}
@@ -106,8 +114,9 @@ class FiberMirror is ObjectMirror {
106114
class MethodMirror is Mirror {
107115
foreign static signature_(signatureIndex)
108116

109-
construct new_(boundToClassMirror, signatureIndex) {
117+
construct new_(boundToClassMirror, signature, signatureIndex) {
110118
_boundToClassMirror = boundToClassMirror
119+
_signature = signature
111120
_signatureIndex = signatureIndex
112121
}
113122

@@ -118,7 +127,64 @@ class MethodMirror is Mirror {
118127
// arity { MethodMirror.arity_(_method) }
119128
// maxSlots { MethodMirror.maxSlots_(_method) }
120129
// numUpvalues { MethodMirror.maxSlots_(_numUpvalues) }
121-
signature { MethodMirror.signature_(_method) }
130+
signature { _signature }
131+
132+
toString { _signature.toString }
133+
134+
validate_(self, arity) {
135+
if (ObjectMirror.typeOf(self) != _boundToClassMirror.reflectee) Fiber.abort()
136+
//if (arity != this.arity) Fiber.abort()
137+
return self
138+
}
139+
140+
call(self) {
141+
return validate_(self, 0).@"%(signature)"()
142+
}
143+
call(self, arg0) {
144+
return validate_(self, 1).@"%(signature)"(arg0)
145+
}
146+
call(self, arg0, arg1) {
147+
return validate_(self, 2).@"%(signature)"(arg0, arg1)
148+
}
149+
call(self, arg0, arg1, arg2) {
150+
return validate_(self, 3).@"%(signature)"(arg0, arg1, arg2)
151+
}
152+
call(self, arg0, arg1, arg2, arg3) {
153+
return validate_(self, 4).@"%(signature)"(arg0, arg1, arg2, arg3)
154+
}
155+
call(self, arg0, arg1, arg2, arg3, arg4) {
156+
return validate_(self, 5).@"%(signature)"(arg0, arg1, arg2, arg3, arg4)
157+
}
158+
call(self, arg0, arg1, arg2, arg3, arg4, arg5) {
159+
return validate_(self, 6).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5)
160+
}
161+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6) {
162+
return validate_(self, 7).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6)
163+
}
164+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {
165+
return validate_(self, 8).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
166+
}
167+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {
168+
return validate_(self, 9).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
169+
}
170+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
171+
return validate_(self, 10).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
172+
}
173+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) {
174+
return validate_(self, 11).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)
175+
}
176+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) {
177+
return validate_(self, 12).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
178+
}
179+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12) {
180+
return validate_(self, 13).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12)
181+
}
182+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13) {
183+
return validate_(self, 14).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13)
184+
}
185+
call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) {
186+
return validate_(self, 15).@"%(signature)"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14)
187+
}
122188
}
123189

124190
class ModuleMirror is Mirror {

src/optional/wren_opt_mirror.wren.inc

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ static const char* mirrorModuleSource =
5757
" methodNames { _methodNames ||\n"
5858
" (_methodNames = ClassMirror.methodNames(reflectee)) }\n"
5959
"\n"
60-
" methodMirrors { _methodMirrors }\n"
60+
" methodMirrors {\n"
61+
" if (_methodMirrors) return _methodMirrors\n"
62+
"\n"
63+
" _methodMirrors = {}\n"
64+
" for (methodName in methodNames) {\n"
65+
" _methodMirrors[methodName] = MethodMirror.new_(this, methodName, 0)\n"
66+
" }\n"
67+
" return _methodMirrors\n"
68+
" }\n"
6169
"\n"
6270
" hasMethod(signature) { ClassMirror.hasMethod(reflectee, signature) }\n"
6371
"}\n"
@@ -108,8 +116,9 @@ static const char* mirrorModuleSource =
108116
"class MethodMirror is Mirror {\n"
109117
" foreign static signature_(signatureIndex)\n"
110118
"\n"
111-
" construct new_(boundToClassMirror, signatureIndex) {\n"
119+
" construct new_(boundToClassMirror, signature, signatureIndex) {\n"
112120
" _boundToClassMirror = boundToClassMirror\n"
121+
" _signature = signature\n"
113122
" _signatureIndex = signatureIndex\n"
114123
" }\n"
115124
"\n"
@@ -120,7 +129,64 @@ static const char* mirrorModuleSource =
120129
"// arity { MethodMirror.arity_(_method) }\n"
121130
"// maxSlots { MethodMirror.maxSlots_(_method) }\n"
122131
"// numUpvalues { MethodMirror.maxSlots_(_numUpvalues) }\n"
123-
" signature { MethodMirror.signature_(_method) }\n"
132+
" signature { _signature }\n"
133+
"\n"
134+
" toString { _signature.toString }\n"
135+
"\n"
136+
" validate_(self, arity) {\n"
137+
" if (ObjectMirror.typeOf(self) != _boundToClassMirror.reflectee) Fiber.abort()\n"
138+
" //if (arity != this.arity) Fiber.abort()\n"
139+
" return self\n"
140+
" }\n"
141+
"\n"
142+
" call(self) {\n"
143+
" return validate_(self, 0).@\"%(signature)\"()\n"
144+
" }\n"
145+
" call(self, arg0) {\n"
146+
" return validate_(self, 1).@\"%(signature)\"(arg0)\n"
147+
" }\n"
148+
" call(self, arg0, arg1) {\n"
149+
" return validate_(self, 2).@\"%(signature)\"(arg0, arg1)\n"
150+
" }\n"
151+
" call(self, arg0, arg1, arg2) {\n"
152+
" return validate_(self, 3).@\"%(signature)\"(arg0, arg1, arg2)\n"
153+
" }\n"
154+
" call(self, arg0, arg1, arg2, arg3) {\n"
155+
" return validate_(self, 4).@\"%(signature)\"(arg0, arg1, arg2, arg3)\n"
156+
" }\n"
157+
" call(self, arg0, arg1, arg2, arg3, arg4) {\n"
158+
" return validate_(self, 5).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4)\n"
159+
" }\n"
160+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5) {\n"
161+
" return validate_(self, 6).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5)\n"
162+
" }\n"
163+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6) {\n"
164+
" return validate_(self, 7).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6)\n"
165+
" }\n"
166+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {\n"
167+
" return validate_(self, 8).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7)\n"
168+
" }\n"
169+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {\n"
170+
" return validate_(self, 9).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)\n"
171+
" }\n"
172+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {\n"
173+
" return validate_(self, 10).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)\n"
174+
" }\n"
175+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) {\n"
176+
" return validate_(self, 11).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10)\n"
177+
" }\n"
178+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11) {\n"
179+
" return validate_(self, 12).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)\n"
180+
" }\n"
181+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12) {\n"
182+
" return validate_(self, 13).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12)\n"
183+
" }\n"
184+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13) {\n"
185+
" return validate_(self, 14).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13)\n"
186+
" }\n"
187+
" call(self, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) {\n"
188+
" return validate_(self, 15).@\"%(signature)\"(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14)\n"
189+
" }\n"
124190
"}\n"
125191
"\n"
126192
"class ModuleMirror is Mirror {\n"

test/mirror/method_mirror/.wren

Whitespace-only changes.

test/mirror/method_mirror/call.wren

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import "mirror" for Mirror
3+
4+
class Foo {
5+
construct new() {}
6+
call() {
7+
return System.print("()")
8+
}
9+
call(arg0) {
10+
return System.print("(%(arg0))")
11+
}
12+
call(arg0, arg1) {
13+
return System.print("(%(arg0), %(arg1))")
14+
}
15+
call(arg0, arg1, arg2) {
16+
return System.print("(%(arg0), %(arg1), %(arg2))")
17+
}
18+
call(arg0, arg1, arg2, arg3) {
19+
return System.print("(%(arg0), %(arg1), %(arg2), %(arg3))")
20+
}
21+
}
22+
23+
var foo = Foo.new()
24+
25+
var FooMirror = Mirror.reflect(Foo)
26+
27+
FooMirror.methodMirrors["call()"].call(foo) // expect: ()
28+
FooMirror.methodMirrors["call(_)"].call(foo, "arg0") // expect: (arg0)
29+
FooMirror.methodMirrors["call(_,_)"].call(foo, "arg0", "arg1") // expect: (arg0, arg1)
30+
FooMirror.methodMirrors["call(_,_,_)"].call(foo, "arg0", "arg1", "arg2") // expect: (arg0, arg1, arg2)
31+
FooMirror.methodMirrors["call(_,_,_,_)"].call(foo, "arg0", "arg1", "arg2", "arg3") // expect: (arg0, arg1, arg2, arg3)

0 commit comments

Comments
 (0)