-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support automatic proxying of Chipmunk objects to Java interfaces whe…
…n calling Java methods.
- Loading branch information
1 parent
8afbad3
commit c38aac5
Showing
7 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2024 MyWorld, LLC | ||
* All rights reserved. | ||
* | ||
* This file is part of Chipmunk. | ||
* | ||
* Chipmunk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Chipmunk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Chipmunk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package chipmunk.vm.invoke; | ||
|
||
import chipmunk.vm.ChipmunkScript; | ||
import chipmunk.vm.ChipmunkVM; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public class ProxyFilter { | ||
|
||
protected final ChipmunkVM vm; | ||
protected final Class<?> target; | ||
|
||
public ProxyFilter(ChipmunkVM vm, Class<?> target){ | ||
this.vm = vm; | ||
this.target = target; | ||
} | ||
|
||
public Object filter(Object param) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { | ||
if(param == null){ | ||
return param; | ||
} | ||
|
||
return vm.proxy(target, param); | ||
} | ||
|
||
public static MethodHandle filterFor(MethodHandles.Lookup lookup, Class<?> target) throws NoSuchMethodException, IllegalAccessException{ | ||
return filterFor(lookup, ChipmunkScript.getCurrentScript().getVM(), target); | ||
} | ||
|
||
public static MethodHandle filterFor(MethodHandles.Lookup lookup, ChipmunkVM vm, Class<?> target) throws NoSuchMethodException, IllegalAccessException { | ||
return lookup.findVirtual(ProxyFilter.class, "filter", MethodType.methodType(Object.class, Object.class)) | ||
.bindTo(new ProxyFilter(vm, target)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2024 MyWorld, LLC | ||
* All rights reserved. | ||
* | ||
* This file is part of Chipmunk. | ||
* | ||
* Chipmunk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Chipmunk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Chipmunk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package chipmunk; | ||
|
||
public interface SimpleDemoProxy { | ||
|
||
int getA(); | ||
float getB(); | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
Lang/src/test/groovy/chipmunk/SimpleDemoProxyReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2024 MyWorld, LLC | ||
* All rights reserved. | ||
* | ||
* This file is part of Chipmunk. | ||
* | ||
* Chipmunk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Chipmunk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Chipmunk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package chipmunk; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class SimpleDemoProxyReceiver { | ||
|
||
public float callWithProxyArguments(SimpleDemoProxy proxy, Supplier<Integer> f){ | ||
return proxy.getA() + proxy.getB() + f.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright (C) 2024 MyWorld, LLC | ||
# All rights reserved. | ||
# | ||
# This file is part of Chipmunk. | ||
# | ||
# Chipmunk is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Chipmunk is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Chipmunk. If not, see <https://www.gnu.org/licenses/>. | ||
module test | ||
|
||
class ProxyTarget { | ||
|
||
def getA() 1 | ||
|
||
def getB() 2.0 | ||
|
||
} | ||
|
||
def main(receiver){ | ||
return receiver.callWithProxyArguments(ProxyTarget.new(), def() 4) | ||
} |