How Mozilla Rhino can use nodejs “fs” module?

孤街醉人 提交于 2019-12-11 09:04:32

问题


Let's say I have my.js file that has dependency on nodejs fs module. Like:

var fs = require('fs');

The question: How can I load Core NodeJS module like "FS" in My Mozilla Rhino Java Code to be able to run my.js that depends on it ?


回答1:


I don't think you can do this. fs module just a wrapper for native bindings built into node.js itself. There are some thoughts flying in the sky of node.js community about being able to use different JS engines inside node.js but I don't think we will see this in the near future.




回答2:


You can also write your own fs.js, only implementing the functions you need. Here is a start:

exports.readFile = function(file, enc, callback) {
  try {
    text = readFile(file, enc);
    callback(null, text);
  }
  catch (e) {
    callback(e, null);
  }
}

I've used this together with jvm-npm.



来源:https://stackoverflow.com/questions/24560360/how-mozilla-rhino-can-use-nodejs-fs-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!