Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Add j2cl demo #11

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ RUN npm install jsvu wasm-opt wasm-pack -g \
&& dpkg -i tinygo_0.23.0_amd64.deb \
&& rm tinygo_0.23.0_amd64.deb

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& curl -fsSL https://storage.googleapis.com/www.bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel-archive-keyring.gpg \
&& mv bazel-archive-keyring.gpg /usr/share/keyrings/ \
&& echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" > /etc/apt/sources.list.d/bazel.list

RUN apt-get update && apt-get install bazel-5.1.0 && ln -s /usr/bin/bazel-5.1.0 /usr/bin/bazel

RUN curl -fsSL https://github.com/WebAssembly/binaryen/releases/download/version_108/binaryen-version_108-x86_64-linux.tar.gz | tar -xzf - \
&& (cd binaryen-version_108; cp -rf * /usr) && rm -rf binaryen*

USER gitpod

RUN rustup target add wasm32-wasi wasm32-unknown-unknown \
Expand Down
1 change: 1 addition & 0 deletions java/browser-hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel-*
9 changes: 9 additions & 0 deletions java/browser-hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Java Implementation of WASM using J2CL

This sample demonstrates how to use [j2cl](https://github.com/google/j2cl) to implement a WASM function in Java and call it from the browser on port 8000.

To launch the browser (you need Chrome unstable):

```
$ google-chrome-unstable --js-flags="--experimental-wasm-typed-funcref --experimental-wasm-gc --experimental-wasm-eh"
```
27 changes: 27 additions & 0 deletions java/browser-hello-world/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
workspace(name = "com_google_j2cl_samples_helloworld")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "com_google_elemental2",
strip_prefix = "elemental2-1.1.0",
url = "https://github.com/google/elemental2/archive/1.1.0.zip",
)

load("@com_google_elemental2//build_defs:repository.bzl", "load_elemental2_repo_deps")
load_elemental2_repo_deps()

load("@com_google_elemental2//build_defs:workspace.bzl", "setup_elemental2_workspace")
setup_elemental2_workspace()

# Load j2cl repository
http_archive(
name = "com_google_j2cl",
strip_prefix = "j2cl-master",
urls = ["https://github.com/google/j2cl/archive/refs/heads/master.zip"],
)

load("@com_google_j2cl//build_defs:repository.bzl", "load_j2cl_repo_deps")
load_j2cl_repo_deps()

load("@com_google_j2cl//build_defs:rules.bzl", "setup_j2cl_workspace")
setup_j2cl_workspace()
5 changes: 5 additions & 0 deletions java/browser-hello-world/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -ex

bazel run src/main/java/com/google/j2cl/samples/wasm:jsapp_dev_server
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Sample for J2WASM copied from https://github.com/google/j2cl.
#
# Note that J2WASM is experimental and it is NOT production ready.
#
# It is mostly developed to help with the evolution of WASM GC specification and
# what is released externally here is minimal working version to provide .wat files
# for community experimentation and it doesn't reflect final workings of the product
# nor the WASM spec.

load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library")
load(
"@com_google_j2cl//build_defs:rules.bzl",
"j2cl_application",
"j2wasm_application",
"j2wasm_library",
)

package(licenses = ["notice"])

# This is the bazel target that compiles our J2WASM library.
# Since J2WASM currently does global compilation this only serves as a way
# to collect sources and byte code required for j2wasm_application compilation.
j2wasm_library(
name = "helloworld",
srcs = glob(["*.java"]),
)

# This is the bazel target that compiles and optimizes whole J2WASM app.
# It provides couple of convenient targets:
# :app produces the app.wat and app.wasm that could be used for production.
# :app_dev produces app_dev.wat and app_dev.wasm as development version
#
# e.g.:
# $ (cd j2cl; bazel build src/main/java/com/google/j2cl/samples/wasm:app)
#
# Note that for .wat files (wasm binary output) to be available, you would
# need to have a recent version of binaryen available in your path.
j2wasm_application(
name = "app",
entry_points = [r"com\.google\.j2cl\.samples\.wasm\.HelloWorld\.getHelloWorld"],
deps = [":helloworld"],
)

# Below is an example of the JS wiring of the wasm app

closure_js_library(
name = "hellojs",
srcs = glob(["*.js"]),
lenient = True,
deps = ["@com_google_j2cl//:j2wasm_js"],
)

# This is the bazel target that serves your J2WASM app.
#
# Give it a try:
# $ (cd j2cl; bazel run src/main/java/com/google/j2cl/samples/wasm:jsapp_dev_server)
#
# To launch the browser (use unstable):
#
# $ google-chrome-unstable --js-flags="--experimental-wasm-typed-funcref --experimental-wasm-gc --experimental-wasm-eh"
#
j2cl_application(
name = "jsapp",
entry_points = ["entry"],
extra_dev_resources = [
":app.wasm",
],
deps = [":hellojs"],
dev_server_port = "8000",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.j2cl.samples.wasm;

import javaemul.internal.WasmExtern;

/** A simple hello world example. */
public class HelloWorld {
public static WasmExtern getHelloWorld() {
return "Hello from Java!".toJsString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

goog.module('entry');

const j2wasm = goog.require('j2wasm');

j2wasm.instantiateStreaming('app.wasm').then((instance) => {
document.body.innerText = instance.exports.getHelloWorld();
}, (err) => {
document.body.style.color = 'red';
document.body.innerText = `Failed to load wasm: ${err}`;
// rethrow so it also bubbles up to the console.
throw err;
});