From 23172c5d859e2ee90c4b440ee064a51039467baf Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 28 Jun 2010 17:27:17 -0700 Subject: [PATCH] Lint node_script.cc --- src/node_script.cc | 174 ++++++++++++++++++++++++----------------------------- src/node_script.h | 8 ++- 2 files changed, 82 insertions(+), 100 deletions(-) diff --git a/src/node_script.cc b/src/node_script.cc index ab38199..a172875 100644 --- a/src/node_script.cc +++ b/src/node_script.cc @@ -2,16 +2,15 @@ #include #include -#include using namespace v8; using namespace node; + Persistent node::Context::constructor_template; -void -node::Context::Initialize (Handle target) -{ + +void node::Context::Initialize (Handle target) { HandleScope scope; Local t = FunctionTemplate::New(node::Context::New); @@ -22,9 +21,8 @@ node::Context::Initialize (Handle target) target->Set(String::NewSymbol("Context"), constructor_template->GetFunction()); } -Handle -node::Context::New (const Arguments& args) -{ + +Handle node::Context::New (const Arguments& args) { HandleScope scope; node::Context *t = new node::Context(); @@ -33,31 +31,32 @@ node::Context::New (const Arguments& args) return args.This(); } + +node::Context::Context() : ObjectWrap() { + context_ = v8::Context::New(); +} + + node::Context::~Context() { - _context.Dispose(); + context_.Dispose(); } -Local -node::Context::NewInstance() -{ + +Local node::Context::NewInstance() { Local context = constructor_template->GetFunction()->NewInstance(); - node::Context *nContext = ObjectWrap::Unwrap(context); - nContext->_context = v8::Context::New(); return context; } -v8::Persistent -node::Context::GetV8Context() -{ - return _context; + +v8::Persistent node::Context::GetV8Context() { + return context_; } Persistent node::Script::constructor_template; -void -node::Script::Initialize (Handle target) -{ + +void node::Script::Initialize (Handle target) { HandleScope scope; Local t = FunctionTemplate::New(node::Script::New); @@ -77,9 +76,8 @@ node::Script::Initialize (Handle target) target->Set(String::NewSymbol("Script"), constructor_template->GetFunction()); } -Handle -node::Script::New (const Arguments& args) -{ + +Handle node::Script::New (const Arguments& args) { HandleScope scope; node::Script *t = new node::Script(); @@ -89,14 +87,13 @@ node::Script::New (const Arguments& args) node::Script::EvalMachine(args); } + node::Script::~Script() { - _script.Dispose(); + script_.Dispose(); } -Handle -node::Script::CreateContext (const Arguments& args) -{ +Handle node::Script::CreateContext (const Arguments& args) { HandleScope scope; Local context = node::Context::NewInstance(); @@ -117,92 +114,79 @@ node::Script::CreateContext (const Arguments& args) return scope.Close(context); } -Handle -node::Script::RunInContext (const Arguments& args) -{ + +Handle node::Script::RunInContext (const Arguments& args) { return node::Script::EvalMachine(args); } -Handle -node::Script::RunInThisContext (const Arguments& args) -{ +Handle node::Script::RunInThisContext (const Arguments& args) { return node::Script::EvalMachine(args); } -Handle -node::Script::RunInNewContext(const Arguments& args) { +Handle node::Script::RunInNewContext(const Arguments& args) { return node::Script::EvalMachine(args); } -Handle -node::Script::CompileRunInContext (const Arguments& args) -{ +Handle node::Script::CompileRunInContext (const Arguments& args) { return node::Script::EvalMachine(args); } -Handle -node::Script::CompileRunInThisContext (const Arguments& args) -{ +Handle node::Script::CompileRunInThisContext (const Arguments& args) { return node::Script::EvalMachine(args); } -Handle -node::Script::CompileRunInNewContext(const Arguments& args) { +Handle node::Script::CompileRunInNewContext(const Arguments& args) { return node::Script::EvalMachine(args); } -// Extracts a C str from a V8 Utf8Value. -const char* ToCString(const v8::String::Utf8Value& value) { - return *value ? *value : ""; -} - template -Handle node::Script::EvalMachine(const Arguments& args) { + node::Script::EvalContextFlags cFlag, + node::Script::EvalOutputFlags oFlag> + Handle node::Script::EvalMachine(const Arguments& args) { + HandleScope scope; if (iFlag == compileCode && args.Length() < 1) { return ThrowException(Exception::TypeError( - String::New("needs at least 'code' argument.") - )); + String::New("needs at least 'code' argument."))); } const int sbIndex = iFlag == compileCode ? 1 : 0; if (cFlag == userContext && args.Length() < (sbIndex + 1)) { return ThrowException(Exception::TypeError( - String::New("needs a 'context' argument.") - )); + String::New("needs a 'context' argument."))); } Local code; - if (iFlag == compileCode) { code = args[0]->ToString(); } + if (iFlag == compileCode) code = args[0]->ToString(); Local sandbox; if (cFlag == newContext) { sandbox = args.Length() > sbIndex ? args[sbIndex]->ToObject() : Object::New(); - } - else if (cFlag == userContext) { + } else if (cFlag == userContext) { sandbox = args[sbIndex]->ToObject(); } + const int fnIndex = sbIndex + (cFlag == newContext ? 1 : 0); - Local filename = args.Length() > fnIndex ? args[fnIndex]->ToString() - : String::New("evalmachine."); + Local filename = args.Length() > fnIndex + ? args[fnIndex]->ToString() + : String::New("evalmachine."); Persistent context; + Local keys; unsigned int i; if (cFlag == newContext) { @@ -218,7 +202,6 @@ Handle node::Script::EvalMachine(const Arguments& args) { // New and user context share code. DRY it up. if (cFlag == userContext || cFlag == newContext) { - // Enter the context context->Enter(); @@ -240,51 +223,48 @@ Handle node::Script::EvalMachine(const Arguments& args) { Handle script; if (iFlag == compileCode) { - // well, here node::Script::New would suffice in all cases, but maybe Compile has a little better performance where possible - script = oFlag == returnResult ? v8::Script::Compile(code, filename) : v8::Script::New(code, filename); + // well, here node::Script::New would suffice in all cases, but maybe + // Compile has a little better performance where possible + script = oFlag == returnResult ? v8::Script::Compile(code, filename) + : v8::Script::New(code, filename); if (script.IsEmpty()) { // Hack because I can't get a proper stacktrace on SyntaxError - result = ThrowException(try_catch.Exception()); + return try_catch.ReThrow(); } } else { node::Script *nScript = ObjectWrap::Unwrap(args.Holder()); if (!nScript) { - Local exception = - Exception::Error(String::New("Must be called as a method of Script.")); - result = ThrowException(exception); - } else if (nScript->_script.IsEmpty()) { - Local exception = - Exception::Error(String::New("'this' must be a result of previous new Script(code) call.")); - result = ThrowException(exception); - } else { - script = nScript->_script; + return ThrowException(Exception::Error( + String::New("Must be called as a method of Script."))); + } else if (nScript->script_.IsEmpty()) { + return ThrowException(Exception::Error( + String::New("'this' must be a result of previous new Script(code) call."))); } + + script = nScript->script_; } - if (result.IsEmpty()) { - if (oFlag == returnResult) { - result = script->Run(); - } else { - node::Script *nScript = ObjectWrap::Unwrap(args.Holder()); - if (!nScript) { - Local exception = - Exception::Error(String::New("Must be called as a method of Script.")); - result = ThrowException(exception); - } else { - nScript->_script = Persistent::New(script); - result = args.This(); - } - } - if (result.IsEmpty()) { - return try_catch.ReThrow(); - } else if (cFlag == userContext || cFlag == newContext) { - // success! copy changes back onto the sandbox object. - keys = context->Global()->GetPropertyNames(); - for (i = 0; i < keys->Length(); i++) { - Handle key = keys->Get(Integer::New(i))->ToString(); - Handle value = context->Global()->Get(key); - sandbox->Set(key, value); - } + + if (oFlag == returnResult) { + result = script->Run(); + if (result.IsEmpty()) return try_catch.ReThrow(); + } else { + node::Script *nScript = ObjectWrap::Unwrap(args.Holder()); + if (!nScript) { + return ThrowException(Exception::Error( + String::New("Must be called as a method of Script."))); + } + nScript->script_ = Persistent::New(script); + result = args.This(); + } + + if (cFlag == userContext || cFlag == newContext) { + // success! copy changes back onto the sandbox object. + keys = context->Global()->GetPropertyNames(); + for (i = 0; i < keys->Length(); i++) { + Handle key = keys->Get(Integer::New(i))->ToString(); + Handle value = context->Global()->Get(key); + sandbox->Set(key, value); } } diff --git a/src/node_script.h b/src/node_script.h index 8cac141..ce65d5c 100644 --- a/src/node_script.h +++ b/src/node_script.h @@ -20,12 +20,13 @@ class Context : ObjectWrap { static v8::Persistent constructor_template; - Context () : ObjectWrap () {} + Context (); ~Context(); - v8::Persistent _context; + v8::Persistent context_; }; + class Script : ObjectWrap { public: static void Initialize (v8::Handle target); @@ -52,8 +53,9 @@ class Script : ObjectWrap { static v8::Handle CompileRunInThisContext (const v8::Arguments& args); static v8::Handle CompileRunInNewContext (const v8::Arguments& args); - v8::Persistent _script; + v8::Persistent script_; }; + } // namespace node #endif // node_script_h -- 2.7.4