This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
151 additions
and
0 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
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() |
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 @@ | ||
/home/gitpod/.cache/bazel/_bazel_gitpod/f0d9a55f512e23344c81342e9a092f3d/execroot/com_google_j2cl_samples_helloworld/bazel-out/k8-fastbuild/bin |
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 @@ | ||
/home/gitpod/.cache/bazel/_bazel_gitpod/f0d9a55f512e23344c81342e9a092f3d/execroot/com_google_j2cl_samples_helloworld |
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 @@ | ||
/home/gitpod/.cache/bazel/_bazel_gitpod/f0d9a55f512e23344c81342e9a092f3d/execroot/com_google_j2cl_samples_helloworld/bazel-out |
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 @@ | ||
/home/gitpod/.cache/bazel/_bazel_gitpod/f0d9a55f512e23344c81342e9a092f3d/execroot/com_google_j2cl_samples_helloworld/bazel-out/k8-fastbuild/testlogs |
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,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
25
j2cl/src/main/java/com/google/j2cl/samples/wasm/HelloWorld.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,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(); | ||
} | ||
} |
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 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; | ||
}); |