Can I include multiple Javascript files as libraries?

Jul 20, 2011 at 8:39 PM

I couldn't find it in documentation. Can I use multiple javascript as library files and use there functions?

e.g.

there are 5 javascript files. function1.js, function2.js function3.js

each containing one function.

how can i include these files as libraries using JINT? I couldn't find it the documentation. Sorry for asking.

//yousuf

Coordinator
Jul 20, 2011 at 8:50 PM

You can just run them all using the same engine instance:

engine.Run(file1);
engine.Run(file2);

And I think you can even register a new 'import' function which once called would load the specific file and run it. Though you would have to allow this security permission, and thus open a hole to scripts.

Jul 20, 2011 at 8:55 PM

thanks for the quick reply. much appreciated.

 

//yousuf

Jul 23, 2011 at 3:43 PM
Edited Jul 23, 2011 at 3:45 PM

If people are interested I would be willing to share a project I was working on a few weeks ago.  It provides CommonJS-style (mostly) require() functionality and allows for easier writing of modules both in pure JS (i.e. just a JS source file) as well as what I call a mixed-module, which is JS and .Net.

 

Edit: forgot to add that each loaded module is sandboxed in its own context...like most would expect

May 30, 2012 at 8:45 PM
Edited May 30, 2012 at 8:48 PM

Sorry to necrothread this but I am rather curious what method you used to add require like functionality.  If I execute a script that has an "include" function that executes another script using the same JintEngine, the original script will continue executing after the included script but it does not appear to know anything about the functions or variables defined in the included file.  aka jint.Run("somefunctions; include(s2); functionDeclaredInS2();")

I did notice that if I run two files directly in a row, instead of executing one from a bound method in the first the JintEngine does get the variables and functions defined in the scripts. aka jint.Run(s1); jint.Run(s2);

Any ideas?

--edit got rid of a couple of extra uses of include that were making this harder to read

May 31, 2012 at 12:23 PM

@digitalpowers:

I had the exact same problem. If I used the include functionality you outlined exact same problem.

Then as I also am using rhino I looked at their include functionality. They had this reference to "move to upper scope", which was odd to me. But then as I thought about it, what I am wondering about is if you are making a function call that the javascript interpreter automatically creates a scope of references. In Jint maybe that scope is not shiftable, but in rhino it is. 

May 31, 2012 at 3:20 PM
Edited May 31, 2012 at 3:22 PM

Well I ended up adding my functions in my include file into a prototype and used that, decalred with out var in the include file and was able to access the stuff.

So it looks like this 

include_type = function() {};

Function.prototype.do_stuff = function(vars)
{

}

That seems to work just fine, not sure why it wasn't before because I am fairly sure I tried this.

edit - trying to clean up my poorly formatted code, and making things worse

May 31, 2012 at 3:38 PM

Interesting, but I found a reason here...

http://www.w3schools.com/js/js_variables.asp

"If you assign values to variables that have not yet been declared, the variables will automatically be declared as global variables. "

In other words it does appear to be the bug that I am thinking about, which IMO is a bug that needs to be fixed... I also as a habit use var... 

May 31, 2012 at 3:41 PM

Right so do I, which might be why I was having trouble.  I just thought I had also tried it without var earlier.  Anyway, in this case it being a global variable is what I want.

Thanks for your help.