1 /* 2 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "bindings/v8/V8HiddenPropertyName.h" 33 34 #include "bindings/v8/V8Binding.h" 35 #include <string.h> 36 #include "wtf/StdLibExtras.h" 37 #include "wtf/Vector.h" 38 39 namespace WebCore { 40 41 #define V8_AS_STRING(x) V8_AS_STRING_IMPL(x) 42 #define V8_AS_STRING_IMPL(x) #x 43 44 #define V8_HIDDEN_PROPERTY_PREFIX "WebCore::HiddenProperty::" 45 46 #define V8_DEFINE_HIDDEN_PROPERTY(name) \ 47 v8::Handle<v8::String> V8HiddenPropertyName::name() \ 48 { \ 49 v8::Isolate* isolate = v8::Isolate::GetCurrent(); \ 50 V8HiddenPropertyName* hiddenPropertyName = V8PerIsolateData::from(isolate)->hiddenPropertyName(); \ 51 if (hiddenPropertyName->m_##name.IsEmpty()) { \ 52 createString(V8_HIDDEN_PROPERTY_PREFIX V8_AS_STRING(name), &(hiddenPropertyName->m_##name)); \ 53 } \ 54 return v8::Local<v8::String>::New(isolate, hiddenPropertyName->m_##name); \ 55 } 56 57 V8_HIDDEN_PROPERTIES(V8_DEFINE_HIDDEN_PROPERTY); 58 59 static v8::Handle<v8::String> hiddenReferenceName(const char* name, unsigned length) 60 { 61 ASSERT(length); 62 Vector<char, 64> prefixedName; 63 prefixedName.append(V8_HIDDEN_PROPERTY_PREFIX, sizeof(V8_HIDDEN_PROPERTY_PREFIX) - 1); 64 prefixedName.append(name, length); 65 return v8::String::NewSymbol(prefixedName.data(), static_cast<int>(prefixedName.size())); 66 } 67 68 void V8HiddenPropertyName::setNamedHiddenReference(v8::Handle<v8::Object> parent, const char* name, v8::Handle<v8::Value> child) 69 { 70 ASSERT(name); 71 parent->SetHiddenValue(hiddenReferenceName(name, strlen(name)), child); 72 } 73 74 void V8HiddenPropertyName::createString(const char* key, v8::Persistent<v8::String>* handle) 75 { 76 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 77 v8::HandleScope scope(isolate); 78 handle->Reset(isolate, v8::String::NewSymbol(key)); 79 } 80 81 } // namespace WebCore 82