Skip to content

Instantly share code, notes, and snippets.

@krk
Created December 31, 2021 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krk/01355fedc9df9dd639498e23b6fa0380 to your computer and use it in GitHub Desktop.
Save krk/01355fedc9df9dd639498e23b6fa0380 to your computer and use it in GitHub Desktop.
google-closure PureScript optimize via `assumeClosuresOnlyCaptureReferences`
diff --git a/src/com/google/javascript/jscomp/CompilationLevel.java b/src/com/google/javascript/jscomp/CompilationLevel.java
index 02ec99aee..0211a6cc1 100644
--- a/src/com/google/javascript/jscomp/CompilationLevel.java
+++ b/src/com/google/javascript/jscomp/CompilationLevel.java
@@ -181,7 +181,7 @@ public enum CompilationLevel {
options.setSmartNameRemoval(true);
options.setInlineConstantVars(true);
options.setInlineFunctions(Reach.ALL);
- options.setAssumeClosuresOnlyCaptureReferences(false);
+ options.setAssumeClosuresOnlyCaptureReferences(true);
options.setInlineVariables(Reach.ALL);
options.setComputeFunctionSideEffects(true);
options.setAssumeStrictThis(true);
FROM debian:buster-20211011-slim as base
RUN apt-get update -y && apt-get install curl -y
RUN curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.11.0/bazelisk-linux-amd64 -o /bin/bazelisk && \
chmod +x /bin/bazelisk && \
bazelisk --help
RUN apt-get install default-jre default-jdk -y
RUN apt-get install -y git
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV LANGUAGE C.UTF-8
# System deps
RUN apt-get update -y && apt-get install curl gnupg -y && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update -y && \
apt-get install -y nodejs yarn git npm
# without this the build fails:
RUN apt-get install unzip zip -y
# ---------
RUN apt-get install patch -y
RUN mkdir -p /app
WORKDIR /app
# Checkout closure compiler.
RUN git clone --single-branch -b master --no-tags https://github.com/google/closure-compiler
WORKDIR /app/closure-compiler
# Checkout a known-good commit and build.
RUN git checkout 2809700cd70d76b6212cd45540151b90c8d003ba
RUN bazelisk build //:compiler_unshaded_deploy.jar
COPY in.js /app
RUN java -jar bazel-bin/compiler_unshaded_deploy.jar -O ADVANCED \
--js /app/in.js --js_output_file /app/out.js
# Patch
COPY 0001-assumeClosuresOnlyCaptureReferences.patch /app
RUN patch src/com/google/javascript/jscomp/CompilationLevel.java /app/0001-assumeClosuresOnlyCaptureReferences.patch
RUN bazelisk build //:compiler_unshaded_deploy.jar
RUN java -jar bazel-bin/compiler_unshaded_deploy.jar -O ADVANCED \
--js /app/in.js --js_output_file /app/out-patched.js
RUN echo Vanilla: && cat /app/out.js && echo
RUN echo Patched: && cat /app/out-patched.js
# Expected output:
# Vanilla:
# (function(){function c(a){return function(b){console.log(a.show(b))}}return function(a){return function(b){return c(a)(b+"!")}}({show:function(a){return a}})("Mary")})();
#
# Patched:
# console.log("Mary!");
(function () {
var Show_string = { show: function (s) { return s; } };
function print(d) {
return function (v) {
console.log(d.show(v));
}
}
function greet(d) {
return function (v) {
return print(d)(v + "!");
}
}
return greet(Show_string)("Mary");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment