@@ -56,10 +56,16 @@ void ContextWrap::Initialize(Handle<Object> target) {
5656 NODE_SET_PROTOTYPE_METHOD (constructor_template,
5757 " onContextCreationFromWebKit" ,
5858 ContextWrap::OnContextCreationFromWebKit);
59+ NODE_SET_PROTOTYPE_METHOD (constructor_template,
60+ " runScript" ,
61+ ContextWrap::RunScript);
5962
6063 NODE_SET_METHOD (constructor_template,
6164 " onContextCreationFromWebKit" ,
6265 ContextWrap::OnContextCreationFromWebKit);
66+ NODE_SET_METHOD (constructor_template,
67+ " runScript" ,
68+ ContextWrap::RunScript);
6369
6470 target->Set (String::NewSymbol (" Context" ),
6571 constructor_template->GetFunction ());
@@ -103,10 +109,6 @@ Persistent<Context> ContextWrap::GetV8Context() {
103109Handle<Value> ContextWrap::OnContextCreationFromWebKit (const Arguments& args) {
104110 HandleScope scope;
105111
106- if (args.Length () > 0 ) {
107- Local<Object> sandbox = args[0 ]->ToObject ();
108- }
109-
110112 ContextWrap* context_wrap = ObjectWrap::Unwrap<ContextWrap> (args.Holder ());
111113 v8::Context* p = static_cast <v8::Context*>(args.Holder ()->GetPointerFromInternalField (1 ));
112114 context_wrap->context_ = p;
@@ -115,4 +117,32 @@ Handle<Value> ContextWrap::OnContextCreationFromWebKit(const Arguments& args) {
115117 return v8::Undefined ();
116118}
117119
120+ Handle<Value> ContextWrap::RunScript (const Arguments& args) {
121+ HandleScope scope;
122+
123+ ContextWrap* context_wrap = ObjectWrap::Unwrap<ContextWrap> (args.Holder ());
124+ if (!context_wrap->context_valid ) {
125+ return v8::Undefined ();
126+ }
127+
128+ TryCatch try_catch;
129+ Local<String> code = args[0 ]->ToString ();
130+ context_wrap->context_ ->Enter ();
131+ Handle<Script> script = Script::Compile (code, String::New (" nwebkit.<anonymous>" ));
132+ if (script.IsEmpty ()) {
133+ // FIXME UGLY HACK TO DISPLAY SYNTAX ERRORS.
134+ node::DisplayExceptionLine (try_catch);
135+
136+ // Hack because I can't get a proper stacktrace on SyntaxError
137+ return try_catch.ReThrow ();
138+ }
139+ Handle<Value> result;
140+ result = script->Run ();
141+ context_wrap->context_ ->Exit ();
142+ if (result.IsEmpty ()) {
143+ return try_catch.ReThrow ();
144+ }
145+
146+ return result == args.This () ? result : scope.Close (result);
147+ }
118148}
0 commit comments