Discussion:
Unclear part in ENGINE_SCOPE and engine.eval()
(too old to reply)
Anuradha Karunarathna
2018-11-13 10:31:18 UTC
Permalink
Hi,

I'm using nashon by embedding it in Java(not viacommand line). I found that

*The default context's ENGINE_SCOPE is a wrapped instance of
ECMAScript "global" object - which is the "this" in top level script
expressions. *

So I wonder where other variables are stored in the engine. I tested a
sample to load JS functions in a one file to another and the codes
could be found below.
Only *Require,require,function1,function2* and *libModule* were found
in the engine scope.
I want to know where other evalated code variables(eg:
*testModule,function3 and math in lib.js and increment in test.js*)
are stored?
I am happy to clearup this thing..

-------------------------------------java
code-----------------------------------------------
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
Invocable invocable = (Invocable) engine;
engine.eval("load('require.js');");
engine.eval("var require = Require()");
engine.eval(new FileReader("script.js"));

Object result = invocable.invokeFunction("function2", 50);

Bindings engineBindings =
engine.getBindings(ScriptContext.ENGINE_SCOPE);
engineBindings.forEach((k,v)->System.out.println("key : " + k
+ " value : " + v));

-------------------------------------------require.js-------------------------------------------
var Require=(function () {
var File = java.io.File,
FileReader = java.io.FileReader,
BufferedReader = java.io.BufferedReader;


var _require = function( path ) {
var file,
moduleInfo,
buffered,
head = '(function(exports,module,require,__filename,__dirname){ ',
code = '',
tail = '})',
line = null;

file = new File(path);


buffered = new BufferedReader(new FileReader(file));
while ( ( line = buffered.readLine()) !== null ) {
code += line + '\n';
}
buffered.close();

moduleInfo = {
exports: {},
require: _requireWrapper()
};

code = head + code + tail;


var compiledWrapper = null;
try {
compiledWrapper = eval(code);
} catch (e) {
throw new Error( "Error evaluating module " + path
+ " line #" + e.lineNumber
+ " : " + e.message, e.lineNumber );
}

var args = [
moduleInfo.exports, /* exports */
moduleInfo, /* module */
moduleInfo.require, /* require */
];

try {
compiledWrapper
.apply(null, args);
} catch (e) {
throw new Error( "Error executing module " + path
+ " line #" + e.lineNumber
+ " : " + e.message, e.lineNumber );
}
return moduleInfo;
};

var _requireWrapper = function() {
return function( path ) {
var module = _require(path );
return module.exports;
};
};
return _requireWrapper();
})

-----------------------------------------script.js----------------------------------------------------
var libModule = require('lib.js');


var function1 =function(name){
print('Hi I am in script.js'+ name);
return "greetings from javascript";
};

var function2 = function(number) {
libModule.function3(number);
return libModule.math(number);
};

-----------------------------------------------lib.js---------------------------------------------------
var testModule=require('test.js');

var function3= function(number) {
print('Hi ' + number);

};

function math(number){
number = testModule.increment(number);
return number;
};


module.exports.math =math;
module.exports.function3=function3;

----------------------------------------------test.js---------------------------------------------------------------
function increment(number){
number+=5;
return number;
}

;
module.exports.increment=increment;
---------------------------------------------------------------------------------------------------------------------

Thanks in advance.

Anuradha

Loading...