javascript - 将需要 CommonJS 功能的 JavaScript 编译为 native Java

标签 javascript java native rhino commonjs

我想知道是否有人尝试过/有幸将实现 CommonJS API (require()) 的 JavaScript 文件编译为 native Java 代码。

我目前正在构建一个网站,该网站将使用与此类似的文本编辑器,该编辑器是 stackoverflow 开源的(WMD 编辑器的端口): http://code.google.com/p/pagedown/

该项目附带了一些 JavaScript 文件,它们可以清理编辑器的标记语言并将其转换为有效的 HTML。

在使用了 Rhino 1.7R4 后,我成功地在运行时加载了 JavaScript 文件/模块,并且能够随时执行 require 函数。

但是,我宁愿将这些脚本预编译为 native Java 代码。我已经按照这个 Rhino 文档成功编译了不使用 CommonJS 功能的单个脚本:

https://developer.mozilla.org/en-US/docs/Rhino/JavaScript_Compiler

但是我不知道编译多个实现 CommonJS 功能的依赖脚本的正确方法,而且 Rhino 本身也没有很好的文档记录。

这个想法是使用原生 Java 代码进行标记转换和清理,而无需编写/维护两个不同代码库的翻译成本(只需将 JS 转换为 JAVA)。

干杯

最佳答案

RingoJS 可以做这样的事情:

Ringo implements the CommonJS Modules specification. In short this means:

Every JavaScript file is treated a module living in its own top-level scope.

Any function or property attached to a module's exports object will be exposed.

The require() function returns a module's exports object.

If the given identifier string starts with ./ or ../ Ringo's module loader searches for a file and tries to load it. Therefore require('./foo') advices Ringo to load the file ./foo.js as module.

Otherwise Ringo looks for the module in every folder of the module path.

The module path is a list of standard locations in which Ringo will look for modules.

The module path can be set in the following ways:

Setting the RINGO_MODULE_PATH environment variable.

Setting the ringo.modulepath Java system property.

Using the -m or --modules option to the ringo command-line tool.

Using the module-path servlet init parameter with JsgiServlet.

Adding or removing elements to the require.paths Array-like property from within Ringo.

Packages provide a means of bundling several modules and other resources into one unit. Packages are directories that contain a package.json package descriptor file. The main property in the package.json descriptor is recognized by Ringo's module loader as the main entry point for a module:

{
    "main": "lib/main.js"
}

If a module id resolves directly to a package directory and package.json defines a main property, Ringo will try to load the specified resource. The value of the main property must be a path relative to the package root.

If a module id resolves to a directory that does not contain a package.json file, or package.json does not define a main property, Ringo will try to load file index.js in that directory.

If part of a module id resolves to a package directory, Ringo will try to resolve the remaining part of the id against the lib directory of that package. The location of the lib directory can be overridden using the directories.lib property in package.json.

{
    "directories": {
        "lib": "new-lib"
    }
}

The CommonJS modules specification was kept deliberately small. Ringo provides some extra niceties for exporting and importing stuff. The downside to using these is that your code is tied to Ringo, but it's relatively easy to convert the code to "pure" CommonJS, and there's also a command line tool for that purpose.

One Ringo extension is the include function. This is similar to require, but instead of returning the other module's exports object as a whole it directly copies each of its properties to the calling module's scope, making them usable like they were locally defined.

include is great for shell work and quick scripts where typing economy is paramount, and that's what it's meant for. It's usually not a great idea to use it for large, long lived programs as it conceals the origin of top-level functions used in the program.

For this purpose, it's more advisable to use require in combination with JavaScript 1.8 destructuring assignment to explicitly include selected properties from another module in the local scope:

var {foo, bar} = require("some/module");

The above statements imports the "foo" and "bar" properties of the API exported by "some/module" directly in the calling scope.

Writing command line scripts with Ringo is straightforward. Every arbitrary JavaScript file can be passed as [script-file] argument to the ringo command:

ringo [script-file] [script-arg1] [script-arg2] ...

Ringo loads the script file and provides the argument via the system module's args array. The first element in the args array is the script file's name.

引用文献

关于javascript - 将需要 CommonJS 功能的 JavaScript 编译为 native Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335057/

相关文章:

android - eclipse无法解析android native 代码

javascript - Google map 多个协同工作的下拉过滤器

javascript - 提交按钮根据一天中的时间显示图像

php - 如何在 PHP 中编码多字节文件名并在 javascript 中解码它们?

java - javafx 应用程序中未显示按钮

swift - UICollectionView swift 3 延迟滚动 UIImage

c - FreeLibrary API 调用失败怎么办?

javascript - 渲染后如何设置回调到 fancybox2?

java - 如何使用 Mersenne Twister RNG 随机数生成器

javascript - 使用 JavaScript 创建 JSP 元素