|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +#include <assert.h> |
| 23 | + |
| 24 | +#include "context_wrap.h" |
| 25 | + |
| 26 | +using v8::Context; |
| 27 | +using v8::Script; |
| 28 | +using v8::Value; |
| 29 | +using v8::Handle; |
| 30 | +using v8::HandleScope; |
| 31 | +using v8::Object; |
| 32 | +using v8::Arguments; |
| 33 | +using v8::ThrowException; |
| 34 | +using v8::TryCatch; |
| 35 | +using v8::String; |
| 36 | +using v8::Exception; |
| 37 | +using v8::Local; |
| 38 | +using v8::Array; |
| 39 | +using v8::Persistent; |
| 40 | +using v8::Integer; |
| 41 | +using v8::Function; |
| 42 | +using v8::FunctionTemplate; |
| 43 | + |
| 44 | +namespace nwebkit { |
| 45 | + |
| 46 | +Persistent<FunctionTemplate> ContextWrap::constructor_template; |
| 47 | + |
| 48 | +void ContextWrap::Initialize(Handle<Object> target) { |
| 49 | + HandleScope scope; |
| 50 | + |
| 51 | + Local<FunctionTemplate> t = FunctionTemplate::New(ContextWrap::New); |
| 52 | + constructor_template = Persistent<FunctionTemplate>::New(t); |
| 53 | + constructor_template->InstanceTemplate()->SetInternalFieldCount(2); |
| 54 | + constructor_template->SetClassName(String::NewSymbol("Context")); |
| 55 | + |
| 56 | + NODE_SET_PROTOTYPE_METHOD(constructor_template, |
| 57 | + "onContextCreationFromWebKit", |
| 58 | + ContextWrap::OnContextCreationFromWebKit); |
| 59 | + |
| 60 | + NODE_SET_METHOD(constructor_template, |
| 61 | + "onContextCreationFromWebKit", |
| 62 | + ContextWrap::OnContextCreationFromWebKit); |
| 63 | + |
| 64 | + target->Set(String::NewSymbol("Context"), |
| 65 | + constructor_template->GetFunction()); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +bool ContextWrap::InstanceOf(Handle<Value> value) { |
| 70 | + return !value.IsEmpty() && constructor_template->HasInstance(value); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +Handle<Value> ContextWrap::New(const Arguments& args) { |
| 75 | + HandleScope scope; |
| 76 | + |
| 77 | + ContextWrap *t = new ContextWrap(); |
| 78 | + t->Wrap(args.This()); |
| 79 | + |
| 80 | + return args.This(); |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +ContextWrap::ContextWrap() : ObjectWrap() { |
| 85 | + context_valid = false; |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +ContextWrap::~ContextWrap() { |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +Local<Object> ContextWrap::NewInstance() { |
| 94 | + Local<Object> wrapper = constructor_template->GetFunction()->NewInstance(); |
| 95 | + return wrapper; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +Persistent<Context> ContextWrap::GetV8Context() { |
| 100 | + return context_; |
| 101 | +} |
| 102 | + |
| 103 | +Handle<Value> ContextWrap::OnContextCreationFromWebKit(const Arguments& args) { |
| 104 | + HandleScope scope; |
| 105 | + |
| 106 | + if (args.Length() > 0) { |
| 107 | + Local<Object> sandbox = args[0]->ToObject(); |
| 108 | + } |
| 109 | + |
| 110 | + ContextWrap* context_wrap = ObjectWrap::Unwrap<ContextWrap> (args.Holder()); |
| 111 | + v8::Context* p = static_cast<v8::Context*>(args.Holder()->GetPointerFromInternalField (1)); |
| 112 | + context_wrap->context_ = p; |
| 113 | + context_wrap->context_valid = true; |
| 114 | + |
| 115 | + return v8::Undefined(); |
| 116 | +} |
| 117 | + |
| 118 | +} |
0 commit comments