Skip to content

Commit

Permalink
Move NSInvocation arguments as Array to a new category
Browse files Browse the repository at this point in the history
  • Loading branch information
gilzoide committed Oct 12, 2023
1 parent 383b722 commit d434140
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/GDObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "GDObject.hpp"

#import "NSMethodSignature+ArgumentsFromData.hpp"
#import "NSInvocation+Godot.hpp"
#import "objc_conversions.hpp"
#import "objc_marshalling.hpp"

Expand Down Expand Up @@ -87,7 +88,7 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation {
String methodName = [GDObject godotNameForSelector:anInvocation.selector];
if (_obj) {
if (_obj->has_method(methodName)) {
Array args = [anInvocation.methodSignature arrayFromInvocationArguments:anInvocation];
Array args = anInvocation.argumentArray;
Variant result = _obj->callv(methodName, args);
set_result_variant(anInvocation, result, args);
return;
Expand Down
36 changes: 36 additions & 0 deletions src/NSInvocation+Godot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2023 Gil Barbosa Reis.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the “Software”), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NSINVOCATION_GODOT_HPP__
#define __NSINVOCATION_GODOT_HPP__

#include <Foundation/Foundation.h>
#include <godot_cpp/variant/variant.hpp>

using namespace godot;

@interface NSInvocation (Godot)

@property(readonly) Array argumentArray;

@end

#endif // __NSINVOCATION_GODOT_HPP__
38 changes: 38 additions & 0 deletions src/NSInvocation+Godot.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2023 Gil Barbosa Reis.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the “Software”), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "NSInvocation+Godot.hpp"

#include "objc_marshalling.hpp"

using namespace objcgdextension;

@implementation NSInvocation (Godot)

- (Array)argumentArray {
Array args;
for (int i = 2; i < self.methodSignature.numberOfArguments; i++) {
args.append(get_argument_variant(self, i));
}
return args;
}

@end
6 changes: 3 additions & 3 deletions src/NSMethodSignature+ArgumentsFromData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ using namespace godot;

@interface NSMethodSignature (ArgumentsFromData)

- (NSUInteger)totalArgumentSize;
- (String)completeSignature;
@property(readonly) NSUInteger totalArgumentSize;
@property(readonly) String completeSignature;

- (Array)arrayFromArgumentData:(const void *)data;
- (Array)arrayFromInvocationArguments:(NSInvocation *)invocation;

@end

Expand Down
8 changes: 0 additions & 8 deletions src/NSMethodSignature+ArgumentsFromData.mm
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,4 @@ - (Array)arrayFromArgumentData:(const void *)data {
return args;
}

- (Array)arrayFromInvocationArguments:(NSInvocation *)invocation {
Array args;
for (int i = 0; i < self.numberOfArguments; i++) {
args.append(get_argument_variant(invocation, i));
}
return args;
}

@end
13 changes: 13 additions & 0 deletions test/unit_tests/GDObject.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ func test_property() -> bool:
return true


func test_method() -> bool:
var objcObject = ObjectiveC.wrap_object(self)
assert(objcObject.invoke("intToBool:", 1) == true)
assert(objcObject.invoke("intToBool:", 0) == false)
return true


func intToBool(value: int) -> bool:
return bool(value)


func methodSignatureForSelector(sel: String) -> String:
match sel:
"my_property":
return "i@:"
"intToBool:":
return "B@:i"
_:
return ""

0 comments on commit d434140

Please sign in to comment.