Skip to content

Commit

Permalink
[block] snapshot
Browse files Browse the repository at this point in the history
- self made block works
- provided block cause crash
  • Loading branch information
umjammer committed Feb 26, 2024
1 parent b83918c commit d61e12e
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.rococoa.ID;
import org.rococoa.ObjCBlock;
import org.rococoa.ObjCBlocks.BlockLiteral;
import org.rococoa.ObjCClass;
import org.rococoa.ObjCObjectByReference;
import org.rococoa.Rococoa;
Expand Down Expand Up @@ -38,7 +39,7 @@ public interface _Class extends ObjCClass {
* Creates a model container to be used with VNCoreMLRequest
* based on a Core ML model, with an optional completion handler.
*/
public abstract VNCoreMLRequest initWithModel_completionHandler(VNCoreMLModel model, ID/*VNRequestCompletionHandler*/ completionHandler);
public abstract VNCoreMLRequest initWithModel_completionHandler(VNCoreMLModel model, BlockLiteral /* ^VNRequestCompletionHandler */ completionHandler);

/** utility */
public static VNCoreMLRequest newRequest(VNCoreMLModel model) {
Expand All @@ -51,6 +52,6 @@ public static VNCoreMLRequest newRequest(VNCoreMLModel model) {
public abstract VNCoreMLModel model();

public interface VNRequestCompletionHandler extends ObjCBlock {
void callback(ID id, ObjCObjectByReference error);
void callback(BlockLiteral literal, ID id, ID error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.rococoa.ObjCBlocks;
import org.rococoa.Rococoa;
import org.rococoa.cocoa.coregraphics.CGImage;
import org.rococoa.cocoa.foundation.NSError;
Expand All @@ -36,6 +35,8 @@
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;

import static org.rococoa.ObjCBlocks.block;


/**
* CoreMLTest.
Expand Down Expand Up @@ -123,22 +124,24 @@ public void paintComponent(Graphics g) {
}

@Test
@Disabled("TODO objc-block")
@Disabled("crash")
@EnabledIf("localPropertiesExists")
@EnabledIfSystemProperty(named = "vavi.test", matches = "ide")
void test2() throws Exception {
MLModel mlModel = MLModel.fromPath(model2);
VNCoreMLModel model = VNCoreMLModel.fromMLModel(mlModel);

VNCoreMLRequest.CLASS.alloc().initWithModel_completionHandler(model, ObjCBlocks.block((VNCoreMLRequest.VNRequestCompletionHandler) (requestId, errorRef) -> {
NSError error = errorRef.getValueAs(NSError.class);
VNCoreMLRequest.CLASS.alloc().initWithModel_completionHandler(model,
block((VNCoreMLRequest.VNRequestCompletionHandler) (literal, requestId, errorRef) -> {
NSError error = Rococoa.wrap(errorRef, NSError.class);
if (error != null) {
throw new IllegalStateException(error.description());
}
Debug.println("here1");
VNCoreMLRequest request = Rococoa.wrap(requestId, VNCoreMLRequest.class);
Debug.println("request: " + request);
}));
Debug.println("here");
Debug.println("here2");
}

@Test
Expand Down
40 changes: 29 additions & 11 deletions rococoa-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
<fileNames>
<fileName>Rococoa.m</fileName>
<fileName>ProxyForJava.m</fileName>
<fileName>ObjCBlocks.m</fileName>
<fileName>test.m</fileName>
</fileNames>
</source>
Expand All @@ -131,7 +130,6 @@
<fileNames>
<fileName>Rococoa.m</fileName>
<fileName>ProxyForJava.m</fileName>
<fileName>ObjCBlocks.m</fileName>
</fileNames>
</source>
</sources>
Expand All @@ -142,20 +140,40 @@
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>on jitpack</id>
<activation>
<property>
<name>env.JITPACK</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<goals>
<goal>run</goal>
<phase>verify</phase>
</goals>
<configuration>
<target>
<copy file="${project.build.outputDirectory}/librococoa.dylib" toFile="src/main/resources/librococoa.dylib" />
</target>
</configuration>
<executions>
<execution>
<id>assets-download</id>
<goals>
<goal>run</goal>
</goals>
<phase>compile</phase>
<configuration>
<target>
<get src="${project.url}/releases/download/${project.version}/librococoa.dylib"
dest="${project.build.outputDirectory}"
usetimestamp="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
42 changes: 14 additions & 28 deletions rococoa-core/src/main/java/org/rococoa/ObjCBlock.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
/*
* BridJ - Dynamic and blazing-fast native interop for Java.
* http://bridj.googlecode.com/
* Copyright (c) 2022 by Naohide Sano, All rights reserved.
*
* Copyright (c) 2010-2015, Olivier Chafik (http://ochafik.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Olivier Chafik nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Programmed by Naohide Sano
*/

package org.rococoa;

import com.sun.jna.Callback;
import org.rococoa.ObjCBlocks.BlockLiteral;


/**
* ObjCBlock.
*
* <p>
* extended class's method must have {@link BlockLiteral} as a 1st argument.
* TODO hide this from user
* </p>
* e.g.
* <pre>
* // for int (^MyBlock)(int)
* public interface MyBlock extends ObjCBlock {
* int apply(BlockLiteral literal, int n);
* }
* </pre>
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2022/10/22 nsano initial version <br>
*/
Expand Down
88 changes: 54 additions & 34 deletions rococoa-core/src/main/java/org/rococoa/ObjCBlocks.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,71 @@
/*
* BridJ - Dynamic and blazing-fast native interop for Java.
* http://bridj.googlecode.com/
* Copyright (c) 2024 by Naohide Sano, All rights reserved.
*
* Copyright (c) 2010-2015, Olivier Chafik (http://ochafik.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Olivier Chafik nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Programmed by Naohide Sano
*/

package org.rococoa;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;


/**
* ObjCBlocks.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2024/02/26 nsano initial version <br>
*/
public class ObjCBlocks {

private static final Logger logging = Logger.getLogger("org.rococoa.foundation");

public static ID block(ObjCBlock instance) {
ID id = Rococoa.proxy(instance).id();
logging.info(String.format("instance: %s, %16x", instance, id.longValue()));
Pointer pBlock = Foundation.getRococoaLibrary().createObjCBlockWithFunctionPointer(id);
logging.info("pBlock: " + pBlock);
Runtime.getRuntime().addShutdownHook(new Thread(() -> Foundation.getRococoaLibrary().releaseObjCBlock(pBlock)));
return ID.fromLong(Pointer.nativeValue(pBlock));
public static final int BLOCK_IS_NOESCAPE = 1 << 23;
public static final int BLOCK_HAS_COPY_DISPOSE = 1 << 25;
/** helpers have C++ code */
public static final int BLOCK_HAS_CTOR = 1 << 26;
public static final int BLOCK_IS_GLOBAL = 1 << 28;
/** IFF BLOCK_HAS_SIGNATURE */
public static final int BLOCK_HAS_STRET = 1 << 29;
public static final int BLOCK_HAS_SIGNATURE = 1 << 30;

/** utility conversion java closure to obj-c block */
public static BlockLiteral block(ObjCBlock block) {
BlockLiteral literal = new BlockLiteral();
literal.flags = 0;
literal.invoke = block;
literal.write();
return literal;
}

/** */
public static class BlockDescriptor extends Structure {
public NativeLong reserved;
public NativeLong block_size;
public Pointer rest;
public BlockDescriptor() {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("reserved", "block_size", "rest");
}
}

/** */
public static class BlockLiteral extends Structure {
public Pointer isa;
public int flags;
public int reserved;
public ObjCBlock invoke;
public BlockDescriptor descriptor;
public BlockLiteral() {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("isa", "flags", "reserved", "invoke", "descriptor");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Pointer;
import org.rococoa.ID;
import org.rococoa.ObjCBlock;


/**
Expand Down Expand Up @@ -63,13 +61,4 @@ interface VoidCallback extends Callback {
* Call callback on the main Cococa event thread
*/
void callOnMainThread(RococoaLibrary.VoidCallback callback, boolean waitUntilDone);

/** for block */
Pointer createObjCBlockWithFunctionPointer(ID pcb);

/** for block */
ObjCBlock getObjCBlockFunctionPointer(Pointer blockPtr);

/** for block */
void releaseObjCBlock(Pointer blockPtr);
}
Loading

0 comments on commit d61e12e

Please sign in to comment.