ClosestStorm / v8cgi

Automatically exported from code.google.com/p/v8cgi
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

include() not modify global scope #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
1. Make file 1.js with content:
include(system.getcwd() + '/2.js'); // BTW why that's not the same with
simple include('2.js') ???
blabla();
2. Make file 2.js with content:
var bla = function() { return 'bla' };
3. Run ./v8cgi 1.js

There is a error: "1.js:2: ReferenceError: bla is not defined".
I expected definition bla() in global scope, like if in 2.js was:
global.bla = function() { return 'bla' };

I use v8cgi 0.6.0 on Mac OS X.

Original issue reported on code.google.com by mr.ve...@gmail.com on 12 Jul 2009 at 9:43

GoogleCodeExporter commented 9 years ago
Please read https://wiki.mozilla.org/ServerJS/Modules/SecurableModules to 
understand
how Securable modules work.

First, include "1.js" searches in global module directory, while system.getcwd()
searches in an absolute path.

Second, include() behaves exactly as require(), but populates global object with
resulting "exports" object. Therefore, in order to have _something_ added to 
global
scope, your module needs to add this to "exports" object. See built-in 
libraries for
further reference.

Original comment by ondrej.zara on 12 Jul 2009 at 9:59

GoogleCodeExporter commented 9 years ago
> First, include "1.js" searches in global module directory, while 
system.getcwd()
searches in an absolute path.
But in http://code.google.com/p/v8cgi/wiki/API#Global_functions :
{{{
# include() - include other file. Four locations are searched for this:
   1. current directory for requested file;
What is it "current directory"?
}}}

> Second, include() behaves exactly as require(), but populates global object 
with
resulting "exports" object.
Ok, i got it. Maybe in http://code.google.com/p/v8cgi/wiki/API#Global_functions 
write
about it like about require()?

Original comment by mr.ve...@gmail.com on 12 Jul 2009 at 10:13

GoogleCodeExporter commented 9 years ago
Exist any way for loading file with modification global without use "exports" in
module? I have some "old-school modules" and i wanna keep it without 
modifications.

Original comment by mr.ve...@gmail.com on 12 Jul 2009 at 10:39

GoogleCodeExporter commented 9 years ago
Yes, the documentation was obsolete. I updated it.

Since v8cgi aims for ServerJS compatibility, there are no mechanisms for 
including a
file by simply executing it and polluting the global scope.

On the other hand, there is nothing easier than:

var myInclude = function(file) {
  var code = new File(file).open("r").read();
  eval.call(global, code)
}

Original comment by ondrej.zara on 13 Jul 2009 at 4:58

GoogleCodeExporter commented 9 years ago

Original comment by ondrej.zara on 29 Jul 2009 at 12:00