diff --git a/README.md b/README.md index 59c2d20..9392d81 100644 --- a/README.md +++ b/README.md @@ -1,166 +1,30 @@ -# Rhino: JavaScript in Java +# 环境 -Rhino (234581759) +JDK 8 -Rhino is an implementation of JavaScript in Java. +# 执行 jar -## License +到 jar 文件的所在目录执行命令 -Rhino is licensed under the [MPL 2.0](./LICENSE.txt). +`-f` 是想要转换的 js 文件 -## Releases +`-o` 是输出的目录 - - - - - - - - - - - - - -
Rhino 1.7R5January 29, 2015
Rhino 1.7.6April 15, 2015
Rhino 1.7.7June 17, 2015
Rhino 1.7.7.1February 2, 2016
Rhino 1.7.7.2August 24, 2017
Rhino 1.7.8January 22, 2018
Rhino 1.7.9March 15, 2018
Rhino 1.7.10April 9, 2018
Rhino 1.7.11May 30, 2019
Rhino 1.7.12January 13, 2020
Rhino 1.7.13September 2, 2020
Rhino 1.7.14January 6, 2022
- -[Release Notes](./RELEASE-NOTES.md) for recent releases. - -[Compatibility table](https://mozilla.github.io/rhino/compat/engines.html) which shows which advanced JavaScript -features from ES6, and ES2016+ are implemented in Rhino. - -[![GitHub Action Status](https://github.com/mozilla/rhino/actions/workflows/gradle.yml/badge.svg)](https://github.com/mozilla/rhino/actions/workflows/gradle.yml) - -[![Mozilla](https://circleci.com/gh/mozilla/rhino.svg?style=shield)](https://app.circleci.com/pipelines/github/mozilla/rhino) - -## Documentation - -Information for script builders and embedders: - -[Archived](http://web.archive.org/web/20210304081342/https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Documentation) - -JavaDoc for all the APIs: - -[https://javadoc.io/doc/org.mozilla/rhino](https://javadoc.io/doc/org.mozilla/rhino) - -More resources if you get stuck: - -[https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Community](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Community) - -## Building - -### How to Build - -Rhino builds with `Gradle`. Here are some useful tasks: -``` -./gradlew jar -``` -Build and create `Rhino` jar in the `buildGradle/libs` directory. -``` -git submodule init -git submodule update -./gradlew test -``` -Build and run all the tests, including the official [ECMAScript Test Suite](https://github.com/tc39/test262). -See [Running tests](testsrc/README.md) for more detailed info about running tests. -``` -./gradlew testBenchmark -``` -Build and run benchmark tests. - -## Releasing and publishing new version - -1. Ensure all tests are passing -2. Remove `-SNAPSHOT` from version in `gradle.properties` in project root folder -3. Create file `gradle.properties` in `$HOME/.gradle` folder with following properties. Populate them with maven repo credentials and repo location. -``` -mavenUser= -mavenPassword= -mavenSnapshotRepo= -mavenReleaseRepo= +```bash +java -jar .\rhino-Rhino1_7_14_Release-1.7.14.jar -f E:\Work\AutojsProject\autox-super-kit\out\main.js -o E:\Work\AutojsProject\autox-super-kit\out\dist\ ``` -4. Run `Gradle` task to publish artifacts to Maven Central. +# 打包 jar ``` -./gradlew publish +IDEA 右侧的 gradle - Tasks - build - jar ``` -5. Increase version and add `-SNAPSHOT` to it in `gradle.properties` in project root folder. -6. Push `gradle.properties` to `GitHub` - -## Running -Rhino can run as a stand-alone interpreter from the command line: +# 修改 main 入口 ``` -java -jar buildGradle/libs/rhino-1.7.12.jar -debug -version 200 -Rhino 1.7.9 2018 03 15 -js> print('Hello, World!'); -Hello, World! -js> -``` -There is also a "rhino" package for many Linux distributions as well as Homebrew for the Mac. - -You can also embed it, as most people do. See below for more docs. - -### Java 16 and later - -If you are using a modular JDK that disallows the reflective access to -non-public fields (16 and later), you may need to configure the JVM with the -[`--add-opens`](https://docs.oracle.com/en/java/javase/17/migrate/migrating-jdk-8-later-jdk-releases.html#GUID-12F945EB-71D6-46AF-8C3D-D354FD0B1781) -option to authorize the packages that your scripts shall use, for example: -``` ---add-opens java.desktop/javax.swing.table=ALL-UNNAMED -``` - -## Issues - -Most issues are managed on GitHub: - -[https://github.com/mozilla/rhino/issues](https://github.com/mozilla/rhino/issues) - -## Contributing PRs - -To submit a new PR, please use the following process: - -* Ensure that your entire build passes "./gradlew check". This will include -code formatting and style checks and runs the tests. -* Please write tests for what you fixed, unless you can show us that existing -tests cover the changes. Use existing tests, such as those in -"testsrc/org/mozilla/javascript/tests", as a guide. -* If you fixed ECMAScript spec compatibility, take a look at test262.properties and see -if you can un-disable some tests. -* Push your change to GitHub and open a pull request. -* Please be patient as Rhino is only maintained by volunteers and we may need -some time to get back to you. -* Thank you for contributing! - -### Code Formatting - -Code formatting was introduced in 2021. The "spotless" plugin will fail your -build if you have changed any files that have not yet been reformatted. -Please use "spotlessApply" to reformat the necessary files. - -If you are the first person to touch a big file that spotless wants to make -hundreds of lines of changes to, please try to put the reformatting changes -alone into a single Git commit so that we can separate reformatting changes -from more substantive changes. - -> **Warning:** If you build with Java 16 or later, you need to apply a -> workaround for a "spotless" issue. Otherwise, the task will be disabled -> and your PR may fail. -> -> The following must be added to your `gradle.properties`. -> ``` -> org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ -> --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \ -> --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ -> --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ -> --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED -> ``` -> For more details, see https://github.com/diffplug/spotless/issues/834#issuecomment-819118761 - -## More Help - -The Google group is the best place to go with questions: +build.gradle 中 修改 Main-Class -[https://groups.google.com/forum/#!forum/mozilla-rhino](https://groups.google.com/forum/#!forum/mozilla-rhino) +jar + manifest + attributes + Main-Class +``` \ No newline at end of file diff --git a/build.gradle b/build.gradle index 6f452c5..8be67d3 100644 --- a/build.gradle +++ b/build.gradle @@ -61,6 +61,7 @@ dependencies { // 添加依赖 implementation 'cn.hutool:hutool-all:5.3.6' implementation group: 'com.itranswarp', name: 'compiler', version: '1.0' + implementation 'commons-cli:commons-cli:1.5.0' // 原始依赖 testImplementation "junit:junit:4.13.2" @@ -211,22 +212,33 @@ task engineJar(type: Jar) { jar { dependsOn compileJava - from ('LICENSE.txt') { - into 'META-INF' - } - from ('NOTICE.txt') { - into 'META-INF' - } - from ('NOTICE-tools.txt') { - into 'META-INF' +// from ('LICENSE.txt') { +// into 'META-INF' +// } +// from ('NOTICE.txt') { +// into 'META-INF' +// } +// from ('NOTICE-tools.txt') { +// into 'META-INF' +// } + + // 将 dx.jar 打包到 最终jar 的根目录 + from ("resources/dx-29.0.3.jar"){ + into "" } from sourceSets.main.output excludes = ["org/mozilla/javascript/engine/**", "META-INF/services/**"] // Class ImplementationVersion uses 'Implementation-Title' + + from { + // 打包第三方的依赖到 jar 中 + configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } + } manifest { attributes( "Manifest-Version": "1.0", - "Main-Class": "org.mozilla.javascript.tools.shell.Main", + //"Main-Class": "org.mozilla.javascript.tools.shell.Main" + "Main-Class": "org.mozilla.mycode.Main", // 修改命令行的 main 入口 "Implementation-Version": project.version, "Implementation-Title": "Mozilla Rhino", "Implementation-Vendor": "Mozilla Foundation", diff --git a/resources/dx-29.0.3.jar b/resources/dx-29.0.3.jar new file mode 100644 index 0000000..0e0aec0 Binary files /dev/null and b/resources/dx-29.0.3.jar differ diff --git a/src/org/mozilla/mycode/main.java b/src/org/mozilla/mycode/main.java index 2fda999..530b11c 100644 --- a/src/org/mozilla/mycode/main.java +++ b/src/org/mozilla/mycode/main.java @@ -1,12 +1,14 @@ package org.mozilla.mycode; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.resource.ResourceUtil; import cn.hutool.core.lang.Console; import cn.hutool.core.util.CharsetUtil; import com.itranswarp.compiler.JavaStringCompiler; import org.mozilla.javascript.CompilerEnvirons; import org.mozilla.javascript.Context; import org.mozilla.javascript.optimizer.ClassCompiler; +import org.apache.commons.cli.*; import java.io.BufferedReader; import java.io.File; @@ -19,8 +21,7 @@ import java.util.Date; import java.util.Map; -class Main -{ +class Main { static final String JAVA_SOURCE_CODE = "" + "package defpackage;" + @@ -43,14 +44,47 @@ class Main " }" + " } "; - public static void main(String[] args) throws Exception - { - String filePath = "E:\\Work\\AutojsProject\\autox-super-kit\\out\\main.js"; - toClassFile(FileUtil.readUtf8String(filePath)); - } + // 工作的目录 + public static String _outDirPath = ""; + + public static void main(String[] args) throws Exception { + + // 创建 Options 对象 + Options options = new Options(); + options.addOption("f", "file", true, "输入文件"); + options.addOption("o", "output", true, "输出的目录"); + + // 创建 CommandLineParser 对象 + CommandLineParser parser = new DefaultParser(); + + try { + // 解析命令行参数 + CommandLine cmd = parser.parse(options, args); - //文件操作的 根路径 - public static String BASE_DIR_PATH = "E:\\Desktop\\RhinoScript\\" + getTimeString() + "\\"; + // 获取解析后的参数值 + String inputFile = cmd.getOptionValue("f"); + String outputDir = cmd.getOptionValue("o"); + + // 打印参数值 + System.out.println("输入文件:" + inputFile); + System.out.println("输出文件夹:" + outputDir); + // 输出目录 再创建一个唯一的文件夹 (使用当前时间精确到秒) + _outDirPath = outputDir + "\\" + getTimeString() + "\\"; + System.out.println("最终的输出地址:" + _outDirPath); + + // 将 当前 jar 程序中的 dx.jar 释放到 outputDir 目录 + byte[] fileData = ResourceUtil.readBytes("dx-29.0.3.jar"); + FileUtil.writeBytes(fileData, _outDirPath + "dx-29.0.3.jar"); + + toClassFile(FileUtil.readUtf8String(inputFile)); + + } catch (ParseException e) { + // 解析失败,打印错误消息 + System.err.println("命令行参数解析失败: " + e.getMessage()); + } + +// String filePath = "E:\\Work\\AutojsProject\\autox-super-kit\\out\\main.js"; + } /** * 获取时间 字符串 @@ -58,24 +92,22 @@ public static void main(String[] args) throws Exception * @return */ - public static String getTimeString() - { + public static String getTimeString() { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss"); return formatter.format(date); } - //这里注意传参是 js 文本数据,不是js路径 - static void toClassFile(String script) throws Exception - { - //创建Rhino编译环境 相关参数.. + //这里注意传参是 js 文本数据,不是 js 路径 + static void toClassFile(String script) throws Exception { + //创建 Rhino 编译环境 相关参数.. CompilerEnvirons compilerEnv = new CompilerEnvirons(); - compilerEnv.setGeneratingSource(false); //编译后,不添加js源码 + compilerEnv.setGeneratingSource(false); //编译后,不添加 js 源码 compilerEnv.setLanguageVersion(Context.VERSION_ES6); //设置 支持es6 compilerEnv.setOptimizationLevel(9); // 优化等级改为 9 级. 如果有问题 就改成0 ClassCompiler compiler = new ClassCompiler(compilerEnv); - //compileToClassFiles的第4个参数比较重要,它表明了js转成.class的类路径,影响到 在autojs调用的方法 + // compileToClassFiles 的第4个参数比较重要,它表明了js转成.class的类路径,影响到 在 autojs 调用的方法 // 不填写包名 则 默认在 defpackage 中 // auto js调用例子 (如果autojs闪退.就是抛出了异常.请使用开源的autojs 代码, 使用 AdnroidStudio 进行调试,查看异常) @@ -89,8 +121,7 @@ static void toClassFile(String script) throws Exception 1, "aaa"); - for (int j = 0; j != compiled.length; j += 2) - { + for (int j = 0; j != compiled.length; j += 2) { //String className = (String) compiled[j]; JavaStringCompiler compiler2 = new JavaStringCompiler(); @@ -103,64 +134,54 @@ static void toClassFile(String script) throws Exception //解密工具类的 数据 byte[] utilsBytes = results.get("defpackage.StrUtils"); - String utilsClassPath = BASE_DIR_PATH + "defpackage//StrUtils.class"; + String utilsClassPath = _outDirPath + "class\\defpackage\\StrUtils.class"; File utilsFile = new File(utilsClassPath); utilsFile.getParentFile().mkdirs(); //创建文件夹 - try (FileOutputStream fos = new FileOutputStream(utilsFile)) - { + try (FileOutputStream fos = new FileOutputStream(utilsFile)) { fos.write(utilsBytes); - } - catch (FileNotFoundException e) - { + } catch (FileNotFoundException e) { System.out.println("utils 文件未找到!"); e.printStackTrace(); - } - catch (IOException e) - { + } catch (IOException e) { System.out.println("utils 文件保存失败!"); e.printStackTrace(); } //------------ - String classPath = BASE_DIR_PATH + "aaa.class"; + String classPath = _outDirPath + "class\\aaa.class"; //js 转为 class byte[] bytes = (byte[]) compiled[(j + 1)]; File file = new File(classPath); file.getParentFile().mkdirs(); //创建文件夹 - try (FileOutputStream fos = new FileOutputStream(file)) - { + try (FileOutputStream fos = new FileOutputStream(file)) { fos.write(bytes); - } - catch (FileNotFoundException e) - { + } catch (FileNotFoundException e) { System.out.println("文件未找到!"); e.printStackTrace(); - } - catch (IOException e) - { + } catch (IOException e) { System.out.println("文件保存失败!"); e.printStackTrace(); } - //将两个class 打包为 一个jar - cmdExec("jar cvf demo.jar *"); + //将两个 class 打包为 一个jar + cmdExec("jar cvf demo.jar -C class ."); System.out.println(utilsFile.exists()); System.out.println("开始转dex,js代码越多,耗时越长,请耐心等待"); //将 jar 转为 dex - cmdExec("java -jar E:\\Software\\androidstudioSDK\\build-tools\\29.0.3\\lib\\dx.jar --dex " + + cmdExec("java -jar dx-29.0.3.jar --dex " + "--output=aaa.dex " + "demo.jar"); System.out.println(); System.out.println(); System.out.println("==================================="); - System.out.println("❤❤❤❤ js转dex 结束"); + System.out.println("js 转 dex 结束"); } - System.out.println("❤❤❤❤ 编译成功!dex文件保存位置: " + BASE_DIR_PATH); + System.out.println("编译成功!dex 文件保存位置: " + _outDirPath); System.out.println("==================================="); } @@ -169,54 +190,41 @@ static void toClassFile(String script) throws Exception * * @param cmd */ - public static void cmdExec(String cmd) - { + public static void cmdExec(String cmd) { Runtime run = Runtime.getRuntime(); - try - { - Process p = run.exec(cmd, new String[]{}, new File(BASE_DIR_PATH)); + try { + Process p = run.exec(cmd, new String[]{}, new File(_outDirPath)); // 设置目录 InputStream ins = p.getInputStream(); InputStream ers = p.getErrorStream(); new Thread(new inputStreamThread(ins)).start(); p.waitFor(); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); - } - catch (InterruptedException e) - { + } catch (InterruptedException e) { e.printStackTrace(); } } - static class inputStreamThread implements Runnable - { + static class inputStreamThread implements Runnable { private InputStream ins = null; private BufferedReader bfr = null; - public inputStreamThread(InputStream ins) - { + public inputStreamThread(InputStream ins) { this.ins = ins; this.bfr = new BufferedReader(new InputStreamReader(ins)); } @Override - public void run() - { + public void run() { String line = null; byte[] b = new byte[100]; int num = 0; - try - { - while ((num = ins.read(b)) != -1) - { + try { + while ((num = ins.read(b)) != -1) { System.out.println(new String(b, CharsetUtil.CHARSET_GBK)); } - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); } }