Reputation: 2981
I've created a simple 'require' mechanism (https://gist.github.com/1031869), in which the included script is compiled and run in a new context. However, when I call a function in the included script and pass it this
, the included script doesn't see any properties in it.
//required.js - compiled and run in new context
exports.logThis = function(what){
for (key in what) log(key + ' : ' + what[key]);
}
//main.js
logger = require('required');
this.someProp = {some: 'prop'}
logger.logThis({one: 'two'}); //works, prints 'one : two'
logger.logThis(this); //doesn't work, prints nothing. expected 'some : prop'
logger.logThis(this.someProp); //works, prints 'some : prop'
Upvotes: 3
Views: 648
Reputation: 2981
The problem was that V8 doesn't allow a Context to access the global variables of another Context. Hence, logger.logThis(this) wasn't printing anything.
This was solved, by setting the security token on the new context:
moduleContext->SetSecurityToken(context->GetSecurityToken());
where context is the 'main' context and moduleContext is the new context in which the included script runs.
Upvotes: 4