Skip to content

Commit 23172c5

Browse files
committed
Lint node_script.cc
1 parent e2db605 commit 23172c5

2 files changed

Lines changed: 82 additions & 100 deletions

File tree

src/node_script.cc

Lines changed: 77 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
#include <node_script.h>
33
#include <assert.h>
44

5-
#include <v8-debug.h>
65

76
using namespace v8;
87
using namespace node;
98

9+
1010
Persistent<FunctionTemplate> node::Context::constructor_template;
1111

12-
void
13-
node::Context::Initialize (Handle<Object> target)
14-
{
12+
13+
void node::Context::Initialize (Handle<Object> target) {
1514
HandleScope scope;
1615

1716
Local<FunctionTemplate> t = FunctionTemplate::New(node::Context::New);
@@ -22,9 +21,8 @@ node::Context::Initialize (Handle<Object> target)
2221
target->Set(String::NewSymbol("Context"), constructor_template->GetFunction());
2322
}
2423

25-
Handle<Value>
26-
node::Context::New (const Arguments& args)
27-
{
24+
25+
Handle<Value> node::Context::New (const Arguments& args) {
2826
HandleScope scope;
2927

3028
node::Context *t = new node::Context();
@@ -33,31 +31,32 @@ node::Context::New (const Arguments& args)
3331
return args.This();
3432
}
3533

34+
35+
node::Context::Context() : ObjectWrap() {
36+
context_ = v8::Context::New();
37+
}
38+
39+
3640
node::Context::~Context() {
37-
_context.Dispose();
41+
context_.Dispose();
3842
}
3943

40-
Local<Object>
41-
node::Context::NewInstance()
42-
{
44+
45+
Local<Object> node::Context::NewInstance() {
4346
Local<Object> context = constructor_template->GetFunction()->NewInstance();
44-
node::Context *nContext = ObjectWrap::Unwrap<node::Context>(context);
45-
nContext->_context = v8::Context::New();
4647
return context;
4748
}
4849

49-
v8::Persistent<v8::Context>
50-
node::Context::GetV8Context()
51-
{
52-
return _context;
50+
51+
v8::Persistent<v8::Context> node::Context::GetV8Context() {
52+
return context_;
5353
}
5454

5555

5656
Persistent<FunctionTemplate> node::Script::constructor_template;
5757

58-
void
59-
node::Script::Initialize (Handle<Object> target)
60-
{
58+
59+
void node::Script::Initialize (Handle<Object> target) {
6160
HandleScope scope;
6261

6362
Local<FunctionTemplate> t = FunctionTemplate::New(node::Script::New);
@@ -77,9 +76,8 @@ node::Script::Initialize (Handle<Object> target)
7776
target->Set(String::NewSymbol("Script"), constructor_template->GetFunction());
7877
}
7978

80-
Handle<Value>
81-
node::Script::New (const Arguments& args)
82-
{
79+
80+
Handle<Value> node::Script::New (const Arguments& args) {
8381
HandleScope scope;
8482

8583
node::Script *t = new node::Script();
@@ -89,14 +87,13 @@ node::Script::New (const Arguments& args)
8987
node::Script::EvalMachine<compileCode, thisContext, wrapExternal>(args);
9088
}
9189

90+
9291
node::Script::~Script() {
93-
_script.Dispose();
92+
script_.Dispose();
9493
}
9594

9695

97-
Handle<Value>
98-
node::Script::CreateContext (const Arguments& args)
99-
{
96+
Handle<Value> node::Script::CreateContext (const Arguments& args) {
10097
HandleScope scope;
10198

10299
Local<v8::Object> context = node::Context::NewInstance();
@@ -117,92 +114,79 @@ node::Script::CreateContext (const Arguments& args)
117114
return scope.Close(context);
118115
}
119116

120-
Handle<Value>
121-
node::Script::RunInContext (const Arguments& args)
122-
{
117+
118+
Handle<Value> node::Script::RunInContext (const Arguments& args) {
123119
return
124120
node::Script::EvalMachine<unwrapExternal, userContext, returnResult>(args);
125121
}
126122

127123

128-
Handle<Value>
129-
node::Script::RunInThisContext (const Arguments& args)
130-
{
124+
Handle<Value> node::Script::RunInThisContext (const Arguments& args) {
131125
return
132126
node::Script::EvalMachine<unwrapExternal, thisContext, returnResult>(args);
133127
}
134128

135129

136-
Handle<Value>
137-
node::Script::RunInNewContext(const Arguments& args) {
130+
Handle<Value> node::Script::RunInNewContext(const Arguments& args) {
138131
return
139132
node::Script::EvalMachine<unwrapExternal, newContext, returnResult>(args);
140133
}
141134

142135

143-
Handle<Value>
144-
node::Script::CompileRunInContext (const Arguments& args)
145-
{
136+
Handle<Value> node::Script::CompileRunInContext (const Arguments& args) {
146137
return
147138
node::Script::EvalMachine<compileCode, userContext, returnResult>(args);
148139
}
149140

150141

151-
Handle<Value>
152-
node::Script::CompileRunInThisContext (const Arguments& args)
153-
{
142+
Handle<Value> node::Script::CompileRunInThisContext (const Arguments& args) {
154143
return
155144
node::Script::EvalMachine<compileCode, thisContext, returnResult>(args);
156145
}
157146

158147

159-
Handle<Value>
160-
node::Script::CompileRunInNewContext(const Arguments& args) {
148+
Handle<Value> node::Script::CompileRunInNewContext(const Arguments& args) {
161149
return
162150
node::Script::EvalMachine<compileCode, newContext, returnResult>(args);
163151
}
164152

165153

166-
// Extracts a C str from a V8 Utf8Value.
167-
const char* ToCString(const v8::String::Utf8Value& value) {
168-
return *value ? *value : "<str conversion failed>";
169-
}
170-
171154
template <node::Script::EvalInputFlags iFlag,
172-
node::Script::EvalContextFlags cFlag,
173-
node::Script::EvalOutputFlags oFlag>
174-
Handle<Value> node::Script::EvalMachine(const Arguments& args) {
155+
node::Script::EvalContextFlags cFlag,
156+
node::Script::EvalOutputFlags oFlag>
157+
Handle<Value> node::Script::EvalMachine(const Arguments& args) {
158+
175159
HandleScope scope;
176160

177161
if (iFlag == compileCode && args.Length() < 1) {
178162
return ThrowException(Exception::TypeError(
179-
String::New("needs at least 'code' argument.")
180-
));
163+
String::New("needs at least 'code' argument.")));
181164
}
182165

183166
const int sbIndex = iFlag == compileCode ? 1 : 0;
184167
if (cFlag == userContext && args.Length() < (sbIndex + 1)) {
185168
return ThrowException(Exception::TypeError(
186-
String::New("needs a 'context' argument.")
187-
));
169+
String::New("needs a 'context' argument.")));
188170
}
189171

190172

191173
Local<String> code;
192-
if (iFlag == compileCode) { code = args[0]->ToString(); }
174+
if (iFlag == compileCode) code = args[0]->ToString();
193175

194176
Local<Object> sandbox;
195177
if (cFlag == newContext) {
196178
sandbox = args.Length() > sbIndex ? args[sbIndex]->ToObject() : Object::New();
197-
}
198-
else if (cFlag == userContext) {
179+
} else if (cFlag == userContext) {
199180
sandbox = args[sbIndex]->ToObject();
200181
}
182+
201183
const int fnIndex = sbIndex + (cFlag == newContext ? 1 : 0);
202-
Local<String> filename = args.Length() > fnIndex ? args[fnIndex]->ToString()
203-
: String::New("evalmachine.<anonymous>");
184+
Local<String> filename = args.Length() > fnIndex
185+
? args[fnIndex]->ToString()
186+
: String::New("evalmachine.<anonymous>");
204187

205188
Persistent<v8::Context> context;
189+
206190
Local<Array> keys;
207191
unsigned int i;
208192
if (cFlag == newContext) {
@@ -218,7 +202,6 @@ Handle<Value> node::Script::EvalMachine(const Arguments& args) {
218202

219203
// New and user context share code. DRY it up.
220204
if (cFlag == userContext || cFlag == newContext) {
221-
222205
// Enter the context
223206
context->Enter();
224207

@@ -240,51 +223,48 @@ Handle<Value> node::Script::EvalMachine(const Arguments& args) {
240223
Handle<v8::Script> script;
241224

242225
if (iFlag == compileCode) {
243-
// well, here node::Script::New would suffice in all cases, but maybe Compile has a little better performance where possible
244-
script = oFlag == returnResult ? v8::Script::Compile(code, filename) : v8::Script::New(code, filename);
226+
// well, here node::Script::New would suffice in all cases, but maybe
227+
// Compile has a little better performance where possible
228+
script = oFlag == returnResult ? v8::Script::Compile(code, filename)
229+
: v8::Script::New(code, filename);
245230
if (script.IsEmpty()) {
246231
// Hack because I can't get a proper stacktrace on SyntaxError
247-
result = ThrowException(try_catch.Exception());
232+
return try_catch.ReThrow();
248233
}
249234
} else {
250235
node::Script *nScript = ObjectWrap::Unwrap<node::Script>(args.Holder());
251236
if (!nScript) {
252-
Local<Value> exception =
253-
Exception::Error(String::New("Must be called as a method of Script."));
254-
result = ThrowException(exception);
255-
} else if (nScript->_script.IsEmpty()) {
256-
Local<Value> exception =
257-
Exception::Error(String::New("'this' must be a result of previous new Script(code) call."));
258-
result = ThrowException(exception);
259-
} else {
260-
script = nScript->_script;
237+
return ThrowException(Exception::Error(
238+
String::New("Must be called as a method of Script.")));
239+
} else if (nScript->script_.IsEmpty()) {
240+
return ThrowException(Exception::Error(
241+
String::New("'this' must be a result of previous new Script(code) call.")));
261242
}
243+
244+
script = nScript->script_;
262245
}
263246

264-
if (result.IsEmpty()) {
265-
if (oFlag == returnResult) {
266-
result = script->Run();
267-
} else {
268-
node::Script *nScript = ObjectWrap::Unwrap<node::Script>(args.Holder());
269-
if (!nScript) {
270-
Local<Value> exception =
271-
Exception::Error(String::New("Must be called as a method of Script."));
272-
result = ThrowException(exception);
273-
} else {
274-
nScript->_script = Persistent<v8::Script>::New(script);
275-
result = args.This();
276-
}
277-
}
278-
if (result.IsEmpty()) {
279-
return try_catch.ReThrow();
280-
} else if (cFlag == userContext || cFlag == newContext) {
281-
// success! copy changes back onto the sandbox object.
282-
keys = context->Global()->GetPropertyNames();
283-
for (i = 0; i < keys->Length(); i++) {
284-
Handle<String> key = keys->Get(Integer::New(i))->ToString();
285-
Handle<Value> value = context->Global()->Get(key);
286-
sandbox->Set(key, value);
287-
}
247+
248+
if (oFlag == returnResult) {
249+
result = script->Run();
250+
if (result.IsEmpty()) return try_catch.ReThrow();
251+
} else {
252+
node::Script *nScript = ObjectWrap::Unwrap<node::Script>(args.Holder());
253+
if (!nScript) {
254+
return ThrowException(Exception::Error(
255+
String::New("Must be called as a method of Script.")));
256+
}
257+
nScript->script_ = Persistent<v8::Script>::New(script);
258+
result = args.This();
259+
}
260+
261+
if (cFlag == userContext || cFlag == newContext) {
262+
// success! copy changes back onto the sandbox object.
263+
keys = context->Global()->GetPropertyNames();
264+
for (i = 0; i < keys->Length(); i++) {
265+
Handle<String> key = keys->Get(Integer::New(i))->ToString();
266+
Handle<Value> value = context->Global()->Get(key);
267+
sandbox->Set(key, value);
288268
}
289269
}
290270

src/node_script.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ class Context : ObjectWrap {
2020

2121
static v8::Persistent<v8::FunctionTemplate> constructor_template;
2222

23-
Context () : ObjectWrap () {}
23+
Context ();
2424
~Context();
2525

26-
v8::Persistent<v8::Context> _context;
26+
v8::Persistent<v8::Context> context_;
2727
};
2828

29+
2930
class Script : ObjectWrap {
3031
public:
3132
static void Initialize (v8::Handle<v8::Object> target);
@@ -52,8 +53,9 @@ class Script : ObjectWrap {
5253
static v8::Handle<v8::Value> CompileRunInThisContext (const v8::Arguments& args);
5354
static v8::Handle<v8::Value> CompileRunInNewContext (const v8::Arguments& args);
5455

55-
v8::Persistent<v8::Script> _script;
56+
v8::Persistent<v8::Script> script_;
5657
};
5758

59+
5860
} // namespace node
5961
#endif // node_script_h

0 commit comments

Comments
 (0)