Permalink
Browse files

[#37] Working global scope and context.

  • Loading branch information...
1 parent 8c81853 commit 6a2c780924422abc1d0ca0b73a717002b8003dbf @assaf committed Feb 15, 2011
Showing with 40 additions and 9 deletions.
  1. +40 −9 src/zombie/windowcontext.cc
@@ -7,8 +7,8 @@ using namespace v8;
class WindowContext: ObjectWrap {
private:
int m_count;
- Handle<ObjectTemplate> global;
Persistent<Context> context;
+ Persistent<Object> global;
public:
@@ -22,18 +22,21 @@ class WindowContext: ObjectWrap {
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
s_ct->SetClassName(String::NewSymbol("WindowContext"));
- //NODE_SET_PROTOTYPE_METHOD(s_ct, "global", GetGlobal);
NODE_SET_PROTOTYPE_METHOD(s_ct, "evaluate", Evaluate);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "global", GetGlobal);
target->Set(String::NewSymbol("WindowContext"), s_ct->GetFunction());
}
WindowContext() : m_count(0) {
- global = ObjectTemplate::New();
- context = Context::New(NULL, global);
+ Handle<ObjectTemplate> tmpl = ObjectTemplate::New();
+ global = Persistent<Object>::New(Object::New());
+ tmpl->SetNamedPropertyHandler(GetGlobalProperty, SetGlobalProperty, NULL, NULL, NULL, global);
+ context = Context::New(NULL, tmpl);
}
~WindowContext() {
+ context.Dispose();
}
static Handle<Value> New(const Arguments& args) {
@@ -43,16 +46,45 @@ class WindowContext: ObjectWrap {
return args.This();
}
+ // Evaluate expression (String) or function in this context.
static Handle<Value> Evaluate(const Arguments& args) {
HandleScope scope;
WindowContext* wc = ObjectWrap::Unwrap<WindowContext>(args.This());
- Context::Scope context_scope(wc->context);
- Local<String> source = args[0]->ToString();
- Handle<Script> script = Script::Compile(source);
- Handle<Value> result = script->Run();
+ wc->context->Enter();
+ Handle<Value> result;
+ if (args[0]->IsFunction()) {
+ // Execute function in the global scope.
+ Function *fn = Function::Cast(*args[0]);
+ result = fn->Call(wc->global, 0, NULL);
+ } else {
+ // Coerce argument into a string and execute that as a function.
+ Local<String> source = args[0]->ToString();
+ Handle<Script> script = Script::Compile(source);
+ result = script->Run();
+ }
+ // TODO: finally
+ wc->context->Exit();
return scope.Close(result);
}
+ static Handle<Value> GetGlobal(const Arguments& args) {
+ WindowContext* wc = ObjectWrap::Unwrap<WindowContext>(args.This());
+ return wc->global;
+ }
+
+ static Handle<Value> GetGlobalProperty(Local<String> property, const AccessorInfo &info) {
+ HandleScope scope;
+ Handle<Object> object = info.Data()->ToObject();
+ Handle<Value> result = object->Get(property);
+ return scope.Close(result);
+ }
+
+ static Handle<Value> SetGlobalProperty(Local<String> property, Local<Value> value, const AccessorInfo &info) {
+ HandleScope scope;
+ Handle<Object> object = info.Data()->ToObject();
+ object->Set(property, value);
+ return scope.Close(value);
+ }
};
Persistent<FunctionTemplate> WindowContext::s_ct;
@@ -63,4 +95,3 @@ extern "C" {
}
NODE_MODULE(windowcontext, init);
}
-

0 comments on commit 6a2c780

Please sign in to comment.