Created
June 17, 2011 17:34
-
-
Save anonymous/1031869 to your computer and use it in GitHub Desktop.
Require implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Include a script file into current context | |
//Raise javascript exception if load failed | |
v8::Handle<v8::Value> _BeaScript::include( const Arguments& args ) | |
{ | |
boost::filesystem::path parentPath = scriptPath.parent_path(); | |
//v8::String::Utf8Value fileName(args[i]); | |
std::string fileName = bea::Convert<std::string>::FromJS(args[0], 0); | |
//Add the script path to it | |
boost::filesystem::path absolutePath = parentPath / fileName; | |
if (!absolutePath.has_extension() && !boost::filesystem::exists(absolutePath)) | |
absolutePath.replace_extension(".js"); | |
HandleScope scope; | |
v8::Handle<v8::Value> result; | |
v8::Handle<v8::String> source; | |
if (boost::filesystem::exists(absolutePath)) | |
source = ReadFile(absolutePath.string().c_str()); | |
if (source.IsEmpty()){ | |
std::stringstream s; | |
s << "Could not include file " << absolutePath.string(); | |
return v8::ThrowException(v8::Exception::Error(v8::String::New(s.str().c_str()))); | |
} | |
//Run the included script in a new context | |
v8::Persistent<v8::Context> newContext = v8::Context::New(); | |
newContext->SetSecurityToken(securityToken); | |
//Inspired by node_script.js - copy all main context's global properties to the new context | |
Handle<Object> module = createModule(absolutePath.string()); | |
v8::Local<v8::Array> keys = args.This()->GetPropertyNames(); | |
for (uint32_t i = 0; i < keys->Length(); i++) { | |
v8::Handle<v8::String> key = keys->Get(i)->ToString(); | |
v8::Handle<v8::Value> value = args.This()->Get(key); | |
newContext->Enter(); | |
newContext->Global()->Set(key, value); | |
newContext->Exit(); | |
} | |
v8::Handle<v8::Value> retVal; | |
{ | |
v8::Context::Scope ctxScope(newContext); | |
newContext->Global()->Set(v8::String::NewSymbol("module"), module); | |
//global.exports = module.exports | |
newContext->Global()->Set(v8::String::NewSymbol("exports"), module->Get(v8::String::NewSymbol("exports"))); | |
result = execute(source, bea::Convert<std::string>::ToJS(absolutePath.string().c_str())->ToString()); | |
if (!result.IsEmpty()){ | |
retVal = newContext->Global()->Get(v8::String::NewSymbol("module")); | |
if (!retVal->IsUndefined() && retVal->IsObject()){ | |
retVal = retVal->ToObject()->Get(v8::String::NewSymbol("exports")); | |
} | |
//If module.exports is undefined, return whatever the included script returns | |
if (retVal->IsUndefined()) | |
retVal = result; | |
} | |
else | |
retVal = v8::ThrowException(v8::Exception::Error(v8::String::New(lastError.c_str()))); | |
} | |
newContext.Dispose(); | |
return scope.Close(retVal); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment