Skip to content

Commit 2992163

Browse files
committed
Improve photon demos
1 parent 1d27c2c commit 2992163

File tree

7 files changed

+52
-32
lines changed

7 files changed

+52
-32
lines changed

graalwasm/graalwasm-micronaut-photon/build-photon.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PHOTON_COMMIT="e95eccf886897c2efe8c2461fae9c6bf1375ff49"
1212

1313
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1414

15-
if [[ -d "$SCRIPT_DIR/target/classes/photon" ]]; then
15+
if [[ -d "${SCRIPT_DIR}/target/classes/photon" ]]; then
1616
echo "Photon already built from source. Nothing to do."
1717
exit 0
1818
fi
@@ -46,11 +46,11 @@ if [[ ! -d "photon-${PHOTON_COMMIT}" ]]; then
4646
fi
4747
pushd "photon-${PHOTON_COMMIT}" > /dev/null
4848

49-
wasm-pack build --dev --target bundler --out-name photon ./crate
49+
wasm-pack build --release --target bundler --out-name photon --out-dir "${SCRIPT_DIR}"/target/classes/photon ./crate
5050

51-
cp crate/examples/input_images/daisies_fuji.jpg $SCRIPT_DIR/target/classes
52-
mkdir -p $SCRIPT_DIR/target/classes/photon
53-
cp crate/pkg/photon* $SCRIPT_DIR/target/classes/photon
51+
echo "Copying example image..."
52+
53+
cp crate/examples/input_images/daisies_fuji.jpg "${SCRIPT_DIR}"/target/classes
5454

5555
popd > /dev/null
5656
popd > /dev/null

graalwasm/graalwasm-micronaut-photon/src/main/java/com/example/Photon.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.graalvm.polyglot.Value;
1010
import org.graalvm.polyglot.io.ByteSequence;
1111

12-
public record Photon(Value module, Value imageContent) {
12+
public record Photon(Value module, Uint8Array imageContent) {
1313

1414
boolean implementsEffect(String effectName) {
1515
return module.hasMember(effectName);
@@ -20,14 +20,23 @@ void applyEffect(String effectName, PhotonImage image) {
2020
}
2121

2222
PhotonImage createImage() {
23-
return module.getMember("PhotonImage").invokeMember("new_from_byteslice", imageContent).as(PhotonImage.class);
23+
PhotonImage photonImage = module.getMember("PhotonImage").as(PhotonImage.class);
24+
return photonImage.new_from_byteslice(imageContent);
2425
}
2526

2627
public interface PhotonImage {
2728
void free();
29+
30+
Uint8Array get_bytes();
31+
32+
PhotonImage new_from_byteslice(Uint8Array imageContent);
33+
}
34+
35+
public interface Uint8Array {
36+
ByteSequence buffer();
2837
}
2938

3039
public static byte[] toByteArray(PhotonImage photonImage) {
31-
return Value.asValue(photonImage).invokeMember("get_bytes").getMember("buffer").as(ByteSequence.class).toByteArray();
40+
return photonImage.get_bytes().buffer().toByteArray();
3241
}
3342
}

graalwasm/graalwasm-micronaut-photon/src/main/java/com/example/PhotonPool.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.example;
88

9+
import com.example.Photon.Uint8Array;
910
import io.micronaut.context.annotation.Context;
1011
import io.micronaut.core.io.ResourceResolver;
1112
import jakarta.annotation.PreDestroy;
@@ -53,21 +54,20 @@ public void close() {
5354
}
5455

5556
private static Photon createPhoton(Engine engine, Source photonSource, Object imageBytes) {
56-
org.graalvm.polyglot.Context context = org.graalvm.polyglot.Context.newBuilder("js", "wasm")
57+
var context = org.graalvm.polyglot.Context.newBuilder("js", "wasm")
5758
.engine(engine)
5859
.allowAllAccess(true)
5960
.allowExperimentalOptions(true)
60-
.option("js.webassembly", "true")
6161
.option("js.esm-eval-returns-exports", "true")
6262
.option("js.text-encoding", "true")
63+
.option("js.webassembly", "true")
6364
.build();
6465

65-
// Get Uint8Array class from JavaScript
66-
Value uint8Array = context.eval("js", "Uint8Array");
6766
// Load Photon module and initialize with wasm content
6867
Value photonModule = context.eval(photonSource);
68+
6969
// Create Uint8Array with image bytes
70-
Value imageContent = uint8Array.newInstance(imageBytes);
70+
Uint8Array imageContent = context.getBindings("js").getMember("Uint8Array").newInstance(imageBytes).as(Uint8Array.class);
7171

7272
return new Photon(photonModule, imageContent);
7373
}

graalwasm/graalwasm-spring-boot-photon/build-photon.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PHOTON_COMMIT="e95eccf886897c2efe8c2461fae9c6bf1375ff49"
1212

1313
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
1414

15-
if [[ -d "$SCRIPT_DIR/target/classes/photon" ]]; then
15+
if [[ -d "${SCRIPT_DIR}/target/classes/photon" ]]; then
1616
echo "Photon already built from source. Nothing to do."
1717
exit 0
1818
fi
@@ -46,11 +46,11 @@ if [[ ! -d "photon-${PHOTON_COMMIT}" ]]; then
4646
fi
4747
pushd "photon-${PHOTON_COMMIT}" > /dev/null
4848

49-
wasm-pack build --dev --target bundler --out-name photon ./crate
49+
wasm-pack build --release --target bundler --out-name photon --out-dir "${SCRIPT_DIR}"/target/classes/photon ./crate
5050

51-
cp crate/examples/input_images/daisies_fuji.jpg $SCRIPT_DIR/target/classes
52-
mkdir -p $SCRIPT_DIR/target/classes/photon
53-
cp crate/pkg/photon* $SCRIPT_DIR/target/classes/photon
51+
echo "Copying example image..."
52+
53+
cp crate/examples/input_images/daisies_fuji.jpg "${SCRIPT_DIR}"/target/classes
5454

5555
popd > /dev/null
5656
popd > /dev/null

graalwasm/graalwasm-spring-boot-photon/src/main/java/com/example/demo/Photon.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import org.graalvm.polyglot.Value;
1010
import org.graalvm.polyglot.io.ByteSequence;
11+
import org.springframework.aot.hint.RuntimeHints;
12+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
1113

12-
public record Photon(Value module, Value imageContent) {
14+
public record Photon(Value module, Uint8Array imageContent) {
1315

1416
boolean implementsEffect(String effectName) {
1517
return module.hasMember(effectName);
@@ -20,14 +22,30 @@ void applyEffect(String effectName, PhotonImage image) {
2022
}
2123

2224
PhotonImage createImage() {
23-
return module.getMember("PhotonImage").invokeMember("new_from_byteslice", imageContent).as(PhotonImage.class);
25+
PhotonImage photonImage = module.getMember("PhotonImage").as(PhotonImage.class);
26+
return photonImage.new_from_byteslice(imageContent);
2427
}
2528

2629
public interface PhotonImage {
2730
void free();
31+
32+
Uint8Array get_bytes();
33+
34+
PhotonImage new_from_byteslice(Uint8Array imageContent);
35+
}
36+
37+
public interface Uint8Array {
38+
ByteSequence buffer();
2839
}
2940

3041
public static byte[] toByteArray(PhotonImage photonImage) {
31-
return Value.asValue(photonImage).invokeMember("get_bytes").getMember("buffer").as(ByteSequence.class).toByteArray();
42+
return photonImage.get_bytes().buffer().toByteArray();
43+
}
44+
45+
static class PhotonRuntimeHints implements RuntimeHintsRegistrar {
46+
@Override
47+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
48+
hints.proxies().registerJdkProxy(PhotonImage.class);
49+
}
3250
}
3351
}

graalwasm/graalwasm-spring-boot-photon/src/main/java/com/example/demo/PhotonPool.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package com.example.demo;
88

9+
import com.example.demo.Photon.Uint8Array;
910
import jakarta.annotation.PreDestroy;
1011
import org.graalvm.polyglot.Context;
1112
import org.graalvm.polyglot.Engine;
@@ -60,17 +61,16 @@ private static Photon createPhoton(Engine engine, Source photonSource, Object im
6061
.engine(engine)
6162
.allowAllAccess(true)
6263
.allowExperimentalOptions(true)
63-
.option("js.webassembly", "true")
6464
.option("js.esm-eval-returns-exports", "true")
6565
.option("js.text-encoding", "true")
66+
.option("js.webassembly", "true")
6667
.build();
6768

68-
// Get Uint8Array class from JavaScript
69-
Value uint8Array = context.eval("js", "Uint8Array");
7069
// Load Photon module and initialize with wasm content
7170
Value photonModule = context.eval(photonSource);
71+
7272
// Create Uint8Array with image bytes
73-
Value imageContent = uint8Array.newInstance(imageBytes);
73+
Uint8Array imageContent = context.getBindings("js").getMember("Uint8Array").newInstance(imageBytes).as(Uint8Array.class);
7474

7575
return new Photon(photonModule, imageContent);
7676
}

graalwasm/graalwasm-spring-boot-photon/src/main/resources/META-INF/native-image/org.example/demo/proxy-config.json

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

0 commit comments

Comments
 (0)