Skip to content

Commit ce5d417

Browse files
committed
runscript example & context support
1 parent 09dfdec commit ce5d417

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/context_wrap.cc

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {
103109
Handle<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
}

src/context_wrap.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class ContextWrap : node::ObjectWrap {
1313
static v8::Local<v8::Object> NewInstance();
1414
static bool InstanceOf(v8::Handle<v8::Value> value);
1515
static v8::Handle<v8::Value> OnContextCreationFromWebKit(const v8::Arguments& args);
16-
protected:
16+
static v8::Handle<v8::Value> RunScript(const v8::Arguments& args);
17+
18+
protected:
1719

1820
static v8::Persistent<v8::FunctionTemplate> constructor_template;
1921

tests/runscript.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var nwebkit;
2+
nwebkit = require('../lib');
3+
4+
5+
nwebkit.init ({'url' : "tests/testfs.html", 'width' : 800, 'height' : 600});
6+
// nwebkit.init({'width' : 400, 'height' : 300});
7+
8+
setTimeout (function() {
9+
console.log("open()");
10+
nwebkit.context.runScript("document.getElementById('output').innerHTML='changed'");
11+
}, 2000);
12+

0 commit comments

Comments
 (0)