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

Commit

Permalink
Add j2cl demo (requires ipv6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Jun 6, 2022
1 parent 2b34a7d commit 086ad0e
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 0 deletions.
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
15 changes: 15 additions & 0 deletions j2cl/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
workspace(name = "com_google_j2cl_samples_helloworld")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# 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()
1 change: 1 addition & 0 deletions j2cl/bazel-bin
1 change: 1 addition & 0 deletions j2cl/bazel-j2cl
1 change: 1 addition & 0 deletions j2cl/bazel-out
1 change: 1 addition & 0 deletions j2cl/bazel-testlogs
69 changes: 69 additions & 0 deletions j2cl/src/main/java/com/google/j2cl/samples/wasm/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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"],
)
25 changes: 25 additions & 0 deletions j2cl/src/main/java/com/google/j2cl/samples/wasm/HelloWorld.java
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();
}
}
28 changes: 28 additions & 0 deletions j2cl/src/main/java/com/google/j2cl/samples/wasm/hello.js
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;
});

0 comments on commit 086ad0e

Please sign in to comment.