From a62672140c19774f418213750332d9a54b6ad116 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 24 Jan 2025 11:15:29 -0800 Subject: [PATCH] Declare global define aliases that J2clUtilGetDefineRewriterPass can read from This will allow goog.defines declared in goog.modules to be consumable by J2CL's System.getProperty infrastructure. Since these reads are out-of-scope of the declaration, we need to provide some hook to link the two. To do this we define a global variable to serve as a well-known alias to the define. For the J2CL pass we can assume what the global variable is based on the define name, so we just simply replace the getProperty references with the appropriate global, if it exists. PiperOrigin-RevId: 719370016 --- .../integration/java/systemgetproperty/BUILD | 29 ++++++++++++++++++- .../java/systemgetproperty/Main.native.js | 2 ++ .../integration/java/systemgetproperty/Zoo.js | 21 ++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Main.native.js create mode 100644 transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Zoo.js diff --git a/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/BUILD b/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/BUILD index 80fcfbb824..23f6253bbb 100644 --- a/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/BUILD +++ b/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/BUILD @@ -1,3 +1,4 @@ +load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library") load( "//transpiler/javatests/com/google/j2cl/integration:integration_test.bzl", "integration_test", @@ -8,10 +9,36 @@ package( licenses = ["notice"], ) +closure_js_library( + name = "zoo", + srcs = ["Zoo.js"], + deps = ["//:jre"], +) + +alias( + name = "zoo-j2cl", + actual = ":zoo", +) + +alias( + name = "zoo-j2wasm", + actual = ":zoo", +) + integration_test( name = "systemgetproperty", - srcs = glob(["*.java"]), + srcs = glob( + [ + "*.java", + "*.js", + ], + exclude = ["Zoo.js"], + ), + closure_defines = { + "zoo": "buzz", + }, # This test is specific to J2CL's implementation of System.getProperty(). enable_jvm_test = False, enable_kt = False, + deps = [":zoo"], ) diff --git a/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Main.native.js b/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Main.native.js new file mode 100644 index 0000000000..1ce8d56e61 --- /dev/null +++ b/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Main.native.js @@ -0,0 +1,2 @@ +/** @suppress {extraRequire} */ +goog.require('systemgetproperty.Zoo'); diff --git a/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Zoo.js b/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Zoo.js new file mode 100644 index 0000000000..fdb8c19dc9 --- /dev/null +++ b/transpiler/javatests/com/google/j2cl/integration/java/systemgetproperty/Zoo.js @@ -0,0 +1,21 @@ +// Copyright 2025 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 +// +// https://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('systemgetproperty.Zoo'); + +const jre = goog.require('jre'); + +/** @define {string} */ +const localZoo = goog.define('zoo', 'default'); +jre.addSystemPropertyFromGoogDefine('zoo', localZoo); \ No newline at end of file